Skip to content

Commit

Permalink
Amend DepositNumberAndRootAtHeight Function to Fix Off-By-One (#8809)
Browse files Browse the repository at this point in the history
* amend deposit cache and add comprehensive unit tests to fix off-by-one error

* fix tests

* still need to +1 deposit index
  • Loading branch information
rauljordan authored Apr 24, 2021
1 parent 8cda50f commit 3d96e3c
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 116 deletions.
19 changes: 9 additions & 10 deletions beacon-chain/cache/depositcache/deposits_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,19 @@ func (dc *DepositCache) AllDeposits(ctx context.Context, untilBlk *big.Int) []*e
return deposits
}

// DepositsNumberAndRootAtHeight returns number of deposits made prior to blockheight and the
// root that corresponds to the latest deposit at that blockheight.
// DepositsNumberAndRootAtHeight returns the number of deposits that exist at a specified
// block height. If no deposits are found at that block height, we return the number of deposits
// at the first block height we find right beneath it. If nothing is found at all, we return 0 and
// the empty root.
func (dc *DepositCache) DepositsNumberAndRootAtHeight(ctx context.Context, blockHeight *big.Int) (uint64, [32]byte) {
ctx, span := trace.StartSpan(ctx, "DepositsCache.DepositsNumberAndRootAtHeight")
defer span.End()
dc.depositsLock.RLock()
defer dc.depositsLock.RUnlock()
heightIdx := sort.Search(len(dc.deposits), func(i int) bool { return dc.deposits[i].Eth1BlockHeight > blockHeight.Uint64() })
// send the deposit root of the empty trie, if eth1follow distance is greater than the time of the earliest
// deposit.
if heightIdx == 0 {
return 0, [32]byte{}
for i := len(dc.deposits) - 1; i >= 0; i-- {
if dc.deposits[i].Eth1BlockHeight <= blockHeight.Uint64() {
return uint64(dc.deposits[i].Index) + 1, bytesutil.ToBytes32(dc.deposits[i].DepositRoot)
}
}
return uint64(heightIdx), bytesutil.ToBytes32(dc.deposits[heightIdx-1].DepositRoot)
return 0, params.BeaconConfig().ZeroHash
}

// DepositByPubkey looks through historical deposits and finds one which contains
Expand Down
239 changes: 133 additions & 106 deletions beacon-chain/cache/depositcache/deposits_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,121 +154,148 @@ func TestAllDeposits_FiltersDepositUpToAndIncludingBlockNumber(t *testing.T) {
assert.Equal(t, 5, len(d))
}

func TestDepositsNumberAndRootAtHeight_ReturnsAppropriateCountAndRoot(t *testing.T) {
dc, err := New()
require.NoError(t, err)

dc.deposits = []*dbpb.DepositContainer{
{
Eth1BlockHeight: 10,
Deposit: &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, 48),
WithdrawalCredentials: make([]byte, 32),
Signature: make([]byte, 96),
},
func TestDepositsNumberAndRootAtHeight(t *testing.T) {
wantedRoot := bytesutil.PadTo([]byte("root"), 32)
t.Run("requesting_last_item_works", func(t *testing.T) {
dc, err := New()
require.NoError(t, err)
dc.deposits = []*dbpb.DepositContainer{
{
Eth1BlockHeight: 10,
Index: 0,
Deposit: &ethpb.Deposit{},
},
},
{
Eth1BlockHeight: 10,
Deposit: &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, 48),
WithdrawalCredentials: make([]byte, 32),
Signature: make([]byte, 96),
},
{
Eth1BlockHeight: 10,
Index: 1,
Deposit: &ethpb.Deposit{},
},
},
{
Eth1BlockHeight: 10,
Deposit: &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, 48),
WithdrawalCredentials: make([]byte, 32),
Signature: make([]byte, 96),
},
{
Eth1BlockHeight: 11,
Index: 2,
Deposit: &ethpb.Deposit{},
},
},
{
Eth1BlockHeight: 10,
Deposit: &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, 48),
WithdrawalCredentials: make([]byte, 32),
Signature: make([]byte, 96),
},
{
Eth1BlockHeight: 13,
Index: 3,
Deposit: &ethpb.Deposit{},
DepositRoot: wantedRoot,
},
},
{
Eth1BlockHeight: 11,
Deposit: &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, 48),
WithdrawalCredentials: make([]byte, 32),
Signature: make([]byte, 96),
},
}
n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(13))
assert.Equal(t, 4, int(n))
require.DeepEqual(t, wantedRoot, root[:])
})
t.Run("only_one_item", func(t *testing.T) {
dc, err := New()
require.NoError(t, err)

dc.deposits = []*dbpb.DepositContainer{
{
Eth1BlockHeight: 10,
Index: 0,
Deposit: &ethpb.Deposit{},
DepositRoot: wantedRoot,
},
DepositRoot: bytesutil.PadTo([]byte("root"), 32),
},
{
Eth1BlockHeight: 12,
Deposit: &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, 48),
WithdrawalCredentials: make([]byte, 32),
Signature: make([]byte, 96),
},
}
n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(10))
assert.Equal(t, 1, int(n))
require.DeepEqual(t, wantedRoot, root[:])
})
t.Run("none_at_height_some_below", func(t *testing.T) {
dc, err := New()
require.NoError(t, err)

dc.deposits = []*dbpb.DepositContainer{
{
Eth1BlockHeight: 8,
Index: 0,
Deposit: &ethpb.Deposit{},
},
},
{
Eth1BlockHeight: 12,
Deposit: &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, 48),
WithdrawalCredentials: make([]byte, 32),
Signature: make([]byte, 96),
},
{
Eth1BlockHeight: 9,
Index: 1,
Deposit: &ethpb.Deposit{},
DepositRoot: wantedRoot,
},
},
}

n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(11))
assert.Equal(t, 5, int(n))
assert.Equal(t, bytesutil.ToBytes32([]byte("root")), root)
}

func TestDepositsNumberAndRootAtHeight_ReturnsEmptyTrieIfBlockHeightLessThanOldestDeposit(t *testing.T) {
dc, err := New()
require.NoError(t, err)

dc.deposits = []*dbpb.DepositContainer{
{
Eth1BlockHeight: 10,
Deposit: &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, 48),
WithdrawalCredentials: make([]byte, 32),
Signature: make([]byte, 96),
},
{
Eth1BlockHeight: 11,
Index: 2,
Deposit: &ethpb.Deposit{},
},
DepositRoot: bytesutil.PadTo([]byte("root"), 32),
},
{
Eth1BlockHeight: 11,
Deposit: &ethpb.Deposit{
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, 48),
WithdrawalCredentials: make([]byte, 32),
Signature: make([]byte, 96),
},
}
n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(10))
assert.Equal(t, 2, int(n))
require.DeepEqual(t, wantedRoot, root[:])
})
t.Run("none_at_height_none_below", func(t *testing.T) {
dc, err := New()
require.NoError(t, err)

dc.deposits = []*dbpb.DepositContainer{
{
Eth1BlockHeight: 8,
Index: 0,
Deposit: &ethpb.Deposit{},
DepositRoot: wantedRoot,
},
DepositRoot: bytesutil.PadTo([]byte("root"), 32),
},
}

n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(2))
assert.Equal(t, 0, int(n))
assert.Equal(t, [32]byte{}, root)
}
n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(7))
assert.Equal(t, 0, int(n))
require.DeepEqual(t, params.BeaconConfig().ZeroHash, root)
})
t.Run("none_at_height_one_below", func(t *testing.T) {
dc, err := New()
require.NoError(t, err)

dc.deposits = []*dbpb.DepositContainer{
{
Eth1BlockHeight: 8,
Index: 0,
Deposit: &ethpb.Deposit{},
DepositRoot: wantedRoot,
},
}
n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(10))
assert.Equal(t, 1, int(n))
require.DeepEqual(t, wantedRoot, root[:])
})
t.Run("some_greater_some_lower", func(t *testing.T) {
dc, err := New()
require.NoError(t, err)

dc.deposits = []*dbpb.DepositContainer{
{
Eth1BlockHeight: 8,
Index: 0,
Deposit: &ethpb.Deposit{},
},
{
Eth1BlockHeight: 8,
Index: 1,
Deposit: &ethpb.Deposit{},
},
{
Eth1BlockHeight: 9,
Index: 2,
Deposit: &ethpb.Deposit{},
DepositRoot: wantedRoot,
},
{
Eth1BlockHeight: 10,
Index: 3,
Deposit: &ethpb.Deposit{},
},
{
Eth1BlockHeight: 10,
Index: 4,
Deposit: &ethpb.Deposit{},
},
}
n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(9))
assert.Equal(t, 3, int(n))
require.DeepEqual(t, wantedRoot, root[:])
})
}

func TestDepositByPubkey_ReturnsFirstMatchingDeposit(t *testing.T) {
Expand Down

0 comments on commit 3d96e3c

Please sign in to comment.