Browse Source

Add fees subtraction on TxProcessors

feature/sql-semaphore1
arnaucube 4 years ago
parent
commit
1e71d7f19e
2 changed files with 21 additions and 5 deletions
  1. +20
    -4
      db/statedb/txprocessors.go
  2. +1
    -1
      db/statedb/txprocessors_test.go

+ 20
- 4
db/statedb/txprocessors.go

@ -538,9 +538,16 @@ func (s *StateDB) applyTransfer(tx common.Tx, auxToIdx common.Idx) error {
// increment nonce // increment nonce
accSender.Nonce++ accSender.Nonce++
// subtract amount to the sender
accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
// add amount to the receiver
if !tx.IsL1 {
// compute fee and subtract it from the accSender
fee := common.CalcFeeAmount(tx.Amount, *tx.Fee)
feeAndAmount := new(big.Int).Add(tx.Amount, fee)
accSender.Balance = new(big.Int).Sub(accSender.Balance, feeAndAmount)
// TODO send the fee to the Fee Idx of the Coordinator for the
// TokenID
}
// add amount-feeAmount to the receiver
accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount) accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
// update sender account in localStateDB // update sender account in localStateDB
@ -653,7 +660,16 @@ func (s *StateDB) applyExit(exitTree *merkletree.MerkleTree, tx common.Tx) (*com
if err != nil { if err != nil {
return nil, false, err return nil, false, err
} }
acc.Balance = new(big.Int).Sub(acc.Balance, tx.Amount)
if !tx.IsL1 {
// compute fee and subtract it from the accSender
fee := common.CalcFeeAmount(tx.Amount, *tx.Fee)
feeAndAmount := new(big.Int).Add(tx.Amount, fee)
acc.Balance = new(big.Int).Sub(acc.Balance, feeAndAmount)
// TODO send the fee to the Fee Idx of the Coordinator for the
// TokenID
}
p, err := s.UpdateAccount(tx.FromIdx, acc) p, err := s.UpdateAccount(tx.FromIdx, acc)
if err != nil { if err != nil {
return nil, false, err return nil, false, err

+ 1
- 1
db/statedb/txprocessors_test.go

@ -74,7 +74,7 @@ func TestProcessTxsSynchronizer(t *testing.T) {
assert.Equal(t, 2, len(exitInfos)) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5' assert.Equal(t, 2, len(exitInfos)) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
acc, err = sdb.GetAccount(common.Idx(256)) acc, err = sdb.GetAccount(common.Idx(256))
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "73", acc.Balance.String())
assert.Equal(t, "78", acc.Balance.String())
} }
/* /*

Loading…
Cancel
Save