Browse Source

Effective amounts add missing checks

feature/sql-semaphore1
arnaucube 3 years ago
parent
commit
0bcf57603c
2 changed files with 54 additions and 2 deletions
  1. +20
    -2
      db/statedb/txprocessors.go
  2. +34
    -0
      db/statedb/txprocessors_test.go

+ 20
- 2
db/statedb/txprocessors.go

@ -1107,6 +1107,20 @@ func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
tx.EffectiveAmount = big.NewInt(0) tx.EffectiveAmount = big.NewInt(0)
return 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 return
} }
@ -1159,8 +1173,12 @@ func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
return return
} }
if accSender.TokenID != accReceiver.TokenID { 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) tx.EffectiveAmount = big.NewInt(0)
return return
} }

+ 34
- 0
db/statedb/txprocessors_test.go

@ -160,6 +160,40 @@ func TestComputeEffectiveAmounts(t *testing.T) {
sdb.computeEffectiveAmounts(&tx) sdb.computeEffectiveAmounts(&tx)
assert.Equal(t, big.NewInt(8), tx.EffectiveDepositAmount) assert.Equal(t, big.NewInt(8), tx.EffectiveDepositAmount)
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) 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) { func TestProcessTxsBalances(t *testing.T) {

Loading…
Cancel
Save