Skip to content

Commit e3212cb

Browse files
author
cheezus1
committed
[GH-#83] Fees are now added to the coinbase transaction
1 parent 0fe9aad commit e3212cb

File tree

4 files changed

+11
-28
lines changed

4 files changed

+11
-28
lines changed

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

-18
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,6 @@ defmodule Aecore.Chain.ChainState do
8080
Enum.all?()
8181
end
8282

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

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ defmodule Aecore.Chain.Worker do
100100
[prior_block | _] = chain
101101
pubkey = elem(Keys.pubkey(), 1)
102102
new_block_state = ChainState.calculate_block_state(b.txs)
103-
new_block_state_with_fees = ChainState.add_fees_to_block_state(b.txs, new_block_state, pubkey)
104-
new_chain_state = ChainState.calculate_chain_state(new_block_state_with_fees, prev_chain_state)
103+
new_chain_state = ChainState.calculate_chain_state(new_block_state, prev_chain_state)
105104

106105
try do
107106
BlockValidation.validate_block!(b, prior_block, new_chain_state)

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ defmodule Aecore.Miner.Worker do
9090
{:next_state, :idle, data}
9191
end
9292

93-
def get_coinbase_transaction(to_acc) do
93+
def get_coinbase_transaction(to_acc, total_fees) do
9494
tx_data = %TxData{
9595
from_acc: nil,
9696
to_acc: to_acc,
97-
value: @coinbase_transaction_value,
97+
value: @coinbase_transaction_value + total_fees,
9898
nonce: 0,
99-
fee: 0
99+
fee: total_fees
100100
}
101101

102102
%SignedTx{data: tx_data, signature: nil}
@@ -119,13 +119,14 @@ defmodule Aecore.Miner.Worker do
119119

120120
valid_txs = BlockValidation.filter_invalid_transactions_chainstate(ordered_txs_list, chain_state)
121121
{_, pubkey} = Keys.pubkey()
122-
valid_txs = [get_coinbase_transaction(pubkey) | valid_txs]
122+
total_fees = List.foldl(valid_txs, 0, fn(tx, acc) ->
123+
acc + tx.data.fee
124+
end)
125+
valid_txs = [get_coinbase_transaction(pubkey, total_fees) | valid_txs]
123126
root_hash = BlockValidation.calculate_root_hash(valid_txs)
124127

125128
new_block_state = ChainState.calculate_block_state(valid_txs)
126-
new_block_state_with_fees = ChainState.add_fees_to_block_state(valid_txs, new_block_state, pubkey)
127-
128-
new_chain_state = ChainState.calculate_chain_state(new_block_state_with_fees, chain_state)
129+
new_chain_state = ChainState.calculate_chain_state(new_block_state, chain_state)
129130
chain_state_hash = ChainState.calculate_chain_state_hash(new_chain_state)
130131

131132
latest_block_hash = BlockValidation.block_header_hash(latest_block.header)

apps/aecore/lib/aecore/utils/blockchain/block_validation.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ defmodule Aecore.Utils.Blockchain.BlockValidation do
3737
throw({:error, "One or more transactions not valid"})
3838

3939
coinbase_transactions_sum > Miner.coinbase_transaction_value() ->
40+
IO.puts(coinbase_transactions_sum)
4041
throw({:error, "Sum of coinbase transactions values exceeds the maximum coinbase transactions value"})
4142

4243
new_block.header.chain_state_hash != chain_state_hash ->
@@ -150,7 +151,7 @@ defmodule Aecore.Utils.Blockchain.BlockValidation do
150151
block.txs
151152
|> Enum.map(
152153
fn tx -> cond do
153-
SignedTx.is_coinbase(tx) -> tx.data.value
154+
SignedTx.is_coinbase(tx) -> tx.data.value - tx.data.fee
154155
true -> 0
155156
end
156157
end

0 commit comments

Comments
 (0)