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 pathsubmit_batch_test.go
98 lines (82 loc) · 3.03 KB
/
submit_batch_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
package eth
import (
"testing"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/batchtype"
"github.com/Worldcoin/hubble-commander/utils"
"github.com/Worldcoin/hubble-commander/utils/merkletree"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type SubmitBatchTestSuite struct {
*require.Assertions
suite.Suite
client *TestClient
commitment models.TxCommitmentWithTxs
}
func (s *SubmitBatchTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
}
func (s *SubmitBatchTestSuite) SetupTest() {
client, err := NewTestClient()
s.NoError(err)
s.client = client
s.commitment = models.TxCommitmentWithTxs{
TxCommitment: models.TxCommitment{
CommitmentBase: models.CommitmentBase{
Type: batchtype.Transfer,
PostStateRoot: utils.RandomHash(),
},
FeeReceiver: uint32(1234),
CombinedSignature: models.MakeRandomSignature(),
},
Transactions: utils.RandomBytes(12),
}
}
func (s *SubmitBatchTestSuite) TearDownTest() {
s.client.Close()
}
func (s *SubmitBatchTestSuite) TestSubmitTransfersBatchAndWait_ReturnsCorrectBatch() {
batchID := models.MakeUint256(1)
commitment := s.commitment
accountRoot, err := s.client.AccountRegistry.Root(nil)
s.NoError(err)
commitment.CalcAndSetBodyHash(accountRoot)
commitmentRoot := utils.HashTwo(commitment.LeafHash(), merkletree.GetZeroHash(0))
minFinalisationBlock := s.getMinFinalisationBlock()
batch, err := s.client.SubmitTransfersBatchAndWait(&batchID, []models.CommitmentWithTxs{&commitment})
s.NoError(err)
s.Equal(batchID, batch.ID)
s.Equal(batchtype.Transfer, batch.Type)
s.Equal(commitmentRoot, *batch.Hash)
s.GreaterOrEqual(*batch.FinalisationBlock, minFinalisationBlock)
s.Equal(common.BytesToHash(accountRoot[:]), *batch.AccountTreeRoot)
}
func (s *SubmitBatchTestSuite) TestSubmitCreate2TransfersBatchAndWait_ReturnsCorrectBatch() {
batchID := models.MakeUint256(1)
commitment := s.commitment
commitment.Type = batchtype.Create2Transfer
accountRoot, err := s.client.AccountRegistry.Root(nil)
s.NoError(err)
commitment.CalcAndSetBodyHash(accountRoot)
commitmentRoot := utils.HashTwo(commitment.LeafHash(), merkletree.GetZeroHash(0))
minFinalisationBlock := s.getMinFinalisationBlock()
batch, err := s.client.SubmitCreate2TransfersBatchAndWait(&batchID, []models.CommitmentWithTxs{&commitment})
s.NoError(err)
s.Equal(batchID, batch.ID)
s.Equal(batchtype.Create2Transfer, batch.Type)
s.Equal(commitmentRoot, *batch.Hash)
s.GreaterOrEqual(*batch.FinalisationBlock, minFinalisationBlock)
s.Equal(common.BytesToHash(accountRoot[:]), *batch.AccountTreeRoot)
}
func (s *SubmitBatchTestSuite) getMinFinalisationBlock() uint32 {
latestBlockNumber, err := s.client.Blockchain.GetLatestBlockNumber()
s.NoError(err)
blocksToFinalise, err := s.client.GetBlocksToFinalise()
s.NoError(err)
return uint32(*latestBlockNumber) + uint32(*blocksToFinalise)
}
func TestSubmitTransferTestSuite(t *testing.T) {
suite.Run(t, new(SubmitBatchTestSuite))
}