arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
Merge pull request #379 from hermeznetwork/feature/upd-effamounts

Effective amounts add missing check
Eduard S eduard@iden3.io
Thu, 17 Dec 2020 18:18:21 +0100
commit

d649dd3395c5343355dfc00a7d1fd1accaabad8a

parent

cae69acecb1a2439ded2e1a0ba02b2fcc501e787

2 files changed, 54 insertions(+), 2 deletions(-)

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

@@ -1107,6 +1107,20 @@ if cmp == -1 { // DepositAmount<Amount

tx.EffectiveAmount = big.NewInt(0) return } + + // check if tx.TokenID==receiver.TokenID + accReceiver, err := s.GetAccount(tx.ToIdx) + if err != nil { + log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: can not get account for tx.ToIdx: %d", tx.ToIdx) + tx.EffectiveDepositAmount = big.NewInt(0) + tx.EffectiveAmount = big.NewInt(0) + return + } + if tx.TokenID != accReceiver.TokenID { + log.Debugf("EffectiveAmount = 0: tx TokenID (%d) != receiver account TokenID (%d)", tx.TokenID, accReceiver.TokenID) + tx.EffectiveAmount = big.NewInt(0) + return + } return }

@@ -1159,8 +1173,12 @@ tx.EffectiveAmount = big.NewInt(0)

return } if accSender.TokenID != accReceiver.TokenID { - log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: sender account TokenID (%d) != receiver account TokenID (%d)", accSender.TokenID, accReceiver.TokenID) - tx.EffectiveDepositAmount = big.NewInt(0) + log.Debugf("EffectiveAmount = 0: sender account TokenID (%d) != receiver account TokenID (%d)", accSender.TokenID, accReceiver.TokenID) + tx.EffectiveAmount = big.NewInt(0) + return + } + if tx.TokenID != accReceiver.TokenID { + log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: tx TokenID (%d) != receiver account TokenID (%d)", tx.TokenID, accReceiver.TokenID) tx.EffectiveAmount = big.NewInt(0) return }
M db/statedb/txprocessors_test.go → db/statedb/txprocessors_test.go

@@ -160,6 +160,40 @@ }

sdb.computeEffectiveAmounts(&tx) assert.Equal(t, big.NewInt(8), tx.EffectiveDepositAmount) assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) + + // CreateAccountDepositTransfer for TokenID=1 when receiver does not + // have an account for that TokenID, expect that the + // EffectiveDepositAmount=DepositAmount, but EffectiveAmount==0 + tx = common.L1Tx{ + FromIdx: 0, + ToIdx: 257, + Amount: big.NewInt(8), + DepositAmount: big.NewInt(8), + FromEthAddr: tc.Users["A"].Addr, + TokenID: 2, + UserOrigin: true, + Type: common.TxTypeCreateAccountDepositTransfer, + } + sdb.computeEffectiveAmounts(&tx) + assert.Equal(t, big.NewInt(8), tx.EffectiveDepositAmount) + assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) + + // DepositTransfer for TokenID=1 when receiver does not have an account + // for that TokenID, expect that the + // EffectiveDepositAmount=DepositAmount, but EffectiveAmount=0 + tx = common.L1Tx{ + FromIdx: 258, + ToIdx: 256, + Amount: big.NewInt(8), + DepositAmount: big.NewInt(8), + FromEthAddr: tc.Users["C"].Addr, + TokenID: 1, + UserOrigin: true, + Type: common.TxTypeDepositTransfer, + } + sdb.computeEffectiveAmounts(&tx) + assert.Equal(t, big.NewInt(8), tx.EffectiveDepositAmount) + assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) } func TestProcessTxsBalances(t *testing.T) {