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 3
/
deposit_sub_tree_test.go
149 lines (130 loc) · 3.78 KB
/
deposit_sub_tree_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package storage
import (
"testing"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/utils"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
var (
depositSubtree = models.PendingDepositSubtree{
ID: models.MakeUint256(932),
Root: utils.RandomHash(),
Deposits: []models.PendingDeposit{
{
ID: models.DepositID{
SubtreeID: models.MakeUint256(932),
DepositIndex: models.MakeUint256(0),
},
ToPubKeyID: 3,
TokenID: models.MakeUint256(4),
L2Amount: models.MakeUint256(500),
},
{
ID: models.DepositID{
SubtreeID: models.MakeUint256(932),
DepositIndex: models.MakeUint256(1),
},
ToPubKeyID: 8,
TokenID: models.MakeUint256(9),
L2Amount: models.MakeUint256(1000),
},
},
}
)
type DepositSubtreeTestSuite struct {
*require.Assertions
suite.Suite
storage *TestStorage
}
func (s *DepositSubtreeTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
}
func (s *DepositSubtreeTestSuite) SetupTest() {
var err error
s.storage, err = NewTestStorage()
s.NoError(err)
}
func (s *DepositSubtreeTestSuite) TearDownTest() {
err := s.storage.Teardown()
s.NoError(err)
}
func (s *DepositSubtreeTestSuite) TestAddPendingDepositSubtree_AddAndRetrieve() {
err := s.storage.AddPendingDepositSubtree(&depositSubtree)
s.NoError(err)
actual, err := s.storage.GetPendingDepositSubtree(depositSubtree.ID)
s.NoError(err)
s.Equal(depositSubtree, *actual)
}
func (s *DepositSubtreeTestSuite) TestGetPendingDepositSubtree_NonexistentTree() {
_, err := s.storage.GetPendingDepositSubtree(depositSubtree.ID)
s.ErrorIs(err, NewNotFoundError("deposit sub tree"))
s.True(IsNotFoundError(err))
}
func (s *DepositSubtreeTestSuite) TestRemovePendingDepositSubtrees() {
subtrees := []models.PendingDepositSubtree{
{
ID: models.MakeUint256(1),
Root: utils.RandomHash(),
Deposits: []models.PendingDeposit{
{
ID: models.DepositID{
SubtreeID: models.MakeUint256(1),
DepositIndex: models.MakeUint256(0),
},
ToPubKeyID: 3,
TokenID: models.MakeUint256(4),
L2Amount: models.MakeUint256(500),
},
},
},
{
ID: models.MakeUint256(4),
Root: utils.RandomHash(),
Deposits: []models.PendingDeposit{
{
ID: models.DepositID{
SubtreeID: models.MakeUint256(4),
DepositIndex: models.MakeUint256(0),
},
ToPubKeyID: 8,
TokenID: models.MakeUint256(9),
L2Amount: models.MakeUint256(1000),
},
},
},
}
for i := range subtrees {
err := s.storage.AddPendingDepositSubtree(&subtrees[i])
s.NoError(err)
}
err := s.storage.RemovePendingDepositSubtrees(subtrees[0].ID, subtrees[1].ID)
s.NoError(err)
for i := range subtrees {
_, err = s.storage.GetPendingDepositSubtree(subtrees[i].ID)
s.ErrorIs(err, NewNotFoundError("deposit sub tree"))
}
}
func (s *DepositSubtreeTestSuite) TestRemovePendingDepositSubtrees_NonexistentTree() {
err := s.storage.RemovePendingDepositSubtrees(models.MakeUint256(1))
s.ErrorIs(err, NewNotFoundError("deposit sub tree"))
}
func (s *DepositSubtreeTestSuite) TestGetFirstPendingDepositSubtree() {
err := s.storage.AddPendingDepositSubtree(&depositSubtree)
s.NoError(err)
secondSubtree := depositSubtree
secondSubtree.ID = models.MakeUint256(1)
err = s.storage.AddPendingDepositSubtree(&secondSubtree)
s.NoError(err)
subtree, err := s.storage.GetFirstPendingDepositSubtree()
s.NoError(err)
s.Equal(secondSubtree, *subtree)
}
func (s *DepositSubtreeTestSuite) TestGetFirstPendingDepositSubtree_NoPendingDepositSubtrees() {
subtree, err := s.storage.GetFirstPendingDepositSubtree()
s.ErrorIs(err, NewNotFoundError("deposit sub tree"))
s.Nil(subtree)
}
func TestDepositSubtreeTestSuite(t *testing.T) {
suite.Run(t, new(DepositSubtreeTestSuite))
}