From 00b92e01d341ff19e3d6a2895491e391c512a03e Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Mon, 11 Apr 2022 15:59:22 +0800 Subject: [PATCH] Fetch Non Finalized Deposits Better (#10505) --- beacon-chain/cache/depositcache/deposits_cache.go | 7 +++---- beacon-chain/cache/depositcache/deposits_cache_test.go | 4 ++-- beacon-chain/deterministic-genesis/service.go | 2 +- beacon-chain/powchain/service_test.go | 4 ++-- .../rpc/prysm/v1alpha1/validator/proposer_deposits.go | 2 +- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/beacon-chain/cache/depositcache/deposits_cache.go b/beacon-chain/cache/depositcache/deposits_cache.go index 9320b6548..c209c4e37 100644 --- a/beacon-chain/cache/depositcache/deposits_cache.go +++ b/beacon-chain/cache/depositcache/deposits_cache.go @@ -36,7 +36,7 @@ type DepositFetcher interface { DepositByPubkey(ctx context.Context, pubKey []byte) (*ethpb.Deposit, *big.Int) DepositsNumberAndRootAtHeight(ctx context.Context, blockHeight *big.Int) (uint64, [32]byte) FinalizedDeposits(ctx context.Context) *FinalizedDeposits - NonFinalizedDeposits(ctx context.Context, untilBlk *big.Int) []*ethpb.Deposit + NonFinalizedDeposits(ctx context.Context, lastFinalizedIndex int64, untilBlk *big.Int) []*ethpb.Deposit } // FinalizedDeposits stores the trie of deposits that have been included @@ -246,7 +246,7 @@ func (dc *DepositCache) FinalizedDeposits(ctx context.Context) *FinalizedDeposit // NonFinalizedDeposits returns the list of non-finalized deposits until the given block number (inclusive). // If no block is specified then this method returns all non-finalized deposits. -func (dc *DepositCache) NonFinalizedDeposits(ctx context.Context, untilBlk *big.Int) []*ethpb.Deposit { +func (dc *DepositCache) NonFinalizedDeposits(ctx context.Context, lastFinalizedIndex int64, untilBlk *big.Int) []*ethpb.Deposit { ctx, span := trace.StartSpan(ctx, "DepositsCache.NonFinalizedDeposits") defer span.End() dc.depositsLock.RLock() @@ -256,10 +256,9 @@ func (dc *DepositCache) NonFinalizedDeposits(ctx context.Context, untilBlk *big. return dc.allDeposits(untilBlk) } - lastFinalizedDepositIndex := dc.finalizedDeposits.MerkleTrieIndex var deposits []*ethpb.Deposit for _, d := range dc.deposits { - if (d.Index > lastFinalizedDepositIndex) && (untilBlk == nil || untilBlk.Uint64() >= d.Eth1BlockHeight) { + if (d.Index > lastFinalizedIndex) && (untilBlk == nil || untilBlk.Uint64() >= d.Eth1BlockHeight) { deposits = append(deposits, d.Deposit) } } diff --git a/beacon-chain/cache/depositcache/deposits_cache_test.go b/beacon-chain/cache/depositcache/deposits_cache_test.go index e281f563b..42f635b34 100644 --- a/beacon-chain/cache/depositcache/deposits_cache_test.go +++ b/beacon-chain/cache/depositcache/deposits_cache_test.go @@ -554,7 +554,7 @@ func TestNonFinalizedDeposits_ReturnsAllNonFinalizedDeposits(t *testing.T) { }) dc.InsertFinalizedDeposits(context.Background(), 1) - deps := dc.NonFinalizedDeposits(context.Background(), nil) + deps := dc.NonFinalizedDeposits(context.Background(), 1, nil) assert.Equal(t, 2, len(deps)) } @@ -611,7 +611,7 @@ func TestNonFinalizedDeposits_ReturnsNonFinalizedDepositsUpToBlockNumber(t *test }) dc.InsertFinalizedDeposits(context.Background(), 1) - deps := dc.NonFinalizedDeposits(context.Background(), big.NewInt(10)) + deps := dc.NonFinalizedDeposits(context.Background(), 1, big.NewInt(10)) assert.Equal(t, 1, len(deps)) } diff --git a/beacon-chain/deterministic-genesis/service.go b/beacon-chain/deterministic-genesis/service.go index 661c70164..502ad30b1 100644 --- a/beacon-chain/deterministic-genesis/service.go +++ b/beacon-chain/deterministic-genesis/service.go @@ -155,7 +155,7 @@ func (_ *Service) FinalizedDeposits(_ context.Context) *depositcache.FinalizedDe } // NonFinalizedDeposits mocks out the deposit cache functionality for interop. -func (_ *Service) NonFinalizedDeposits(_ context.Context, _ *big.Int) []*ethpb.Deposit { +func (_ *Service) NonFinalizedDeposits(_ context.Context, _ int64, _ *big.Int) []*ethpb.Deposit { return []*ethpb.Deposit{} } diff --git a/beacon-chain/powchain/service_test.go b/beacon-chain/powchain/service_test.go index 1f8b49d50..a11dbcf8f 100644 --- a/beacon-chain/powchain/service_test.go +++ b/beacon-chain/powchain/service_test.go @@ -480,8 +480,8 @@ func TestInitDepositCacheWithFinalization_OK(t *testing.T) { s.chainStartData.Chainstarted = true require.NoError(t, s.initDepositCaches(context.Background(), ctrs)) - - deps := s.cfg.depositCache.NonFinalizedDeposits(context.Background(), nil) + fDeposits := s.cfg.depositCache.FinalizedDeposits(ctx) + deps := s.cfg.depositCache.NonFinalizedDeposits(context.Background(), fDeposits.MerkleTrieIndex, nil) assert.Equal(t, 0, len(deps)) } diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_deposits.go b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_deposits.go index 34938ac63..0d04983d0 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_deposits.go +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_deposits.go @@ -161,7 +161,7 @@ func (vs *Server) depositTrie(ctx context.Context, canonicalEth1Data *ethpb.Eth1 finalizedDeposits := vs.DepositFetcher.FinalizedDeposits(ctx) depositTrie = finalizedDeposits.Deposits - upToEth1DataDeposits := vs.DepositFetcher.NonFinalizedDeposits(ctx, canonicalEth1DataHeight) + upToEth1DataDeposits := vs.DepositFetcher.NonFinalizedDeposits(ctx, finalizedDeposits.MerkleTrieIndex, canonicalEth1DataHeight) insertIndex := finalizedDeposits.MerkleTrieIndex + 1 for _, dep := range upToEth1DataDeposits {