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