diff --git a/db/statedb/txprocessors.go b/db/statedb/txprocessors.go index 2602a10..d6c8368 100644 --- a/db/statedb/txprocessors.go +++ b/db/statedb/txprocessors.go @@ -538,9 +538,16 @@ func (s *StateDB) applyTransfer(tx common.Tx, auxToIdx common.Idx) error { // increment 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) // update sender account in localStateDB @@ -653,7 +660,16 @@ func (s *StateDB) applyExit(exitTree *merkletree.MerkleTree, tx common.Tx) (*com if err != nil { 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) if err != nil { return nil, false, err diff --git a/db/statedb/txprocessors_test.go b/db/statedb/txprocessors_test.go index adfb97b..f657e16 100644 --- a/db/statedb/txprocessors_test.go +++ b/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' acc, err = sdb.GetAccount(common.Idx(256)) assert.Nil(t, err) - assert.Equal(t, "73", acc.Balance.String()) + assert.Equal(t, "78", acc.Balance.String()) } /* diff --git a/test/til/README.md b/test/til/README.md index 99231d7..2f2293a 100644 --- a/test/til/README.md +++ b/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 // 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 // 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 // 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, // paying a fee of 3. Transaction will be a L2Tx 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 -// 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. // Transaction will be L1UserTx of ForceExit type @@ -79,8 +79,9 @@ Type: PoolL2 // paying a fee of 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 diff --git a/test/til/lang.go b/test/til/lang.go index 7f463db..31b80d5 100644 --- a/test/til/lang.go +++ b/test/til/lang.go @@ -332,6 +332,7 @@ func (p *parser) parseLine(setType setType) (*instruction, error) { return c, fmt.Errorf("Set type not defined") } transferring := false + fee := false if setType == setTypeBlockchain { switch lit { @@ -339,9 +340,11 @@ func (p *parser) parseLine(setType setType) (*instruction, error) { c.typ = common.TxTypeDeposit case "Exit": c.typ = common.TxTypeExit + fee = true case "Transfer": c.typ = common.TxTypeTransfer transferring = true + fee = true case "CreateAccountDeposit": c.typ = common.TxTypeCreateAccountDeposit case "CreateAccountDepositTransfer": @@ -355,6 +358,7 @@ func (p *parser) parseLine(setType setType) (*instruction, error) { transferring = true case "ForceTransfer": c.typ = common.TxTypeForceTransfer + transferring = true case "ForceExit": c.typ = common.TxTypeForceExit default: @@ -365,14 +369,18 @@ func (p *parser) parseLine(setType setType) (*instruction, error) { case "PoolTransfer": c.typ = common.TxTypeTransfer transferring = true + fee = true case "PoolTransferToEthAddr": c.typ = common.TxTypeTransferToEthAddr transferring = true + fee = true case "PoolTransferToBJJ": c.typ = common.TxTypeTransferToBJJ transferring = true + fee = true case "PoolExit": c.typ = common.TxTypeExit + fee = true default: return c, fmt.Errorf("Unexpected PoolL2 tx type: %s", lit) } @@ -450,7 +458,7 @@ func (p *parser) parseLine(setType setType) (*instruction, error) { } else { c.amount = uint64(amount) } - if transferring { + if fee { if err := p.expectChar(c, "("); err != nil { return c, err } diff --git a/test/til/lang_test.go b/test/til/lang_test.go index e22392b..6d9a987 100644 --- a/test/til/lang_test.go +++ b/test/til/lang_test.go @@ -24,7 +24,7 @@ func TestParseBlockchainTxs(t *testing.T) { Deposit(2) A: 20 Deposit(1) B: 5 CreateAccountDeposit(1) C: 5 - CreateAccountDepositTransfer(1) D-A: 15, 10 (3) + CreateAccountDepositTransfer(1) D-A: 15, 10 CreateAccountDepositCoordinator(1) E // L2 transactions @@ -36,7 +36,7 @@ func TestParseBlockchainTxs(t *testing.T) { > batch AddToken(3) - DepositTransfer(1) A-B: 15, 10 (1) + DepositTransfer(1) A-B: 15, 10 Transfer(1) C-A : 3 (1) Transfer(2) A-B: 15 (1) @@ -53,7 +53,7 @@ func TestParseBlockchainTxs(t *testing.T) { > block // Exits - Exit(1) A: 5 + Exit(1) A: 5 (1) ` 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, typeNewBatch, instructions.instructions[11].typ) 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, "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()) @@ -87,7 +87,7 @@ func TestParsePoolTxs(t *testing.T) { PoolTransfer(2) A-B: 3 (3) PoolTransfer(1) B-D: 3 (1) PoolTransfer(1) C-D: 3 (1) - PoolExit(1) A: 5 + PoolExit(1) A: 5 (1) ` parser := newParser(strings.NewReader(s)) diff --git a/test/til/sets.go b/test/til/sets.go index f32dd83..9a795a8 100644 --- a/test/til/sets.go +++ b/test/til/sets.go @@ -130,10 +130,10 @@ Transfer(1) Y-E: 5 (1) Transfer(1) Z-A: 5 (1) // exits 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 @@ -153,7 +153,7 @@ Transfer(1) B-N: 5 (1) Transfer(1) C-O: 5 (1) Transfer(1) H-O: 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)' CreateAccountDepositCoordinator(2) D @@ -172,7 +172,7 @@ PoolTransfer(1) C-A: 3 (1) PoolTransfer(1) A-B: 1 (1) PoolTransfer(2) A-B: 15 (1) PoolTransfer(2) B-D: 3 (1) -PoolExit(1) A: 3 +PoolExit(1) A: 3 (1) PoolTransfer(1) A-B: 6 (1) PoolTransfer(1) B-C: 3 (1) PoolTransfer(1) A-C: 3 (1) diff --git a/test/til/txs_test.go b/test/til/txs_test.go index 4feeaeb..3fdf0e0 100644 --- a/test/til/txs_test.go +++ b/test/til/txs_test.go @@ -46,7 +46,7 @@ func TestGenerateBlocks(t *testing.T) { > batchL1 // batchNum = 1 > batchL1 // batchNum = 2 - CreateAccountDepositTransfer(1) F-A: 15, 10 (3) + CreateAccountDepositTransfer(1) F-A: 15, 10 Transfer(1) A-B: 6 (1) Transfer(1) B-D: 3 (1) @@ -57,7 +57,7 @@ func TestGenerateBlocks(t *testing.T) { CreateAccountDepositCoordinator(1) E CreateAccountDepositCoordinator(2) B - DepositTransfer(1) A-B: 15, 10 (1) + DepositTransfer(1) A-B: 15, 10 Transfer(1) C-A : 3 (1) Transfer(2) A-B: 15 (1) Transfer(1) A-E: 1 (1) @@ -79,7 +79,7 @@ func TestGenerateBlocks(t *testing.T) { // Exits Transfer(1) A-B: 1 (1) - Exit(1) A: 5 + Exit(1) A: 5 (1) > batch // batchNum = 6 > block @@ -206,7 +206,7 @@ func TestGeneratePoolL2Txs(t *testing.T) { PoolTransfer(1) User0-User1: 15 (1) PoolTransfer(3) User1-User0: 15 (1) PoolTransfer(2) B-D: 3 (1) - PoolExit(1) A: 3 + PoolExit(1) A: 3 (1) PoolTransferToEthAddr(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) // on purpose this is moving more money that what it has in the account, Til should not fail Transfer(1) B-A: 6 (1) - Exit(1) A: 3 + Exit(1) A: 3 (1) > batch ` tc = NewContext(eth.RollupConstMaxL1UserTx)