From d099c2790b59eab53800602b0ccf52c05f31b812 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Sat, 4 Jun 2022 09:58:03 +0800 Subject: [PATCH] Add Back Timestamp Related Deposit Processing (#10806) * use it * add test * fix Co-authored-by: terencechain --- beacon-chain/powchain/block_reader.go | 2 +- beacon-chain/powchain/service.go | 15 +++++++++----- beacon-chain/powchain/service_test.go | 30 +++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/beacon-chain/powchain/block_reader.go b/beacon-chain/powchain/block_reader.go index 6ce9cda9f..704713065 100644 --- a/beacon-chain/powchain/block_reader.go +++ b/beacon-chain/powchain/block_reader.go @@ -210,7 +210,7 @@ func (s *Service) retrieveHeaderInfo(ctx context.Context, bNum uint64) (*types.H return nil, err } if blk == nil { - return nil, errors.New("header with the provided number does not exist") + return nil, errors.Errorf("header with the number %d does not exist", bNum) } if err := s.headerCache.AddHeader(blk); err != nil { return nil, err diff --git a/beacon-chain/powchain/service.go b/beacon-chain/powchain/service.go index 3a3dc8034..ac4b1aaa7 100644 --- a/beacon-chain/powchain/service.go +++ b/beacon-chain/powchain/service.go @@ -369,12 +369,17 @@ func (s *Service) ETH1ConnectionErrors() []error { // refers to the latest eth1 block which follows the condition: eth1_timestamp + // SECONDS_PER_ETH1_BLOCK * ETH1_FOLLOW_DISTANCE <= current_unix_time -func (s *Service) followedBlockHeight(_ context.Context) (uint64, error) { - latestValidBlock := uint64(0) - if s.latestEth1Data.BlockHeight > params.BeaconConfig().Eth1FollowDistance { - latestValidBlock = s.latestEth1Data.BlockHeight - params.BeaconConfig().Eth1FollowDistance +func (s *Service) followedBlockHeight(ctx context.Context) (uint64, error) { + followTime := params.BeaconConfig().Eth1FollowDistance * params.BeaconConfig().SecondsPerETH1Block + latestBlockTime := uint64(0) + if s.latestEth1Data.BlockTime > followTime { + latestBlockTime = s.latestEth1Data.BlockTime - followTime } - return latestValidBlock, nil + blk, err := s.BlockByTimestamp(ctx, latestBlockTime) + if err != nil { + return 0, err + } + return blk.Number.Uint64(), nil } func (s *Service) initDepositCaches(ctx context.Context, ctrs []*ethpb.DepositContainer) error { diff --git a/beacon-chain/powchain/service_test.go b/beacon-chain/powchain/service_test.go index fba5c3d89..10d2e430e 100644 --- a/beacon-chain/powchain/service_test.go +++ b/beacon-chain/powchain/service_test.go @@ -81,7 +81,8 @@ func (g *goodNotifier) StateFeed() *event.Feed { } type goodFetcher struct { - backend *backends.SimulatedBackend + backend *backends.SimulatedBackend + blockNumMap map[uint64]*gethTypes.Header } func (_ *goodFetcher) Close() {} @@ -104,12 +105,15 @@ func (g *goodFetcher) HeaderByHash(_ context.Context, hash common.Hash) (*gethTy } func (g *goodFetcher) HeaderByNumber(_ context.Context, number *big.Int) (*gethTypes.Header, error) { - if g.backend == nil { + if g.backend == nil && g.blockNumMap == nil { return &gethTypes.Header{ Number: big.NewInt(15), Time: 150, }, nil } + if g.blockNumMap != nil { + return g.blockNumMap[number.Uint64()], nil + } var header *gethTypes.Header if number == nil { header = g.backend.Blockchain().CurrentHeader() @@ -864,6 +868,28 @@ func TestService_CacheBlockHeaders(t *testing.T) { assert.Equal(t, 5, rClient.numOfCalls) } +func TestService_FollowBlock(t *testing.T) { + followTime := params.BeaconConfig().Eth1FollowDistance * params.BeaconConfig().SecondsPerETH1Block + followTime += 10000 + bMap := make(map[uint64]*gethTypes.Header) + for i := uint64(3000); i > 0; i-- { + bMap[i] = &gethTypes.Header{ + Number: big.NewInt(int64(i)), + Time: followTime + (i * 40), + } + } + s := &Service{ + cfg: &config{eth1HeaderReqLimit: 1000}, + eth1DataFetcher: &goodFetcher{blockNumMap: bMap}, + headerCache: newHeaderCache(), + latestEth1Data: ðpb.LatestETH1Data{BlockTime: (3000 * 40) + followTime, BlockHeight: 3000}, + } + h, err := s.followedBlockHeight(context.Background()) + assert.NoError(t, err) + // With a much higher blocktime, the follow height is respectively shortened. + assert.Equal(t, uint64(2283), h) +} + type slowRPCClient struct { limit int numOfCalls int