hermez-node/common/tx.go
common/tx.goRaw
1package common
2
3import (
4 "database/sql/driver"
5 "encoding/hex"
6 "errors"
7 "fmt"
8 "math/big"
9 "strings"
10
11 ethCommon "github.com/ethereum/go-ethereum/common"
12 "github.com/iden3/go-iden3-crypto/babyjub"
13)
14
15const (
16 // TXIDPrefixL1UserTx is the prefix that determines that the TxID is
17 // for a L1UserTx
18 //nolinter:gomnd
19 TxIDPrefixL1UserTx = byte(0)
20
21 // TXIDPrefixL1CoordTx is the prefix that determines that the TxID is
22 // for a L1CoordinatorTx
23 //nolinter:gomnd
24 TxIDPrefixL1CoordTx = byte(1)
25
26 // TxIDPrefixL2Tx is the prefix that determines that the TxID is for a
27 // L2Tx (or PoolL2Tx)
28 //nolinter:gomnd
29 TxIDPrefixL2Tx = byte(2)
30
31 // TxIDLen is the length of the TxID byte array
32 TxIDLen = 12
33)
34
35// TxID is the identifier of a Hermez network transaction
36type TxID [TxIDLen]byte
37
38// Scan implements Scanner for database/sql.
39func (txid *TxID) Scan(src interface{}) error {
40 srcB, ok := src.([]byte)
41 if !ok {
42 return fmt.Errorf("can't scan %T into TxID", src)
43 }
44 if len(srcB) != TxIDLen {
45 return fmt.Errorf("can't scan []byte of len %d into TxID, need %d", len(srcB), TxIDLen)
46 }
47 copy(txid[:], srcB)
48 return nil
49}
50
51// Value implements valuer for database/sql.
52func (txid TxID) Value() (driver.Value, error) {
53 return txid[:], nil
54}
55
56// String returns a string hexadecimal representation of the TxID
57func (txid TxID) String() string {
58 return "0x" + hex.EncodeToString(txid[:])
59}
60
61// NewTxIDFromString returns a string hexadecimal representation of the TxID
62func NewTxIDFromString(idStr string) (TxID, error) {
63 txid := TxID{}
64 idStr = strings.TrimPrefix(idStr, "0x")
65 decoded, err := hex.DecodeString(idStr)
66 if err != nil {
67 return TxID{}, err
68 }
69 if len(decoded) != TxIDLen {
70 return txid, errors.New("Invalid idStr")
71 }
72 copy(txid[:], decoded)
73 return txid, nil
74}
75
76// MarshalText marshals a TxID
77func (txid TxID) MarshalText() ([]byte, error) {
78 return []byte(txid.String()), nil
79}
80
81// UnmarshalText unmarshals a TxID
82func (txid *TxID) UnmarshalText(data []byte) error {
83 idStr := string(data)
84 id, err := NewTxIDFromString(idStr)
85 if err != nil {
86 return err
87 }
88 *txid = id
89 return nil
90}
91
92// TxType is a string that represents the type of a Hermez network transaction
93type TxType string
94
95const (
96 // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
97 TxTypeExit TxType = "Exit"
98 // TxTypeTransfer represents L2->L2 token transfer
99 TxTypeTransfer TxType = "Transfer"
100 // TxTypeDeposit represents L1->L2 transfer
101 TxTypeDeposit TxType = "Deposit"
102 // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
103 TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
104 // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
105 TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
106 // TxTypeDepositTransfer TBD
107 TxTypeDepositTransfer TxType = "DepositTransfer"
108 // TxTypeForceTransfer TBD
109 TxTypeForceTransfer TxType = "ForceTransfer"
110 // TxTypeForceExit TBD
111 TxTypeForceExit TxType = "ForceExit"
112 // TxTypeTransferToEthAddr TBD
113 TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
114 // TxTypeTransferToBJJ TBD
115 TxTypeTransferToBJJ TxType = "TransferToBJJ"
116)
117
118// Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
119// TODO: this should be changed for "mini Tx"
120type Tx struct {
121 // Generic
122 IsL1 bool `meddler:"is_l1"`
123 TxID TxID `meddler:"id"`
124 Type TxType `meddler:"type"`
125 Position int `meddler:"position"`
126 FromIdx Idx `meddler:"from_idx"`
127 ToIdx Idx `meddler:"to_idx"`
128 Amount *big.Int `meddler:"amount,bigint"`
129 AmountFloat float64 `meddler:"amount_f"`
130 TokenID TokenID `meddler:"token_id"`
131 USD *float64 `meddler:"amount_usd"`
132 BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
133 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
134 // L1
135 ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
136 UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
137 FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
138 FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
139 LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
140 LoadAmountFloat *float64 `meddler:"load_amount_f"`
141 LoadAmountUSD *float64 `meddler:"load_amount_usd"`
142 // L2
143 Fee *FeeSelector `meddler:"fee"`
144 FeeUSD *float64 `meddler:"fee_usd"`
145 Nonce *Nonce `meddler:"nonce"`
146}
147
148// L1Tx returns a *L1Tx from the Tx
149func (tx *Tx) L1Tx() (*L1Tx, error) {
150 return &L1Tx{
151 TxID: tx.TxID,
152 ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
153 Position: tx.Position,
154 UserOrigin: *tx.UserOrigin,
155 FromIdx: tx.FromIdx,
156 FromEthAddr: tx.FromEthAddr,
157 FromBJJ: tx.FromBJJ,
158 ToIdx: tx.ToIdx,
159 TokenID: tx.TokenID,
160 Amount: tx.Amount,
161 LoadAmount: tx.LoadAmount,
162 EthBlockNum: tx.EthBlockNum,
163 Type: tx.Type,
164 BatchNum: tx.BatchNum,
165 }, nil
166}