github.com/evdatsion/aphelion-dpos-bft@v0.32.1/evidence/store_test.go (about)

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