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

@@ -227,7 +227,11 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
if s.zki != nil {
// Txs
// s.zki.TxCompressedData[s.i] = tx.TxCompressedData() // uncomment once L1Tx.TxCompressedData is ready
s.zki.FromIdx[s.i] = tx.FromIdx.BigInt()
if tx.FromIdx != nil {
s.zki.FromIdx[s.i] = tx.FromIdx.BigInt()
} else {
s.zki.FromIdx[s.i] = big.NewInt(0)
}
s.zki.ToIdx[s.i] = tx.ToIdx.BigInt()
s.zki.OnChain[s.i] = big.NewInt(1)
@@ -292,7 +296,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
if err != nil {
return nil, nil, false, err
}
return &tx.FromIdx, exitAccount, newExit, nil
return tx.FromIdx, exitAccount, newExit, nil
default:
}
@@ -307,7 +311,7 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
var err error
var auxToIdx common.Idx
// if tx.ToIdx==0, get toIdx by ToEthAddr or ToBJJ
if tx.ToIdx == common.Idx(0) {
if tx.ToIdx == nil || *tx.ToIdx == common.Idx(0) {
auxToIdx, err = s.GetIdxByEthAddrBJJ(tx.ToEthAddr, tx.ToBJJ)
if err != nil {
log.Error(err)
@@ -324,7 +328,7 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
s.zki.ToIdx[s.i] = tx.ToIdx.BigInt()
// fill AuxToIdx if needed
if tx.ToIdx == common.Idx(0) {
if tx.ToIdx == nil {
// use toIdx that can have been filled by tx.ToIdx or
// if tx.Idx==0 (this case), toIdx is filled by the Idx
// from db by ToEthAddr&ToBJJ
@@ -332,7 +336,12 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
}
s.zki.ToBJJAy[s.i] = tx.ToBJJ.Y
s.zki.ToEthAddr[s.i] = common.EthAddrToBigInt(tx.ToEthAddr)
if tx.ToEthAddr != nil {
s.zki.ToEthAddr[s.i] = common.EthAddrToBigInt(*tx.ToEthAddr)
} else {
// Not sure if this should throw an error
s.zki.ToEthAddr[s.i] = big.NewInt(0)
}
s.zki.OnChain[s.i] = big.NewInt(0)
s.zki.NewAccount[s.i] = big.NewInt(0)
@@ -362,13 +371,21 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
case common.TxTypeTransfer:
// go to the MT account of sender and receiver, and update
// balance & nonce
err = s.applyTransfer(tx.Tx(), auxToIdx)
tmpTx, err := tx.Tx()
if err != nil {
return nil, nil, false, err
}
err = s.applyTransfer(tmpTx, auxToIdx)
if err != nil {
return nil, nil, false, err
}
case common.TxTypeExit:
// execute exit flow
exitAccount, newExit, err := s.applyExit(exitTree, tx.Tx())
tmpTx, err := tx.Tx()
if err != nil {
return nil, nil, false, err
}
exitAccount, newExit, err := s.applyExit(exitTree, tmpTx)
if err != nil {
return nil, nil, false, err
}
@@ -428,7 +445,7 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
// in case that the tx is a L1Tx>DepositTransfer
var accReceiver *common.Account
if transfer {
accReceiver, err = s.GetAccount(tx.ToIdx)
accReceiver, err = s.GetAccount(&tx.ToIdx)
if err != nil {
return err
}
@@ -458,7 +475,7 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
// this is done after updating Sender Account (depositer)
if transfer {
// update receiver account in localStateDB
p, err := s.UpdateAccount(tx.ToIdx, accReceiver)
p, err := s.UpdateAccount(&tx.ToIdx, accReceiver)
if err != nil {
return err
}
@@ -487,14 +504,17 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
// the real ToIdx is found trhrough the ToEthAddr or ToBJJ.
func (s *StateDB) applyTransfer(tx *common.Tx, auxToIdx common.Idx) error {
if auxToIdx == 0 {
auxToIdx = tx.ToIdx
if tx.ToIdx == nil {
return errors.New("tx.ToIdx cannot be nil if auxToIdx is 0")
}
auxToIdx = *tx.ToIdx
}
// get sender and receiver accounts from localStateDB
accSender, err := s.GetAccount(tx.FromIdx)
if err != nil {
return err
}
accReceiver, err := s.GetAccount(auxToIdx)
accReceiver, err := s.GetAccount(&auxToIdx)
if err != nil {
return err
}
@@ -525,7 +545,7 @@ func (s *StateDB) applyTransfer(tx *common.Tx, auxToIdx common.Idx) error {
}
// update receiver account in localStateDB
pReceiver, err := s.UpdateAccount(auxToIdx, accReceiver)
pReceiver, err := s.UpdateAccount(&auxToIdx, accReceiver)
if err != nil {
return err
}
@@ -555,7 +575,7 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
EthAddr: tx.FromEthAddr,
}
accSender.Balance = new(big.Int).Add(accSender.Balance, tx.LoadAmount)
accReceiver, err := s.GetAccount(tx.ToIdx)
accReceiver, err := s.GetAccount(&tx.ToIdx)
if err != nil {
return err
}
@@ -587,7 +607,7 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
}
// update receiver account in localStateDB
p, err = s.UpdateAccount(tx.ToIdx, accReceiver)
p, err = s.UpdateAccount(&tx.ToIdx, accReceiver)
if err != nil {
return err
}