arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/txs_test.go
test/txs_test.goRaw
  1package test
  2
  3import (
  4	"math/big"
  5	"testing"
  6
  7	"github.com/hermeznetwork/hermez-node/common"
  8	"github.com/stretchr/testify/assert"
  9)
 10
 11func TestGenerateBlocks(t *testing.T) {
 12	set := `
 13		Deposit(1) A: 10
 14		Deposit(2) A: 20
 15		Deposit(1) B: 5
 16		CreateAccountDeposit(1) C: 5
 17		CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
 18
 19		Transfer(1) A-B: 6 (1)
 20		Transfer(1) B-D: 3 (1)
 21		Transfer(1) A-D: 1 (1)
 22
 23		// set new batch
 24		> batch
 25
 26		DepositTransfer(1) A-B: 15, 10 (1)
 27		Transfer(1) C-A : 3 (1)
 28		Transfer(2) A-B: 15 (1)
 29
 30		Deposit(1) User0: 20
 31		Deposit(3) User1: 20
 32		Transfer(1) User0-User1: 15 (1)
 33		Transfer(3) User1-User0: 15 (1)
 34		Transfer(1) A-C: 1 (1)
 35
 36		> batch
 37
 38		Transfer(1) User1-User0: 1 (1)
 39
 40		> block
 41
 42		// Exits
 43		Transfer(1) A-B: 1 (1)
 44		Exit(1) A: 5
 45	`
 46	tc := NewTestContext(t)
 47	blocks := tc.GenerateBlocks(set)
 48	assert.Equal(t, 2, len(blocks))
 49	assert.Equal(t, 3, len(blocks[0].Batches))
 50	assert.Equal(t, 1, len(blocks[1].Batches))
 51	assert.Equal(t, 5, len(blocks[0].Batches[0].L1UserTxs))
 52	assert.Equal(t, 0, len(blocks[1].Batches[0].L1UserTxs))
 53
 54	// Check expected values generated by each line
 55	// #0: Deposit(1) A: 10
 56	tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[0], common.TxTypeDeposit, "A1", "", big.NewInt(10), nil)
 57	// #1: Deposit(2) A: 20
 58	tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[1], common.TxTypeDeposit, "A2", "", big.NewInt(20), nil)
 59	// #2: Deposit(1) A: 20
 60	tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[2], common.TxTypeDeposit, "B1", "", big.NewInt(5), nil)
 61	// #3: CreateAccountDeposit(1) C: 5
 62	tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[3], common.TxTypeCreateAccountDeposit, "C1", "", big.NewInt(5), nil)
 63	// #4: CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
 64	tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[4], common.TxTypeCreateAccountDepositTransfer, "D1", "A1", big.NewInt(15), big.NewInt(10))
 65	// #5: Transfer(1) A-B: 6 (1)
 66	tc.checkL2TxParams(t, blocks[0].Batches[0].L2Txs[0], common.TxTypeTransfer, "A1", "B1", big.NewInt(6), common.BatchNum(0), common.Nonce(1))
 67	// #6: Transfer(1) B-D: 3 (1)
 68	tc.checkL2TxParams(t, blocks[0].Batches[0].L2Txs[1], common.TxTypeTransfer, "B1", "D1", big.NewInt(3), common.BatchNum(0), common.Nonce(1))
 69	// #7: Transfer(1) A-D: 1 (1)
 70	tc.checkL2TxParams(t, blocks[0].Batches[0].L2Txs[2], common.TxTypeTransfer, "A1", "D1", big.NewInt(1), common.BatchNum(0), common.Nonce(2))
 71	// change of Batch
 72	// #8: DepositTransfer(1) A-B: 15, 10 (1)
 73	tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[0], common.TxTypeDepositTransfer, "A1", "B1", big.NewInt(15), big.NewInt(10))
 74	// #9: Deposit(1) User0: 20
 75	tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[0], common.TxTypeDepositTransfer, "A1", "B1", big.NewInt(15), big.NewInt(10))
 76	// #10: Transfer(1) C-A : 3 (1)
 77	tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[0], common.TxTypeTransfer, "C1", "A1", big.NewInt(3), common.BatchNum(1), common.Nonce(1))
 78	// #11: Transfer(2) A-B: 15 (1)
 79	tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[1], common.TxTypeTransfer, "A2", "B2", big.NewInt(15), common.BatchNum(1), common.Nonce(1))
 80	// #12: Deposit(1) User0: 20
 81	tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[1], common.TxTypeDeposit, "User01", "", big.NewInt(20), nil)
 82	// #13: Deposit(3) User1: 20
 83	tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[2], common.TxTypeDeposit, "User13", "", big.NewInt(20), nil)
 84	// #14: Transfer(1) User0-User1: 15 (1)
 85	tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[2], common.TxTypeTransfer, "User01", "User11", big.NewInt(15), common.BatchNum(1), common.Nonce(1))
 86	// #15: Transfer(3) User1-User0: 15 (1)
 87	tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[3], common.TxTypeTransfer, "User13", "User03", big.NewInt(15), common.BatchNum(1), common.Nonce(1))
 88	// #16: Transfer(1) A-C: 1 (1)
 89	tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[4], common.TxTypeTransfer, "A1", "C1", big.NewInt(1), common.BatchNum(1), common.Nonce(3))
 90	// change of Batch
 91	// #17: Transfer(1) User1-User0: 1 (1)
 92	tc.checkL2TxParams(t, blocks[0].Batches[2].L2Txs[0], common.TxTypeTransfer, "User11", "User01", big.NewInt(1), common.BatchNum(2), common.Nonce(1))
 93	// change of Block (implies also a change of batch)
 94	// #18: Transfer(1) A-B: 1 (1)
 95	tc.checkL2TxParams(t, blocks[1].Batches[0].L2Txs[0], common.TxTypeTransfer, "A1", "B1", big.NewInt(1), common.BatchNum(3), common.Nonce(4))
 96}
 97
 98func (tc *TestContext) checkL1TxParams(t *testing.T, tx common.L1Tx, typ common.TxType, from, to string, loadAmount, amount *big.Int) {
 99	assert.Equal(t, typ, tx.Type)
100	if tx.FromIdx != nil {
101		assert.Equal(t, tc.accounts[from].Idx, *tx.FromIdx)
102	}
103	assert.Equal(t, tc.accounts[from].Addr.Hex(), tx.FromEthAddr.Hex())
104	assert.Equal(t, tc.accounts[from].BJJ.Public(), tx.FromBJJ)
105	if tx.ToIdx != common.Idx(0) {
106		assert.Equal(t, tc.accounts[to].Idx, tx.ToIdx)
107	}
108	if loadAmount != nil {
109		assert.Equal(t, loadAmount, tx.LoadAmount)
110	}
111	if amount != nil {
112		assert.Equal(t, amount, tx.Amount)
113	}
114}
115func (tc *TestContext) checkL2TxParams(t *testing.T, tx common.L2Tx, typ common.TxType, from, to string, amount *big.Int, batchNum common.BatchNum, nonce common.Nonce) {
116	assert.Equal(t, typ, tx.Type)
117	assert.Equal(t, tc.accounts[from].Idx, tx.FromIdx)
118	if tx.Type != common.TxTypeExit {
119		assert.Equal(t, tc.accounts[to].Idx, tx.ToIdx)
120	}
121	if amount != nil {
122		assert.Equal(t, amount, tx.Amount)
123	}
124	assert.Equal(t, batchNum, tx.BatchNum)
125	assert.Equal(t, nonce, tx.Nonce)
126}
127
128func TestGeneratePoolL2Txs(t *testing.T) {
129	set := `
130		PoolTransfer(1) A-B: 6 (1)
131		PoolTransfer(1) B-C: 3 (1)
132		PoolTransfer(1) C-A: 3 (1)
133		PoolTransfer(1) A-B: 1 (1)
134		PoolTransfer(2) A-B: 15 (1)
135		PoolTransfer(1) User0-User1: 15 (1)
136		PoolTransfer(3) User1-User0: 15 (1)
137		PoolTransfer(2) B-D: 3 (1)
138		PoolExit(1) A: 3
139	`
140	tc := NewTestContext(t)
141	poolL2Txs := tc.GeneratePoolL2Txs(set)
142	assert.Equal(t, 9, len(poolL2Txs))
143	assert.Equal(t, common.TxTypeTransfer, poolL2Txs[0].Type)
144	assert.Equal(t, common.TxTypeExit, poolL2Txs[8].Type)
145	assert.Equal(t, tc.accounts["B1"].Addr.Hex(), poolL2Txs[0].ToEthAddr.Hex())
146	assert.Equal(t, tc.accounts["B1"].BJJ.Public().String(), poolL2Txs[0].ToBJJ.String())
147	assert.Equal(t, tc.accounts["User11"].Addr.Hex(), poolL2Txs[5].ToEthAddr.Hex())
148	assert.Equal(t, tc.accounts["User11"].BJJ.Public().String(), poolL2Txs[5].ToBJJ.String())
149
150	assert.Equal(t, common.Nonce(1), poolL2Txs[0].Nonce)
151	assert.Equal(t, common.Nonce(2), poolL2Txs[3].Nonce)
152	assert.Equal(t, common.Nonce(3), poolL2Txs[8].Nonce)
153
154	// load another set in the same TestContext
155	set = `
156		PoolTransfer(1) A-B: 6 (1)
157		PoolTransfer(1) B-C: 3 (1)
158		PoolTransfer(1) A-C: 3 (1)
159	`
160	poolL2Txs = tc.GeneratePoolL2Txs(set)
161	assert.Equal(t, common.Nonce(4), poolL2Txs[0].Nonce)
162	assert.Equal(t, common.Nonce(2), poolL2Txs[1].Nonce)
163	assert.Equal(t, common.Nonce(5), poolL2Txs[2].Nonce)
164}