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
10 ethCommon "github.com/ethereum/go-ethereum/common"
11 "github.com/iden3/go-iden3-crypto/babyjub"
12)
13
14const (
15 // TXIDPrefixL1CoordTx is the prefix that determines that the TxID is
16 // for a L1CoordinatorTx
17 //nolinter:gomnd
18 TxIDPrefixL1CoordTx = byte(1)
19
20 // TxIDPrefixL2Tx is the prefix that determines that the TxID is for a
21 // L2Tx (or PoolL2Tx)
22 //nolinter:gomnd
23 TxIDPrefixL2Tx = byte(2)
24
25 // TxIDLen is the length of the TxID byte array
26 TxIDLen = 12
27)
28
29// TxID is the identifier of a Hermez network transaction
30type TxID [TxIDLen]byte
31
32// Scan implements Scanner for database/sql.
33func (txid *TxID) Scan(src interface{}) error {
34 srcB, ok := src.([]byte)
35 if !ok {
36 return fmt.Errorf("can't scan %T into TxID", src)
37 }
38 if len(srcB) != TxIDLen {
39 return fmt.Errorf("can't scan []byte of len %d into TxID, need %d", len(srcB), TxIDLen)
40 }
41 copy(txid[:], srcB)
42 return nil
43}
44
45// Value implements valuer for database/sql.
46func (txid TxID) Value() (driver.Value, error) {
47 return txid[:], nil
48}
49
50// String returns a string hexadecimal representation of the TxID
51func (txid TxID) String() string {
52 return "0x" + hex.EncodeToString(txid[:])
53}
54
55// TxType is a string that represents the type of a Hermez network transaction
56type TxType string
57
58const (
59 // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
60 TxTypeExit TxType = "Exit"
61 // TxTypeTransfer represents L2->L2 token transfer
62 TxTypeTransfer TxType = "Transfer"
63 // TxTypeDeposit represents L1->L2 transfer
64 TxTypeDeposit TxType = "Deposit"
65 // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
66 TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
67 // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
68 TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
69 // TxTypeDepositTransfer TBD
70 TxTypeDepositTransfer TxType = "DepositTransfer"
71 // TxTypeForceTransfer TBD
72 TxTypeForceTransfer TxType = "ForceTransfer"
73 // TxTypeForceExit TBD
74 TxTypeForceExit TxType = "ForceExit"
75 // TxTypeTransferToEthAddr TBD
76 TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
77 // TxTypeTransferToBJJ TBD
78 TxTypeTransferToBJJ TxType = "TransferToBJJ"
79)
80
81// Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
82type Tx struct {
83 // Generic
84 IsL1 bool `meddler:"is_l1"`
85 TxID TxID `meddler:"id"`
86 Type TxType `meddler:"type"`
87 Position int `meddler:"position"`
88 FromIdx *Idx `meddler:"from_idx"`
89 ToIdx *Idx `meddler:"to_idx"`
90 Amount *big.Int `meddler:"amount,bigint"`
91 AmountFloat float64 `meddler:"amount_f"`
92 TokenID TokenID `meddler:"token_id"`
93 USD *float64 `meddler:"amount_usd"`
94 BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
95 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
96 // L1
97 ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
98 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
99 FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
100 FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
101 LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
102 LoadAmountFloat *float64 `meddler:"load_amount_f"`
103 LoadAmountUSD *float64 `meddler:"load_amount_usd"`
104 // L2
105 Fee *FeeSelector `meddler:"fee"`
106 FeeUSD *float64 `meddler:"fee_usd"`
107 Nonce *Nonce `meddler:"nonce"`
108}
109
110// L1Tx returns a *L1Tx from the Tx
111func (tx *Tx) L1Tx() (*L1Tx, error) {
112 if tx.UserOrigin == nil || tx.FromEthAddr == nil {
113 return nil, errors.New("Tx must have UserOrigin != nil and FromEthAddr != nil in order to be able to transform to L1Tx")
114 }
115 return &L1Tx{
116 TxID: tx.TxID,
117 ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
118 Position: tx.Position,
119 UserOrigin: *tx.UserOrigin,
120 FromIdx: tx.FromIdx,
121 FromEthAddr: *tx.FromEthAddr,
122 FromBJJ: tx.FromBJJ,
123 ToIdx: *tx.ToIdx,
124 TokenID: tx.TokenID,
125 Amount: tx.Amount,
126 LoadAmount: tx.LoadAmount,
127 EthBlockNum: tx.EthBlockNum,
128 Type: tx.Type,
129 BatchNum: tx.BatchNum,
130 }, nil
131}