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

Add deposit index to beacon state #2232

Merged
merged 24 commits into from
Apr 14, 2019

Conversation

0xKiwi
Copy link
Contributor

@0xKiwi 0xKiwi commented Apr 11, 2019

Resolves #2199

Currently, we have an issue that we don't properly make sure deposits aren't duplicated. The spec has recently implemented a deposit_index in the beacon state that maintains what deposit index has been processed for the sake of ensuring duplicate deposits aren't sent in.

Changes made:

Beacon State now has DepositIndex which is:

  • Initialized as 0.
  • Incremented with each deposit.
  • Used as the reference for pruning pending deposits.

MerkleBranchHash32S -> MerkleProofHash32S

@@ -47,17 +46,7 @@ func (s *InitialSync) processState(msg p2p.Message) {
return
}

exists, blkNum, err := s.powchain.BlockExists(ctx, bytesutil.ToBytes32(finalizedState.LatestEth1Data.BlockHash32))
Copy link
Member

Choose a reason for hiding this comment

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

Why remove this? dont we still want to verify that the block exists?

Copy link
Contributor Author

@0xKiwi 0xKiwi Apr 11, 2019

Choose a reason for hiding this comment

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

Good catch, adding it back. My mistake.

// Deposits must be processed in order
if deposit.MerkleTreeIndex != beaconState.DepositIndex {
return fmt.Errorf(
"expected deposit merkle branch index to match beacon state deposit index, wanted: %d, received: %d",
Copy link
Member

Choose a reason for hiding this comment

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

Merkle branch index or merkle tree index?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thank you. Changed to merkle tree index.

@@ -113,7 +113,7 @@ func (db *BeaconDB) PrunePendingDeposits(ctx context.Context, b *big.Int) {
// added deposit and works backwards.
idx := -1
for i := depositListSize - 1; i >= 0; i-- {
if db.pendingDeposits[i].block.Cmp(b) == -1 {
if db.pendingDeposits[i].deposit.MerkleTreeIndex < merkleTreeIndex {
Copy link
Member

Choose a reason for hiding this comment

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

this relies on the pending deposits being sorted by their indexes, right now they are sorted by the block in which the deposit is logged in

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

@@ -146,6 +147,8 @@ func GenesisBeaconState(
if err != nil {
return nil, fmt.Errorf("could not process validator deposit: %v", err)
}

state.DepositIndex++
Copy link
Member

Choose a reason for hiding this comment

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

you should not increment it here, it should be in the ProcessDepositfunction

@@ -17,18 +17,18 @@ test_cases:
num_slots: 64 # Testing advancing state to exactly slot == SlotsPerEpoch
deposits:
- slot: 9223372036854775809
amount: 32
merkle_index: 0
amount: 0
Copy link
Member

Choose a reason for hiding this comment

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

wait , what is going on here ? why is this 1.
the same below. We always use 32 eth

@codecov
Copy link

codecov bot commented Apr 11, 2019

Codecov Report

Merging #2232 into master will decrease coverage by 0.7%.
The diff coverage is 89.47%.

@@            Coverage Diff             @@
##           master    #2232      +/-   ##
==========================================
- Coverage   69.57%   68.86%   -0.71%     
==========================================
  Files         116      116              
  Lines        9080     8882     -198     
==========================================
- Hits         6317     6117     -200     
+ Misses       2111     2108       -3     
- Partials      652      657       +5

@0xKiwi
Copy link
Contributor Author

0xKiwi commented Apr 11, 2019

Progress so far, I believe it's mostly done except with some bugs on the RPC side. Too late now so I'll resume tomorrow.

Also, @nisdas and I don't think the RPC should only return

deposit.index < current_state.deposit_index

Shouldn't it at least include the current deposit index, so the validators can send it?
And would the validators even need the previous deposits, doesn't it only need current and upcoming?

@0xKiwi 0xKiwi marked this pull request as ready for review April 11, 2019 23:43
@0xKiwi 0xKiwi requested review from prestonvanloon, nisdas, rauljordan and terencechain and removed request for prestonvanloon April 11, 2019 23:45
prestonvanloon
prestonvanloon previously approved these changes Apr 12, 2019
Copy link
Member

@prestonvanloon prestonvanloon left a comment

Choose a reason for hiding this comment

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

Looks amazing. Let's try in cluster before merge.

terencechain
terencechain previously approved these changes Apr 12, 2019
Copy link
Member

@terencechain terencechain left a comment

Choose a reason for hiding this comment

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

👍

var pendingDeps []*pbp2p.Deposit
for _, dep := range allPendingDeps {
if dep.MerkleTreeIndex >= beaconState.DepositIndex {
pendingDeps = append(pendingDeps, dep)
Copy link
Member

Choose a reason for hiding this comment

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

this isnt ordered, we want the indexes to be sorted in an ascending order.

Copy link
Member

Choose a reason for hiding this comment

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

nvm , ignore it, its sorted by the PendingDeposits function

nisdas
nisdas previously approved these changes Apr 12, 2019
Copy link
Member

@nisdas nisdas left a comment

Choose a reason for hiding this comment

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

Great job on this, looks good to me

var pendingDeps []*pbp2p.Deposit
for _, dep := range allPendingDeps {
if dep.MerkleTreeIndex >= beaconState.DepositIndex {
pendingDeps = append(pendingDeps, dep)
Copy link
Member

Choose a reason for hiding this comment

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

nvm , ignore it, its sorted by the PendingDeposits function

rauljordan
rauljordan previously approved these changes Apr 13, 2019
prestonvanloon
prestonvanloon previously approved these changes Apr 13, 2019
rauljordan
rauljordan previously approved these changes Apr 13, 2019
@rauljordan
Copy link
Contributor

Looks great, awesome job on this non-trivial feature!

prestonvanloon
prestonvanloon previously approved these changes Apr 13, 2019
@prestonvanloon prestonvanloon merged commit 6227948 into prysmaticlabs:master Apr 14, 2019
@0xKiwi 0xKiwi deleted the fix-deposits-sync branch June 6, 2019 13:48
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.

Regular Sync Inserts PostChainStart Deposits Again Into the State
5 participants