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"` // EthereumBlockNumber 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 amountFloat40, err := NewFloat40(tx.Amount)
93 if err != nil {
94 return txID, tracerr.Wrap(fmt.Errorf("%s: %d", err, tx.Amount))
95 }
96 amountFloat40Bytes, err := amountFloat40.Bytes()
97 if err != nil {
98 return txID, tracerr.Wrap(err)
99 }
100 b = append(b, amountFloat40Bytes...)
101 // Nonce
102 nonceBytes, err := tx.Nonce.Bytes()
103 if err != nil {
104 return txID, tracerr.Wrap(err)
105 }
106 b = append(b, nonceBytes[:]...)
107 // Fee
108 b = append(b, byte(tx.Fee))
109
110 // calculate hash
111 h := ethCrypto.Keccak256Hash(b).Bytes()
112
113 txID[0] = TxIDPrefixL2Tx
114 copy(txID[1:], h)
115 return txID, nil
116}
117
118// Tx returns a *Tx from the L2Tx
119func (tx *L2Tx) Tx() *Tx {
120 batchNum := new(BatchNum)
121 *batchNum = tx.BatchNum
122 fee := new(FeeSelector)
123 *fee = tx.Fee
124 nonce := new(Nonce)
125 *nonce = tx.Nonce
126 return &Tx{
127 IsL1: false,
128 TxID: tx.TxID,
129 Type: tx.Type,
130 Position: tx.Position,
131 FromIdx: tx.FromIdx,
132 ToIdx: tx.ToIdx,
133 TokenID: tx.TokenID,
134 Amount: tx.Amount,
135 BatchNum: batchNum,
136 EthBlockNum: tx.EthBlockNum,
137 Fee: fee,
138 Nonce: nonce,
139 }
140}
141
142// PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
143// L2Tx filled
144func (tx L2Tx) PoolL2Tx() *PoolL2Tx {
145 return &PoolL2Tx{
146 TxID: tx.TxID,
147 FromIdx: tx.FromIdx,
148 ToIdx: tx.ToIdx,
149 TokenID: tx.TokenID,
150 Amount: tx.Amount,
151 Fee: tx.Fee,
152 Nonce: tx.Nonce,
153 Type: tx.Type,
154 }
155}
156
157// L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
158// where the PoolL2Tx only have the parameters of a L2Tx filled.
159func L2TxsToPoolL2Txs(txs []L2Tx) []PoolL2Tx {
160 var r []PoolL2Tx
161 for _, tx := range txs {
162 r = append(r, *tx.PoolL2Tx())
163 }
164 return r
165}
166
167// TxIDsFromL2Txs returns an array of TxID from the []L2Tx
168func TxIDsFromL2Txs(txs []L2Tx) []TxID {
169 txIDs := make([]TxID, len(txs))
170 for i, tx := range txs {
171 txIDs[i] = tx.TxID
172 }
173 return txIDs
174}
175
176// BytesDataAvailability encodes a L2Tx into []byte for the Data Availability
177// [ fromIdx | toIdx | amountFloat40 | Fee ]
178func (tx L2Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) {
179 idxLen := nLevels / 8 //nolint:gomnd
180
181 b := make([]byte, ((nLevels*2)+40+8)/8) //nolint:gomnd
182
183 fromIdxBytes, err := tx.FromIdx.Bytes()
184 if err != nil {
185 return nil, tracerr.Wrap(err)
186 }
187 copy(b[0:idxLen], fromIdxBytes[6-idxLen:]) // [6-idxLen:] as is BigEndian
188
189 toIdxBytes, err := tx.ToIdx.Bytes()
190 if err != nil {
191 return nil, tracerr.Wrap(err)
192 }
193 copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
194
195 amountFloat40, err := NewFloat40(tx.Amount)
196 if err != nil {
197 return nil, tracerr.Wrap(err)
198 }
199 amountFloat40Bytes, err := amountFloat40.Bytes()
200 if err != nil {
201 return nil, tracerr.Wrap(err)
202 }
203 copy(b[idxLen*2:idxLen*2+Float40BytesLength], amountFloat40Bytes)
204 b[idxLen*2+Float40BytesLength] = byte(tx.Fee)
205
206 return b[:], nil
207}
208
209// L2TxFromBytesDataAvailability decodes a L2Tx from []byte (Data Availability)
210func L2TxFromBytesDataAvailability(b []byte, nLevels int) (*L2Tx, error) {
211 idxLen := nLevels / 8 //nolint:gomnd
212 tx := &L2Tx{}
213 var err error
214
215 var paddedFromIdxBytes [6]byte
216 copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
217 tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
218 if err != nil {
219 return nil, tracerr.Wrap(err)
220 }
221
222 var paddedToIdxBytes [6]byte
223 copy(paddedToIdxBytes[6-idxLen:6], b[idxLen:idxLen*2])
224 tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
225 if err != nil {
226 return nil, tracerr.Wrap(err)
227 }
228
229 tx.Amount, err = Float40FromBytes(b[idxLen*2 : idxLen*2+Float40BytesLength]).BigInt()
230 if err != nil {
231 return nil, tracerr.Wrap(err)
232 }
233 tx.Fee = FeeSelector(b[idxLen*2+Float40BytesLength])
234 return tx, nil
235}