Skip to content

Commit

Permalink
Merge remote-tracking branches 'origin/gh-42-extra-refresh-rebar-lock…
Browse files Browse the repository at this point in the history
…' and 'origin/gh-3-coinbase-tx' into gh-29-merkle-tree-wip
  • Loading branch information
Luca Favatella committed Sep 1, 2017
2 parents 484df06 + 14ffcdc commit 3309353
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 13 deletions.
1 change: 1 addition & 0 deletions apps/aecore/include/common.hrl
@@ -0,0 +1 @@
-type(pubkey() :: binary()).
11 changes: 10 additions & 1 deletion 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()}).
2 changes: 1 addition & 1 deletion apps/aecore/include/txs.hrl
@@ -1,4 +1,4 @@
-type(pubkey() :: binary()).
-include("common.hrl").

-record(coinbase_tx, {
account = <<>> :: pubkey(),
Expand Down
32 changes: 32 additions & 0 deletions 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}}.
Expand Up @@ -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,
Expand Down Expand Up @@ -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/binary, Difficulty:16, Nonce:?HASH_BITS>>),
Hash1 = aec_sha256:hash(Data),
Hash2 = aec_sha256:hash(<<Hash1/binary, Difficulty:16, Nonce:?HASH_BITS>>),
case binary_to_scientific(Hash2) of
HD when HD < Difficulty ->
%% Hash satisfies condition: return nonce
Expand Down
Expand Up @@ -5,7 +5,7 @@
%%% @end
%%%=============================================================================

-module(sha256).
-module(aec_sha256).

-export([hash/1]).

Expand Down
13 changes: 13 additions & 0 deletions 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}.
26 changes: 26 additions & 0 deletions 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}.
9 changes: 9 additions & 0 deletions 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()}.
@@ -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").

Expand All @@ -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,
Expand Down
@@ -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}
]
}.
Expand Down

0 comments on commit 3309353

Please sign in to comment.