1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/pow.cpp
Stephen Shelton 273270916e
The Great Wall of Blame
This commit reflects changes to clang-format rules. Unfortunately,
these rule changes create a massive change to the codebase, which
causes an apparent rewrite of git history.

Git blame's --ignore-rev flag can be used to ignore this commit when
attempting to `git blame` some code.
2020-04-07 12:38:56 -06:00

68 lines
1.4 KiB
C++

#include <pow.hpp>
#include <crypto/crypto.hpp>
#include <util/buffer.hpp>
#include <cmath>
namespace llarp
{
PoW::~PoW() = default;
bool
PoW::DecodeKey(const llarp_buffer_t& /*k*/, 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_time_t now) const
{
if (now - timestamp > extendedLifetime)
return false;
ShortHash digest;
std::array<byte_t, MaxSize> tmp;
llarp_buffer_t buf(tmp);
// encode
if (!BEncode(&buf))
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
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::ostream&
PoW::print(std::ostream& stream, int level, int spaces) const
{
Printer printer(stream, level, spaces);
printer.printAttribute("pow timestamp", timestamp.count());
printer.printAttribute("lifetime", extendedLifetime.count());
printer.printAttribute("nonce", nonce);
return stream;
}
} // namespace llarp