github.com/evdatsion/aphelion-dpos-bft@v0.32.1/evidence/pool_test.go (about) 1 package evidence 2 3 import ( 4 "os" 5 "sync" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 dbm "github.com/evdatsion/aphelion-dpos-bft/libs/db" 11 sm "github.com/evdatsion/aphelion-dpos-bft/state" 12 "github.com/evdatsion/aphelion-dpos-bft/types" 13 tmtime "github.com/evdatsion/aphelion-dpos-bft/types/time" 14 ) 15 16 func TestMain(m *testing.M) { 17 types.RegisterMockEvidences(cdc) 18 19 code := m.Run() 20 os.Exit(code) 21 } 22 23 func initializeValidatorState(valAddr []byte, height int64) dbm.DB { 24 stateDB := dbm.NewMemDB() 25 26 // create validator set and state 27 valSet := &types.ValidatorSet{ 28 Validators: []*types.Validator{ 29 {Address: valAddr}, 30 }, 31 } 32 state := sm.State{ 33 LastBlockHeight: 0, 34 LastBlockTime: tmtime.Now(), 35 Validators: valSet, 36 NextValidators: valSet.CopyIncrementProposerPriority(1), 37 LastHeightValidatorsChanged: 1, 38 ConsensusParams: types.ConsensusParams{ 39 Evidence: types.EvidenceParams{ 40 MaxAge: 1000000, 41 }, 42 }, 43 } 44 45 // save all states up to height 46 for i := int64(0); i < height; i++ { 47 state.LastBlockHeight = i 48 sm.SaveState(stateDB, state) 49 } 50 51 return stateDB 52 } 53 54 func TestEvidencePool(t *testing.T) { 55 56 valAddr := []byte("val1") 57 height := int64(5) 58 stateDB := initializeValidatorState(valAddr, height) 59 evidenceDB := dbm.NewMemDB() 60 pool := NewEvidencePool(stateDB, evidenceDB) 61 62 goodEvidence := types.NewMockGoodEvidence(height, 0, valAddr) 63 badEvidence := types.MockBadEvidence{MockGoodEvidence: goodEvidence} 64 65 // bad evidence 66 err := pool.AddEvidence(badEvidence) 67 assert.NotNil(t, err) 68 69 var wg sync.WaitGroup 70 wg.Add(1) 71 go func() { 72 <-pool.EvidenceWaitChan() 73 wg.Done() 74 }() 75 76 err = pool.AddEvidence(goodEvidence) 77 assert.Nil(t, err) 78 wg.Wait() 79 80 assert.Equal(t, 1, pool.evidenceList.Len()) 81 82 // if we send it again, it shouldnt change the size 83 err = pool.AddEvidence(goodEvidence) 84 assert.Nil(t, err) 85 assert.Equal(t, 1, pool.evidenceList.Len()) 86 } 87 88 func TestEvidencePoolIsCommitted(t *testing.T) { 89 // Initialization: 90 valAddr := []byte("validator_address") 91 height := int64(42) 92 stateDB := initializeValidatorState(valAddr, height) 93 evidenceDB := dbm.NewMemDB() 94 pool := NewEvidencePool(stateDB, evidenceDB) 95 96 // evidence not seen yet: 97 evidence := types.NewMockGoodEvidence(height, 0, valAddr) 98 assert.False(t, pool.IsCommitted(evidence)) 99 100 // evidence seen but not yet committed: 101 assert.NoError(t, pool.AddEvidence(evidence)) 102 assert.False(t, pool.IsCommitted(evidence)) 103 104 // evidence seen and committed: 105 pool.MarkEvidenceAsCommitted(height, []types.Evidence{evidence}) 106 assert.True(t, pool.IsCommitted(evidence)) 107 }