arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/txsets/txhelpers.go
test/txsets/txhelpers.goRaw
  1// Package txsets contains Til sets of transactions & Transactions generation
  2// that are used at tests of other packages of hermez-node
  3//nolint:gomnd
  4package txsets
  5
  6import (
  7	"encoding/hex"
  8	"math/big"
  9	"strconv"
 10	"testing"
 11
 12	ethCommon "github.com/ethereum/go-ethereum/common"
 13	"github.com/hermeznetwork/hermez-node/common"
 14	"github.com/hermeznetwork/hermez-node/test/til"
 15	"github.com/iden3/go-iden3-crypto/babyjub"
 16	"github.com/stretchr/testify/assert"
 17	"github.com/stretchr/testify/require"
 18)
 19
 20// The methods from this file are used at txprocessor package to test the
 21// ZKInputs generation & at tests of the test/zkproof to test the integration
 22// of the ZKInputs generation with the proof server
 23
 24// GenerateJsUsers generates the same values than in the js test
 25func GenerateJsUsers(t *testing.T) []til.User {
 26	// same values than in the js test
 27	// skJsHex is equivalent to the 0000...000i js private key in commonjs
 28	skJsHex := []string{"7eb258e61862aae75c6c1d1f7efae5006ffc9e4d5596a6ff95f3df4ea209ea7f",
 29		"c005700f76f4b4cec710805c21595688648524df0a9d467afae537b7a7118819",
 30		"b373d14c67fb2a517bf4ac831c93341eec8e1b38dbc14e7d725b292a7cf84707",
 31		"2064b68d04a7aaae0ac3b36bf6f1850b380f1423be94a506c531940bd4a48b76"}
 32	addrHex := []string{"0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
 33		"0x2b5ad5c4795c026514f8317c7a215e218dccd6cf",
 34		"0x6813eb9362372eef6200f3b1dbc3f819671cba69",
 35		"0x1eff47bc3a10a45d4b230b5d10e37751fe6aa718"}
 36	var users []til.User
 37	for i := 0; i < len(skJsHex); i++ {
 38		skJs, err := hex.DecodeString(skJsHex[i])
 39		require.NoError(t, err)
 40		var sk babyjub.PrivateKey
 41		copy(sk[:], skJs)
 42		// bjj := sk.Public()
 43		user := til.User{
 44			Name: strconv.Itoa(i),
 45			BJJ:  &sk,
 46			Addr: ethCommon.HexToAddress(addrHex[i]),
 47		}
 48		users = append(users, user)
 49	}
 50	assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021",
 51		users[0].BJJ.Public().String())
 52	assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909",
 53		users[1].BJJ.Public().String())
 54	assert.Equal(t, "38ffa002724562eb2a952a2503e206248962406cf16392ff32759b6f2a41fe11",
 55		users[2].BJJ.Public().String())
 56	assert.Equal(t, "c719e6401190be7fa7fbfcd3448fe2755233c01575341a3b09edadf5454f760b",
 57		users[3].BJJ.Public().String())
 58
 59	return users
 60}
 61
 62func signL2Tx(t *testing.T, chainID uint16, user til.User, l2Tx common.PoolL2Tx) common.PoolL2Tx {
 63	toSign, err := l2Tx.HashToSign(chainID)
 64	require.NoError(t, err)
 65	sig := user.BJJ.SignPoseidon(toSign)
 66	l2Tx.Signature = sig.Compress()
 67	return l2Tx
 68}
 69
 70// GenerateTxsZKInputsHash0 generates the transactions for the TestZKInputsHash0
 71func GenerateTxsZKInputsHash0(t *testing.T, chainID uint16) (users []til.User,
 72	coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx,
 73	l2Txs []common.PoolL2Tx) {
 74	// same values than in the js test
 75	users = GenerateJsUsers(t)
 76
 77	l1UserTxs = []common.L1Tx{
 78		{
 79			FromIdx:       0,
 80			DepositAmount: big.NewInt(16000000),
 81			Amount:        big.NewInt(0),
 82			TokenID:       1,
 83			FromBJJ:       users[0].BJJ.Public().Compress(),
 84			FromEthAddr:   users[0].Addr,
 85			ToIdx:         0,
 86			Type:          common.TxTypeCreateAccountDeposit,
 87			UserOrigin:    true,
 88		},
 89	}
 90	l2Txs = []common.PoolL2Tx{
 91		{
 92			FromIdx: 256,
 93			ToIdx:   256,
 94			TokenID: 1,
 95			Amount:  big.NewInt(1000),
 96			Nonce:   0,
 97			Fee:     126,
 98			Type:    common.TxTypeTransfer,
 99		},
100	}
101
102	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
103
104	return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs
105}
106
107// GenerateTxsZKInputsHash1 generates the transactions for the TestZKInputsHash1
108func GenerateTxsZKInputsHash1(t *testing.T, chainID uint16) (users []til.User,
109	coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx,
110	l2Txs []common.PoolL2Tx) {
111	// same values than in the js test
112	users = GenerateJsUsers(t)
113	l1UserTxs = []common.L1Tx{
114		{
115			FromIdx:       0,
116			DepositAmount: big.NewInt(16000000),
117			Amount:        big.NewInt(0),
118			TokenID:       1,
119			FromBJJ:       users[0].BJJ.Public().Compress(),
120			FromEthAddr:   users[0].Addr,
121			ToIdx:         0,
122			Type:          common.TxTypeCreateAccountDeposit,
123			UserOrigin:    true,
124		},
125		{
126			FromIdx:       0,
127			DepositAmount: big.NewInt(16000000),
128			Amount:        big.NewInt(0),
129			TokenID:       1,
130			FromBJJ:       users[1].BJJ.Public().Compress(),
131			FromEthAddr:   users[1].Addr,
132			ToIdx:         0,
133			Type:          common.TxTypeCreateAccountDeposit,
134			UserOrigin:    true,
135		},
136	}
137	l2Txs = []common.PoolL2Tx{
138		{
139			FromIdx: 257,
140			ToIdx:   256,
141			TokenID: 1,
142			Amount:  big.NewInt(1000),
143			Nonce:   0,
144			Fee:     137,
145			Type:    common.TxTypeTransfer,
146		},
147	}
148
149	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
150
151	return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs
152}
153
154// GenerateTxsZKInputs0 generates the transactions for the TestZKInputs0
155func GenerateTxsZKInputs0(t *testing.T, chainID uint16) (users []til.User,
156	coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx,
157	l2Txs []common.PoolL2Tx) {
158	// same values than in the js test
159	users = GenerateJsUsers(t)
160
161	depositAmount, err := common.Float40(10400).BigInt()
162	require.Nil(t, err)
163	l1UserTxs = []common.L1Tx{
164		{
165			FromIdx:       0,
166			DepositAmount: depositAmount,
167			Amount:        big.NewInt(0),
168			TokenID:       1,
169			FromBJJ:       users[0].BJJ.Public().Compress(),
170			FromEthAddr:   users[0].Addr,
171			ToIdx:         0,
172			Type:          common.TxTypeCreateAccountDeposit,
173			UserOrigin:    true,
174		},
175	}
176	l2Txs = []common.PoolL2Tx{
177		{
178			FromIdx: 256,
179			ToIdx:   256,
180			TokenID: 1,
181			Amount:  big.NewInt(1000),
182			Nonce:   0,
183			Fee:     0,
184			Type:    common.TxTypeTransfer,
185		},
186	}
187
188	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
189
190	return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs
191}
192
193// GenerateTxsZKInputs1 generates the transactions for the TestZKInputs1
194func GenerateTxsZKInputs1(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
195	l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
196	// same values than in the js test
197	users = GenerateJsUsers(t)
198
199	l1UserTxs = []common.L1Tx{
200		{
201			FromIdx:       0,
202			DepositAmount: big.NewInt(16000000),
203			Amount:        big.NewInt(0),
204			TokenID:       1,
205			FromBJJ:       users[0].BJJ.Public().Compress(),
206			FromEthAddr:   users[0].Addr,
207			ToIdx:         0,
208			Type:          common.TxTypeCreateAccountDeposit,
209			UserOrigin:    true,
210		},
211		{
212			FromIdx:       0,
213			DepositAmount: big.NewInt(16000000),
214			Amount:        big.NewInt(0),
215			TokenID:       1,
216			FromBJJ:       users[1].BJJ.Public().Compress(),
217			FromEthAddr:   users[1].Addr,
218			ToIdx:         0,
219			Type:          common.TxTypeCreateAccountDeposit,
220			UserOrigin:    true,
221		},
222	}
223	l2Txs = []common.PoolL2Tx{
224		{
225			FromIdx: 256,
226			ToIdx:   256,
227			TokenID: 1,
228			Amount:  big.NewInt(1000),
229			Nonce:   0,
230			Fee:     126,
231			Type:    common.TxTypeTransfer,
232		},
233	}
234
235	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
236
237	coordIdxs = []common.Idx{257}
238	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
239}
240
241// GenerateTxsZKInputs2 generates the transactions for the TestZKInputs2
242func GenerateTxsZKInputs2(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
243	l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
244	// same values than in the js test
245	users = GenerateJsUsers(t)
246
247	l1UserTxs = []common.L1Tx{
248		{
249			FromIdx:       0,
250			DepositAmount: big.NewInt(16000000),
251			Amount:        big.NewInt(0),
252			TokenID:       1,
253			FromBJJ:       users[0].BJJ.Public().Compress(),
254			FromEthAddr:   users[0].Addr,
255			ToIdx:         0,
256			Type:          common.TxTypeCreateAccountDeposit,
257			UserOrigin:    true,
258		},
259		{
260			FromIdx:       0,
261			DepositAmount: big.NewInt(16000000),
262			Amount:        big.NewInt(0),
263			TokenID:       1,
264			FromBJJ:       users[1].BJJ.Public().Compress(),
265			FromEthAddr:   users[1].Addr,
266			ToIdx:         0,
267			Type:          common.TxTypeCreateAccountDeposit,
268			UserOrigin:    true,
269		},
270		{
271			FromIdx:       0,
272			DepositAmount: big.NewInt(16000000),
273			Amount:        big.NewInt(0),
274			TokenID:       1,
275			FromBJJ:       users[2].BJJ.Public().Compress(),
276			FromEthAddr:   users[2].Addr,
277			ToIdx:         0,
278			Type:          common.TxTypeCreateAccountDeposit,
279			UserOrigin:    true,
280		},
281		{
282			FromIdx:       0,
283			DepositAmount: big.NewInt(16000000),
284			Amount:        big.NewInt(0),
285			TokenID:       1,
286			FromBJJ:       users[3].BJJ.Public().Compress(),
287			FromEthAddr:   users[3].Addr,
288			ToIdx:         0,
289			Type:          common.TxTypeCreateAccountDeposit,
290			UserOrigin:    true,
291		},
292	}
293	l2Txs = []common.PoolL2Tx{
294		{
295			FromIdx: 256,
296			ToIdx:   258,
297			TokenID: 1,
298			Amount:  big.NewInt(1000),
299			Nonce:   0,
300			Fee:     126,
301			Type:    common.TxTypeTransfer,
302		},
303		{
304			FromIdx: 256,
305			ToIdx:   259,
306			TokenID: 1,
307			Amount:  big.NewInt(1000),
308			Nonce:   1,
309			Fee:     126,
310			Type:    common.TxTypeTransfer,
311		},
312	}
313
314	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
315	l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
316
317	coordIdxs = []common.Idx{257}
318	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
319}
320
321// GenerateTxsZKInputs3 generates the transactions for the TestZKInputs3
322func GenerateTxsZKInputs3(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
323	l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
324	// same values than in the js test
325	users = GenerateJsUsers(t)
326
327	l1UserTxs = []common.L1Tx{
328		{
329			FromIdx:       0,
330			DepositAmount: big.NewInt(16000000),
331			Amount:        big.NewInt(0),
332			TokenID:       1,
333			FromBJJ:       users[0].BJJ.Public().Compress(),
334			FromEthAddr:   users[0].Addr,
335			ToIdx:         0,
336			Type:          common.TxTypeCreateAccountDeposit,
337			UserOrigin:    true,
338		},
339		{
340			FromIdx:       0,
341			DepositAmount: big.NewInt(16000000),
342			Amount:        big.NewInt(0),
343			TokenID:       1,
344			FromBJJ:       users[1].BJJ.Public().Compress(),
345			FromEthAddr:   users[1].Addr,
346			ToIdx:         0,
347			Type:          common.TxTypeCreateAccountDeposit,
348			UserOrigin:    true,
349		},
350		{
351			FromIdx:       0,
352			DepositAmount: big.NewInt(16000000),
353			Amount:        big.NewInt(0),
354			TokenID:       1,
355			FromBJJ:       users[2].BJJ.Public().Compress(),
356			FromEthAddr:   users[2].Addr,
357			ToIdx:         0,
358			Type:          common.TxTypeCreateAccountDeposit,
359			UserOrigin:    true,
360		},
361		{
362			FromIdx:       0,
363			DepositAmount: big.NewInt(16000000),
364			Amount:        big.NewInt(1000),
365			TokenID:       1,
366			FromBJJ:       users[3].BJJ.Public().Compress(),
367			FromEthAddr:   users[3].Addr,
368			ToIdx:         258,
369			Type:          common.TxTypeCreateAccountDepositTransfer,
370			UserOrigin:    true,
371		},
372	}
373	l2Txs = []common.PoolL2Tx{
374		{
375			FromIdx: 256,
376			ToIdx:   258,
377			TokenID: 1,
378			Amount:  big.NewInt(1000),
379			Nonce:   0,
380			Fee:     126,
381			Type:    common.TxTypeTransfer,
382		},
383		{
384			FromIdx: 256,
385			ToIdx:   259,
386			TokenID: 1,
387			Amount:  big.NewInt(1000),
388			Nonce:   1,
389			Fee:     126,
390			Type:    common.TxTypeTransfer,
391		},
392	}
393
394	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
395	l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
396
397	coordIdxs = []common.Idx{257}
398	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
399}
400
401// GenerateTxsZKInputs4 generates the transactions for the TestZKInputs4
402func GenerateTxsZKInputs4(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
403	l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
404	// same values than in the js test
405	users = GenerateJsUsers(t)
406
407	l1UserTxs = []common.L1Tx{
408		{
409			FromIdx:       0,
410			DepositAmount: big.NewInt(16000000),
411			Amount:        big.NewInt(0),
412			TokenID:       1,
413			FromBJJ:       users[0].BJJ.Public().Compress(),
414			FromEthAddr:   users[0].Addr,
415			ToIdx:         0,
416			Type:          common.TxTypeCreateAccountDeposit,
417			UserOrigin:    true,
418		},
419		{
420			FromIdx:       0,
421			DepositAmount: big.NewInt(16000000),
422			Amount:        big.NewInt(0),
423			TokenID:       1,
424			FromBJJ:       users[1].BJJ.Public().Compress(),
425			FromEthAddr:   users[1].Addr,
426			ToIdx:         0,
427			Type:          common.TxTypeCreateAccountDeposit,
428			UserOrigin:    true,
429		},
430		{
431			FromIdx:       0,
432			DepositAmount: big.NewInt(16000000),
433			Amount:        big.NewInt(0),
434			TokenID:       1,
435			FromBJJ:       users[2].BJJ.Public().Compress(),
436			FromEthAddr:   users[2].Addr,
437			ToIdx:         0,
438			Type:          common.TxTypeCreateAccountDeposit,
439			UserOrigin:    true,
440		},
441		{
442			FromIdx:       0,
443			DepositAmount: big.NewInt(16000000),
444			Amount:        big.NewInt(1000),
445			TokenID:       1,
446			FromBJJ:       users[3].BJJ.Public().Compress(),
447			FromEthAddr:   users[3].Addr,
448			ToIdx:         258,
449			Type:          common.TxTypeCreateAccountDepositTransfer,
450			UserOrigin:    true,
451		},
452		{
453			FromIdx:       258,
454			DepositAmount: big.NewInt(16000000),
455			Amount:        big.NewInt(1000),
456			TokenID:       1,
457			FromEthAddr:   users[2].Addr,
458			ToIdx:         259,
459			Type:          common.TxTypeDepositTransfer,
460			UserOrigin:    true,
461		},
462	}
463	l2Txs = []common.PoolL2Tx{
464		{
465			FromIdx: 256,
466			ToIdx:   258,
467			TokenID: 1,
468			Amount:  big.NewInt(1000),
469			Nonce:   0,
470			Fee:     126,
471			Type:    common.TxTypeTransfer,
472		},
473		{
474			FromIdx: 256,
475			ToIdx:   259,
476			TokenID: 1,
477			Amount:  big.NewInt(1000),
478			Nonce:   1,
479			Fee:     126,
480			Type:    common.TxTypeTransfer,
481		},
482	}
483
484	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
485	l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
486
487	coordIdxs = []common.Idx{257}
488	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
489}
490
491// GenerateTxsZKInputs5 generates the transactions for the TestZKInputs5
492func GenerateTxsZKInputs5(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
493	l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
494	// same values than in the js test
495	users = GenerateJsUsers(t)
496
497	l1UserTxs = []common.L1Tx{
498		{
499			FromIdx:       0,
500			DepositAmount: big.NewInt(16000000),
501			Amount:        big.NewInt(0),
502			TokenID:       1,
503			FromBJJ:       users[0].BJJ.Public().Compress(),
504			FromEthAddr:   users[0].Addr,
505			ToIdx:         0,
506			Type:          common.TxTypeCreateAccountDeposit,
507			UserOrigin:    true,
508		},
509		{
510			FromIdx:       0,
511			DepositAmount: big.NewInt(16000000),
512			Amount:        big.NewInt(0),
513			TokenID:       1,
514			FromBJJ:       users[1].BJJ.Public().Compress(),
515			FromEthAddr:   users[1].Addr,
516			ToIdx:         0,
517			Type:          common.TxTypeCreateAccountDeposit,
518			UserOrigin:    true,
519		},
520		{
521			FromIdx:       257,
522			DepositAmount: big.NewInt(0),
523			Amount:        big.NewInt(1000),
524			TokenID:       1,
525			FromBJJ:       users[1].BJJ.Public().Compress(),
526			FromEthAddr:   users[1].Addr,
527			ToIdx:         1,
528			Type:          common.TxTypeForceExit,
529			UserOrigin:    true,
530		},
531	}
532	l2Txs = []common.PoolL2Tx{
533		{
534			FromIdx: 256,
535			ToIdx:   257,
536			TokenID: 1,
537			Amount:  big.NewInt(1000),
538			Nonce:   0,
539			Fee:     126,
540			Type:    common.TxTypeTransfer,
541		},
542		{
543			FromIdx: 256,
544			ToIdx:   1,
545			TokenID: 1,
546			Amount:  big.NewInt(1000),
547			Nonce:   1,
548			Fee:     126,
549			Type:    common.TxTypeExit,
550		},
551	}
552
553	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
554	l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
555
556	coordIdxs = []common.Idx{257}
557	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
558}