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

@@ -19,8 +19,9 @@ type Batch struct {
StateRoot Hash `meddler:"state_root"`
NumAccounts int `meddler:"num_accounts"`
ExitRoot Hash `meddler:"exit_root"`
ForgeL1TxsNum int64 `meddler:"forge_l1_txs_num"` // optional, Only when the batch forges L1 txs. Identifier that corresponds to the group of L1 txs forged in the current batch.
ForgeL1TxsNum *int64 `meddler:"forge_l1_txs_num"` // optional, Only when the batch forges L1 txs. Identifier that corresponds to the group of L1 txs forged in the current batch.
SlotNum SlotNum `meddler:"slot_num"` // Slot in which the batch is forged
TotalFeesUSD *float64 `meddler:"total_fees_usd"`
}
// BatchNum identifies a batch

View File

@@ -25,10 +25,10 @@ type L1Tx struct {
// - L1UserTx: 0
// - L1CoordinatorTx: 1
TxID TxID
ToForgeL1TxsNum int64 // toForgeL1TxsNum in which the tx was forged / will be forged
ToForgeL1TxsNum *int64 // toForgeL1TxsNum in which the tx was forged / will be forged
Position int
UserOrigin bool // 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
FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
FromIdx *Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
FromEthAddr ethCommon.Address
FromBJJ *babyjub.PublicKey
ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
@@ -47,7 +47,7 @@ type L1Tx struct {
func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
// calculate TxType
var txType TxType
if l1Tx.FromIdx == Idx(0) {
if l1Tx.FromIdx == nil {
if l1Tx.ToIdx == Idx(0) {
txType = TxTypeCreateAccountDeposit
} else if l1Tx.ToIdx >= IdxUserThreshold {
@@ -55,7 +55,7 @@ func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
} else {
return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx)
}
} else if l1Tx.FromIdx >= IdxUserThreshold {
} else if *l1Tx.FromIdx >= IdxUserThreshold {
if l1Tx.ToIdx == Idx(0) {
txType = TxTypeDeposit
} else if l1Tx.ToIdx == Idx(1) {
@@ -83,7 +83,11 @@ func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
txid[0] = TxIDPrefixL1CoordTx
}
var toForgeL1TxsNumBytes [8]byte
binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], uint64(l1Tx.ToForgeL1TxsNum))
var toForge uint64 = 0
if l1Tx.ToForgeL1TxsNum != nil {
toForge = uint64(*l1Tx.ToForgeL1TxsNum)
}
binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], toForge)
copy(txid[1:9], toForgeL1TxsNumBytes[:])
var positionBytes [2]byte
@@ -98,19 +102,25 @@ func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
func (tx *L1Tx) Tx() *Tx {
f := new(big.Float).SetInt(tx.Amount)
amountFloat, _ := f.Float64()
userOrigin := new(bool)
*userOrigin = tx.UserOrigin
fromEthAddr := new(ethCommon.Address)
*fromEthAddr = tx.FromEthAddr
toIdx := new(Idx)
*toIdx = tx.ToIdx
genericTx := &Tx{
IsL1: true,
TxID: tx.TxID,
Type: tx.Type,
Position: tx.Position,
FromIdx: tx.FromIdx,
ToIdx: tx.ToIdx,
ToIdx: toIdx,
Amount: tx.Amount,
AmountFloat: amountFloat,
TokenID: tx.TokenID,
ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
UserOrigin: tx.UserOrigin,
FromEthAddr: tx.FromEthAddr,
UserOrigin: userOrigin,
FromEthAddr: fromEthAddr,
FromBJJ: tx.FromBJJ,
LoadAmount: tx.LoadAmount,
EthBlockNum: tx.EthBlockNum,
@@ -171,10 +181,14 @@ func L1TxFromBytes(b []byte) (*L1Tx, error) {
if err != nil {
return nil, err
}
tx.FromIdx, err = IdxFromBytes(b[52:58])
fromIdx, err := IdxFromBytes(b[52:58])
if err != nil {
return nil, err
}
if fromIdx != 0 {
tx.FromIdx = new(Idx)
*tx.FromIdx = fromIdx
}
tx.LoadAmount = Float16FromBytes(b[58:60]).BigInt()
tx.Amount = Float16FromBytes(b[60:62]).BigInt()
tx.TokenID, err = TokenIDFromBytes(b[62:66])

View File

@@ -12,14 +12,18 @@ import (
)
func TestNewL1Tx(t *testing.T) {
toForge := new(int64)
*toForge = 123456
fromIdx := new(Idx)
*fromIdx = 300
l1Tx := &L1Tx{
ToForgeL1TxsNum: int64(123456),
ToForgeL1TxsNum: toForge,
Position: 71,
ToIdx: 301,
TokenID: 5,
Amount: big.NewInt(1),
LoadAmount: big.NewInt(2),
FromIdx: 300,
FromIdx: fromIdx,
}
l1Tx, err := NewL1Tx(l1Tx)
assert.Nil(t, err)
@@ -34,12 +38,14 @@ func TestL1TxByteParsers(t *testing.T) {
pk, err := pkComp.Decompress()
require.Nil(t, err)
fromIdx := new(Idx)
*fromIdx = 2
l1Tx := &L1Tx{
ToIdx: 3,
TokenID: 5,
Amount: big.NewInt(1),
LoadAmount: big.NewInt(2),
FromIdx: 2,
FromIdx: fromIdx,
FromBJJ: pk,
FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
}

View File

@@ -68,13 +68,17 @@ func (tx *L2Tx) Tx() *Tx {
*fee = tx.Fee
nonce := new(Nonce)
*nonce = tx.Nonce
fromIdx := new(Idx)
*fromIdx = tx.FromIdx
toIdx := new(Idx)
*toIdx = tx.ToIdx
return &Tx{
IsL1: false,
TxID: tx.TxID,
Type: tx.Type,
Position: tx.Position,
FromIdx: tx.FromIdx,
ToIdx: tx.ToIdx,
FromIdx: fromIdx,
ToIdx: toIdx,
Amount: tx.Amount,
USD: tx.USD,
AmountFloat: amountFloat,
@@ -89,11 +93,15 @@ func (tx *L2Tx) Tx() *Tx {
// PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
// L2Tx filled
func (tx *L2Tx) PoolL2Tx() *PoolL2Tx {
batchNum := new(BatchNum)
*batchNum = tx.BatchNum
toIdx := new(Idx)
*toIdx = tx.ToIdx
return &PoolL2Tx{
TxID: tx.TxID,
BatchNum: tx.BatchNum,
BatchNum: batchNum,
FromIdx: tx.FromIdx,
ToIdx: tx.ToIdx,
ToIdx: toIdx,
Amount: tx.Amount,
Fee: tx.Fee,
Nonce: tx.Nonce,

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

View File

@@ -11,9 +11,11 @@ import (
)
func TestNewPoolL2Tx(t *testing.T) {
toIdx := new(Idx)
*toIdx = 300
poolL2Tx := &PoolL2Tx{
FromIdx: 87654,
ToIdx: 300,
ToIdx: toIdx,
Amount: big.NewInt(4),
TokenID: 5,
Nonce: 144,
@@ -27,10 +29,11 @@ func TestTxCompressedData(t *testing.T) {
var sk babyjub.PrivateKey
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
assert.Nil(t, err)
toIdx := new(Idx)
*toIdx = 3
tx := PoolL2Tx{
FromIdx: 2,
ToIdx: 3,
ToIdx: toIdx,
Amount: big.NewInt(4),
TokenID: 5,
Nonce: 6,
@@ -45,10 +48,10 @@ func TestTxCompressedData(t *testing.T) {
assert.True(t, ok)
assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
assert.Equal(t, "10000000000060000000500040000000000030000000000020001c60be60f", hex.EncodeToString(txCompressedData.Bytes())[1:])
*toIdx = 8
tx = PoolL2Tx{
FromIdx: 7,
ToIdx: 8,
ToIdx: toIdx,
Amount: big.NewInt(9),
TokenID: 10,
Nonce: 11,
@@ -71,15 +74,16 @@ func TestHashToSign(t *testing.T) {
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
assert.Nil(t, err)
ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
toIdx := new(Idx)
*toIdx = 3
tx := PoolL2Tx{
FromIdx: 2,
ToIdx: 3,
ToIdx: toIdx,
Amount: big.NewInt(4),
TokenID: 5,
Nonce: 6,
ToBJJ: sk.Public(),
RqToEthAddr: ethAddr,
RqToEthAddr: &ethAddr,
RqToBJJ: sk.Public(),
}
toSign, err := tx.HashToSign()
@@ -92,15 +96,16 @@ func TestVerifyTxSignature(t *testing.T) {
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
assert.Nil(t, err)
ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
toIdx := new(Idx)
*toIdx = 3
tx := PoolL2Tx{
FromIdx: 2,
ToIdx: 3,
ToIdx: toIdx,
Amount: big.NewInt(4),
TokenID: 5,
Nonce: 6,
ToBJJ: sk.Public(),
RqToEthAddr: ethAddr,
RqToEthAddr: &ethAddr,
RqToBJJ: sk.Public(),
}
toSign, err := tx.HashToSign()

View File

@@ -3,6 +3,7 @@ package common
import (
"database/sql/driver"
"encoding/hex"
"errors"
"fmt"
"math/big"
@@ -57,8 +58,6 @@ type TxType string
const (
// TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
TxTypeExit TxType = "Exit"
// TxTypeWithdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract
TxTypeWithdrawn TxType = "Withdrawn"
// TxTypeTransfer represents L2->L2 token transfer
TxTypeTransfer TxType = "Transfer"
// TxTypeDeposit represents L1->L2 transfer
@@ -86,8 +85,8 @@ type Tx struct {
TxID TxID `meddler:"id"`
Type TxType `meddler:"type"`
Position int `meddler:"position"`
FromIdx Idx `meddler:"from_idx"`
ToIdx Idx `meddler:"to_idx"`
FromIdx *Idx `meddler:"from_idx"`
ToIdx *Idx `meddler:"to_idx"`
Amount *big.Int `meddler:"amount,bigint"`
AmountFloat float64 `meddler:"amount_f"`
TokenID TokenID `meddler:"token_id"`
@@ -95,9 +94,9 @@ type Tx struct {
BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
// L1
ToForgeL1TxsNum int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
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
FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
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
FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
LoadAmountFloat *float64 `meddler:"load_amount_f"`
@@ -109,22 +108,24 @@ type Tx struct {
}
// L1Tx returns a *L1Tx from the Tx
func (tx *Tx) L1Tx() *L1Tx {
l1Tx := &L1Tx{
func (tx *Tx) L1Tx() (*L1Tx, error) {
if tx.UserOrigin == nil || tx.FromEthAddr == nil {
return nil, errors.New("Tx must have UserOrigin != nil and FromEthAddr != nil in order to be able to transform to L1Tx")
}
return &L1Tx{
TxID: tx.TxID,
ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
Position: tx.Position,
UserOrigin: tx.UserOrigin,
UserOrigin: *tx.UserOrigin,
FromIdx: tx.FromIdx,
FromEthAddr: tx.FromEthAddr,
FromEthAddr: *tx.FromEthAddr,
FromBJJ: tx.FromBJJ,
ToIdx: tx.ToIdx,
ToIdx: *tx.ToIdx,
TokenID: tx.TokenID,
Amount: tx.Amount,
LoadAmount: tx.LoadAmount,
EthBlockNum: tx.EthBlockNum,
Type: tx.Type,
BatchNum: tx.BatchNum,
}
return l1Tx
}, nil
}