github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/evidence/store_test.go (about) 1 package evidence 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 dbm "github.com/tendermint/tm-db" 10 11 "github.com/tendermint/tendermint/types" 12 ) 13 14 //------------------------------------------- 15 16 func TestStoreAddDuplicate(t *testing.T) { 17 assert := assert.New(t) 18 19 db := dbm.NewMemDB() 20 store := NewStore(db) 21 22 priority := int64(10) 23 ev := types.NewMockEvidence(2, time.Now().UTC(), 1, []byte("val1")) 24 25 added, err := store.AddNewEvidence(ev, priority) 26 require.NoError(t, err) 27 assert.True(added) 28 29 // cant add twice 30 added, err = store.AddNewEvidence(ev, priority) 31 require.NoError(t, err) 32 assert.False(added) 33 } 34 35 func TestStoreCommitDuplicate(t *testing.T) { 36 assert := assert.New(t) 37 38 db := dbm.NewMemDB() 39 store := NewStore(db) 40 41 priority := int64(10) 42 ev := types.NewMockEvidence(2, time.Now().UTC(), 1, []byte("val1")) 43 44 store.MarkEvidenceAsCommitted(ev) 45 46 added, err := store.AddNewEvidence(ev, priority) 47 require.NoError(t, err) 48 assert.False(added) 49 } 50 51 func TestStoreMark(t *testing.T) { 52 assert := assert.New(t) 53 54 db := dbm.NewMemDB() 55 store := NewStore(db) 56 57 // before we do anything, priority/pending are empty 58 priorityEv := store.PriorityEvidence() 59 pendingEv := store.PendingEvidence(-1) 60 assert.Equal(0, len(priorityEv)) 61 assert.Equal(0, len(pendingEv)) 62 63 priority := int64(10) 64 ev := types.NewMockEvidence(2, time.Now().UTC(), 1, []byte("val1")) 65 66 added, err := store.AddNewEvidence(ev, priority) 67 require.NoError(t, err) 68 assert.True(added) 69 70 // get the evidence. verify. should be uncommitted 71 ei := store.GetInfo(ev.Height(), ev.Hash()) 72 assert.Equal(ev, ei.Evidence) 73 assert.Equal(priority, ei.Priority) 74 assert.False(ei.Committed) 75 76 // new evidence should be returns in priority/pending 77 priorityEv = store.PriorityEvidence() 78 pendingEv = store.PendingEvidence(-1) 79 assert.Equal(1, len(priorityEv)) 80 assert.Equal(1, len(pendingEv)) 81 82 // priority is now empty 83 store.MarkEvidenceAsBroadcasted(ev) 84 priorityEv = store.PriorityEvidence() 85 pendingEv = store.PendingEvidence(-1) 86 assert.Equal(0, len(priorityEv)) 87 assert.Equal(1, len(pendingEv)) 88 89 // priority and pending are now empty 90 store.MarkEvidenceAsCommitted(ev) 91 priorityEv = store.PriorityEvidence() 92 pendingEv = store.PendingEvidence(-1) 93 assert.Equal(0, len(priorityEv)) 94 assert.Equal(0, len(pendingEv)) 95 96 // evidence should show committed 97 newPriority := int64(0) 98 ei = store.GetInfo(ev.Height(), ev.Hash()) 99 assert.Equal(ev, ei.Evidence) 100 assert.Equal(newPriority, ei.Priority) 101 assert.True(ei.Committed) 102 } 103 104 func TestStorePriority(t *testing.T) { 105 assert := assert.New(t) 106 107 db := dbm.NewMemDB() 108 store := NewStore(db) 109 110 // sorted by priority and then height 111 cases := []struct { 112 ev types.MockEvidence 113 priority int64 114 }{ 115 {types.NewMockEvidence(2, time.Now().UTC(), 1, []byte("val1")), 17}, 116 {types.NewMockEvidence(5, time.Now().UTC(), 2, []byte("val2")), 15}, 117 {types.NewMockEvidence(10, time.Now().UTC(), 2, []byte("val2")), 13}, 118 {types.NewMockEvidence(100, time.Now().UTC(), 2, []byte("val2")), 11}, 119 {types.NewMockEvidence(90, time.Now().UTC(), 2, []byte("val2")), 11}, 120 {types.NewMockEvidence(80, time.Now().UTC(), 2, []byte("val2")), 11}, 121 } 122 123 for _, c := range cases { 124 added, err := store.AddNewEvidence(c.ev, c.priority) 125 require.NoError(t, err) 126 assert.True(added) 127 } 128 129 evList := store.PriorityEvidence() 130 for i, ev := range evList { 131 assert.Equal(ev, cases[i].ev) 132 } 133 }