1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/proofofwork.cpp

58 lines
1.2 KiB
C++
Raw Normal View History

2018-07-09 19:32:11 +02:00
#include <llarp/time.h>
2018-06-19 00:03:50 +02:00
#include <cmath>
#include <llarp/pow.hpp>
#include "buffer.hpp"
namespace llarp
{
2018-07-09 19:32:11 +02:00
PoW::~PoW()
{
}
bool
PoW::DecodeKey(__attribute__((unused)) llarp_buffer_t k,
__attribute__((unused)) llarp_buffer_t* val)
2018-07-09 19:32:11 +02:00
{
// TODO: implement me
2018-06-19 00:03:50 +02:00
return false;
}
bool
PoW::BEncode(llarp_buffer_t* buf) const
{
2018-07-09 19:32:11 +02:00
// TODO: implement me
2018-08-10 23:34:11 +02:00
if(!bencode_start_dict(buf))
return false;
return bencode_end(buf);
2018-06-19 00:03:50 +02:00
}
bool
2018-10-29 17:48:36 +01:00
PoW::IsValid(llarp_shorthash_func hashfunc, llarp_time_t now) const
2018-06-19 00:03:50 +02:00
{
2018-07-09 19:32:11 +02:00
if(now - timestamp > (uint64_t(extendedLifetime) * 1000))
2018-06-19 00:03:50 +02:00
return false;
2018-07-09 19:32:11 +02:00
2018-06-19 00:03:50 +02:00
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