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