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) []*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 tx := &common.PoolL2Tx{
48 TxID: common.TxID(common.Hash([]byte(strconv.Itoa(i)))),
49 FromIdx: common.Idx(i),
50 ToIdx: common.Idx(i + 1),
51 ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
52 ToBJJ: privK.Public(),
53 TokenID: common.TokenID(i),
54 Amount: big.NewInt(int64(i)),
55 AmountFloat: amountF,
56 //nolint:gomnd
57 Fee: common.FeeSelector(i % 255),
58 Nonce: common.Nonce(i),
59 State: state,
60 Signature: privK.SignPoseidon(big.NewInt(int64(i))),
61 Timestamp: time.Now().UTC(),
62 }
63 if i%2 == 0 { // Optional parameters: rq
64 tx.RqFromIdx = common.Idx(i)
65 tx.RqToIdx = common.Idx(i + 1)
66 tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
67 tx.RqToBJJ = privK.Public()
68 tx.RqTokenID = common.TokenID(i)
69 tx.RqAmount = big.NewInt(int64(i))
70 tx.RqFee = common.FeeSelector(i)
71 tx.RqNonce = uint64(i)
72 }
73 if i%3 == 0 { // Optional parameters: things that get updated "a posteriori"
74 tx.BatchNum = 489
75 tx.AbsoluteFee = 39.12345
76 tx.AbsoluteFeeUpdate = time.Now().UTC()
77 }
78 txs = append(txs, tx)
79 }
80 return txs
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}