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

Support withdrawals in t8n #614

Merged
merged 1 commit into from
Apr 21, 2023
Merged

Support withdrawals in t8n #614

merged 1 commit into from
Apr 21, 2023

Conversation

rodiazet
Copy link
Collaborator

@rodiazet rodiazet commented Apr 11, 2023

  • Add support for withdrawals in t8n interface. It's required by execution-spec-tests
  • Tests

@rodiazet rodiazet requested a review from chfast April 11, 2023 13:02
test/t8n/t8n.cpp Outdated
@@ -160,6 +160,10 @@ int main(int argc, const char* argv[])
if (block_reward.has_value())
state.touch(block.coinbase).balance += *block_reward;

// Apply withdrawals. Amount value is in gwei.
for (const auto& withdraw : block.withdrawals)
state.touch(withdraw.first).balance += withdraw.second * 1000000000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are withdrawals applies before block finalization? I.e. are touched accounts possibly removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spec says that amount is non-zero. So I will not be removed on finalizing. Am I right? https://eips.ethereum.org/EIPS/eip-4895

test/state/state.hpp Outdated Show resolved Hide resolved
@rodiazet rodiazet force-pushed the withdrawals-t8n branch 2 times, most recently from f942863 to 815339a Compare April 14, 2023 14:12
@codecov
Copy link

codecov bot commented Apr 14, 2023

Codecov Report

Merging #614 (f062020) into master (4211942) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #614      +/-   ##
==========================================
+ Coverage   97.29%   97.30%   +0.01%     
==========================================
  Files          78       79       +1     
  Lines        7648     7679      +31     
==========================================
+ Hits         7441     7472      +31     
  Misses        207      207              
Flag Coverage Δ
blockchaintests 65.16% <ø> (ø)
statetests 63.11% <53.84%> (-0.08%) ⬇️
unittests 94.86% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
test/state/state.cpp 97.69% <100.00%> (+0.03%) ⬆️
test/state/state.hpp 100.00% <100.00%> (ø)
test/statetest/statetest_loader.cpp 89.69% <100.00%> (+0.31%) ⬆️
test/statetest/statetest_runner.cpp 100.00% <100.00%> (ø)
test/unittests/state_transition.cpp 100.00% <100.00%> (ø)
test/unittests/state_transition_block_test.cpp 100.00% <100.00%> (ø)
...est/unittests/statetest_loader_block_info_test.cpp 100.00% <100.00%> (ø)

@@ -66,6 +66,8 @@ class State
[[nodiscard]] const auto& get_accounts() const noexcept { return m_accounts; }
};

using Withdrawals = std::vector<std::pair<address, uint64_t>>;
Copy link
Member

@axic axic Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary but perhaps a comment explaining what the value is and what range it has is useful (e.g. in gwei).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better will be to just define single using Withdrawal = std::pair<address, uint64_t>. But at this point a struct with named fields will do better.

@@ -66,6 +66,8 @@ class State
[[nodiscard]] const auto& get_accounts() const noexcept { return m_accounts; }
};

using Withdrawals = std::vector<std::pair<address, uint64_t>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better will be to just define single using Withdrawal = std::pair<address, uint64_t>. But at this point a struct with named fields will do better.

test/statetest/statetest_loader.cpp Outdated Show resolved Hide resolved
test/t8n/t8n.cpp Outdated Show resolved Hide resolved
@rodiazet rodiazet force-pushed the withdrawals-t8n branch 3 times, most recently from adf1def to 08cb281 Compare April 18, 2023 09:44

// Apply withdrawals. Amount value is in gwei.
for (const auto& withdraw : withdrawals)
state.touch(withdraw.first).balance += intx::uint256{withdraw.second} * 1000000000;
Copy link
Member

@axic axic Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is better but avoids counting zeroes:

Suggested change
state.touch(withdraw.first).balance += intx::uint256{withdraw.second} * 1000000000;
state.touch(withdraw.first).balance += intx::uint256{withdraw.second} * 1e9;

Not sure what is our preference in the codebase, @chfast.

Could also have constexpr helpers like from_gwei, which also removes the need for comments.

test/state/state.hpp Outdated Show resolved Hide resolved
@rodiazet rodiazet force-pushed the withdrawals-t8n branch 2 times, most recently from ab1c40c to 02e6b21 Compare April 20, 2023 10:33
@axic axic force-pushed the withdrawals-t8n branch 8 times, most recently from 0fa05d0 to fe96d0e Compare April 20, 2023 11:50
EXPECT_EQ(bi.withdrawals[0].recipient, 0x0000000000000000000000000000000000000100_address);
EXPECT_EQ(bi.withdrawals[0].amount, 0x800000000);
EXPECT_EQ(bi.withdrawals[1].recipient, 0x0000000000000000000000000000000000000200_address);
EXPECT_EQ(bi.withdrawals[1].amount, 0xffffffffffffffff);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have the amount_wei helper, could also check for the output that here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

test/state/state.hpp Show resolved Hide resolved
}
};

using Withdrawals = std::vector<Withdrawal>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed.

@@ -32,3 +32,11 @@ TEST_F(state_transition, create_tx)

expect.post[create_address].code = bytes{0xFE};
}

TEST_F(state_transition, apply_withdrawal)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be in "create" test suite. You need a new file, e.g. "block" or "env".

EXPECT_EQ(bi.number, 0);
EXPECT_EQ(bi.withdrawals.size(), 2);
EXPECT_EQ(bi.withdrawals[0].recipient, 0x0000000000000000000000000000000000000100_address);
EXPECT_EQ(bi.withdrawals[0].get_amount(), intx::uint256{0x800000000} * 1'000'000'000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should rather test .amount_in_gwei here.

Copy link
Collaborator Author

@rodiazet rodiazet Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to test get_mount in one go too, but I can add additional check for gwei value only.

struct Withdrawal
{
address recipient;
/// The amount is denominated in gwei.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The amount is denominated in gwei.
/// The amount is denominated in gwei.

or use ///<.

Copy link
Member

@chfast chfast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please squash.

Copy link
Member

@axic axic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me but please address @chfast's comments first.

Co-authored-by: Alex Beregszaszi <alex@rtfs.hu>
@rodiazet rodiazet merged commit 62fb126 into master Apr 21, 2023
@rodiazet rodiazet deleted the withdrawals-t8n branch April 21, 2023 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants