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