github.com/aergoio/aergo@v1.3.1/consensus/impl/dpos/dpos_test.go (about) 1 package dpos 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/aergoio/aergo/consensus/impl/dpos/slot" 8 "github.com/aergoio/aergo/types" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 const ( 13 nSlots = 5 14 bpInterval = 1 15 ) 16 17 func TestDposFutureBlock(t *testing.T) { 18 slot.Init(bpInterval) 19 20 dpos := &DPoS{} 21 22 block := types.NewBlock(nil, nil, nil, nil, nil, time.Now().Add(3*time.Second).UnixNano()) 23 assert.True(t, !dpos.VerifyTimestamp(block), "future block check failed") 24 25 block = types.NewBlock(nil, nil, nil, nil, nil, time.Now().UnixNano()) 26 assert.True(t, dpos.VerifyTimestamp(block), "future block check failed") 27 28 block = types.NewBlock(nil, nil, nil, nil, nil, time.Now().Add(-time.Second).UnixNano()) 29 assert.True(t, dpos.VerifyTimestamp(block), "future block check failed") 30 31 } 32 33 func TestDposPastBlock(t *testing.T) { 34 slot.Init(bpInterval) 35 36 dpos := &DPoS{} 37 38 block0 := types.NewBlock(nil, nil, nil, nil, nil, time.Now().UnixNano()) 39 assert.True(t, dpos.VerifyTimestamp(block0), "invalid timestamp") 40 41 time.Sleep(time.Second) 42 now := time.Now().UnixNano() 43 block1 := types.NewBlock(block0, nil, nil, nil, nil, now) 44 assert.True(t, dpos.VerifyTimestamp(block1), "invalid timestamp") 45 46 // Add LIB, manually. 47 dpos.Status = &Status{libState: &libStatus{}} 48 dpos.Status.libState.Lib = newBlockInfo(block1) 49 block2 := types.NewBlock(block0, nil, nil, nil, nil, now) 50 // Test whether a block number error is raised or not by checking the 51 // return value. 52 assert.True(t, !dpos.VerifyTimestamp(block2), "block number error must be raised") 53 }