2021-03-09 22:24:35 +00:00
|
|
|
#include "pow.hpp"
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "crypto/crypto.hpp"
|
|
|
|
#include "util/buffer.hpp"
|
2019-01-14 21:46:07 +00:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
PoW::~PoW() = default;
|
2019-01-14 21:46:07 +00:00
|
|
|
|
|
|
|
bool
|
2023-08-31 16:28:02 +00:00
|
|
|
PoW::decode_key(const llarp_buffer_t& /*k*/, llarp_buffer_t* /*val*/)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
// TODO: implement me
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
std::string
|
|
|
|
PoW::bt_encode() const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
return ""s;
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
PoW::IsValid(llarp_time_t now) const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (now - timestamp > extendedLifetime)
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ShortHash digest;
|
2020-04-07 18:38:56 +00:00
|
|
|
std::array<byte_t, MaxSize> tmp;
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2023-08-31 16:28:02 +00:00
|
|
|
|
|
|
|
auto bte = bt_encode();
|
|
|
|
|
|
|
|
if (auto b = buf.write(bte.begin(), bte.end()); not b)
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2023-08-31 16:28:02 +00:00
|
|
|
|
2019-01-14 21:46:07 +00:00
|
|
|
// rewind
|
2020-04-07 18:38:56 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
2019-01-14 21:46:07 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
// hash
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!CryptoManager::instance()->shorthash(digest, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
// check bytes required
|
2020-02-24 19:40:45 +00:00
|
|
|
uint32_t required = std::floor(std::log(extendedLifetime.count()));
|
2020-04-07 18:38:56 +00:00
|
|
|
for (uint32_t idx = 0; idx < required; ++idx)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (digest[idx])
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
std::string
|
|
|
|
PoW::ToString() const
|
|
|
|
{
|
2022-07-18 17:56:09 +00:00
|
|
|
return fmt::format(
|
|
|
|
"[PoW timestamp={} lifetime={} nonce={}]",
|
|
|
|
timestamp.count(),
|
|
|
|
extendedLifetime.count(),
|
|
|
|
nonce);
|
2022-07-16 00:41:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 21:46:07 +00:00
|
|
|
} // namespace llarp
|