arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/til/txs.go
test/til/txs.goRaw
  1package til
  2
  3import (
  4	"crypto/ecdsa"
  5	"fmt"
  6	"math/big"
  7	"strconv"
  8	"strings"
  9	"time"
 10
 11	ethCommon "github.com/ethereum/go-ethereum/common"
 12	ethCrypto "github.com/ethereum/go-ethereum/crypto"
 13	"github.com/hermeznetwork/hermez-node/common"
 14	"github.com/hermeznetwork/hermez-node/log"
 15	"github.com/iden3/go-iden3-crypto/babyjub"
 16)
 17
 18// Context contains the data of the test
 19type Context struct {
 20	Instructions          []instruction
 21	accountsNames         []string
 22	Users                 map[string]*User
 23	lastRegisteredTokenID common.TokenID
 24	l1CreatedAccounts     map[string]*Account
 25
 26	// rollupConstMaxL1UserTx Maximum L1-user transactions allowed to be queued in a batch
 27	rollupConstMaxL1UserTx int
 28
 29	idx           int
 30	currBlock     common.BlockData
 31	currBatch     common.BatchData
 32	currBatchNum  int
 33	queues        [][]L1Tx
 34	toForgeNum    int
 35	openToForge   int
 36	currBatchTest struct {
 37		l1CoordinatorTxs []L1Tx
 38		l2Txs            []L2Tx
 39	}
 40}
 41
 42// NewContext returns a new Context
 43func NewContext(rollupConstMaxL1UserTx int) *Context {
 44	currBatchNum := 1 // The protocol defines the first batchNum to be 1
 45	return &Context{
 46		Users:                 make(map[string]*User),
 47		l1CreatedAccounts:     make(map[string]*Account),
 48		lastRegisteredTokenID: 0,
 49
 50		rollupConstMaxL1UserTx: rollupConstMaxL1UserTx,
 51		idx:                    common.UserThreshold,
 52		// We use some placeholder values for StateRoot and ExitTree
 53		// because these values will never be nil
 54		currBatch: common.BatchData{Batch: common.Batch{
 55			BatchNum:  common.BatchNum(currBatchNum),
 56			StateRoot: big.NewInt(0), ExitRoot: big.NewInt(0)}},
 57		currBatchNum: currBatchNum,
 58		// start with 2 queues, one for toForge, and the other for openToForge
 59		queues:      make([][]L1Tx, 2),
 60		toForgeNum:  0,
 61		openToForge: 1,
 62	}
 63}
 64
 65// Account contains the data related to the account for a specific TokenID of a User
 66type Account struct {
 67	Idx   common.Idx
 68	Nonce common.Nonce
 69}
 70
 71// User contains the data related to a testing user
 72type User struct {
 73	BJJ      *babyjub.PrivateKey
 74	Addr     ethCommon.Address
 75	Accounts map[common.TokenID]*Account
 76}
 77
 78// L1Tx is the data structure used internally for transaction test generation,
 79// which contains a common.L1Tx data plus some intermediate data for the
 80// transaction generation.
 81type L1Tx struct {
 82	lineNum     int
 83	fromIdxName string
 84	toIdxName   string
 85
 86	L1Tx common.L1Tx
 87}
 88
 89// L2Tx is the data structure used internally for transaction test generation,
 90// which contains a common.L2Tx data plus some intermediate data for the
 91// transaction generation.
 92type L2Tx struct {
 93	lineNum     int
 94	fromIdxName string
 95	toIdxName   string
 96	tokenID     common.TokenID
 97	L2Tx        common.L2Tx
 98}
 99
100// GenerateBlocks returns an array of BlockData for a given set. It uses the
101// accounts (keys & nonces) of the Context.
102func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
103	parser := newParser(strings.NewReader(set))
104	parsedSet, err := parser.parse()
105	if err != nil {
106		return nil, err
107	}
108
109	tc.Instructions = parsedSet.instructions
110	tc.accountsNames = parsedSet.accounts
111
112	tc.generateKeys(tc.accountsNames)
113
114	var blocks []common.BlockData
115	for _, inst := range parsedSet.instructions {
116		switch inst.typ {
117		case txTypeCreateAccountDepositCoordinator:
118			if err := tc.checkIfTokenIsRegistered(inst); err != nil {
119				log.Error(err)
120				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
121			}
122			tx := common.L1Tx{
123				FromEthAddr: tc.Users[inst.from].Addr,
124				FromBJJ:     tc.Users[inst.from].BJJ.Public(),
125				TokenID:     inst.tokenID,
126				Amount:      big.NewInt(0),
127				LoadAmount:  big.NewInt(int64(inst.loadAmount)),
128				Type:        common.TxTypeCreateAccountDeposit, // as txTypeCreateAccountDepositCoordinator is not valid oustide Til package
129			}
130			testTx := L1Tx{
131				lineNum:     inst.lineNum,
132				fromIdxName: inst.from,
133				L1Tx:        tx,
134			}
135			tc.currBatchTest.l1CoordinatorTxs = append(tc.currBatchTest.l1CoordinatorTxs, testTx)
136		case common.TxTypeCreateAccountDeposit, common.TxTypeCreateAccountDepositTransfer:
137			if err := tc.checkIfTokenIsRegistered(inst); err != nil {
138				log.Error(err)
139				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
140			}
141			tx := common.L1Tx{
142				FromEthAddr: tc.Users[inst.from].Addr,
143				FromBJJ:     tc.Users[inst.from].BJJ.Public(),
144				TokenID:     inst.tokenID,
145				Amount:      big.NewInt(0),
146				LoadAmount:  big.NewInt(int64(inst.loadAmount)),
147				Type:        inst.typ,
148			}
149			if inst.typ == common.TxTypeCreateAccountDepositTransfer {
150				tx.Amount = big.NewInt(int64(inst.amount))
151			}
152			testTx := L1Tx{
153				lineNum:     inst.lineNum,
154				fromIdxName: inst.from,
155				toIdxName:   inst.to,
156				L1Tx:        tx,
157			}
158			tc.addToL1Queue(testTx)
159		case common.TxTypeDeposit, common.TxTypeDepositTransfer:
160			if err := tc.checkIfTokenIsRegistered(inst); err != nil {
161				log.Error(err)
162				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
163			}
164			if err := tc.checkIfAccountExists(inst.from, inst); err != nil {
165				log.Error(err)
166				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
167			}
168			tx := common.L1Tx{
169				TokenID:    inst.tokenID,
170				Amount:     big.NewInt(0),
171				LoadAmount: big.NewInt(int64(inst.loadAmount)),
172				Type:       inst.typ,
173			}
174			if inst.typ == common.TxTypeDepositTransfer {
175				tx.Amount = big.NewInt(int64(inst.amount))
176			}
177			testTx := L1Tx{
178				lineNum:     inst.lineNum,
179				fromIdxName: inst.from,
180				toIdxName:   inst.to,
181				L1Tx:        tx,
182			}
183			tc.addToL1Queue(testTx)
184		case common.TxTypeTransfer:
185			if err := tc.checkIfTokenIsRegistered(inst); err != nil {
186				log.Error(err)
187				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
188			}
189			tx := common.L2Tx{
190				Amount: big.NewInt(int64(inst.amount)),
191				Fee:    common.FeeSelector(inst.fee),
192				Type:   common.TxTypeTransfer,
193			}
194			tx.BatchNum = common.BatchNum(tc.currBatchNum) // when converted to PoolL2Tx BatchNum parameter is lost
195			testTx := L2Tx{
196				lineNum:     inst.lineNum,
197				fromIdxName: inst.from,
198				toIdxName:   inst.to,
199				tokenID:     inst.tokenID,
200				L2Tx:        tx,
201			}
202			tc.currBatchTest.l2Txs = append(tc.currBatchTest.l2Txs, testTx)
203		case common.TxTypeExit:
204			if err := tc.checkIfTokenIsRegistered(inst); err != nil {
205				log.Error(err)
206				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
207			}
208			tx := common.L2Tx{
209				ToIdx:  common.Idx(1), // as is an Exit
210				Amount: big.NewInt(int64(inst.amount)),
211				Type:   common.TxTypeExit,
212			}
213			tx.BatchNum = common.BatchNum(tc.currBatchNum) // when converted to PoolL2Tx BatchNum parameter is lost
214			testTx := L2Tx{
215				lineNum:     inst.lineNum,
216				fromIdxName: inst.from,
217				toIdxName:   inst.to,
218				tokenID:     inst.tokenID,
219				L2Tx:        tx,
220			}
221			tc.currBatchTest.l2Txs = append(tc.currBatchTest.l2Txs, testTx)
222		case common.TxTypeForceExit:
223			if err := tc.checkIfTokenIsRegistered(inst); err != nil {
224				log.Error(err)
225				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
226			}
227			tx := common.L1Tx{
228				ToIdx:      common.Idx(1), // as is an Exit
229				TokenID:    inst.tokenID,
230				Amount:     big.NewInt(int64(inst.amount)),
231				LoadAmount: big.NewInt(0),
232				Type:       common.TxTypeExit,
233			}
234			testTx := L1Tx{
235				lineNum:     inst.lineNum,
236				fromIdxName: inst.from,
237				toIdxName:   inst.to,
238				L1Tx:        tx,
239			}
240			tc.addToL1Queue(testTx)
241		case typeNewBatch:
242			if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
243				return nil, err
244			}
245			if err = tc.setIdxs(); err != nil {
246				log.Error(err)
247				return nil, err
248			}
249		case typeNewBatchL1:
250			// for each L1UserTx of the queues[ToForgeNum], calculate the Idx
251			if err = tc.calculateIdxForL1Txs(false, tc.queues[tc.toForgeNum]); err != nil {
252				return nil, err
253			}
254			if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
255				return nil, err
256			}
257
258			// once Idxs are calculated, update transactions to use the real Idxs
259			for i := 0; i < len(tc.queues[tc.toForgeNum]); i++ {
260				testTx := &tc.queues[tc.toForgeNum][i]
261				if testTx.L1Tx.Type != common.TxTypeCreateAccountDeposit && testTx.L1Tx.Type != common.TxTypeCreateAccountDepositTransfer {
262					testTx.L1Tx.FromIdx = tc.Users[testTx.fromIdxName].Accounts[testTx.L1Tx.TokenID].Idx
263				}
264				testTx.L1Tx.FromEthAddr = tc.Users[testTx.fromIdxName].Addr
265				testTx.L1Tx.FromBJJ = tc.Users[testTx.fromIdxName].BJJ.Public()
266				if testTx.toIdxName == "" {
267					testTx.L1Tx.ToIdx = common.Idx(0)
268				} else {
269					testTx.L1Tx.ToIdx = tc.Users[testTx.toIdxName].Accounts[testTx.L1Tx.TokenID].Idx
270				}
271				if testTx.L1Tx.Type == common.TxTypeExit {
272					testTx.L1Tx.ToIdx = common.Idx(1)
273				}
274				// bn := common.BatchNum(tc.currBatchNum)
275				// testTx.L1Tx.BatchNum = &bn
276				nTx, err := common.NewL1Tx(&testTx.L1Tx)
277				if err != nil {
278					fmt.Println(testTx)
279					return nil, fmt.Errorf("Line %d: %s", testTx.lineNum, err.Error())
280				}
281				testTx.L1Tx = *nTx
282
283				tc.currBlock.L1UserTxs = append(tc.currBlock.L1UserTxs, testTx.L1Tx)
284			}
285			if err = tc.setIdxs(); err != nil {
286				log.Error(err)
287				return nil, err
288			}
289			// advance batch
290			tc.toForgeNum++
291			if tc.toForgeNum == tc.openToForge {
292				tc.openToForge++
293				newQueue := []L1Tx{}
294				tc.queues = append(tc.queues, newQueue)
295			}
296		case typeNewBlock:
297			blocks = append(blocks, tc.currBlock)
298			tc.currBlock = common.BlockData{}
299		case typeAddToken:
300			newToken := common.Token{
301				TokenID:     inst.tokenID,
302				EthBlockNum: int64(len(blocks)),
303			}
304			if inst.tokenID != tc.lastRegisteredTokenID+1 {
305				return nil, fmt.Errorf("Line %d: AddToken TokenID should be sequential, expected TokenID: %d, defined TokenID: %d", inst.lineNum, tc.lastRegisteredTokenID+1, inst.tokenID)
306			}
307			tc.lastRegisteredTokenID++
308			tc.currBlock.AddedTokens = append(tc.currBlock.AddedTokens, newToken)
309		default:
310			return nil, fmt.Errorf("Line %d: Unexpected type: %s", inst.lineNum, inst.typ)
311		}
312	}
313
314	return blocks, nil
315}
316
317// calculateIdxsForL1Txs calculates new Idx for new created accounts. If
318// 'isCoordinatorTxs==true', adds the tx to tc.currBatch.L1CoordinatorTxs.
319func (tc *Context) calculateIdxForL1Txs(isCoordinatorTxs bool, txs []L1Tx) error {
320	// for each batch.L1CoordinatorTxs of the queues[ToForgeNum], calculate the Idx
321	for i := 0; i < len(txs); i++ {
322		tx := txs[i]
323		if tx.L1Tx.Type == common.TxTypeCreateAccountDeposit || tx.L1Tx.Type == common.TxTypeCreateAccountDepositTransfer {
324			if tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID] != nil { // if account already exists, return error
325				return fmt.Errorf("Can not create same account twice (same User & same TokenID) (this is a design property of Til)")
326			}
327			tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID] = &Account{
328				Idx:   common.Idx(tc.idx),
329				Nonce: common.Nonce(0),
330			}
331			tc.l1CreatedAccounts[idxTokenIDToString(tx.fromIdxName, tx.L1Tx.TokenID)] = tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID]
332			tc.idx++
333		}
334		if isCoordinatorTxs {
335			tc.currBatch.L1CoordinatorTxs = append(tc.currBatch.L1CoordinatorTxs, tx.L1Tx)
336		}
337	}
338	return nil
339}
340
341// setIdxs sets the Idxs to the transactions of the tc.currBatch
342func (tc *Context) setIdxs() error {
343	// once Idxs are calculated, update transactions to use the new Idxs
344	for i := 0; i < len(tc.currBatchTest.l2Txs); i++ {
345		testTx := &tc.currBatchTest.l2Txs[i]
346
347		if tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID] == nil {
348			return fmt.Errorf("Line %d: %s from User %s for TokenID %d while account not created yet", testTx.lineNum, testTx.L2Tx.Type, testTx.fromIdxName, testTx.tokenID)
349		}
350		if testTx.L2Tx.Type == common.TxTypeTransfer {
351			if _, ok := tc.l1CreatedAccounts[idxTokenIDToString(testTx.toIdxName, testTx.tokenID)]; !ok {
352				return fmt.Errorf("Line %d: Can not create Transfer for a non existing account. Batch %d, ToIdx name: %s, TokenID: %d", testTx.lineNum, tc.currBatchNum, testTx.toIdxName, testTx.tokenID)
353			}
354		}
355		tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Nonce++
356		testTx.L2Tx.Nonce = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Nonce
357
358		// set real Idx
359		testTx.L2Tx.FromIdx = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Idx
360		if testTx.L2Tx.Type == common.TxTypeTransfer {
361			testTx.L2Tx.ToIdx = tc.Users[testTx.toIdxName].Accounts[testTx.tokenID].Idx
362		}
363		// in case Type==Exit, ToIdx=1, already set at the
364		// GenerateBlocks main switch inside TxTypeExit case
365
366		nTx, err := common.NewL2Tx(&testTx.L2Tx)
367		if err != nil {
368			return fmt.Errorf("Line %d: %s", testTx.lineNum, err.Error())
369		}
370		testTx.L2Tx = *nTx
371
372		tc.currBatch.L2Txs = append(tc.currBatch.L2Txs, testTx.L2Tx)
373	}
374
375	tc.currBlock.Batches = append(tc.currBlock.Batches, tc.currBatch)
376	tc.currBatchNum++
377	tc.currBatch = common.BatchData{Batch: tc.currBatch.Batch}
378	tc.currBatch.Batch.BatchNum = common.BatchNum(tc.currBatchNum)
379	tc.currBatchTest.l1CoordinatorTxs = nil
380	tc.currBatchTest.l2Txs = nil
381	return nil
382}
383
384// addToL1Queue adds the L1Tx into the queue that is open and has space
385func (tc *Context) addToL1Queue(tx L1Tx) {
386	if len(tc.queues[tc.openToForge]) >= tc.rollupConstMaxL1UserTx {
387		// if current OpenToForge queue reached its Max, move into a
388		// new queue
389		tc.openToForge++
390		newQueue := []L1Tx{}
391		tc.queues = append(tc.queues, newQueue)
392	}
393	tx.L1Tx.UserOrigin = true
394	toForgeL1TxsNum := int64(tc.openToForge)
395	tx.L1Tx.ToForgeL1TxsNum = &toForgeL1TxsNum
396	tc.queues[tc.openToForge] = append(tc.queues[tc.openToForge], tx)
397}
398
399func (tc *Context) checkIfAccountExists(tf string, inst instruction) error {
400	if tc.Users[tf].Accounts[inst.tokenID] == nil {
401		return fmt.Errorf("%s at User: %s, for TokenID: %d, while account not created yet", inst.typ, tf, inst.tokenID)
402	}
403	return nil
404}
405func (tc *Context) checkIfTokenIsRegistered(inst instruction) error {
406	if inst.tokenID > tc.lastRegisteredTokenID {
407		return fmt.Errorf("Can not process %s: TokenID %d not registered, last registered TokenID: %d", inst.typ, inst.tokenID, tc.lastRegisteredTokenID)
408	}
409	return nil
410}
411
412// GeneratePoolL2Txs returns an array of common.PoolL2Tx from a given set. It
413// uses the accounts (keys & nonces) of the Context.
414func (tc *Context) GeneratePoolL2Txs(set string) ([]common.PoolL2Tx, error) {
415	parser := newParser(strings.NewReader(set))
416	parsedSet, err := parser.parse()
417	if err != nil {
418		return nil, err
419	}
420
421	tc.Instructions = parsedSet.instructions
422	tc.accountsNames = parsedSet.accounts
423
424	tc.generateKeys(tc.accountsNames)
425
426	txs := []common.PoolL2Tx{}
427	for _, inst := range tc.Instructions {
428		switch inst.typ {
429		case common.TxTypeTransfer:
430			if err := tc.checkIfAccountExists(inst.from, inst); err != nil {
431				log.Error(err)
432				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
433			}
434			if err := tc.checkIfAccountExists(inst.to, inst); err != nil {
435				log.Error(err)
436				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
437			}
438			tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
439			// if account of receiver does not exist, don't use
440			// ToIdx, and use only ToEthAddr & ToBJJ
441			tx := common.PoolL2Tx{
442				FromIdx:     tc.Users[inst.from].Accounts[inst.tokenID].Idx,
443				ToIdx:       tc.Users[inst.to].Accounts[inst.tokenID].Idx,
444				ToEthAddr:   tc.Users[inst.to].Addr,
445				ToBJJ:       tc.Users[inst.to].BJJ.Public(),
446				TokenID:     inst.tokenID,
447				Amount:      big.NewInt(int64(inst.amount)),
448				Fee:         common.FeeSelector(inst.fee),
449				Nonce:       tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
450				State:       common.PoolL2TxStatePending,
451				Timestamp:   time.Now(),
452				RqToEthAddr: common.EmptyAddr,
453				RqToBJJ:     nil,
454				Type:        common.TxTypeTransfer,
455			}
456			nTx, err := common.NewPoolL2Tx(&tx)
457			if err != nil {
458				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
459			}
460			tx = *nTx
461			// perform signature and set it to tx.Signature
462			toSign, err := tx.HashToSign()
463			if err != nil {
464				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
465			}
466			sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
467			tx.Signature = sig.Compress()
468
469			txs = append(txs, tx)
470		case common.TxTypeExit:
471			tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
472			tx := common.PoolL2Tx{
473				FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx,
474				ToIdx:   common.Idx(1), // as is an Exit
475				TokenID: inst.tokenID,
476				Amount:  big.NewInt(int64(inst.amount)),
477				Nonce:   tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
478				Type:    common.TxTypeExit,
479			}
480			nTx, err := common.NewPoolL2Tx(&tx)
481			if err != nil {
482				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
483			}
484			tx = *nTx
485			// perform signature and set it to tx.Signature
486			toSign, err := tx.HashToSign()
487			if err != nil {
488				return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
489			}
490			sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
491			tx.Signature = sig.Compress()
492			txs = append(txs, tx)
493		default:
494			return nil, fmt.Errorf("Line %d: instruction type unrecognized: %s", inst.lineNum, inst.typ)
495		}
496	}
497
498	return txs, nil
499}
500
501// generateKeys generates BabyJubJub & Address keys for the given list of
502// account names in a deterministic way. This means, that for the same given
503// 'accNames' in a certain order, the keys will be always the same.
504func (tc *Context) generateKeys(accNames []string) {
505	for i := 1; i < len(accNames)+1; i++ {
506		if _, ok := tc.Users[accNames[i-1]]; ok {
507			// account already created
508			continue
509		}
510		// babyjubjub key
511		var sk babyjub.PrivateKey
512		copy(sk[:], []byte(strconv.Itoa(i))) // only for testing
513
514		// eth address
515		var key ecdsa.PrivateKey
516		key.D = big.NewInt(int64(i)) // only for testing
517		key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
518		key.Curve = ethCrypto.S256()
519		addr := ethCrypto.PubkeyToAddress(key.PublicKey)
520
521		u := User{
522			BJJ:      &sk,
523			Addr:     addr,
524			Accounts: make(map[common.TokenID]*Account),
525		}
526		tc.Users[accNames[i-1]] = &u
527	}
528}