mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 19:36:44 +01:00
Add TxID calculation & New{Layer}Tx Type
Add TxID calculation & New{Layer}Tx Type
New{Layer}Tx methods that compute the `TxID` & `TxType` values from the
transaction values:
- NewL1Tx
- NewL2Tx
- NewPoolL2Tx
Add TxID Scanner & Valuer for database/sql
HistoryDB & L2DB & API packages tests will need to be addapted to the
TestTransaction generation once is done.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
@@ -21,6 +22,42 @@ type L2Tx struct {
|
||||
EthBlockNum int64 // Ethereum Block Number in which this L2Tx was added to the queue
|
||||
}
|
||||
|
||||
// NewL2Tx returns the given L2Tx with the TxId & Type parameters calculated
|
||||
// from the L2Tx values
|
||||
func NewL2Tx(l2Tx *L2Tx) (*L2Tx, error) {
|
||||
// calculate TxType
|
||||
var txType TxType
|
||||
if l2Tx.ToIdx == Idx(1) {
|
||||
txType = TxTypeExit
|
||||
} else if l2Tx.ToIdx >= IdxUserThreshold {
|
||||
txType = TxTypeTransfer
|
||||
} else {
|
||||
return l2Tx, fmt.Errorf("Can not determine type of L2Tx, invalid ToIdx value: %d", l2Tx.ToIdx)
|
||||
}
|
||||
|
||||
// if TxType!=l2Tx.TxType return error
|
||||
if l2Tx.Type != "" && l2Tx.Type != txType {
|
||||
return l2Tx, fmt.Errorf("L2Tx.Type: %s, should be: %s", l2Tx.Type, txType)
|
||||
}
|
||||
l2Tx.Type = txType
|
||||
|
||||
var txid [TxIDLen]byte
|
||||
txid[0] = TxIDPrefixL2Tx
|
||||
fromIdxBytes, err := l2Tx.FromIdx.Bytes()
|
||||
if err != nil {
|
||||
return l2Tx, err
|
||||
}
|
||||
copy(txid[1:7], fromIdxBytes[:])
|
||||
nonceBytes, err := l2Tx.Nonce.Bytes()
|
||||
if err != nil {
|
||||
return l2Tx, err
|
||||
}
|
||||
copy(txid[7:12], nonceBytes[:])
|
||||
l2Tx.TxID = TxID(txid)
|
||||
|
||||
return l2Tx, nil
|
||||
}
|
||||
|
||||
// Tx returns a *Tx from the L2Tx
|
||||
func (tx *L2Tx) Tx() *Tx {
|
||||
f := new(big.Float).SetInt(tx.Amount)
|
||||
|
||||
Reference in New Issue
Block a user