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