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