Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache hash for multiple block->hash () calls #2536

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions nano/core_test/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,25 +366,36 @@ TEST (state_block, hashing)
nano::keypair key;
nano::state_block block (key.pub, 0, key.pub, 0, 0, key.prv, key.pub, 0);
auto hash (block.hash ());
ASSERT_EQ (hash, block.hash ()); // check cache works
block.hashables.account.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_NE (hash, block.hash ());
block.hashables.account.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_EQ (hash, block.hash ());
block.hashables.previous.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_NE (hash, block.hash ());
block.hashables.previous.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_EQ (hash, block.hash ());
block.hashables.representative.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_NE (hash, block.hash ());
block.hashables.representative.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_EQ (hash, block.hash ());
block.hashables.balance.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_NE (hash, block.hash ());
block.hashables.balance.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_EQ (hash, block.hash ());
block.hashables.link.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_NE (hash, block.hash ());
block.hashables.link.bytes[0] ^= 0x1;
block.refresh ();
ASSERT_EQ (hash, block.hash ());
}

Expand Down
1 change: 1 addition & 0 deletions nano/core_test/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ TEST (bulk_pull, end_not_owned)
open.hashables.account = key2.pub;
open.hashables.representative = key2.pub;
open.hashables.source = latest;
open.refresh ();
open.signature = nano::sign_message (key2.prv, key2.pub, open.hash ());
system.nodes[0]->work_generate_blocking (open);
ASSERT_EQ (nano::process_result::progress, system.nodes[0]->process (open).code);
Expand Down
26 changes: 25 additions & 1 deletion nano/lib/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ size_t nano::block::size (nano::block_type type_a)
return result;
}

nano::block_hash nano::block::hash () const
nano::block_hash nano::block::generate_hash () const
{
nano::block_hash result;
blake2b_state hash_l;
Expand All @@ -88,6 +88,30 @@ nano::block_hash nano::block::hash () const
return result;
}

void nano::block::refresh ()
{
if (!cached_hash.is_zero ())
{
cached_hash = generate_hash ();
}
}

nano::block_hash const & nano::block::hash () const
{
if (!cached_hash.is_zero ())
{
// Once a block is created, it should not be modified (unless using refresh ())
// This would invalidate the cache; check it hasn't changed.
assert (cached_hash == generate_hash ());
}
else
{
cached_hash = generate_hash ();
}

return cached_hash;
}

nano::block_hash nano::block::full_hash () const
{
nano::block_hash result;
Expand Down
10 changes: 9 additions & 1 deletion nano/lib/blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class block
{
public:
// Return a digest of the hashables in this block.
nano::block_hash hash () const;
nano::block_hash const & hash () const;
// Return a digest of hashables and non-hashables in this block.
nano::block_hash full_hash () const;
std::string to_json () const;
Expand Down Expand Up @@ -88,6 +88,14 @@ class block
virtual ~block () = default;
virtual bool valid_predecessor (nano::block const &) const = 0;
static size_t size (nano::block_type);
// If there are any changes to the hashables, call this to update the cached hash
void refresh ();

protected:
mutable nano::block_hash cached_hash{ 0 };

private:
nano::block_hash generate_hash () const;
};
class send_hashables
{
Expand Down
2 changes: 1 addition & 1 deletion nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void nano::active_transactions::search_frontiers (nano::transaction const & tran
// Calculate votes for local representatives
if (representative)
{
this->node.block_processor.generator.add (block->hash ());
this->node.block_processor.generator.add (info.head);
}
}
}
Expand Down