Skip to content

Commit

Permalink
Add copying operation to block builder (nanocurrency#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptocode authored and Guilherme Lawless committed Apr 15, 2019
1 parent 4685679 commit 258e3a8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions nano/core_test/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,27 @@ TEST (block_uniquer, cleanup)
}
}

TEST (block_builder, from)
{
std::error_code ec;
nano::block_builder builder;
auto block = builder
.state ()
.account_address ("xrb_15nhh1kzw3x8ohez6s75wy3jr6dqgq65oaede1fzk5hqxk4j8ehz7iqtb3to")
.previous_hex ("FEFBCE274E75148AB31FF63EFB3082EF1126BF72BF3FA9C76A97FD5A9F0EBEC5")
.balance_dec ("2251569974100400000000000000000000")
.representative_address ("xrb_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou")
.link_hex ("E16DD58C1EFA8B521545B0A74375AA994D9FC43828A4266D75ECF57F07A7EE86")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "2D243F8F92CDD0AD94A1D456A6B15F3BE7A6FCBD98D4C5831D06D15C818CD81F");

auto block2 = builder.state ().from (*block).build (ec);
ASSERT_EQ (block2->hash ().to_string (), "2D243F8F92CDD0AD94A1D456A6B15F3BE7A6FCBD98D4C5831D06D15C818CD81F");

auto block3 = builder.state ().from (*block).sign_zero ().work (0).build (ec);
ASSERT_EQ (block3->hash ().to_string (), "2D243F8F92CDD0AD94A1D456A6B15F3BE7A6FCBD98D4C5831D06D15C818CD81F");
}

TEST (block_builder, zeroed_state_block)
{
std::error_code ec;
Expand Down
12 changes: 12 additions & 0 deletions nano/lib/blockbuilders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ nano::state_block_builder & nano::state_block_builder::make_block ()
return *this;
}

nano::state_block_builder & nano::state_block_builder::from (nano::state_block const & other_block)
{
block->work = other_block.work;
block->signature = other_block.signature;
block->hashables.account = other_block.hashables.account;
block->hashables.balance = other_block.hashables.balance;
block->hashables.link = other_block.hashables.link;
block->hashables.previous = other_block.hashables.previous;
block->hashables.representative = other_block.hashables.representative;
return *this;
}

void nano::state_block_builder::validate ()
{
if (!ec)
Expand Down
2 changes: 2 additions & 0 deletions nano/lib/blockbuilders.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class state_block_builder : public abstract_builder<nano::state_block, state_blo
public:
/** Creates a state block builder by calling make_block() */
state_block_builder ();
/** Initialize from an existing block */
state_block_builder & from (nano::state_block const & block);
/** Creates a new block with fields, signature and work set to sentinel values. All fields must be set or zeroed for build() to succeed. */
state_block_builder & make_block ();
/** Sets all hashables, signature and work to zero. */
Expand Down

0 comments on commit 258e3a8

Please sign in to comment.