arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/l2tx.go
common/l2tx.goRaw
  1package common
  2
  3import (
  4	"fmt"
  5	"math/big"
  6)
  7
  8// L2Tx is a struct that represents an already forged L2 tx
  9type L2Tx struct {
 10	// Stored in DB: mandatory fileds
 11	TxID        TxID
 12	BatchNum    BatchNum // batchNum in which this tx was forged.
 13	Position    int
 14	FromIdx     Idx
 15	ToIdx       Idx
 16	Amount      *big.Int
 17	USD         *float64
 18	Fee         FeeSelector
 19	FeeUSD      *float64
 20	Nonce       Nonce
 21	Type        TxType
 22	EthBlockNum int64 // Ethereum Block Number in which this L2Tx was added to the queue
 23}
 24
 25// NewL2Tx returns the given L2Tx with the TxId & Type parameters calculated
 26// from the L2Tx values
 27func NewL2Tx(l2Tx *L2Tx) (*L2Tx, error) {
 28	// calculate TxType
 29	var txType TxType
 30	if l2Tx.ToIdx == Idx(1) {
 31		txType = TxTypeExit
 32	} else if l2Tx.ToIdx >= IdxUserThreshold {
 33		txType = TxTypeTransfer
 34	} else {
 35		return l2Tx, fmt.Errorf("Can not determine type of L2Tx, invalid ToIdx value: %d", l2Tx.ToIdx)
 36	}
 37
 38	// if TxType!=l2Tx.TxType return error
 39	if l2Tx.Type != "" && l2Tx.Type != txType {
 40		return l2Tx, fmt.Errorf("L2Tx.Type: %s, should be: %s", l2Tx.Type, txType)
 41	}
 42	l2Tx.Type = txType
 43
 44	var txid [TxIDLen]byte
 45	txid[0] = TxIDPrefixL2Tx
 46	fromIdxBytes, err := l2Tx.FromIdx.Bytes()
 47	if err != nil {
 48		return l2Tx, err
 49	}
 50	copy(txid[1:7], fromIdxBytes[:])
 51	nonceBytes, err := l2Tx.Nonce.Bytes()
 52	if err != nil {
 53		return l2Tx, err
 54	}
 55	copy(txid[7:12], nonceBytes[:])
 56	l2Tx.TxID = TxID(txid)
 57
 58	return l2Tx, nil
 59}
 60
 61// Tx returns a *Tx from the L2Tx
 62func (tx *L2Tx) Tx() *Tx {
 63	f := new(big.Float).SetInt(tx.Amount)
 64	amountFloat, _ := f.Float64()
 65	batchNum := new(BatchNum)
 66	*batchNum = tx.BatchNum
 67	fee := new(FeeSelector)
 68	*fee = tx.Fee
 69	nonce := new(Nonce)
 70	*nonce = tx.Nonce
 71	fromIdx := new(Idx)
 72	*fromIdx = tx.FromIdx
 73	toIdx := new(Idx)
 74	*toIdx = tx.ToIdx
 75	return &Tx{
 76		IsL1:        false,
 77		TxID:        tx.TxID,
 78		Type:        tx.Type,
 79		Position:    tx.Position,
 80		FromIdx:     fromIdx,
 81		ToIdx:       toIdx,
 82		Amount:      tx.Amount,
 83		USD:         tx.USD,
 84		AmountFloat: amountFloat,
 85		BatchNum:    batchNum,
 86		EthBlockNum: tx.EthBlockNum,
 87		Fee:         fee,
 88		FeeUSD:      tx.FeeUSD,
 89		Nonce:       nonce,
 90	}
 91}
 92
 93// PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
 94// L2Tx filled
 95func (tx *L2Tx) PoolL2Tx() *PoolL2Tx {
 96	batchNum := new(BatchNum)
 97	*batchNum = tx.BatchNum
 98	toIdx := new(Idx)
 99	*toIdx = tx.ToIdx
100	return &PoolL2Tx{
101		TxID:     tx.TxID,
102		BatchNum: batchNum,
103		FromIdx:  tx.FromIdx,
104		ToIdx:    toIdx,
105		Amount:   tx.Amount,
106		Fee:      tx.Fee,
107		Nonce:    tx.Nonce,
108		Type:     tx.Type,
109	}
110}
111
112// L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
113// where the PoolL2Tx only have the parameters of a L2Tx filled.
114func L2TxsToPoolL2Txs(txs []*L2Tx) []*PoolL2Tx {
115	var r []*PoolL2Tx
116	for _, tx := range txs {
117		r = append(r, tx.PoolL2Tx())
118	}
119	return r
120}