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