Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/aecore/lib/aecore/chain/chain_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule Aecore.Chain.ChainState do
cond do
transaction.data.from_acc != nil ->
update_block_state(block_state, transaction.data.from_acc,
-transaction.data.value, transaction.data.nonce)
-(transaction.data.value + transaction.data.fee),
transaction.data.nonce)

true ->
block_state
Expand Down
1 change: 1 addition & 0 deletions apps/aecore/lib/aecore/chain/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Aecore.Chain.Worker do
alias Aecore.Structures.Block
alias Aecore.Chain.ChainState
alias Aecore.Txs.Pool.Worker, as: Pool
alias Aecore.Keys.Worker, as: Keys
alias Aecore.Utils.Blockchain.BlockValidation
alias Aecore.Peers.Worker, as: Peers

Expand Down
6 changes: 3 additions & 3 deletions apps/aecore/lib/aecore/keys/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ defmodule Aecore.Keys.Worker do
- value: The amount of a transaction

"""
@spec sign_tx(binary(), integer(), integer()) :: {:ok, %SignedTx{}}
def sign_tx(to_acc, value, nonce) do
@spec sign_tx(binary(), integer(), integer(), integer()) :: {:ok, %SignedTx{}}
def sign_tx(to_acc, value, nonce, fee) do
{:ok, from_acc} = pubkey()
{:ok, tx_data} = TxData.create(from_acc, to_acc, value, nonce)
{:ok, tx_data} = TxData.create(from_acc, to_acc, value, nonce, fee)
{:ok, signature} = sign(tx_data)
signed_tx = %SignedTx{data: tx_data, signature: signature}
{:ok, signed_tx}
Expand Down
16 changes: 12 additions & 4 deletions apps/aecore/lib/aecore/miner/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,26 @@ defmodule Aecore.Miner.Worker do
{:next_state, :idle, data}
end

def get_coinbase_transaction(to_acc) do
def get_coinbase_transaction(to_acc, total_fees) do
tx_data = %TxData{
from_acc: nil,
to_acc: to_acc,
value: @coinbase_transaction_value,
nonce: 0
value: @coinbase_transaction_value + total_fees,
nonce: 0,
fee: 0
}

%SignedTx{data: tx_data, signature: nil}
end

def coinbase_transaction_value, do: @coinbase_transaction_value

def calculate_total_fees(txs) do
List.foldl(txs, 0, fn(tx, acc) ->
acc + tx.data.fee
end)
end

## Internal
@spec mine_next_block(integer()) :: :ok | :error
defp mine_next_block(start_nonce) do
Expand All @@ -135,7 +142,8 @@ defmodule Aecore.Miner.Worker do

valid_txs = BlockValidation.filter_invalid_transactions_chainstate(ordered_txs_list, chain_state)
{_, pubkey} = Keys.pubkey()
valid_txs = [get_coinbase_transaction(pubkey) | valid_txs]
total_fees = calculate_total_fees(valid_txs)
valid_txs = [get_coinbase_transaction(pubkey, total_fees) | valid_txs]
root_hash = BlockValidation.calculate_root_hash(valid_txs)

new_block_state = ChainState.calculate_block_state(valid_txs)
Expand Down
8 changes: 4 additions & 4 deletions apps/aecore/lib/aecore/structures/tx_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ defmodule Aecore.Structures.TxData do
- to_acc: To account is the public address of the account receiving the transaction
- value: The amount of a transaction
"""
defstruct [:nonce, :from_acc, :to_acc, :value]
defstruct [:nonce, :from_acc, :to_acc, :value, :fee]
use ExConstructor

@spec create(binary(), binary(), integer(), integer()) :: {:ok, %TxData{}}
def create(from_acc, to_acc, value, nonce) do
{:ok, %TxData{from_acc: from_acc, to_acc: to_acc, value: value, nonce: nonce}}
@spec create(binary(), binary(), integer(), integer(), integer()) :: {:ok, %TxData{}}
def create(from_acc, to_acc, value, nonce, fee) do
{:ok, %TxData{from_acc: from_acc, to_acc: to_acc, value: value, nonce: nonce, fee: fee}}
end
end
3 changes: 2 additions & 1 deletion apps/aecore/lib/aecore/utils/blockchain/block_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Aecore.Utils.Blockchain.BlockValidation do

is_difficulty_target_met = Cuckoo.verify(new_block.header)
coinbase_transactions_sum = sum_coinbase_transactions(new_block)
total_fees = Miner.calculate_total_fees(new_block.txs)

cond do
# do not check previous block hash for genesis block, there is none
Expand All @@ -36,7 +37,7 @@ defmodule Aecore.Utils.Blockchain.BlockValidation do
!(validate_block_transactions(new_block) |> Enum.all?()) ->
throw({:error, "One or more transactions not valid"})

coinbase_transactions_sum > Miner.coinbase_transaction_value() ->
coinbase_transactions_sum > Miner.coinbase_transaction_value() + total_fees ->
throw({:error, "Sum of coinbase transactions values exceeds the maximum coinbase transactions value"})

new_block.header.chain_state_hash != chain_state_hash ->
Expand Down
14 changes: 7 additions & 7 deletions apps/aecore/test/aecore_chain_state_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ defmodule AecoreChainStateTest do
test "block state" do
block = get_block()

assert %{"a" => %{balance: 3, nonce: 102},
"b" => %{balance: -1, nonce: 1},
"c" => %{balance: -2, nonce: 1}} ==
assert %{"a" => %{balance: 1, nonce: 102},
"b" => %{balance: -2, nonce: 1},
"c" => %{balance: -3, nonce: 1}} ==
ChainState.calculate_block_state(block.txs)
end

Expand All @@ -38,13 +38,13 @@ defmodule AecoreChainStateTest do
txs_hash: <<12, 123, 12>>, difficulty_target: 0, nonce: 0,
timestamp: System.system_time(:milliseconds), version: 1}, txs: [
%SignedTx{data: %TxData{from_acc: "a", to_acc: "b",
value: 5, nonce: 101}, signature: <<0>>},
value: 5, nonce: 101, fee: 1}, signature: <<0>>},
%SignedTx{data: %TxData{from_acc: "a", to_acc: "c",
value: 2, nonce: 102}, signature: <<0>>},
value: 2, nonce: 102, fee: 1}, signature: <<0>>},
%SignedTx{data: %TxData{from_acc: "c", to_acc: "b",
value: 4, nonce: 1}, signature: <<0>>},
value: 4, nonce: 1, fee: 1}, signature: <<0>>},
%SignedTx{data: %TxData{from_acc: "b", to_acc: "a",
value: 10, nonce: 1}, signature: <<0>>}]}
value: 10, nonce: 1, fee: 1}, signature: <<0>>}]}
end

end
2 changes: 1 addition & 1 deletion apps/aecore/test/aecore_keys_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule AecoreKeysTest do

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

test "check pubkey length" do
Expand Down
2 changes: 1 addition & 1 deletion apps/aecore/test/aecore_tx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule AecoreTxTest do

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

assert :true = Keys.verify_tx(tx)
end
Expand Down
6 changes: 4 additions & 2 deletions apps/aecore/test/aecore_txs_pool_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ defmodule AecoreTxsPoolTest do
@tag timeout: 1000000000
test "add transaction, remove it and get pool" do
{:ok, to_account} = Keys.pubkey()
{:ok, tx1} = Keys.sign_tx(to_account, 5, Map.get(Chain.chain_state(), to_account, %{nonce: 0}).nonce + 1)
{:ok, tx2} = Keys.sign_tx(to_account, 5, Map.get(Chain.chain_state(), to_account, %{nonce: 0}).nonce + 1)
{:ok, tx1} = Keys.sign_tx(to_account, 5,
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)
{:ok, tx2} = Keys.sign_tx(to_account, 5,
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)
Miner.resume()
Miner.suspend()
assert :ok = Pool.add_transaction(tx1)
Expand Down
4 changes: 2 additions & 2 deletions apps/aecore/test/aecore_validation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ defmodule AecoreValidationTest do
test "validate transactions in a block" do
{:ok, to_account} = Keys.pubkey()
{:ok, tx1} = Keys.sign_tx(to_account, 5,
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)
{:ok, tx2} = Keys.sign_tx(to_account, 10,
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1, 1)

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