Add transakcio coordinator tx instruction feature

Transakcio:
- Transaction generation to fail when Transfer to an account that don't exist (not created yet)
- Add CreateAccountDepositCoordinator instruction feature
- Updated lang.go instruction type parser approach for more simplicity of code
- Remove `*testing.T` from transactions generation methods, use `log.Fatal` instead
This commit is contained in:
arnaucube
2020-10-14 17:17:28 +02:00
parent 250f1aa119
commit 17ff917a20
8 changed files with 116 additions and 108 deletions

View File

@@ -248,12 +248,14 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
// & nonce
err := s.applyTransfer(tx.Tx(), 0) // 0 for the parameter toIdx, as at L1Tx ToIdx can only be 0 in the Deposit type case.
if err != nil {
log.Error(err)
return nil, nil, false, err
}
case common.TxTypeCreateAccountDeposit:
// add new account to the MT, update balance of the MT account
err := s.applyCreateAccount(tx)
if err != nil {
log.Error(err)
return nil, nil, false, err
}
// TODO applyCreateAccount will return the created account,
@@ -268,6 +270,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
// update balance of the MT account
err := s.applyDeposit(tx, false)
if err != nil {
log.Error(err)
return nil, nil, false, err
}
case common.TxTypeDepositTransfer:
@@ -275,6 +278,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
// & receiver
err := s.applyDeposit(tx, true)
if err != nil {
log.Error(err)
return nil, nil, false, err
}
case common.TxTypeCreateAccountDepositTransfer:
@@ -282,6 +286,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
// update balance & nonce of sender & receiver
err := s.applyCreateAccountDepositTransfer(tx)
if err != nil {
log.Error(err)
return nil, nil, false, err
}
@@ -293,6 +298,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
// execute exit flow
exitAccount, newExit, err := s.applyExit(exitTree, tx.Tx())
if err != nil {
log.Error(err)
return nil, nil, false, err
}
return &tx.FromIdx, exitAccount, newExit, nil
@@ -356,6 +362,7 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
// as type==TypeSynchronizer, always tx.ToIdx!=0
acc, err := s.GetAccount(tx.FromIdx)
if err != nil {
log.Error(err)
return nil, nil, false, err
}
tx.Nonce = acc.Nonce
@@ -371,12 +378,14 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
// balance & nonce
err = s.applyTransfer(tx.Tx(), tx.AuxToIdx)
if err != nil {
log.Error(err)
return nil, nil, false, err
}
case common.TxTypeExit:
// execute exit flow
exitAccount, newExit, err := s.applyExit(exitTree, tx.Tx())
if err != nil {
log.Error(err)
return nil, nil, false, err
}
return &tx.FromIdx, exitAccount, newExit, nil
@@ -493,16 +502,18 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
// the receiver. This parameter is used when the tx.ToIdx is not specified and
// the real ToIdx is found trhrough the ToEthAddr or ToBJJ.
func (s *StateDB) applyTransfer(tx common.Tx, auxToIdx common.Idx) error {
if auxToIdx == 0 {
if auxToIdx == common.Idx(0) {
auxToIdx = tx.ToIdx
}
// get sender and receiver accounts from localStateDB
accSender, err := s.GetAccount(tx.FromIdx)
if err != nil {
log.Error(err)
return err
}
accReceiver, err := s.GetAccount(auxToIdx)
if err != nil {
log.Error(err)
return err
}
@@ -517,6 +528,7 @@ func (s *StateDB) applyTransfer(tx common.Tx, auxToIdx common.Idx) error {
// update sender account in localStateDB
pSender, err := s.UpdateAccount(tx.FromIdx, accSender)
if err != nil {
log.Error(err)
return err
}
if s.zki != nil {

View File

@@ -21,7 +21,7 @@ func TestProcessTxsSynchronizer(t *testing.T) {
assert.Nil(t, err)
// generate test transactions from test.SetTest0 code
tc := transakcio.NewTestContext(t)
tc := transakcio.NewTestContext()
blocks := tc.GenerateBlocks(transakcio.SetBlockchain0)
assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
@@ -31,7 +31,7 @@ func TestProcessTxsSynchronizer(t *testing.T) {
assert.Equal(t, 1, len(blocks[0].Batches[1].L1CoordinatorTxs))
assert.Equal(t, 59, len(blocks[0].Batches[1].L2Txs))
assert.Equal(t, 9, len(blocks[0].Batches[2].L1UserTxs))
assert.Equal(t, 0, len(blocks[0].Batches[2].L1CoordinatorTxs))
assert.Equal(t, 1, len(blocks[0].Batches[2].L1CoordinatorTxs))
assert.Equal(t, 8, len(blocks[0].Batches[2].L2Txs))
// use first batch
@@ -76,7 +76,7 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
assert.Nil(t, err)
// generate test transactions from test.SetTest0 code
tc := transakcio.NewTestContext(t)
tc := transakcio.NewTestContext()
blocks := tc.GenerateBlocks(transakcio.SetBlockchain0)
assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
@@ -125,7 +125,7 @@ func TestZKInputsGeneration(t *testing.T) {
assert.Nil(t, err)
// generate test transactions from test.SetTest0 code
tc := transakcio.NewTestContext(t)
tc := transakcio.NewTestContext()
blocks := tc.GenerateBlocks(transakcio.SetBlockchain0)
assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
assert.Equal(t, 0, len(blocks[0].Batches[0].L1CoordinatorTxs))