Skip to content

Commit

Permalink
Add Test For Earliest Voting Block (#7882)
Browse files Browse the repository at this point in the history
  • Loading branch information
nisdas committed Nov 21, 2020
1 parent f8a855d commit ac60ff2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
5 changes: 3 additions & 2 deletions beacon-chain/powchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,15 +782,16 @@ func (s *Service) cacheHeadersForEth1DataVote(ctx context.Context) error {
// determines the earliest voting block from which to start caching all our previous headers from.
func (s *Service) determineEarliestVotingBlock(ctx context.Context, followBlock uint64) (uint64, error) {
genesisTime := s.chainStartData.GenesisTime
currSlot := helpers.CurrentSlot(genesisTime)

// In the event genesis has not occurred yet, we just request go back follow_distance blocks.
if genesisTime == 0 {
if genesisTime == 0 || currSlot == 0 {
earliestBlk := uint64(0)
if followBlock > params.BeaconConfig().Eth1FollowDistance {
earliestBlk = followBlock - params.BeaconConfig().Eth1FollowDistance
}
return earliestBlk, nil
}
currSlot := helpers.CurrentSlot(genesisTime)
votingTime := helpers.VotingPeriodStartTime(genesisTime, currSlot)
followBackDist := 2 * params.BeaconConfig().SecondsPerETH1Block * params.BeaconConfig().Eth1FollowDistance
if followBackDist > votingTime {
Expand Down
52 changes: 52 additions & 0 deletions beacon-chain/powchain/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,55 @@ func TestInitDepositCache_OK(t *testing.T) {
require.NoError(t, s.initDepositCaches(context.Background(), ctrs))
require.Equal(t, 3, len(s.depositCache.PendingContainers(context.Background(), nil)))
}

func TestNewService_EarliestVotingBlock(t *testing.T) {
testAcc, err := contracts.Setup()
require.NoError(t, err, "Unable to set up simulated backend")
beaconDB, _ := dbutil.SetupDB(t)
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
HTTPEndPoint: endpoint,
DepositContract: testAcc.ContractAddr,
BeaconDB: beaconDB,
})
require.NoError(t, err, "unable to setup web3 ETH1.0 chain service")
web3Service.eth1DataFetcher = &goodFetcher{backend: testAcc.Backend}
// simulated backend sets eth1 block
// time as 10 seconds
conf := params.BeaconConfig()
conf.SecondsPerETH1Block = 10
conf.Eth1FollowDistance = 50
params.OverrideBeaconConfig(conf)
defer func() {
params.UseMainnetConfig()
}()

// Genesis not set
followBlock := uint64(2000)
blk, err := web3Service.determineEarliestVotingBlock(context.Background(), followBlock)
require.NoError(t, err)
assert.Equal(t, followBlock-conf.Eth1FollowDistance, blk, "unexpected earliest voting block")

// Genesis is set.

numToForward := 1500
// forward 1500 blocks
for i := 0; i < numToForward; i++ {
testAcc.Backend.Commit()
}
currTime := testAcc.Backend.Blockchain().CurrentHeader().Time
now := time.Now()
err = testAcc.Backend.AdjustTime(now.Sub(time.Unix(int64(currTime), 0)))
require.NoError(t, err)
testAcc.Backend.Commit()

currTime = testAcc.Backend.Blockchain().CurrentHeader().Time
web3Service.latestEth1Data.BlockHeight = testAcc.Backend.Blockchain().CurrentHeader().Number.Uint64()
web3Service.latestEth1Data.BlockTime = testAcc.Backend.Blockchain().CurrentHeader().Time
web3Service.chainStartData.GenesisTime = currTime

// With a current slot of zero, only request follow_blocks behind.
blk, err = web3Service.determineEarliestVotingBlock(context.Background(), followBlock)
require.NoError(t, err)
assert.Equal(t, followBlock-conf.Eth1FollowDistance, blk, "unexpected earliest voting block")

}

0 comments on commit ac60ff2

Please sign in to comment.