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 := 256; i < 256+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		toIdx := new(common.Idx)
 57		*toIdx = common.Idx(i + 1)
 58		toEthAddr := new(ethCommon.Address)
 59		*toEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
 60		tx := &common.PoolL2Tx{
 61			FromIdx:           common.Idx(i),
 62			ToIdx:             toIdx,
 63			ToEthAddr:         toEthAddr,
 64			ToBJJ:             privK.Public(),
 65			TokenID:           token.TokenID,
 66			Amount:            big.NewInt(int64(i)),
 67			AmountFloat:       amountF,
 68			USD:               usd,
 69			Fee:               fee,
 70			Nonce:             common.Nonce(i),
 71			State:             state,
 72			Signature:         privK.SignPoseidon(big.NewInt(int64(i))),
 73			Timestamp:         time.Now().UTC(),
 74			AbsoluteFee:       absFee,
 75			AbsoluteFeeUpdate: token.USDUpdate,
 76		}
 77		var err error
 78		tx, err = common.NewPoolL2Tx(tx)
 79		if err != nil {
 80			panic(err)
 81		}
 82		if i%2 == 0 { // Optional parameters: rq
 83			rqFromIdx := new(common.Idx)
 84			*rqFromIdx = common.Idx(i)
 85			tx.RqFromIdx = rqFromIdx
 86			rqToIdx := new(common.Idx)
 87			*rqToIdx = common.Idx(i + 1)
 88			tx.RqToIdx = rqToIdx
 89			rqToEthAddr := new(ethCommon.Address)
 90			*rqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
 91			tx.RqToEthAddr = rqToEthAddr
 92			tx.RqToBJJ = privK.Public()
 93			rqTokenID := new(common.TokenID)
 94			*rqTokenID = common.TokenID(i)
 95			tx.RqTokenID = rqTokenID
 96			tx.RqAmount = big.NewInt(int64(i))
 97			rqFee := new(common.FeeSelector)
 98			*rqFee = common.FeeSelector(i)
 99			tx.RqFee = rqFee
100			rqNonce := new(uint64)
101			*rqNonce = uint64(i)
102			tx.RqNonce = rqNonce
103		}
104		if i%3 == 0 { // Optional parameters: things that get updated "a posteriori"
105			batchNum := new(common.BatchNum)
106			*batchNum = 489
107			tx.BatchNum = batchNum
108		}
109		txs = append(txs, tx)
110	}
111	return txs
112}
113
114// GenAuths generates account creation authorizations
115func GenAuths(nAuths int) []*common.AccountCreationAuth {
116	auths := []*common.AccountCreationAuth{}
117	for i := 0; i < nAuths; i++ {
118		privK := babyjub.NewRandPrivKey()
119		auths = append(auths, &common.AccountCreationAuth{
120			EthAddr:   ethCommon.BigToAddress(big.NewInt(int64(i))),
121			BJJ:       privK.Public(),
122			Signature: []byte(strconv.Itoa(i)),
123			Timestamp: time.Now(),
124		})
125	}
126	return auths
127}