diff --git a/apps/aecore/include/common.hrl b/apps/aecore/include/common.hrl new file mode 100644 index 0000000000..82027ffbe4 --- /dev/null +++ b/apps/aecore/include/common.hrl @@ -0,0 +1 @@ +-type(pubkey() :: binary()). diff --git a/apps/aecore/include/trees.hrl b/apps/aecore/include/trees.hrl index 010ba5d87f..01d99b548e 100644 --- a/apps/aecore/include/trees.hrl +++ b/apps/aecore/include/trees.hrl @@ -1,4 +1,13 @@ --record(trees, {}). +-include("common.hrl"). + +-record(trees, { + accounts}). -type(trees() :: #trees{}). %% Placeholder to define state Merkle trees + +-record(account, { + pubkey = <<>> :: pubkey(), + balance = 0 :: non_neg_integer(), + nonce = 0 :: non_neg_integer(), + height = 0 :: non_neg_integer()}). diff --git a/apps/aecore/include/txs.hrl b/apps/aecore/include/txs.hrl index ec733795ac..ebad6bdd2a 100644 --- a/apps/aecore/include/txs.hrl +++ b/apps/aecore/include/txs.hrl @@ -1,4 +1,4 @@ --type(pubkey() :: binary()). +-include("common.hrl"). -record(coinbase_tx, { account = <<>> :: pubkey(), diff --git a/apps/aecore/src/aec_accounts.erl b/apps/aecore/src/aec_accounts.erl new file mode 100644 index 0000000000..8eb29a9802 --- /dev/null +++ b/apps/aecore/src/aec_accounts.erl @@ -0,0 +1,32 @@ +-module(aec_accounts). + +%% API +-export([get/2, + get_with_proof/2, + put/2, + earn/3]). + +-include("trees.hrl"). + +get(Pubkey, _AccountsTree) -> + %% To be implemented when we have state Merkle trees + %% to hold accounts with balances + Account = #account{pubkey = Pubkey, + balance = 0}, + {ok, Account}. + +get_with_proof(Pubkey, _AccountsTree) -> + %% To be implemented when we have state Merkle trees + %% to hold accounts with balances + Account = #account{pubkey = Pubkey, + balance = 0}, + {ok, {Account, <<"fakeproof">>}}. + +put(_Account, AccountsTrees) -> + %% To be implemented when we have state Merkle trees + %% to hold accounts with balances + {ok, AccountsTrees}. + +earn(#account{balance = Balance0}, Amount, Height) -> + {ok, #account{balance = Balance0 + Amount, + height = Height}}. diff --git a/apps/aecore/src/pow_sha256.erl b/apps/aecore/src/aec_pow_sha256.erl similarity index 96% rename from apps/aecore/src/pow_sha256.erl rename to apps/aecore/src/aec_pow_sha256.erl index feb71904c3..c0a1301b2b 100644 --- a/apps/aecore/src/pow_sha256.erl +++ b/apps/aecore/src/aec_pow_sha256.erl @@ -4,7 +4,7 @@ %%% Module dealing with SHA256 Proof of Work calculation and hash generation %%% @end %%%============================================================================= --module(pow_sha256). +-module(aec_pow_sha256). -export([generate/3, recalculate_difficulty/3, @@ -64,8 +64,8 @@ generate_with_nonce(_Data, _Difficulty, _Nonce, 0) -> %% Count exhausted: fail {error, generation_count_exhausted}; generate_with_nonce(Data, Difficulty, Nonce, Count) -> - Hash1 = sha256:hash(Data), - Hash2 = sha256:hash(<>), + Hash1 = aec_sha256:hash(Data), + Hash2 = aec_sha256:hash(<>), case binary_to_scientific(Hash2) of HD when HD < Difficulty -> %% Hash satisfies condition: return nonce diff --git a/apps/aecore/src/sha256.erl b/apps/aecore/src/aec_sha256.erl similarity index 97% rename from apps/aecore/src/sha256.erl rename to apps/aecore/src/aec_sha256.erl index aaf24e8fa9..427674f7ef 100644 --- a/apps/aecore/src/sha256.erl +++ b/apps/aecore/src/aec_sha256.erl @@ -5,7 +5,7 @@ %%% @end %%%============================================================================= --module(sha256). +-module(aec_sha256). -export([hash/1]). diff --git a/apps/aecore/src/aec_trees.erl b/apps/aecore/src/aec_trees.erl new file mode 100644 index 0000000000..1d8ec4d4d8 --- /dev/null +++ b/apps/aecore/src/aec_trees.erl @@ -0,0 +1,13 @@ +-module(aec_trees). + +-include("trees.hrl"). + +%% API +-export([accounts/1, + with_accounts/2]). + +accounts(Trees) -> + Trees#trees.accounts. + +with_accounts(Trees, Accounts) -> + Trees#trees{accounts = Accounts}. diff --git a/apps/aecore/src/txs/aec_coinbase_tx.erl b/apps/aecore/src/txs/aec_coinbase_tx.erl new file mode 100644 index 0000000000..aeeedb7e1c --- /dev/null +++ b/apps/aecore/src/txs/aec_coinbase_tx.erl @@ -0,0 +1,26 @@ +-module(aec_coinbase_tx). + +%% API +-export([new/2, run/3]). + +-behavior(aec_tx). + +-include("txs.hrl"). + +-define(BLOCK_MINE_REWARD, 10). %% To be set in governance + + +new(#{account := AccountPubkey}, Trees) -> + AccountsTrees = aec_trees:accounts(Trees), + {ok, {_Account, Proof}} = aec_accounts:get_with_proof(AccountPubkey, AccountsTrees), + {ok, {#coinbase_tx{account = AccountPubkey}, [Proof]}}. + +run(#coinbase_tx{account = AccountPubkey}, Trees0, Height) -> + AccountsTrees0 = aec_trees:accounts(Trees0), + {ok, Account0} = aec_accounts:get(AccountPubkey, AccountsTrees0), + + {ok, Account} = aec_accounts:earn(Account0, ?BLOCK_MINE_REWARD, Height), + + {ok, AccountsTrees} = aec_accounts:put(Account, AccountsTrees0), + Trees = aec_trees:with_accounts(Trees0, AccountsTrees), + {ok, Trees}. diff --git a/apps/aecore/src/txs/aec_tx.erl b/apps/aecore/src/txs/aec_tx.erl new file mode 100644 index 0000000000..6e50b8bbe0 --- /dev/null +++ b/apps/aecore/src/txs/aec_tx.erl @@ -0,0 +1,9 @@ +-module(aec_tx). + +-include("trees.hrl"). + +-callback new(Args :: map(), Trees :: trees()) -> + {ok, {Tx :: term(), Proofs :: list()}}. + +-callback run(Tx :: term(), Trees :: trees(), Height :: non_neg_integer()) -> + {ok, NewTrees :: trees()}. diff --git a/apps/aecore/test/pow_sha256_tests.erl b/apps/aecore/test/aec_pow_sha256_tests.erl similarity index 95% rename from apps/aecore/test/pow_sha256_tests.erl rename to apps/aecore/test/aec_pow_sha256_tests.erl index 25c09cca8b..8da95a6f11 100644 --- a/apps/aecore/test/pow_sha256_tests.erl +++ b/apps/aecore/test/aec_pow_sha256_tests.erl @@ -1,10 +1,10 @@ %%%============================================================================= %%% @copyright (C) 2017, Aeternity Anstalt %%% @doc -%%% Unit tests for the pow_sha256 module +%%% Unit tests for the aec_pow_sha256 module %%% @end %%%============================================================================= --module(pow_sha256_tests). +-module(aec_pow_sha256_tests). -include_lib("eunit/include/eunit.hrl"). @@ -14,7 +14,7 @@ -include_lib("eunit/include/eunit.hrl"). -include("sha256.hrl"). --define(TEST_MODULE, pow_sha256). +-define(TEST_MODULE, aec_pow_sha256). conversion_test_() -> {setup, diff --git a/apps/aecore/test/sha256_tests.erl b/apps/aecore/test/aec_sha256_tests.erl similarity index 68% rename from apps/aecore/test/sha256_tests.erl rename to apps/aecore/test/aec_sha256_tests.erl index 6a29e0fff5..dca09d0351 100644 --- a/apps/aecore/test/sha256_tests.erl +++ b/apps/aecore/test/aec_sha256_tests.erl @@ -1,27 +1,29 @@ %%%============================================================================= %%% @copyright (C) 2017, Aeternity Anstalt %%% @doc -%%% Unit tests for the sha256 module +%%% Unit tests for the aec_sha256 module %%% @end %%%============================================================================= --module(sha256_tests). +-module(aec_sha256_tests). -ifdef(TEST). -include_lib("eunit/include/eunit.hrl"). -include("sha256.hrl"). +-define(TEST_MODULE, aec_sha256). + all_test_() -> {setup, fun setup/0, fun teardown/1, [{"Hash a binary", fun() -> - ?assertEqual(?HASH_BYTES, size(sha256:hash(<<"hello there!">>))) + ?assertEqual(?HASH_BYTES, size(?TEST_MODULE:hash(<<"hello there!">>))) end}, {"Hash an erlang term", fun() -> - ?assertEqual(?HASH_BYTES, size(sha256:hash({a, b, c}))) + ?assertEqual(?HASH_BYTES, size(?TEST_MODULE:hash({a, b, c}))) end} ] }.