Feature/null refactor (#173)

* WIP: rebase

* Make nullable fields use pointers
This commit is contained in:
a_bennassar
2020-10-06 10:34:49 +02:00
committed by GitHub
parent 7df9288977
commit 49366e3fa4
32 changed files with 422 additions and 240 deletions

View File

@@ -1,6 +1,7 @@
package common
import (
"errors"
"fmt"
"math/big"
"time"
@@ -19,8 +20,8 @@ type PoolL2Tx struct {
// values: | type | FromIdx | Nonce |
TxID TxID `meddler:"tx_id"`
FromIdx Idx `meddler:"from_idx"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
ToIdx Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
ToEthAddr ethCommon.Address `meddler:"to_eth_addr"`
ToIdx *Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
ToEthAddr *ethCommon.Address `meddler:"to_eth_addr"`
ToBJJ *babyjub.PublicKey `meddler:"to_bjj"` // TODO: stop using json, use scanner/valuer
TokenID TokenID `meddler:"token_id"`
Amount *big.Int `meddler:"amount,bigint"` // TODO: change to float16
@@ -32,21 +33,20 @@ type PoolL2Tx struct {
Signature *babyjub.Signature `meddler:"signature"` // tx signature
Timestamp time.Time `meddler:"timestamp,utctime"` // time when added to the tx pool
// Stored in DB: optional fileds, may be uninitialized
BatchNum BatchNum `meddler:"batch_num,zeroisnull"` // batchNum in which this tx was forged. Presence indicates "forged" state.
RqFromIdx Idx `meddler:"rq_from_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
RqToIdx Idx `meddler:"rq_to_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
RqToEthAddr ethCommon.Address `meddler:"rq_to_eth_addr"`
BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. Presence indicates "forged" state.
RqFromIdx *Idx `meddler:"rq_from_idx"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
RqToIdx *Idx `meddler:"rq_to_idx"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
RqToEthAddr *ethCommon.Address `meddler:"rq_to_eth_addr"`
RqToBJJ *babyjub.PublicKey `meddler:"rq_to_bjj"` // TODO: stop using json, use scanner/valuer
RqTokenID TokenID `meddler:"rq_token_id,zeroisnull"`
RqTokenID *TokenID `meddler:"rq_token_id"`
RqAmount *big.Int `meddler:"rq_amount,bigintnull"` // TODO: change to float16
RqFee FeeSelector `meddler:"rq_fee,zeroisnull"`
RqNonce uint64 `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
RqFee *FeeSelector `meddler:"rq_fee"`
RqNonce *uint64 `meddler:"rq_nonce"` // effective 48 bits used
AbsoluteFee *float64 `meddler:"fee_usd"`
AbsoluteFeeUpdate *time.Time `meddler:"usd_update,utctime"`
Type TxType `meddler:"tx_type"`
// Extra metadata, may be uninitialized
RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs
TokenSymbol string `meddler:"token_symbol"`
}
// NewPoolL2Tx returns the given L2Tx with the TxId & Type parameters calculated
@@ -54,11 +54,11 @@ type PoolL2Tx struct {
func NewPoolL2Tx(poolL2Tx *PoolL2Tx) (*PoolL2Tx, error) {
// calculate TxType
var txType TxType
if poolL2Tx.ToIdx == Idx(0) {
if poolL2Tx.ToIdx == nil || *poolL2Tx.ToIdx == Idx(0) {
txType = TxTypeTransfer
} else if poolL2Tx.ToIdx == Idx(1) {
} else if *poolL2Tx.ToIdx == Idx(1) {
txType = TxTypeExit
} else if poolL2Tx.ToIdx >= IdxUserThreshold {
} else if *poolL2Tx.ToIdx >= IdxUserThreshold {
txType = TxTypeTransfer
} else {
return poolL2Tx, fmt.Errorf("Can not determine type of PoolL2Tx, invalid ToIdx value: %d", poolL2Tx.ToIdx)
@@ -189,14 +189,21 @@ func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
if err != nil {
return nil, err
}
toEthAddr := EthAddrToBigInt(tx.ToEthAddr)
toEthAddr := big.NewInt(0)
if tx.ToEthAddr != nil {
toEthAddr = EthAddrToBigInt(*tx.ToEthAddr)
}
rqToEthAddr := big.NewInt(0)
if tx.RqToEthAddr != nil {
rqToEthAddr = EthAddrToBigInt(*tx.RqToEthAddr)
}
toBJJAy := tx.ToBJJ.Y
rqTxCompressedDataV2, err := tx.TxCompressedDataV2()
if err != nil {
return nil, err
}
return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJAy, rqTxCompressedDataV2, EthAddrToBigInt(tx.RqToEthAddr), tx.RqToBJJ.Y})
return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJAy, rqTxCompressedDataV2, rqToEthAddr, tx.RqToBJJ.Y})
}
// VerifySignature returns true if the signature verification is correct for the given PublicKey
@@ -209,39 +216,51 @@ func (tx *PoolL2Tx) VerifySignature(pk *babyjub.PublicKey) bool {
}
// L2Tx returns a *L2Tx from the PoolL2Tx
func (tx *PoolL2Tx) L2Tx() *L2Tx {
func (tx *PoolL2Tx) L2Tx() (*L2Tx, error) {
if tx.ToIdx == nil || tx.BatchNum == nil {
return nil, errors.New("PoolL2Tx must have ToIdx != nil and BatchNum != nil in order to be able to transform to Tx")
}
return &L2Tx{
TxID: tx.TxID,
BatchNum: tx.BatchNum,
BatchNum: *tx.BatchNum,
FromIdx: tx.FromIdx,
ToIdx: tx.ToIdx,
ToIdx: *tx.ToIdx,
Amount: tx.Amount,
Fee: tx.Fee,
Nonce: tx.Nonce,
Type: tx.Type,
}
}, nil
}
// Tx returns a *Tx from the PoolL2Tx
func (tx *PoolL2Tx) Tx() *Tx {
func (tx *PoolL2Tx) Tx() (*Tx, error) {
if tx.ToIdx == nil {
return nil, errors.New("PoolL2Tx must have ToIdx != nil in order to be able to transform to Tx")
}
fromIdx := new(Idx)
*fromIdx = tx.FromIdx
return &Tx{
TxID: tx.TxID,
FromIdx: tx.FromIdx,
FromIdx: fromIdx,
ToIdx: tx.ToIdx,
Amount: tx.Amount,
Nonce: &tx.Nonce,
Fee: &tx.Fee,
Type: tx.Type,
}
}, nil
}
// PoolL2TxsToL2Txs returns an array of []*L2Tx from an array of []*PoolL2Tx
func PoolL2TxsToL2Txs(txs []*PoolL2Tx) []*L2Tx {
func PoolL2TxsToL2Txs(txs []*PoolL2Tx) ([]*L2Tx, error) {
var r []*L2Tx
for _, tx := range txs {
r = append(r, tx.L2Tx())
for _, poolTx := range txs {
tx, err := poolTx.L2Tx()
if err != nil {
return nil, err
}
r = append(r, tx)
}
return r
return r, nil
}
// PoolL2TxState is a struct that represents the status of a L2 transaction