lokinet/llarp/pow.cpp

67 lines
1.2 KiB
C++
Raw Normal View History

#include "pow.hpp"
#include "crypto/crypto.hpp"
#include "util/buffer.hpp"
#include <cmath>
namespace llarp
{
2019-07-30 23:42:13 +00:00
PoW::~PoW() = default;
bool
PoW::decode_key(const llarp_buffer_t& /*k*/, llarp_buffer_t* /*val*/)
{
// TODO: implement me
return false;
}
std::string
PoW::bt_encode() const
{
return ""s;
}
bool
PoW::IsValid(llarp_time_t now) const
{
if (now - timestamp > extendedLifetime)
return false;
ShortHash digest;
std::array<byte_t, MaxSize> tmp;
2019-02-02 23:12:42 +00:00
llarp_buffer_t buf(tmp);
auto bte = bt_encode();
if (auto b = buf.write(bte.begin(), bte.end()); not b)
return false;
// rewind
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
// hash
if (!CryptoManager::instance()->shorthash(digest, buf))
return false;
// check bytes required
2020-02-24 19:40:45 +00:00
uint32_t required = std::floor(std::log(extendedLifetime.count()));
for (uint32_t idx = 0; idx < required; ++idx)
{
if (digest[idx])
return false;
}
return true;
}
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);
}
} // namespace llarp