mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-10-29 11:05:43 +00:00
33bfaf7ecd
test criteria: - test build on macOS - test build in debian docker container - test build in fedora docker container
58 lines
1.2 KiB
C++
58 lines
1.2 KiB
C++
#include <llarp/time.h>
|
|
#include <cmath>
|
|
#include <llarp/pow.hpp>
|
|
#include "buffer.hpp"
|
|
|
|
namespace llarp
|
|
{
|
|
PoW::~PoW()
|
|
{
|
|
}
|
|
|
|
bool
|
|
PoW::DecodeKey(__attribute__((unused)) llarp_buffer_t k,
|
|
__attribute__((unused)) llarp_buffer_t* val)
|
|
{
|
|
// 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
|
|
PoW::IsValid(llarp_shorthash_func hashfunc, llarp_time_t now) const
|
|
{
|
|
if(now - timestamp > (uint64_t(extendedLifetime) * 1000))
|
|
return false;
|
|
|
|
byte_t digest[SHORTHASHSIZE];
|
|
byte_t tmp[MaxSize];
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
// encode
|
|
if(!BEncode(&buf))
|
|
return false;
|
|
// rewind
|
|
buf.sz = buf.cur - buf.base;
|
|
buf.cur = buf.base;
|
|
// hash
|
|
if(!hashfunc(digest, buf))
|
|
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;
|
|
}
|
|
|
|
} // namespace llarp
|