hermez-node/common/l2tx.go
common/l2tx.goRaw
1package common
2
3import (
4 "fmt"
5 "math/big"
6
7 ethCrypto "github.com/ethereum/go-ethereum/crypto"
8 "github.com/hermeznetwork/tracerr"
9)
10
11// L2Tx is a struct that represents an already forged L2 tx
12type L2Tx struct {
13 // Stored in DB: mandatory fileds
14 TxID TxID `meddler:"id"`
15 BatchNum BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged.
16 Position int `meddler:"position"`
17 FromIdx Idx `meddler:"from_idx"`
18 ToIdx Idx `meddler:"to_idx"`
19 // TokenID is filled by the TxProcessor
20 TokenID TokenID `meddler:"token_id"`
21 Amount *big.Int `meddler:"amount,bigint"`
22 Fee FeeSelector `meddler:"fee"`
23 // Nonce is filled by the TxProcessor
24 Nonce Nonce `meddler:"nonce"`
25 Type TxType `meddler:"type"`
26 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L2Tx was added to the queue
27}
28
29// NewL2Tx returns the given L2Tx with the TxId & Type parameters calculated
30// from the L2Tx values
31func NewL2Tx(tx *L2Tx) (*L2Tx, error) {
32 txTypeOld := tx.Type
33 if err := tx.SetType(); err != nil {
34 return nil, tracerr.Wrap(err)
35 }
36 // If original Type doesn't match the correct one, return error
37 if txTypeOld != "" && txTypeOld != tx.Type {
38 return nil, tracerr.Wrap(fmt.Errorf("L2Tx.Type: %s, should be: %s",
39 tx.Type, txTypeOld))
40 }
41
42 txIDOld := tx.TxID
43 if err := tx.SetID(); err != nil {
44 return nil, tracerr.Wrap(err)
45 }
46 // If original TxID doesn't match the correct one, return error
47 if txIDOld != (TxID{}) && txIDOld != tx.TxID {
48 return tx, tracerr.Wrap(fmt.Errorf("L2Tx.TxID: %s, should be: %s",
49 tx.TxID.String(), txIDOld.String()))
50 }
51
52 return tx, nil
53}
54
55// SetType sets the type of the transaction. Uses (FromIdx, Nonce).
56func (tx *L2Tx) SetType() error {
57 if tx.ToIdx == Idx(1) {
58 tx.Type = TxTypeExit
59 } else if tx.ToIdx >= IdxUserThreshold {
60 tx.Type = TxTypeTransfer
61 } else {
62 return tracerr.Wrap(fmt.Errorf(
63 "cannot determine type of L2Tx, invalid ToIdx value: %d", tx.ToIdx))
64 }
65 return nil
66}
67
68// SetID sets the ID of the transaction
69func (tx *L2Tx) SetID() error {
70 txID, err := tx.CalculateTxID()
71 if err != nil {
72 return err
73 }
74 tx.TxID = txID
75 return nil
76}
77
78// CalculateTxID returns the TxID of the transaction. This method is used to
79// set the TxID for L2Tx and for PoolL2Tx.
80func (tx L2Tx) CalculateTxID() ([TxIDLen]byte, error) {
81 var txID TxID
82 var b []byte
83 // FromIdx
84 fromIdxBytes, err := tx.FromIdx.Bytes()
85 if err != nil {
86 return txID, tracerr.Wrap(err)
87 }
88 b = append(b, fromIdxBytes[:]...)
89 // TokenID
90 b = append(b, tx.TokenID.Bytes()[:]...)
91 // Amount
92 amountFloat16, err := NewFloat16(tx.Amount)
93 if err != nil {
94 return txID, tracerr.Wrap(fmt.Errorf("%s: %d", err, tx.Amount))
95 }
96 b = append(b, amountFloat16.Bytes()...)
97 // Nonce
98 nonceBytes, err := tx.Nonce.Bytes()
99 if err != nil {
100 return txID, tracerr.Wrap(err)
101 }
102 b = append(b, nonceBytes[:]...)
103 // Fee
104 b = append(b, byte(tx.Fee))
105
106 // calculate hash
107 h := ethCrypto.Keccak256Hash(b).Bytes()
108
109 txID[0] = TxIDPrefixL2Tx
110 copy(txID[1:], h)
111 return txID, nil
112}
113
114// Tx returns a *Tx from the L2Tx
115func (tx *L2Tx) Tx() *Tx {
116 batchNum := new(BatchNum)
117 *batchNum = tx.BatchNum
118 fee := new(FeeSelector)
119 *fee = tx.Fee
120 nonce := new(Nonce)
121 *nonce = tx.Nonce
122 return &Tx{
123 IsL1: false,
124 TxID: tx.TxID,
125 Type: tx.Type,
126 Position: tx.Position,
127 FromIdx: tx.FromIdx,
128 ToIdx: tx.ToIdx,
129 TokenID: tx.TokenID,
130 Amount: tx.Amount,
131 BatchNum: batchNum,
132 EthBlockNum: tx.EthBlockNum,
133 Fee: fee,
134 Nonce: nonce,
135 }
136}
137
138// PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
139// L2Tx filled
140func (tx L2Tx) PoolL2Tx() *PoolL2Tx {
141 return &PoolL2Tx{
142 TxID: tx.TxID,
143 FromIdx: tx.FromIdx,
144 ToIdx: tx.ToIdx,
145 TokenID: tx.TokenID,
146 Amount: tx.Amount,
147 Fee: tx.Fee,
148 Nonce: tx.Nonce,
149 Type: tx.Type,
150 }
151}
152
153// L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
154// where the PoolL2Tx only have the parameters of a L2Tx filled.
155func L2TxsToPoolL2Txs(txs []L2Tx) []PoolL2Tx {
156 var r []PoolL2Tx
157 for _, tx := range txs {
158 r = append(r, *tx.PoolL2Tx())
159 }
160 return r
161}
162
163// TxIDsFromL2Txs returns an array of TxID from the []L2Tx
164func TxIDsFromL2Txs(txs []L2Tx) []TxID {
165 txIDs := make([]TxID, len(txs))
166 for i, tx := range txs {
167 txIDs[i] = tx.TxID
168 }
169 return txIDs
170}
171
172// BytesDataAvailability encodes a L2Tx into []byte for the Data Availability
173// [ fromIdx | toIdx | amountFloat16 | Fee ]
174func (tx L2Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) {
175 idxLen := nLevels / 8 //nolint:gomnd
176
177 b := make([]byte, ((nLevels*2)+16+8)/8) //nolint:gomnd
178
179 fromIdxBytes, err := tx.FromIdx.Bytes()
180 if err != nil {
181 return nil, tracerr.Wrap(err)
182 }
183 copy(b[0:idxLen], fromIdxBytes[6-idxLen:]) // [6-idxLen:] as is BigEndian
184
185 toIdxBytes, err := tx.ToIdx.Bytes()
186 if err != nil {
187 return nil, tracerr.Wrap(err)
188 }
189 copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
190
191 amountFloat16, err := NewFloat16(tx.Amount)
192 if err != nil {
193 return nil, tracerr.Wrap(err)
194 }
195
196 copy(b[idxLen*2:idxLen*2+2], amountFloat16.Bytes())
197 b[idxLen*2+2] = byte(tx.Fee)
198
199 return b[:], nil
200}
201
202// L2TxFromBytesDataAvailability decodes a L2Tx from []byte (Data Availability)
203func L2TxFromBytesDataAvailability(b []byte, nLevels int) (*L2Tx, error) {
204 idxLen := nLevels / 8 //nolint:gomnd
205 tx := &L2Tx{}
206 var err error
207
208 var paddedFromIdxBytes [6]byte
209 copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
210 tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
211 if err != nil {
212 return nil, tracerr.Wrap(err)
213 }
214
215 var paddedToIdxBytes [6]byte
216 copy(paddedToIdxBytes[6-idxLen:6], b[idxLen:idxLen*2])
217 tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
218 if err != nil {
219 return nil, tracerr.Wrap(err)
220 }
221
222 tx.Amount = Float16FromBytes(b[idxLen*2 : idxLen*2+2]).BigInt()
223 tx.Fee = FeeSelector(b[idxLen*2+2])
224 return tx, nil
225}