hermez-node/common/tx.go
common/tx.goRaw
1package common
2
3import (
4 "encoding/binary"
5 "fmt"
6 "math/big"
7)
8
9const (
10 idxBytesLen = 4
11 // maxIdxValue is the maximum value that Idx can have (32 bits: maxIdxValue=2**32-1)
12 maxIdxValue = 0xffffffff
13)
14
15// Idx represents the account Index in the MerkleTree
16type Idx uint32
17
18// Bytes returns a byte array representing the Idx
19func (idx Idx) Bytes() []byte {
20 var b [4]byte
21 binary.LittleEndian.PutUint32(b[:], uint32(idx))
22 return b[:]
23}
24
25// BigInt returns a *big.Int representing the Idx
26func (idx Idx) BigInt() *big.Int {
27 return big.NewInt(int64(idx))
28}
29
30// IdxFromBytes returns Idx from a byte array
31func IdxFromBytes(b []byte) (Idx, error) {
32 if len(b) != idxBytesLen {
33 return 0, fmt.Errorf("can not parse Idx, bytes len %d, expected 4", len(b))
34 }
35 idx := binary.LittleEndian.Uint32(b[:4])
36 return Idx(idx), nil
37}
38
39// IdxFromBigInt converts a *big.Int to Idx type
40func IdxFromBigInt(b *big.Int) (Idx, error) {
41 if b.Int64() > maxIdxValue {
42 return 0, ErrNumOverflow
43 }
44 return Idx(uint32(b.Int64())), nil
45}
46
47// TxID is the identifier of a Hermez network transaction
48type TxID Hash // Hash is a guess
49
50// TxType is a string that represents the type of a Hermez network transaction
51type TxType string
52
53const (
54 // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
55 TxTypeExit TxType = "Exit"
56 // TxTypeWithdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract
57 TxTypeWithdrawn TxType = "Withdrawn"
58 // TxTypeTransfer represents L2->L2 token transfer
59 TxTypeTransfer TxType = "Transfer"
60 // TxTypeDeposit represents L1->L2 transfer
61 TxTypeDeposit TxType = "Deposit"
62 // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
63 TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
64 // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
65 TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
66 // TxTypeDepositTransfer TBD
67 TxTypeDepositTransfer TxType = "TxTypeDepositTransfer"
68 // TxTypeForceTransfer TBD
69 TxTypeForceTransfer TxType = "TxTypeForceTransfer"
70 // TxTypeForceExit TBD
71 TxTypeForceExit TxType = "TxTypeForceExit"
72 // TxTypeTransferToEthAddr TBD
73 TxTypeTransferToEthAddr TxType = "TxTypeTransferToEthAddr"
74 // TxTypeTransferToBJJ TBD
75 TxTypeTransferToBJJ TxType = "TxTypeTransferToBJJ"
76)
77
78// Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
79type Tx struct {
80 TxID TxID
81 FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
82 ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositTransfer
83 Amount *big.Int
84 Nonce Nonce // effective 40 bits used
85 Fee FeeSelector
86 Type TxType // optional, descrives which kind of tx it's
87 BatchNum BatchNum // batchNum in which this tx was forged. Presence indicates "forged" state.
88}