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	/*
29		WARNING: this should be replaced by transaktio
30		txs := make([]*common.PoolL2Tx, 0, n)
31		privK := babyjub.NewRandPrivKey()
32		for i := 256; i < 256+n; i++ {
33			var state common.PoolL2TxState
34			//nolint:gomnd
35			if i%4 == 0 {
36				state = common.PoolL2TxStatePending
37				//nolint:gomnd
38			} else if i%4 == 1 {
39				state = common.PoolL2TxStateInvalid
40				//nolint:gomnd
41			} else if i%4 == 2 {
42				state = common.PoolL2TxStateForging
43				//nolint:gomnd
44			} else if i%4 == 3 {
45				state = common.PoolL2TxStateForged
46			}
47			fee := common.FeeSelector(i % 255) //nolint:gomnd
48			token := tokens[i%len(tokens)]
49			tx := &common.PoolL2Tx{
50				FromIdx:   common.Idx(i),
51				ToIdx:     common.Idx(i + 1),
52				ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
53				ToBJJ:     privK.Public(),
54				TokenID:   token.TokenID,
55				Amount:    big.NewInt(int64(i)),
56				Fee:       fee,
57				Nonce:     common.Nonce(i),
58				State:     state,
59				Signature: privK.SignPoseidon(big.NewInt(int64(i))).Compress(),
60			}
61			var err error
62			tx, err = common.NewPoolL2Tx(tx)
63			if err != nil {
64				panic(err)
65			}
66			if i%2 == 0 { // Optional parameters: rq
67				tx.RqFromIdx = common.Idx(i)
68				tx.RqToIdx = common.Idx(i + 1)
69				tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
70				tx.RqToBJJ = privK.Public()
71				tx.RqTokenID = common.TokenID(i)
72				tx.RqAmount = big.NewInt(int64(i))
73				tx.RqFee = common.FeeSelector(i)
74				tx.RqNonce = common.Nonce(i)
75			}
76			txs = append(txs, tx)
77		}
78		return txs
79	*/
80	return nil
81}
82
83// GenAuths generates account creation authorizations
84func GenAuths(nAuths int) []*common.AccountCreationAuth {
85	auths := []*common.AccountCreationAuth{}
86	for i := 0; i < nAuths; i++ {
87		privK := babyjub.NewRandPrivKey()
88		auths = append(auths, &common.AccountCreationAuth{
89			EthAddr:   ethCommon.BigToAddress(big.NewInt(int64(i))),
90			BJJ:       privK.Public(),
91			Signature: []byte(strconv.Itoa(i)),
92			Timestamp: time.Now(),
93		})
94	}
95	return auths
96}