hermez-node/common/l2tx.go
common/l2tx.goRaw
1package common
2
3import (
4 "fmt"
5 "math/big"
6)
7
8// L2Tx is a struct that represents an already forged L2 tx
9type L2Tx struct {
10 // Stored in DB: mandatory fileds
11 TxID TxID `meddler:"id"`
12 BatchNum BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged.
13 Position int `meddler:"position"`
14 FromIdx Idx `meddler:"from_idx"`
15 ToIdx Idx `meddler:"to_idx"`
16 Amount *big.Int `meddler:"amount,bigint"`
17 Fee FeeSelector `meddler:"fee"`
18 Nonce Nonce `meddler:"nonce"`
19 Type TxType `meddler:"type"`
20 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L2Tx was added to the queue
21}
22
23// NewL2Tx returns the given L2Tx with the TxId & Type parameters calculated
24// from the L2Tx values
25func NewL2Tx(l2Tx *L2Tx) (*L2Tx, error) {
26 // calculate TxType
27 var txType TxType
28 if l2Tx.ToIdx == Idx(1) {
29 txType = TxTypeExit
30 } else if l2Tx.ToIdx >= IdxUserThreshold {
31 txType = TxTypeTransfer
32 } else {
33 return l2Tx, fmt.Errorf("Can not determine type of L2Tx, invalid ToIdx value: %d", l2Tx.ToIdx)
34 }
35
36 // if TxType!=l2Tx.TxType return error
37 if l2Tx.Type != "" && l2Tx.Type != txType {
38 return l2Tx, fmt.Errorf("L2Tx.Type: %s, should be: %s", l2Tx.Type, txType)
39 }
40 l2Tx.Type = txType
41
42 var txid [TxIDLen]byte
43 txid[0] = TxIDPrefixL2Tx
44 fromIdxBytes, err := l2Tx.FromIdx.Bytes()
45 if err != nil {
46 return l2Tx, err
47 }
48 copy(txid[1:7], fromIdxBytes[:])
49 nonceBytes, err := l2Tx.Nonce.Bytes()
50 if err != nil {
51 return l2Tx, err
52 }
53 copy(txid[7:12], nonceBytes[:])
54 l2Tx.TxID = TxID(txid)
55
56 return l2Tx, nil
57}
58
59// Tx returns a *Tx from the L2Tx
60func (tx *L2Tx) Tx() *Tx {
61 batchNum := new(BatchNum)
62 *batchNum = tx.BatchNum
63 fee := new(FeeSelector)
64 *fee = tx.Fee
65 nonce := new(Nonce)
66 *nonce = tx.Nonce
67 return &Tx{
68 IsL1: false,
69 TxID: tx.TxID,
70 Type: tx.Type,
71 Position: tx.Position,
72 FromIdx: tx.FromIdx,
73 ToIdx: tx.ToIdx,
74 Amount: tx.Amount,
75 BatchNum: batchNum,
76 EthBlockNum: tx.EthBlockNum,
77 Fee: fee,
78 Nonce: nonce,
79 }
80}
81
82// PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
83// L2Tx filled
84func (tx L2Tx) PoolL2Tx() *PoolL2Tx {
85 return &PoolL2Tx{
86 TxID: tx.TxID,
87 FromIdx: tx.FromIdx,
88 ToIdx: tx.ToIdx,
89 Amount: tx.Amount,
90 Fee: tx.Fee,
91 Nonce: tx.Nonce,
92 Type: tx.Type,
93 }
94}
95
96// L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
97// where the PoolL2Tx only have the parameters of a L2Tx filled.
98func L2TxsToPoolL2Txs(txs []L2Tx) []PoolL2Tx {
99 var r []PoolL2Tx
100 for _, tx := range txs {
101 r = append(r, *tx.PoolL2Tx())
102 }
103 return r
104}
105
106// Bytes encodes a L2Tx into []byte
107func (tx L2Tx) Bytes(nLevels uint32) ([]byte, error) {
108 idxLen := nLevels / 8 //nolint:gomnd
109
110 b := make([]byte, ((nLevels*2)+16+8)/8) //nolint:gomnd
111
112 fromIdxBytes, err := tx.FromIdx.Bytes()
113 if err != nil {
114 return nil, err
115 }
116 copy(b[0:idxLen], fromIdxBytes[6-idxLen:]) // [6-idxLen:] as is BigEndian
117
118 toIdxBytes, err := tx.ToIdx.Bytes()
119 if err != nil {
120 return nil, err
121 }
122 copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
123
124 amountFloat16, err := NewFloat16(tx.Amount)
125 if err != nil {
126 return nil, err
127 }
128
129 copy(b[idxLen*2:idxLen*2+2], amountFloat16.Bytes())
130 b[idxLen*2+2] = byte(tx.Fee)
131
132 return b[:], nil
133}
134
135// L2TxFromBytes decodes a L1Tx from []byte
136func L2TxFromBytes(b []byte, nLevels int) (*L2Tx, error) {
137 idxLen := nLevels / 8 //nolint:gomnd
138 tx := &L2Tx{}
139 var err error
140
141 var paddedFromIdxBytes [6]byte
142 copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
143 tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
144 if err != nil {
145 return nil, err
146 }
147
148 var paddedToIdxBytes [6]byte
149 copy(paddedToIdxBytes[6-idxLen:6], b[idxLen:idxLen*2])
150 tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
151 if err != nil {
152 return nil, err
153 }
154
155 tx.Amount = Float16FromBytes(b[idxLen*2 : idxLen*2+2]).BigInt()
156 tx.Fee = FeeSelector(b[idxLen*2+2])
157 return tx, nil
158}