Skip to content

Commit

Permalink
Add Back Timestamp Related Deposit Processing (#10806)
Browse files Browse the repository at this point in the history
* use it

* add test

* fix

Co-authored-by: terencechain <terence@prysmaticlabs.com>
  • Loading branch information
nisdas and terencechain authored Jun 4, 2022
1 parent 2fc3d41 commit d099c27
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/powchain/block_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions beacon-chain/powchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 28 additions & 2 deletions beacon-chain/powchain/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand All @@ -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()
Expand Down Expand Up @@ -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: &ethpb.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
Expand Down

0 comments on commit d099c27

Please sign in to comment.