arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
Merge pull request #357 from hermeznetwork/feature/chec-effamount

Update StateDB computeEffectiveAmounts
Eduard S eduard@iden3.io
Wed, 16 Dec 2020 13:44:52 +0100
commit

7277dc1eb4856c30ba25c7c26bb589bb8cbcb012

parent

b53cf10b3506331f1b461aa11242763ab2f4f648

2 files changed, 37 insertions(+), 7 deletions(-)

jump to
M db/statedb/txprocessors.go → db/statedb/txprocessors.go

@@ -1086,6 +1086,9 @@ }

// computeEffectiveAmounts checks that the L1Tx data is correct func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) { + tx.EffectiveAmount = tx.Amount + tx.EffectiveDepositAmount = tx.DepositAmount + if !tx.UserOrigin { // case where the L1Tx is generated by the Coordinator tx.EffectiveAmount = big.NewInt(0)

@@ -1093,8 +1096,6 @@ tx.EffectiveDepositAmount = big.NewInt(0)

return } - tx.EffectiveAmount = tx.Amount - tx.EffectiveDepositAmount = tx.DepositAmount if tx.Type == common.TxTypeCreateAccountDeposit { return }

@@ -1140,10 +1141,8 @@

// check that the tx.FromEthAddr is the same than the EthAddress of the // Sender if !bytes.Equal(tx.FromEthAddr.Bytes(), accSender.EthAddr.Bytes()) { - log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: tx.FromEthAddr (%s) must be the same EthAddr of the sender account by the Idx (%s)", tx.FromEthAddr.Hex(), accSender.EthAddr.Hex()) - tx.EffectiveDepositAmount = big.NewInt(0) + log.Debugf("EffectiveAmount = 0: tx.FromEthAddr (%s) must be the same EthAddr of the sender account by the Idx (%s)", tx.FromEthAddr.Hex(), accSender.EthAddr.Hex()) tx.EffectiveAmount = big.NewInt(0) - return } if tx.ToIdx == common.Idx(1) || tx.ToIdx == common.Idx(0) {
M db/statedb/txprocessors_test.go → db/statedb/txprocessors_test.go

@@ -23,7 +23,7 @@ require.Nil(t, err)

assert.Equal(t, expected, acc.Balance.String()) } -func TestCheckL1TxInvalidData(t *testing.T) { +func TestComputeEffectiveAmounts(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) defer assert.Nil(t, os.RemoveAll(dir))

@@ -80,7 +80,7 @@ sdb.computeEffectiveAmounts(&tx)

assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount) assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) - // expect no-error due not enough funds in a + // expect no-error as there are enough funds in a // CreateAccountDepositTransfer transction tx = common.L1Tx{ FromIdx: 0,

@@ -130,6 +130,35 @@ UserOrigin: true,

} sdb.computeEffectiveAmounts(&tx) assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount) + assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) + + // expect on TxTypeDepositTransfer EffectiveAmount=0, but + // EffectiveDepositAmount!=0, due not enough funds to make the transfer + tx = common.L1Tx{ + FromIdx: 256, + ToIdx: 257, + Amount: big.NewInt(20), + DepositAmount: big.NewInt(8), + FromEthAddr: tc.Users["A"].Addr, + UserOrigin: true, + } + sdb.computeEffectiveAmounts(&tx) + assert.Equal(t, big.NewInt(8), tx.EffectiveDepositAmount) + assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) + + // expect on TxTypeDepositTransfer EffectiveAmount=0, but + // EffectiveDepositAmount!=0, due different EthAddr from FromIdx + // address + tx = common.L1Tx{ + FromIdx: 256, + ToIdx: 257, + Amount: big.NewInt(8), + DepositAmount: big.NewInt(8), + FromEthAddr: tc.Users["B"].Addr, + UserOrigin: true, + } + sdb.computeEffectiveAmounts(&tx) + assert.Equal(t, big.NewInt(8), tx.EffectiveDepositAmount) assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) }

@@ -477,6 +506,8 @@ acc, err = sdb.GetAccount(common.Idx(257))

require.Nil(t, err) assert.Equal(t, common.TokenID(1), acc.TokenID) assert.Equal(t, "2", acc.Balance.String()) + + assert.Equal(t, "2720257526434001367979405991743527513807903085728407823609738212616896104498", sdb.mt.Root().BigInt().String()) } func TestProcessTxsRootTestVectors(t *testing.T) {