hermez-node/test/l2db.go
test/l2db.goRaw
1package test
2
3import (
4 ethCrypto "github.com/ethereum/go-ethereum/crypto"
5 "github.com/hermeznetwork/hermez-node/common"
6 "github.com/iden3/go-iden3-crypto/babyjub"
7 "github.com/jmoiron/sqlx"
8)
9
10// CleanL2DB deletes 'tx_pool' and 'account_creation_auth' from the given DB
11func CleanL2DB(db *sqlx.DB) {
12 if _, err := db.Exec("DELETE FROM tx_pool;"); err != nil {
13 panic(err)
14 }
15 if _, err := db.Exec("DELETE FROM account_creation_auth;"); err != nil {
16 panic(err)
17 }
18}
19
20// GenPoolTxs generates L2 pool txs.
21// WARNING: This tx doesn't follow the protocol (signature, txID, ...)
22// it's just to test getting/setting from/to the DB.
23func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
24 /*
25 WARNING: this should be replaced by transaktio
26 txs := make([]*common.PoolL2Tx, 0, n)
27 privK := babyjub.NewRandPrivKey()
28 for i := 256; i < 256+n; i++ {
29 var state common.PoolL2TxState
30 //nolint:gomnd
31 if i%4 == 0 {
32 state = common.PoolL2TxStatePending
33 //nolint:gomnd
34 } else if i%4 == 1 {
35 state = common.PoolL2TxStateInvalid
36 //nolint:gomnd
37 } else if i%4 == 2 {
38 state = common.PoolL2TxStateForging
39 //nolint:gomnd
40 } else if i%4 == 3 {
41 state = common.PoolL2TxStateForged
42 }
43 fee := common.FeeSelector(i % 255) //nolint:gomnd
44 token := tokens[i%len(tokens)]
45 tx := &common.PoolL2Tx{
46 FromIdx: common.Idx(i),
47 ToIdx: common.Idx(i + 1),
48 ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
49 ToBJJ: privK.Public(),
50 TokenID: token.TokenID,
51 Amount: big.NewInt(int64(i)),
52 Fee: fee,
53 Nonce: common.Nonce(i),
54 State: state,
55 Signature: privK.SignPoseidon(big.NewInt(int64(i))).Compress(),
56 }
57 var err error
58 tx, err = common.NewPoolL2Tx(tx)
59 if err != nil {
60 panic(err)
61 }
62 if i%2 == 0 { // Optional parameters: rq
63 tx.RqFromIdx = common.Idx(i)
64 tx.RqToIdx = common.Idx(i + 1)
65 tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
66 tx.RqToBJJ = privK.Public()
67 tx.RqTokenID = common.TokenID(i)
68 tx.RqAmount = big.NewInt(int64(i))
69 tx.RqFee = common.FeeSelector(i)
70 tx.RqNonce = common.Nonce(i)
71 }
72 txs = append(txs, tx)
73 }
74 return txs
75 */
76 return nil
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 // Generate keys
84 ethPrivK, err := ethCrypto.GenerateKey()
85 if err != nil {
86 panic(err)
87 }
88 bjjPrivK := babyjub.NewRandPrivKey()
89 // Generate auth
90 auth := &common.AccountCreationAuth{
91 EthAddr: ethCrypto.PubkeyToAddress(ethPrivK.PublicKey),
92 BJJ: bjjPrivK.Public(),
93 }
94 // Sign
95 h, err := auth.HashToSign()
96 if err != nil {
97 panic(err)
98 }
99 signature, err := ethCrypto.Sign(h, ethPrivK)
100 if err != nil {
101 panic(err)
102 }
103 auth.Signature = signature
104 auths = append(auths, auth)
105 }
106 return auths
107}