This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
state_update_test.go
114 lines (100 loc) · 2.78 KB
/
state_update_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package storage
import (
"testing"
"github.com/Worldcoin/hubble-commander/models"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type StateUpdateTestSuite struct {
*require.Assertions
suite.Suite
storage *TestStorage
}
func (s *StateUpdateTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
}
func (s *StateUpdateTestSuite) SetupTest() {
var err error
s.storage, err = NewTestStorage()
s.NoError(err)
}
func (s *StateUpdateTestSuite) TearDownTest() {
err := s.storage.Teardown()
s.NoError(err)
}
func (s *StateUpdateTestSuite) TestAddStateUpdate_AddAndRetrieve() {
update := &models.StateUpdate{
ID: 0,
CurrentRoot: common.BytesToHash([]byte{1, 2, 3}),
PrevRoot: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
PrevStateLeaf: models.StateLeaf{
StateID: 12,
DataHash: [32]byte{1, 2, 3, 4},
UserState: models.UserState{
PubKeyID: 1,
TokenID: models.MakeUint256(1),
Balance: models.MakeUint256(100),
Nonce: models.MakeUint256(0),
},
},
}
err := s.storage.StateTree.addStateUpdate(update)
s.NoError(err)
res, err := s.storage.StateTree.getStateUpdate(update.ID)
s.NoError(err)
s.Equal(update, res)
}
func (s *StateUpdateTestSuite) TestGetStateUpdateByRootHash_NonexistentUpdate() {
res, err := s.storage.StateTree.getStateUpdate(10)
s.ErrorIs(err, NewNotFoundError("state update"))
s.Nil(res)
}
func (s *StateUpdateTestSuite) TestRemoveStateUpdate() {
updates := []models.StateUpdate{
{
ID: 0,
CurrentRoot: common.BytesToHash([]byte{1}),
PrevRoot: common.BytesToHash([]byte{2}),
PrevStateLeaf: models.StateLeaf{
StateID: 12,
DataHash: [32]byte{1, 2, 3, 4},
UserState: models.UserState{
PubKeyID: 1,
TokenID: models.MakeUint256(1),
Balance: models.MakeUint256(100),
Nonce: models.MakeUint256(0),
},
},
},
{
ID: 1,
CurrentRoot: common.BytesToHash([]byte{2}),
PrevRoot: common.BytesToHash([]byte{2}),
PrevStateLeaf: models.StateLeaf{
StateID: 12,
DataHash: [32]byte{1, 2, 3, 4},
UserState: models.UserState{
PubKeyID: 1,
TokenID: models.MakeUint256(1),
Balance: models.MakeUint256(100),
Nonce: models.MakeUint256(0),
},
},
},
}
err := s.storage.StateTree.addStateUpdate(&updates[0])
s.NoError(err)
err = s.storage.StateTree.addStateUpdate(&updates[1])
s.NoError(err)
err = s.storage.StateTree.removeStateUpdate(1)
s.NoError(err)
_, err = s.storage.StateTree.getStateUpdate(updates[1].ID)
s.ErrorIs(err, NewNotFoundError("state update"))
res, err := s.storage.StateTree.getStateUpdate(updates[0].ID)
s.NoError(err)
s.Equal(&updates[0], res)
}
func TestStateUpdateTestSuite(t *testing.T) {
suite.Run(t, new(StateUpdateTestSuite))
}