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