Skip to content

Commit 53f40ab

Browse files
author
cheezus1
committed
[GH-#83] Added fee to transactions
1 parent 4e89cc9 commit 53f40ab

File tree

11 files changed

+129
-103
lines changed

11 files changed

+129
-103
lines changed

apps/aecore/lib/aecore/chain/chain_state.ex

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule Aecore.Chain.ChainState do
44
The chain state is a map, telling us what amount of tokens each account has.
55
"""
66

7+
alias Aecore.Keys.Worker, as: Keys
8+
79
@doc """
810
Calculates the balance of each account mentioned
911
in the transactions a single block, returns a map with the
@@ -19,7 +21,8 @@ defmodule Aecore.Chain.ChainState do
1921
cond do
2022
transaction.data.from_acc != nil ->
2123
update_block_state(block_state, transaction.data.from_acc,
22-
-transaction.data.value, transaction.data.nonce)
24+
-(transaction.data.value + transaction.data.fee),
25+
transaction.data.nonce)
2326

2427
true ->
2528
block_state
@@ -79,6 +82,25 @@ defmodule Aecore.Chain.ChainState do
7982
Enum.all?()
8083
end
8184

85+
@spec add_fees_to_block_state(list(), map()) :: map()
86+
def add_fees_to_block_state(txs, block_state) do
87+
pubkey = elem(Keys.pubkey(), 1)
88+
total_fees = List.foldl(txs, 0, fn(tx, acc) ->
89+
acc + tx.data.fee
90+
end)
91+
92+
cond do
93+
!Map.has_key?(block_state, pubkey) ->
94+
Map.put(block_state, pubkey, %{balance: total_fees, nonce: 0})
95+
96+
true ->
97+
current_pubkey_block_state = Map.get(block_state, pubkey)
98+
new_pubkey_block_state = %{balance: current_pubkey_block_state.balance + total_fees,
99+
nonce: current_pubkey_block_state.nonce}
100+
Map.replace(block_state, pubkey, new_pubkey_block_state)
101+
end
102+
end
103+
82104
@spec update_block_state(map(), binary(), integer(), integer()) :: map()
83105
defp update_block_state(block_state, account, value, nonce) do
84106
block_state_filled_empty =

apps/aecore/lib/aecore/chain/worker.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ defmodule Aecore.Chain.Worker do
9898
{chain, prev_chain_state} = state
9999
[prior_block | _] = chain
100100
new_block_state = ChainState.calculate_block_state(b.txs)
101-
new_chain_state = ChainState.calculate_chain_state(new_block_state, prev_chain_state)
101+
new_block_state_with_fees = ChainState.add_fees_to_block_state(b.txs, new_block_state)
102+
new_chain_state = ChainState.calculate_chain_state(new_block_state_with_fees, prev_chain_state)
102103

103104
try do
104105
BlockValidation.validate_block!(b, prior_block, new_chain_state)

apps/aecore/lib/aecore/keys/worker.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ defmodule Aecore.Keys.Worker do
3838
- value: The amount of a transaction
3939
4040
"""
41-
@spec sign_tx(binary(), integer(), integer()) :: {:ok, %SignedTx{}}
42-
def sign_tx(to_acc, value, nonce) do
41+
@spec sign_tx(binary(), integer(), integer(), integer()) :: {:ok, %SignedTx{}}
42+
def sign_tx(to_acc, value, nonce, fee) do
4343
{:ok, from_acc} = pubkey()
44-
{:ok, tx_data} = TxData.create(from_acc, to_acc, value, nonce)
44+
{:ok, tx_data} = TxData.create(from_acc, to_acc, value, nonce, fee)
4545
{:ok, signature} = sign(tx_data)
4646
signed_tx = %SignedTx{data: tx_data, signature: signature}
4747
{:ok, signed_tx}

apps/aecore/lib/aecore/miner/worker.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ defmodule Aecore.Miner.Worker do
9595
from_acc: nil,
9696
to_acc: to_acc,
9797
value: @coinbase_transaction_value,
98-
nonce: 0
98+
nonce: 0,
99+
fee: 0
99100
}
100101

101102
%SignedTx{data: tx_data, signature: nil}
@@ -122,7 +123,9 @@ defmodule Aecore.Miner.Worker do
122123
root_hash = BlockValidation.calculate_root_hash(valid_txs)
123124

124125
new_block_state = ChainState.calculate_block_state(valid_txs)
125-
new_chain_state = ChainState.calculate_chain_state(new_block_state, chain_state)
126+
new_block_state_with_fees = ChainState.add_fees_to_block_state(valid_txs, new_block_state)
127+
128+
new_chain_state = ChainState.calculate_chain_state(new_block_state_with_fees, chain_state)
126129
chain_state_hash = ChainState.calculate_chain_state_hash(new_chain_state)
127130

128131
latest_block_hash = BlockValidation.block_header_hash(latest_block.header)

apps/aecore/lib/aecore/structures/tx_data.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ defmodule Aecore.Structures.TxData do
1515
- to_acc: To account is the public address of the account receiving the transaction
1616
- value: The amount of a transaction
1717
"""
18-
defstruct [:nonce, :from_acc, :to_acc, :value]
18+
defstruct [:nonce, :from_acc, :to_acc, :value, :fee]
1919
use ExConstructor
2020

21-
@spec create(binary(), binary(), integer(), integer()) :: {:ok, %TxData{}}
22-
def create(from_acc, to_acc, value, nonce) do
23-
{:ok, %TxData{from_acc: from_acc, to_acc: to_acc, value: value, nonce: nonce}}
21+
@spec create(binary(), binary(), integer(), integer(), integer()) :: {:ok, %TxData{}}
22+
def create(from_acc, to_acc, value, nonce, fee) do
23+
{:ok, %TxData{from_acc: from_acc, to_acc: to_acc, value: value, nonce: nonce, fee: fee}}
2424
end
2525
end

apps/aecore/test/aecore_chain_state_test.exs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ defmodule AecoreChainStateTest do
1414
test "block state" do
1515
block = get_block()
1616

17-
assert %{"a" => %{balance: 3, nonce: 102},
18-
"b" => %{balance: -1, nonce: 1},
19-
"c" => %{balance: -2, nonce: 1}} ==
17+
assert %{"a" => %{balance: 1, nonce: 102},
18+
"b" => %{balance: -2, nonce: 1},
19+
"c" => %{balance: -3, nonce: 1}} ==
2020
ChainState.calculate_block_state(block.txs)
2121
end
2222

@@ -38,13 +38,13 @@ defmodule AecoreChainStateTest do
3838
txs_hash: <<12, 123, 12>>, difficulty_target: 0, nonce: 0,
3939
timestamp: System.system_time(:milliseconds), version: 1}, txs: [
4040
%SignedTx{data: %TxData{from_acc: "a", to_acc: "b",
41-
value: 5, nonce: 101}, signature: <<0>>},
41+
value: 5, nonce: 101, fee: 1}, signature: <<0>>},
4242
%SignedTx{data: %TxData{from_acc: "a", to_acc: "c",
43-
value: 2, nonce: 102}, signature: <<0>>},
43+
value: 2, nonce: 102, fee: 1}, signature: <<0>>},
4444
%SignedTx{data: %TxData{from_acc: "c", to_acc: "b",
45-
value: 4, nonce: 1}, signature: <<0>>},
45+
value: 4, nonce: 1, fee: 1}, signature: <<0>>},
4646
%SignedTx{data: %TxData{from_acc: "b", to_acc: "a",
47-
value: 10, nonce: 1}, signature: <<0>>}]}
47+
value: 10, nonce: 1, fee: 1}, signature: <<0>>}]}
4848
end
4949

5050
end

apps/aecore/test/aecore_keys_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ defmodule AecoreKeysTest do
2020

2121
test "sign transaction" do
2222
{:ok, to_account} = Keys.pubkey()
23-
assert {:ok, _} = Keys.sign_tx(to_account, 5, Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
23+
assert {:ok, _} = Keys.sign_tx(to_account, 5, Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)
2424
end
2525
end

apps/aecore/test/aecore_tx_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule AecoreTxTest do
1515

1616
test "create and verify a signed tx" do
1717
{:ok, to_account} = Keys.pubkey()
18-
{:ok, tx} = Keys.sign_tx(to_account, 5, Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
18+
{:ok, tx} = Keys.sign_tx(to_account, 5, Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)
1919

2020
assert :true = Keys.verify_tx(tx)
2121
end

apps/aecore/test/aecore_txs_pool_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ defmodule AecoreTxsPoolTest do
1818
test "add transaction, remove it and get pool" do
1919
{:ok, to_account} = Keys.pubkey()
2020
{:ok, tx1} = Keys.sign_tx(to_account, 5,
21-
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
21+
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)
2222
{:ok, tx2} = Keys.sign_tx(to_account, 5,
23-
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
23+
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)
2424
Miner.resume()
2525
Miner.suspend()
2626
assert :ok = Pool.add_transaction(tx1)

apps/aecore/test/aecore_validation_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ defmodule AecoreValidationTest do
5555
test "validate transactions in a block" do
5656
{:ok, to_account} = Keys.pubkey()
5757
{:ok, tx1} = Keys.sign_tx(to_account, 5,
58-
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
58+
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)
5959
{:ok, tx2} = Keys.sign_tx(to_account, 10,
60-
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
60+
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)
6161

6262
block = %{Block.genesis_block | txs: [tx1, tx2]}
6363
assert block |> BlockValidation.validate_block_transactions

0 commit comments

Comments
 (0)