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 fee := common.FeeSelector(i % 255) //nolint:gomnd
46 token := tokens[i%len(tokens)]
47 tx := &common.PoolL2Tx{
48 FromIdx: common.Idx(i),
49 ToIdx: common.Idx(i + 1),
50 ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
51 ToBJJ: privK.Public(),
52 TokenID: token.TokenID,
53 Amount: big.NewInt(int64(i)),
54 Fee: fee,
55 Nonce: common.Nonce(i),
56 State: state,
57 Signature: privK.SignPoseidon(big.NewInt(int64(i))),
58 }
59 var err error
60 tx, err = common.NewPoolL2Tx(tx)
61 if err != nil {
62 panic(err)
63 }
64 if i%2 == 0 { // Optional parameters: rq
65 tx.RqFromIdx = common.Idx(i)
66 tx.RqToIdx = common.Idx(i + 1)
67 tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
68 tx.RqToBJJ = privK.Public()
69 tx.RqTokenID = common.TokenID(i)
70 tx.RqAmount = big.NewInt(int64(i))
71 tx.RqFee = common.FeeSelector(i)
72 tx.RqNonce = uint64(i)
73 }
74 txs = append(txs, tx)
75 }
76 return txs
77}
78
79// GenAuths generates account creation authorizations
80func GenAuths(nAuths int) []*common.AccountCreationAuth {
81 auths := []*common.AccountCreationAuth{}
82 for i := 0; i < nAuths; i++ {
83 privK := babyjub.NewRandPrivKey()
84 auths = append(auths, &common.AccountCreationAuth{
85 EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
86 BJJ: privK.Public(),
87 Signature: []byte(strconv.Itoa(i)),
88 Timestamp: time.Now(),
89 })
90 }
91 return auths
92}