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	depositAmount, err := common.Float40(10400).BigInt()
146	require.Nil(t, err)
147	l1UserTxs = []common.L1Tx{
148		{
149			FromIdx:       0,
150			DepositAmount: depositAmount,
151			Amount:        big.NewInt(0),
152			TokenID:       1,
153			FromBJJ:       users[0].BJJ.Public().Compress(),
154			FromEthAddr:   users[0].Addr,
155			ToIdx:         0,
156			Type:          common.TxTypeCreateAccountDeposit,
157			UserOrigin:    true,
158		},
159	}
160	l2Txs = []common.PoolL2Tx{
161		{
162			FromIdx: 256,
163			ToIdx:   256,
164			TokenID: 1,
165			Amount:  big.NewInt(1000),
166			Nonce:   0,
167			Fee:     0,
168			Type:    common.TxTypeTransfer,
169		},
170	}
171
172	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
173
174	return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs
175}
176
177// GenerateTxsZKInputs1 generates the transactions for the TestZKInputs1
178func GenerateTxsZKInputs1(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
179	// same values than in the js test
180	users = GenerateJsUsers(t)
181
182	l1UserTxs = []common.L1Tx{
183		{
184			FromIdx:       0,
185			DepositAmount: big.NewInt(16000000),
186			Amount:        big.NewInt(0),
187			TokenID:       1,
188			FromBJJ:       users[0].BJJ.Public().Compress(),
189			FromEthAddr:   users[0].Addr,
190			ToIdx:         0,
191			Type:          common.TxTypeCreateAccountDeposit,
192			UserOrigin:    true,
193		},
194		{
195			FromIdx:       0,
196			DepositAmount: big.NewInt(16000000),
197			Amount:        big.NewInt(0),
198			TokenID:       1,
199			FromBJJ:       users[1].BJJ.Public().Compress(),
200			FromEthAddr:   users[1].Addr,
201			ToIdx:         0,
202			Type:          common.TxTypeCreateAccountDeposit,
203			UserOrigin:    true,
204		},
205	}
206	l2Txs = []common.PoolL2Tx{
207		{
208			FromIdx: 256,
209			ToIdx:   256,
210			TokenID: 1,
211			Amount:  big.NewInt(1000),
212			Nonce:   0,
213			Fee:     126,
214			Type:    common.TxTypeTransfer,
215		},
216	}
217
218	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
219
220	coordIdxs = []common.Idx{257}
221	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
222}
223
224// GenerateTxsZKInputs2 generates the transactions for the TestZKInputs2
225func GenerateTxsZKInputs2(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
226	// same values than in the js test
227	users = GenerateJsUsers(t)
228
229	l1UserTxs = []common.L1Tx{
230		{
231			FromIdx:       0,
232			DepositAmount: big.NewInt(16000000),
233			Amount:        big.NewInt(0),
234			TokenID:       1,
235			FromBJJ:       users[0].BJJ.Public().Compress(),
236			FromEthAddr:   users[0].Addr,
237			ToIdx:         0,
238			Type:          common.TxTypeCreateAccountDeposit,
239			UserOrigin:    true,
240		},
241		{
242			FromIdx:       0,
243			DepositAmount: big.NewInt(16000000),
244			Amount:        big.NewInt(0),
245			TokenID:       1,
246			FromBJJ:       users[1].BJJ.Public().Compress(),
247			FromEthAddr:   users[1].Addr,
248			ToIdx:         0,
249			Type:          common.TxTypeCreateAccountDeposit,
250			UserOrigin:    true,
251		},
252		{
253			FromIdx:       0,
254			DepositAmount: big.NewInt(16000000),
255			Amount:        big.NewInt(0),
256			TokenID:       1,
257			FromBJJ:       users[2].BJJ.Public().Compress(),
258			FromEthAddr:   users[2].Addr,
259			ToIdx:         0,
260			Type:          common.TxTypeCreateAccountDeposit,
261			UserOrigin:    true,
262		},
263		{
264			FromIdx:       0,
265			DepositAmount: big.NewInt(16000000),
266			Amount:        big.NewInt(0),
267			TokenID:       1,
268			FromBJJ:       users[3].BJJ.Public().Compress(),
269			FromEthAddr:   users[3].Addr,
270			ToIdx:         0,
271			Type:          common.TxTypeCreateAccountDeposit,
272			UserOrigin:    true,
273		},
274	}
275	l2Txs = []common.PoolL2Tx{
276		{
277			FromIdx: 256,
278			ToIdx:   258,
279			TokenID: 1,
280			Amount:  big.NewInt(1000),
281			Nonce:   0,
282			Fee:     126,
283			Type:    common.TxTypeTransfer,
284		},
285		{
286			FromIdx: 256,
287			ToIdx:   259,
288			TokenID: 1,
289			Amount:  big.NewInt(1000),
290			Nonce:   1,
291			Fee:     126,
292			Type:    common.TxTypeTransfer,
293		},
294	}
295
296	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
297	l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
298
299	coordIdxs = []common.Idx{257}
300	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
301}
302
303// GenerateTxsZKInputs3 generates the transactions for the TestZKInputs3
304func GenerateTxsZKInputs3(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
305	// same values than in the js test
306	users = GenerateJsUsers(t)
307
308	l1UserTxs = []common.L1Tx{
309		{
310			FromIdx:       0,
311			DepositAmount: big.NewInt(16000000),
312			Amount:        big.NewInt(0),
313			TokenID:       1,
314			FromBJJ:       users[0].BJJ.Public().Compress(),
315			FromEthAddr:   users[0].Addr,
316			ToIdx:         0,
317			Type:          common.TxTypeCreateAccountDeposit,
318			UserOrigin:    true,
319		},
320		{
321			FromIdx:       0,
322			DepositAmount: big.NewInt(16000000),
323			Amount:        big.NewInt(0),
324			TokenID:       1,
325			FromBJJ:       users[1].BJJ.Public().Compress(),
326			FromEthAddr:   users[1].Addr,
327			ToIdx:         0,
328			Type:          common.TxTypeCreateAccountDeposit,
329			UserOrigin:    true,
330		},
331		{
332			FromIdx:       0,
333			DepositAmount: big.NewInt(16000000),
334			Amount:        big.NewInt(0),
335			TokenID:       1,
336			FromBJJ:       users[2].BJJ.Public().Compress(),
337			FromEthAddr:   users[2].Addr,
338			ToIdx:         0,
339			Type:          common.TxTypeCreateAccountDeposit,
340			UserOrigin:    true,
341		},
342		{
343			FromIdx:       0,
344			DepositAmount: big.NewInt(16000000),
345			Amount:        big.NewInt(1000),
346			TokenID:       1,
347			FromBJJ:       users[3].BJJ.Public().Compress(),
348			FromEthAddr:   users[3].Addr,
349			ToIdx:         258,
350			Type:          common.TxTypeCreateAccountDepositTransfer,
351			UserOrigin:    true,
352		},
353	}
354	l2Txs = []common.PoolL2Tx{
355		{
356			FromIdx: 256,
357			ToIdx:   258,
358			TokenID: 1,
359			Amount:  big.NewInt(1000),
360			Nonce:   0,
361			Fee:     126,
362			Type:    common.TxTypeTransfer,
363		},
364		{
365			FromIdx: 256,
366			ToIdx:   259,
367			TokenID: 1,
368			Amount:  big.NewInt(1000),
369			Nonce:   1,
370			Fee:     126,
371			Type:    common.TxTypeTransfer,
372		},
373	}
374
375	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
376	l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
377
378	coordIdxs = []common.Idx{257}
379	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
380}
381
382// GenerateTxsZKInputs4 generates the transactions for the TestZKInputs4
383func GenerateTxsZKInputs4(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
384	// same values than in the js test
385	users = GenerateJsUsers(t)
386
387	l1UserTxs = []common.L1Tx{
388		{
389			FromIdx:       0,
390			DepositAmount: big.NewInt(16000000),
391			Amount:        big.NewInt(0),
392			TokenID:       1,
393			FromBJJ:       users[0].BJJ.Public().Compress(),
394			FromEthAddr:   users[0].Addr,
395			ToIdx:         0,
396			Type:          common.TxTypeCreateAccountDeposit,
397			UserOrigin:    true,
398		},
399		{
400			FromIdx:       0,
401			DepositAmount: big.NewInt(16000000),
402			Amount:        big.NewInt(0),
403			TokenID:       1,
404			FromBJJ:       users[1].BJJ.Public().Compress(),
405			FromEthAddr:   users[1].Addr,
406			ToIdx:         0,
407			Type:          common.TxTypeCreateAccountDeposit,
408			UserOrigin:    true,
409		},
410		{
411			FromIdx:       0,
412			DepositAmount: big.NewInt(16000000),
413			Amount:        big.NewInt(0),
414			TokenID:       1,
415			FromBJJ:       users[2].BJJ.Public().Compress(),
416			FromEthAddr:   users[2].Addr,
417			ToIdx:         0,
418			Type:          common.TxTypeCreateAccountDeposit,
419			UserOrigin:    true,
420		},
421		{
422			FromIdx:       0,
423			DepositAmount: big.NewInt(16000000),
424			Amount:        big.NewInt(1000),
425			TokenID:       1,
426			FromBJJ:       users[3].BJJ.Public().Compress(),
427			FromEthAddr:   users[3].Addr,
428			ToIdx:         258,
429			Type:          common.TxTypeCreateAccountDepositTransfer,
430			UserOrigin:    true,
431		},
432		{
433			FromIdx:       258,
434			DepositAmount: big.NewInt(16000000),
435			Amount:        big.NewInt(1000),
436			TokenID:       1,
437			FromEthAddr:   users[2].Addr,
438			ToIdx:         259,
439			Type:          common.TxTypeDepositTransfer,
440			UserOrigin:    true,
441		},
442	}
443	l2Txs = []common.PoolL2Tx{
444		{
445			FromIdx: 256,
446			ToIdx:   258,
447			TokenID: 1,
448			Amount:  big.NewInt(1000),
449			Nonce:   0,
450			Fee:     126,
451			Type:    common.TxTypeTransfer,
452		},
453		{
454			FromIdx: 256,
455			ToIdx:   259,
456			TokenID: 1,
457			Amount:  big.NewInt(1000),
458			Nonce:   1,
459			Fee:     126,
460			Type:    common.TxTypeTransfer,
461		},
462	}
463
464	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
465	l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
466
467	coordIdxs = []common.Idx{257}
468	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
469}
470
471// GenerateTxsZKInputs5 generates the transactions for the TestZKInputs5
472func GenerateTxsZKInputs5(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
473	// same values than in the js test
474	users = GenerateJsUsers(t)
475
476	l1UserTxs = []common.L1Tx{
477		{
478			FromIdx:       0,
479			DepositAmount: big.NewInt(16000000),
480			Amount:        big.NewInt(0),
481			TokenID:       1,
482			FromBJJ:       users[0].BJJ.Public().Compress(),
483			FromEthAddr:   users[0].Addr,
484			ToIdx:         0,
485			Type:          common.TxTypeCreateAccountDeposit,
486			UserOrigin:    true,
487		},
488		{
489			FromIdx:       0,
490			DepositAmount: big.NewInt(16000000),
491			Amount:        big.NewInt(0),
492			TokenID:       1,
493			FromBJJ:       users[1].BJJ.Public().Compress(),
494			FromEthAddr:   users[1].Addr,
495			ToIdx:         0,
496			Type:          common.TxTypeCreateAccountDeposit,
497			UserOrigin:    true,
498		},
499		{
500			FromIdx:       257,
501			DepositAmount: big.NewInt(0),
502			Amount:        big.NewInt(1000),
503			TokenID:       1,
504			FromBJJ:       users[1].BJJ.Public().Compress(),
505			FromEthAddr:   users[1].Addr,
506			ToIdx:         1,
507			Type:          common.TxTypeForceExit,
508			UserOrigin:    true,
509		},
510	}
511	l2Txs = []common.PoolL2Tx{
512		{
513			FromIdx: 256,
514			ToIdx:   257,
515			TokenID: 1,
516			Amount:  big.NewInt(1000),
517			Nonce:   0,
518			Fee:     126,
519			Type:    common.TxTypeTransfer,
520		},
521		{
522			FromIdx: 256,
523			ToIdx:   1,
524			TokenID: 1,
525			Amount:  big.NewInt(1000),
526			Nonce:   1,
527			Fee:     126,
528			Type:    common.TxTypeExit,
529		},
530	}
531
532	l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
533	l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
534
535	coordIdxs = []common.Idx{257}
536	return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
537}