2018-12-15 16:21:52 +00:00
|
|
|
#include <pow.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2019-05-28 19:45:08 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <util/buffer.hpp>
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
PoW::~PoW() = default;
|
2019-01-14 21:46:07 +00:00
|
|
|
|
|
|
|
bool
|
2019-04-19 18:24:33 +00:00
|
|
|
PoW::DecodeKey(ABSL_ATTRIBUTE_UNUSED const llarp_buffer_t& k,
|
|
|
|
ABSL_ATTRIBUTE_UNUSED llarp_buffer_t* val)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
// TODO: implement me
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PoW::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
// TODO: implement me
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
PoW::IsValid(llarp_time_t now) const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
if(now - timestamp > (uint64_t(extendedLifetime) * 1000))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ShortHash digest;
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, MaxSize > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2019-01-14 21:46:07 +00:00
|
|
|
// encode
|
|
|
|
if(!BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
// rewind
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
// hash
|
2019-05-28 19:45:08 +00:00
|
|
|
if(!CryptoManager::instance()->shorthash(digest, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
// check bytes required
|
|
|
|
uint32_t required = std::floor(std::log(extendedLifetime));
|
|
|
|
for(uint32_t idx = 0; idx < required; ++idx)
|
|
|
|
{
|
|
|
|
if(digest[idx])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-24 23:46:37 +00:00
|
|
|
std::ostream&
|
|
|
|
PoW::print(std::ostream& stream, int level, int spaces) const
|
|
|
|
{
|
|
|
|
Printer printer(stream, level, spaces);
|
|
|
|
|
|
|
|
printer.printAttribute("pow timestamp", timestamp);
|
|
|
|
printer.printAttribute("lifetime", extendedLifetime);
|
|
|
|
printer.printAttribute("nonce", nonce);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:46:07 +00:00
|
|
|
} // namespace llarp
|