arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/l2db.go
test/l2db.goRaw
  1package test
  2
  3import (
  4	"math/big"
  5	"strconv"
  6	"time"
  7
  8	ethCommon "github.com/ethereum/go-ethereum/common"
  9	"github.com/hermeznetwork/hermez-node/common"
 10	"github.com/iden3/go-iden3-crypto/babyjub"
 11	"github.com/jmoiron/sqlx"
 12)
 13
 14// CleanL2DB deletes 'tx_pool' and 'account_creation_auth' from the given DB
 15func CleanL2DB(db *sqlx.DB) {
 16	if _, err := db.Exec("DELETE FROM tx_pool;"); err != nil {
 17		panic(err)
 18	}
 19	if _, err := db.Exec("DELETE FROM account_creation_auth;"); err != nil {
 20		panic(err)
 21	}
 22}
 23
 24// GenPoolTxs generates L2 pool txs.
 25// WARNING: This tx doesn't follow the protocol (signature, txID, ...)
 26// it's just to test getting/setting from/to the DB.
 27func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
 28	txs := make([]*common.PoolL2Tx, 0, n)
 29	privK := babyjub.NewRandPrivKey()
 30	for i := 0; i < n; i++ {
 31		var state common.PoolL2TxState
 32		//nolint:gomnd
 33		if i%4 == 0 {
 34			state = common.PoolL2TxStatePending
 35			//nolint:gomnd
 36		} else if i%4 == 1 {
 37			state = common.PoolL2TxStateInvalid
 38			//nolint:gomnd
 39		} else if i%4 == 2 {
 40			state = common.PoolL2TxStateForging
 41			//nolint:gomnd
 42		} else if i%4 == 3 {
 43			state = common.PoolL2TxStateForged
 44		}
 45		f := new(big.Float).SetInt(big.NewInt(int64(i)))
 46		amountF, _ := f.Float64()
 47		var usd, absFee *float64
 48		fee := common.FeeSelector(i % 255) //nolint:gomnd
 49		token := tokens[i%len(tokens)]
 50		if token.USD != nil {
 51			usd = new(float64)
 52			absFee = new(float64)
 53			*usd = *token.USD * amountF
 54			*absFee = fee.Percentage() * *usd
 55		}
 56		tx := &common.PoolL2Tx{
 57			TxID:              common.TxID(common.Hash([]byte(strconv.Itoa(i)))),
 58			FromIdx:           common.Idx(i),
 59			ToIdx:             common.Idx(i + 1),
 60			ToEthAddr:         ethCommon.BigToAddress(big.NewInt(int64(i))),
 61			ToBJJ:             privK.Public(),
 62			TokenID:           token.TokenID,
 63			Amount:            big.NewInt(int64(i)),
 64			AmountFloat:       amountF,
 65			USD:               usd,
 66			Fee:               fee,
 67			Nonce:             common.Nonce(i),
 68			State:             state,
 69			Signature:         privK.SignPoseidon(big.NewInt(int64(i))),
 70			Timestamp:         time.Now().UTC(),
 71			TokenSymbol:       token.Symbol,
 72			AbsoluteFee:       absFee,
 73			AbsoluteFeeUpdate: token.USDUpdate,
 74		}
 75		if i%2 == 0 { // Optional parameters: rq
 76			tx.RqFromIdx = common.Idx(i)
 77			tx.RqToIdx = common.Idx(i + 1)
 78			tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
 79			tx.RqToBJJ = privK.Public()
 80			tx.RqTokenID = common.TokenID(i)
 81			tx.RqAmount = big.NewInt(int64(i))
 82			tx.RqFee = common.FeeSelector(i)
 83			tx.RqNonce = uint64(i)
 84		}
 85		if i%3 == 0 { // Optional parameters: things that get updated "a posteriori"
 86			tx.BatchNum = 489
 87		}
 88		txs = append(txs, tx)
 89	}
 90	return txs
 91}
 92
 93// GenAuths generates account creation authorizations
 94func GenAuths(nAuths int) []*common.AccountCreationAuth {
 95	auths := []*common.AccountCreationAuth{}
 96	for i := 0; i < nAuths; i++ {
 97		privK := babyjub.NewRandPrivKey()
 98		auths = append(auths, &common.AccountCreationAuth{
 99			EthAddr:   ethCommon.BigToAddress(big.NewInt(int64(i))),
100			BJJ:       privK.Public(),
101			Signature: []byte(strconv.Itoa(i)),
102			Timestamp: time.Now(),
103		})
104	}
105	return auths
106}