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
12 BatchNum BatchNum // batchNum in which this tx was forged.
13 Position int
14 FromIdx Idx
15 ToIdx Idx
16 Amount *big.Int
17 Fee FeeSelector
18 Nonce Nonce
19 Type TxType
20 EthBlockNum int64 // 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 int) ([]byte, error) {
108 fromIdxNumBytes := nLevels / 8 //nolint:gomnd
109 toIdxNumBytes := nLevels / 8 //nolint:gomnd
110 var b []byte
111 fromIdxBytes, err := tx.FromIdx.Bytes()
112 if err != nil {
113 return nil, err
114 }
115 b = append(b, fromIdxBytes[6-fromIdxNumBytes:]...)
116 toIdxBytes, err := tx.ToIdx.Bytes()
117 if err != nil {
118 return nil, err
119 }
120 b = append(b, toIdxBytes[6-toIdxNumBytes:]...)
121 amountFloat16, err := NewFloat16(tx.Amount)
122 if err != nil {
123 return nil, err
124 }
125 b = append(b, amountFloat16.Bytes()...)
126 b = append(b, byte(tx.Fee))
127 return b[:], nil
128}
129
130// L2TxFromBytes decodes a L1Tx from []byte
131func L2TxFromBytes(b []byte, nLevels int) (*L2Tx, error) {
132 fromIdxNumByte := nLevels / 8 //nolint:gomnd
133 toIdxNumByte := fromIdxNumByte + nLevels/8 //nolint:gomnd
134 amountLenBytes := 2
135 amountNumByte := toIdxNumByte + amountLenBytes
136 tx := &L2Tx{}
137 var err error
138 var paddedFromIdxBytes [6]byte
139 copy(paddedFromIdxBytes[6-len(b[0:fromIdxNumByte]):], b[0:fromIdxNumByte])
140 tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
141 if err != nil {
142 return nil, err
143 }
144 var paddedToIdxBytes [6]byte
145 copy(paddedToIdxBytes[6-len(b[fromIdxNumByte:toIdxNumByte]):6], b[fromIdxNumByte:toIdxNumByte])
146 tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
147 if err != nil {
148 return nil, err
149 }
150 tx.Amount = Float16FromBytes(b[toIdxNumByte:amountNumByte]).BigInt()
151 tx.Fee = FeeSelector(b[amountNumByte])
152 return tx, nil
153}