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
/
Copy pathaccounts_test.go
126 lines (105 loc) · 3.31 KB
/
accounts_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
package commander
import (
"context"
"testing"
"github.com/Worldcoin/hubble-commander/eth"
"github.com/Worldcoin/hubble-commander/metrics"
"github.com/Worldcoin/hubble-commander/models"
st "github.com/Worldcoin/hubble-commander/storage"
"github.com/Worldcoin/hubble-commander/utils/ref"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type AccountsTestSuite struct {
*require.Assertions
suite.Suite
teardown func() error
testClient *eth.TestClient
cmd *Commander
}
func (s *AccountsTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
}
func (s *AccountsTestSuite) SetupTest() {
testStorage, err := st.NewTestStorage()
s.NoError(err)
s.teardown = testStorage.Teardown
s.testClient, err = eth.NewTestClient()
s.NoError(err)
s.cmd = &Commander{
storage: testStorage.Storage,
client: s.testClient.Client,
metrics: metrics.NewCommanderMetrics(),
}
}
func (s *AccountsTestSuite) TearDownTest() {
s.testClient.Close()
err := s.teardown()
s.NoError(err)
}
func (s *AccountsTestSuite) TestSyncAccounts() {
accounts := s.registerBatchAccount()
accounts = append(accounts, s.registerSingleAccount())
latestBlockNumber, err := s.testClient.GetLatestBlockNumber()
s.NoError(err)
err = s.cmd.syncAccounts(context.Background(), 0, *latestBlockNumber)
s.NoError(err)
s.validateAccountsAfterSync(accounts)
}
func (s *AccountsTestSuite) TestSyncSingleAccounts() {
account := s.registerSingleAccount()
latestBlockNumber, err := s.testClient.GetLatestBlockNumber()
s.NoError(err)
newAccountsCount, err := s.cmd.syncSingleAccounts(0, *latestBlockNumber)
s.NoError(err)
s.Equal(ref.Int(1), newAccountsCount)
accounts, err := s.cmd.storage.AccountTree.Leaves(&account.PublicKey)
s.NoError(err)
s.Len(accounts, 1)
s.Equal(account.PubKeyID, accounts[0].PubKeyID)
}
func (s *AccountsTestSuite) TestSyncBatchAccounts() {
accounts := s.registerBatchAccount()
latestBlockNumber, err := s.testClient.GetLatestBlockNumber()
s.NoError(err)
newAccountsCount, err := s.cmd.syncBatchAccounts(context.Background(), 0, *latestBlockNumber)
s.NoError(err)
s.Equal(ref.Int(st.AccountBatchSize), newAccountsCount)
s.validateAccountsAfterSync(accounts)
}
func (s *AccountsTestSuite) registerSingleAccount() models.AccountLeaf {
publicKey := models.PublicKey{2, 3, 4}
pubKeyID, err := s.testClient.RegisterAccountAndWait(&publicKey)
s.NoError(err)
return models.AccountLeaf{
PubKeyID: *pubKeyID,
PublicKey: publicKey,
}
}
func (s *AccountsTestSuite) registerBatchAccount() []models.AccountLeaf {
publicKeys := make([]models.PublicKey, st.AccountBatchSize)
for i := range publicKeys {
publicKeys[i] = models.PublicKey{1, 1, byte(i)}
}
pubKeyIDs, err := s.testClient.RegisterBatchAccountAndWait(publicKeys)
s.NoError(err)
accounts := make([]models.AccountLeaf, st.AccountBatchSize)
for i := range accounts {
accounts[i] = models.AccountLeaf{
PubKeyID: pubKeyIDs[i],
PublicKey: publicKeys[i],
}
}
return accounts
}
func (s *AccountsTestSuite) validateAccountsAfterSync(accounts []models.AccountLeaf) {
for i := range accounts {
leaves, err := s.cmd.storage.AccountTree.Leaves(&accounts[i].PublicKey)
s.NoError(err)
s.Len(leaves, 1)
s.Equal(accounts[i].PubKeyID, leaves[0].PubKeyID)
}
}
func TestAccountsTestSuite(t *testing.T) {
suite.Run(t, new(AccountsTestSuite))
}