Browse Source

Merge pull request #225 from hermeznetwork/feature/fees-update

Feature/fees update
feature/sql-semaphore1
Eduard S 3 years ago
committed by GitHub
parent
commit
b70001f92f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 33 deletions
  1. +20
    -4
      db/statedb/txprocessors.go
  2. +1
    -1
      db/statedb/txprocessors_test.go
  3. +12
    -11
      test/til/README.md
  4. +9
    -1
      test/til/lang.go
  5. +5
    -5
      test/til/lang_test.go
  6. +6
    -6
      test/til/sets.go
  7. +5
    -5
      test/til/txs_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())
} }
/* /*

+ 12
- 11
test/til/README.md

@ -27,8 +27,8 @@ CreateAccountDeposit(1) A: 50
// create the account of TokenID=1 for the user B, deposit of TokenID=1, on the // create the account of TokenID=1 for the user B, deposit of TokenID=1, on the
// account of tokenID=1 for the user B, of an amount of 40 units and atomically // account of tokenID=1 for the user B, of an amount of 40 units and atomically
// transfer 10 units to account of tokenID=1 for the user A, paying a fee of 2
CreateAccountDepositTransfer(1) B-A: 40, 10 (2)
// transfer 10 units to account of tokenID=1 for the user A
CreateAccountDepositTransfer(1) B-A: 40, 10
// transaction generated by the Coordinator, create account for user User0 for // transaction generated by the Coordinator, create account for user User0 for
// the TokenID=2, with a deposit of 0 // the TokenID=2, with a deposit of 0
@ -40,20 +40,20 @@ Deposit(1) A: 6
// deposit of TokenID=1, on the account of tokenID=1 for the user B, of an // deposit of TokenID=1, on the account of tokenID=1 for the user B, of an
// amount of 6 units and atomically transfer 10 units to account of tokenID=1 for // amount of 6 units and atomically transfer 10 units to account of tokenID=1 for
// the user A, paying a fee of 2
DepositTransfer(1) B-A: 6, 4 (2)
// the user A
DepositTransfer(1) B-A: 6, 4
// transfer of TokenID=1, from the account A to B (for that token), of 6 units, // transfer of TokenID=1, from the account A to B (for that token), of 6 units,
// paying a fee of 3. Transaction will be a L2Tx // paying a fee of 3. Transaction will be a L2Tx
Transfer(1) A-B: 6 (3) Transfer(1) A-B: 6 (3)
// exit of TokenID=1, from the account A (for that token), of 5 units.
// Transaction will be a L2Tx
Exit(1) A: 5
// exit of TokenID=1, from the account A (for that token), of 5 units, paying a
// fee of 1. Transaction will be a L2Tx
Exit(1) A: 5 (1)
// force-transfer of TokenID=1, from the account A to B (for that token), of 6 // force-transfer of TokenID=1, from the account A to B (for that token), of 6
// units, paying a fee of 3. Transaction will be L1UserTx of ForceTransfer type
ForceTransfer(1) A-B: 6 (3)
// units. Transaction will be L1UserTx of ForceTransfer type
ForceTransfer(1) A-B: 6
// force-exit of TokenID=1, from the account A (for that token), of 5 units. // force-exit of TokenID=1, from the account A (for that token), of 5 units.
// Transaction will be L1UserTx of ForceExit type // Transaction will be L1UserTx of ForceExit type
@ -79,8 +79,9 @@ Type: PoolL2
// paying a fee of 4 // paying a fee of 4
PoolTransfer(1) A-B: 6 (4) PoolTransfer(1) A-B: 6 (4)
// exit of TokenID=1, from the account A (for that token), of 3 units
PoolExit(1) A: 3
// exit of TokenID=1, from the account A (for that token), of 3 units, paying a
// fee of 1
PoolExit(1) A: 3 (1)
``` ```
## Usage ## Usage

+ 9
- 1
test/til/lang.go

@ -332,6 +332,7 @@ func (p *parser) parseLine(setType setType) (*instruction, error) {
return c, fmt.Errorf("Set type not defined") return c, fmt.Errorf("Set type not defined")
} }
transferring := false transferring := false
fee := false
if setType == setTypeBlockchain { if setType == setTypeBlockchain {
switch lit { switch lit {
@ -339,9 +340,11 @@ func (p *parser) parseLine(setType setType) (*instruction, error) {
c.typ = common.TxTypeDeposit c.typ = common.TxTypeDeposit
case "Exit": case "Exit":
c.typ = common.TxTypeExit c.typ = common.TxTypeExit
fee = true
case "Transfer": case "Transfer":
c.typ = common.TxTypeTransfer c.typ = common.TxTypeTransfer
transferring = true transferring = true
fee = true
case "CreateAccountDeposit": case "CreateAccountDeposit":
c.typ = common.TxTypeCreateAccountDeposit c.typ = common.TxTypeCreateAccountDeposit
case "CreateAccountDepositTransfer": case "CreateAccountDepositTransfer":
@ -355,6 +358,7 @@ func (p *parser) parseLine(setType setType) (*instruction, error) {
transferring = true transferring = true
case "ForceTransfer": case "ForceTransfer":
c.typ = common.TxTypeForceTransfer c.typ = common.TxTypeForceTransfer
transferring = true
case "ForceExit": case "ForceExit":
c.typ = common.TxTypeForceExit c.typ = common.TxTypeForceExit
default: default:
@ -365,14 +369,18 @@ func (p *parser) parseLine(setType setType) (*instruction, error) {
case "PoolTransfer": case "PoolTransfer":
c.typ = common.TxTypeTransfer c.typ = common.TxTypeTransfer
transferring = true transferring = true
fee = true
case "PoolTransferToEthAddr": case "PoolTransferToEthAddr":
c.typ = common.TxTypeTransferToEthAddr c.typ = common.TxTypeTransferToEthAddr
transferring = true transferring = true
fee = true
case "PoolTransferToBJJ": case "PoolTransferToBJJ":
c.typ = common.TxTypeTransferToBJJ c.typ = common.TxTypeTransferToBJJ
transferring = true transferring = true
fee = true
case "PoolExit": case "PoolExit":
c.typ = common.TxTypeExit c.typ = common.TxTypeExit
fee = true
default: default:
return c, fmt.Errorf("Unexpected PoolL2 tx type: %s", lit) return c, fmt.Errorf("Unexpected PoolL2 tx type: %s", lit)
} }
@ -450,7 +458,7 @@ func (p *parser) parseLine(setType setType) (*instruction, error) {
} else { } else {
c.amount = uint64(amount) c.amount = uint64(amount)
} }
if transferring {
if fee {
if err := p.expectChar(c, "("); err != nil { if err := p.expectChar(c, "("); err != nil {
return c, err return c, err
} }

+ 5
- 5
test/til/lang_test.go

@ -24,7 +24,7 @@ func TestParseBlockchainTxs(t *testing.T) {
Deposit(2) A: 20 Deposit(2) A: 20
Deposit(1) B: 5 Deposit(1) B: 5
CreateAccountDeposit(1) C: 5 CreateAccountDeposit(1) C: 5
CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
CreateAccountDepositTransfer(1) D-A: 15, 10
CreateAccountDepositCoordinator(1) E CreateAccountDepositCoordinator(1) E
// L2 transactions // L2 transactions
@ -36,7 +36,7 @@ func TestParseBlockchainTxs(t *testing.T) {
> batch > batch
AddToken(3) AddToken(3)
DepositTransfer(1) A-B: 15, 10 (1)
DepositTransfer(1) A-B: 15, 10
Transfer(1) C-A : 3 (1) Transfer(1) C-A : 3 (1)
Transfer(2) A-B: 15 (1) Transfer(2) A-B: 15 (1)
@ -53,7 +53,7 @@ func TestParseBlockchainTxs(t *testing.T) {
> block > block
// Exits // Exits
Exit(1) A: 5
Exit(1) A: 5 (1)
` `
parser := newParser(strings.NewReader(s)) parser := newParser(strings.NewReader(s))
@ -72,7 +72,7 @@ func TestParseBlockchainTxs(t *testing.T) {
assert.Equal(t, txTypeCreateAccountDepositCoordinator, instructions.instructions[7].typ) assert.Equal(t, txTypeCreateAccountDepositCoordinator, instructions.instructions[7].typ)
assert.Equal(t, typeNewBatch, instructions.instructions[11].typ) assert.Equal(t, typeNewBatch, instructions.instructions[11].typ)
assert.Equal(t, "Deposit(1)User0:20", instructions.instructions[16].raw()) assert.Equal(t, "Deposit(1)User0:20", instructions.instructions[16].raw())
assert.Equal(t, "Type: DepositTransfer, From: A, To: B, LoadAmount: 15, Amount: 10, Fee: 1, TokenID: 1\n", instructions.instructions[13].String())
assert.Equal(t, "Type: DepositTransfer, From: A, To: B, LoadAmount: 15, Amount: 10, Fee: 0, TokenID: 1\n", instructions.instructions[13].String())
assert.Equal(t, "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n", instructions.instructions[19].String()) assert.Equal(t, "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n", instructions.instructions[19].String())
assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.instructions[15].raw()) assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.instructions[15].raw())
assert.Equal(t, "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n", instructions.instructions[15].String()) assert.Equal(t, "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n", instructions.instructions[15].String())
@ -87,7 +87,7 @@ func TestParsePoolTxs(t *testing.T) {
PoolTransfer(2) A-B: 3 (3) PoolTransfer(2) A-B: 3 (3)
PoolTransfer(1) B-D: 3 (1) PoolTransfer(1) B-D: 3 (1)
PoolTransfer(1) C-D: 3 (1) PoolTransfer(1) C-D: 3 (1)
PoolExit(1) A: 5
PoolExit(1) A: 5 (1)
` `
parser := newParser(strings.NewReader(s)) parser := newParser(strings.NewReader(s))

+ 6
- 6
test/til/sets.go

@ -130,10 +130,10 @@ Transfer(1) Y-E: 5 (1)
Transfer(1) Z-A: 5 (1) Transfer(1) Z-A: 5 (1)
// exits // exits
ForceExit(1) A: 5 ForceExit(1) A: 5
Exit(1) K: 5
Exit(1) X: 5
Exit(1) Y: 5
Exit(1) Z: 5
Exit(1) K: 5 (1)
Exit(1) X: 5 (1)
Exit(1) Y: 5 (1)
Exit(1) Z: 5 (1)
> batch > batch
@ -153,7 +153,7 @@ Transfer(1) B-N: 5 (1)
Transfer(1) C-O: 5 (1) Transfer(1) C-O: 5 (1)
Transfer(1) H-O: 5 (1) Transfer(1) H-O: 5 (1)
Transfer(1) I-H: 5 (1) Transfer(1) I-H: 5 (1)
Exit(1) A: 5
Exit(1) A: 5 (1)
// create CoordinatorTx CreateAccount for D, TokenId 2, used at SetPool0 for 'PoolTransfer(2) B-D: 3 (1)' // create CoordinatorTx CreateAccount for D, TokenId 2, used at SetPool0 for 'PoolTransfer(2) B-D: 3 (1)'
CreateAccountDepositCoordinator(2) D CreateAccountDepositCoordinator(2) D
@ -172,7 +172,7 @@ PoolTransfer(1) C-A: 3 (1)
PoolTransfer(1) A-B: 1 (1) PoolTransfer(1) A-B: 1 (1)
PoolTransfer(2) A-B: 15 (1) PoolTransfer(2) A-B: 15 (1)
PoolTransfer(2) B-D: 3 (1) PoolTransfer(2) B-D: 3 (1)
PoolExit(1) A: 3
PoolExit(1) A: 3 (1)
PoolTransfer(1) A-B: 6 (1) PoolTransfer(1) A-B: 6 (1)
PoolTransfer(1) B-C: 3 (1) PoolTransfer(1) B-C: 3 (1)
PoolTransfer(1) A-C: 3 (1) PoolTransfer(1) A-C: 3 (1)

+ 5
- 5
test/til/txs_test.go

@ -46,7 +46,7 @@ func TestGenerateBlocks(t *testing.T) {
> batchL1 // batchNum = 1 > batchL1 // batchNum = 1
> batchL1 // batchNum = 2 > batchL1 // batchNum = 2
CreateAccountDepositTransfer(1) F-A: 15, 10 (3)
CreateAccountDepositTransfer(1) F-A: 15, 10
Transfer(1) A-B: 6 (1) Transfer(1) A-B: 6 (1)
Transfer(1) B-D: 3 (1) Transfer(1) B-D: 3 (1)
@ -57,7 +57,7 @@ func TestGenerateBlocks(t *testing.T) {
CreateAccountDepositCoordinator(1) E CreateAccountDepositCoordinator(1) E
CreateAccountDepositCoordinator(2) B CreateAccountDepositCoordinator(2) B
DepositTransfer(1) A-B: 15, 10 (1)
DepositTransfer(1) A-B: 15, 10
Transfer(1) C-A : 3 (1) Transfer(1) C-A : 3 (1)
Transfer(2) A-B: 15 (1) Transfer(2) A-B: 15 (1)
Transfer(1) A-E: 1 (1) Transfer(1) A-E: 1 (1)
@ -79,7 +79,7 @@ func TestGenerateBlocks(t *testing.T) {
// Exits // Exits
Transfer(1) A-B: 1 (1) Transfer(1) A-B: 1 (1)
Exit(1) A: 5
Exit(1) A: 5 (1)
> batch // batchNum = 6 > batch // batchNum = 6
> block > block
@ -206,7 +206,7 @@ func TestGeneratePoolL2Txs(t *testing.T) {
PoolTransfer(1) User0-User1: 15 (1) PoolTransfer(1) User0-User1: 15 (1)
PoolTransfer(3) User1-User0: 15 (1) PoolTransfer(3) User1-User0: 15 (1)
PoolTransfer(2) B-D: 3 (1) PoolTransfer(2) B-D: 3 (1)
PoolExit(1) A: 3
PoolExit(1) A: 3 (1)
PoolTransferToEthAddr(1) A-B: 1 (1) PoolTransferToEthAddr(1) A-B: 1 (1)
PoolTransferToBJJ(1) A-B: 1 (1) PoolTransferToBJJ(1) A-B: 1 (1)
` `
@ -350,7 +350,7 @@ func TestGenerateErrors(t *testing.T) {
Transfer(1) A-B: 6 (1) Transfer(1) A-B: 6 (1)
Transfer(1) A-B: 6 (1) // on purpose this is moving more money that what it has in the account, Til should not fail Transfer(1) A-B: 6 (1) // on purpose this is moving more money that what it has in the account, Til should not fail
Transfer(1) B-A: 6 (1) Transfer(1) B-A: 6 (1)
Exit(1) A: 3
Exit(1) A: 3 (1)
> batch > batch
` `
tc = NewContext(eth.RollupConstMaxL1UserTx) tc = NewContext(eth.RollupConstMaxL1UserTx)

Loading…
Cancel
Save