Browse Source

Merge pull request #357 from hermeznetwork/feature/chec-effamount

Update StateDB computeEffectiveAmounts
feature/sql-semaphore1
Eduard S 3 years ago
committed by GitHub
parent
commit
7277dc1eb4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 7 deletions
  1. +4
    -5
      db/statedb/txprocessors.go
  2. +33
    -2
      db/statedb/txprocessors_test.go

+ 4
- 5
db/statedb/txprocessors.go

@ -1086,6 +1086,9 @@ func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx,
// computeEffectiveAmounts checks that the L1Tx data is correct // computeEffectiveAmounts checks that the L1Tx data is correct
func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) { func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
tx.EffectiveAmount = tx.Amount
tx.EffectiveDepositAmount = tx.DepositAmount
if !tx.UserOrigin { if !tx.UserOrigin {
// case where the L1Tx is generated by the Coordinator // case where the L1Tx is generated by the Coordinator
tx.EffectiveAmount = big.NewInt(0) tx.EffectiveAmount = big.NewInt(0)
@ -1093,8 +1096,6 @@ func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
return return
} }
tx.EffectiveAmount = tx.Amount
tx.EffectiveDepositAmount = tx.DepositAmount
if tx.Type == common.TxTypeCreateAccountDeposit { if tx.Type == common.TxTypeCreateAccountDeposit {
return return
} }
@ -1140,10 +1141,8 @@ func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
// check that the tx.FromEthAddr is the same than the EthAddress of the // check that the tx.FromEthAddr is the same than the EthAddress of the
// Sender // Sender
if !bytes.Equal(tx.FromEthAddr.Bytes(), accSender.EthAddr.Bytes()) { 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) tx.EffectiveAmount = big.NewInt(0)
return
} }
if tx.ToIdx == common.Idx(1) || tx.ToIdx == common.Idx(0) { if tx.ToIdx == common.Idx(1) || tx.ToIdx == common.Idx(0) {

+ 33
- 2
db/statedb/txprocessors_test.go

@ -23,7 +23,7 @@ func checkBalance(t *testing.T, tc *til.Context, sdb *StateDB, username string,
assert.Equal(t, expected, acc.Balance.String()) assert.Equal(t, expected, acc.Balance.String())
} }
func TestCheckL1TxInvalidData(t *testing.T) {
func TestComputeEffectiveAmounts(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb") dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err) require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir)) defer assert.Nil(t, os.RemoveAll(dir))
@ -80,7 +80,7 @@ func TestCheckL1TxInvalidData(t *testing.T) {
assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount) assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) 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 // CreateAccountDepositTransfer transction
tx = common.L1Tx{ tx = common.L1Tx{
FromIdx: 0, FromIdx: 0,
@ -131,6 +131,35 @@ func TestCheckL1TxInvalidData(t *testing.T) {
sdb.computeEffectiveAmounts(&tx) sdb.computeEffectiveAmounts(&tx)
assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount) assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount) 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)
} }
func TestProcessTxsBalances(t *testing.T) { func TestProcessTxsBalances(t *testing.T) {
@ -477,6 +506,8 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, common.TokenID(1), acc.TokenID) assert.Equal(t, common.TokenID(1), acc.TokenID)
assert.Equal(t, "2", acc.Balance.String()) assert.Equal(t, "2", acc.Balance.String())
assert.Equal(t, "2720257526434001367979405991743527513807903085728407823609738212616896104498", sdb.mt.Root().BigInt().String())
} }
func TestProcessTxsRootTestVectors(t *testing.T) { func TestProcessTxsRootTestVectors(t *testing.T) {

Loading…
Cancel
Save