Skip to content

Commit 26bdad0

Browse files
committed
Merge branch 'master' into GH-88-rebase
# Conflicts: # apps/aecore/lib/aecore/miner/worker.ex
2 parents a4d5e00 + 870331c commit 26bdad0

File tree

12 files changed

+123
-56
lines changed

12 files changed

+123
-56
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ defmodule Aecore.Chain.Worker do
1818
genesis_block_map = %{genesis_block_hash => Block.genesis_block()}
1919
genesis_chain_state = ChainState.calculate_block_state(Block.genesis_block().txs)
2020
latest_block_chain_state = %{genesis_block_hash => genesis_chain_state}
21-
# latest_block_chain_state = genesis_chain_state
2221

2322
initial_state = {genesis_block_map, latest_block_chain_state}
2423
GenServer.start_link(__MODULE__, initial_state, name: __MODULE__)

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Aecore.Keys.Worker do
99

1010
@filename_pub "key.pub"
1111
@filename_priv "key"
12+
@pub_key_length 65
1213

1314
def start_link() do
1415
GenServer.start_link(
@@ -123,19 +124,23 @@ defmodule Aecore.Keys.Worker do
123124
0
124125
}
125126
end
126-
127127
def handle_call(
128128
{:verify, {term, signature, pub_key}},
129129
_from,
130130
%{algo: algo, digest: digest, curve: curve} = state
131131
) do
132-
result =
133-
:crypto.verify(algo, digest, :erlang.term_to_binary(term), signature, [
134-
pub_key,
135-
:crypto.ec_curve(curve)
136-
])
132+
case is_valid_pub_key(pub_key) do
133+
true ->
134+
result =
135+
:crypto.verify(algo, digest, :erlang.term_to_binary(term), signature, [
136+
pub_key,
137+
:crypto.ec_curve(curve)
138+
])
137139

138-
{:reply, result, state}
140+
{:reply, result, state}
141+
false ->
142+
{:reply, {:error, "Key length is not valid!"}, state}
143+
end
139144
end
140145

141146
def handle_call(
@@ -294,6 +299,11 @@ defmodule Aecore.Keys.Worker do
294299
pub
295300
end
296301

302+
defp is_valid_pub_key(pub_key_str) do
303+
pub_key_str
304+
|> byte_size() == @pub_key_length
305+
end
306+
297307
defp padding128(bin) do
298308
pad0 = 128 - :erlang.size(bin)
299309
pad1 = pad0 * 8

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule Aecore.Miner.Worker do
66
alias Aecore.Utils.Blockchain.Difficulty
77
alias Aecore.Structures.Header
88
alias Aecore.Structures.Block
9+
alias Aecore.Pow.Cuckoo
910
alias Aecore.Keys.Worker, as: Keys
1011
alias Aecore.Structures.TxData
1112
alias Aecore.Structures.SignedTx
@@ -119,7 +120,7 @@ defmodule Aecore.Miner.Worker do
119120
blocks = Chain.get_blocks(latest_block_hash, 2)
120121
Enum.at(blocks, 1)
121122
end
122-
123+
123124
BlockValidation.validate_block!(latest_block, previous_block, chain_state)
124125

125126
valid_txs = BlockValidation.filter_invalid_transactions_chainstate(ordered_txs_list, chain_state)
@@ -146,8 +147,8 @@ defmodule Aecore.Miner.Worker do
146147
Block.current_block_version()
147148
)
148149
Logger.debug("start nonce #{start_nonce}. Final nonce = #{start_nonce + @nonce_per_cycle}")
149-
case Aecore.Pow.Cuckoo.generate(%{unmined_header
150-
| nonce: start_nonce + @nonce_per_cycle}) do
150+
case Cuckoo.generate(%{unmined_header
151+
| nonce: start_nonce + @nonce_per_cycle}) do
151152
{:ok, mined_header} ->
152153
block = %Block{header: mined_header, txs: valid_txs}
153154
Chain.add_block(block)

apps/aecore/lib/aecore/txs/pool/worker.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ defmodule Aecore.Txs.Pool.Worker do
4545
updated_pool = Map.put_new(tx_pool, :crypto.hash(:sha256, :erlang.term_to_binary(tx)), tx)
4646
case tx_pool == updated_pool do
4747
true -> Logger.info(" This transaction has already been added")
48-
false -> Aecore.Peers.Worker.broadcast_tx(tx)
48+
false -> Peers.broadcast_tx(tx)
4949
end
5050
{:reply, :ok, updated_pool}
5151
else

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Aecore.Utils.Blockchain.BlockValidation do
22

33
alias Aecore.Keys.Worker, as: KeyManager
4-
alias Aecore.Pow.Hashcash
4+
alias Aecore.Pow.Cuckoo
55
alias Aecore.Miner.Worker, as: Miner
66
alias Aecore.Structures.Block
77
alias Aecore.Structures.Header
@@ -15,7 +15,7 @@ defmodule Aecore.Utils.Blockchain.BlockValidation do
1515
chain_state_hash = ChainState.calculate_chain_state_hash(chain_state)
1616
is_valid_chain_state = ChainState.validate_chain_state(chain_state)
1717

18-
is_difficulty_target_met = Aecore.Pow.Cuckoo.verify(new_block.header)
18+
is_difficulty_target_met = Cuckoo.verify(new_block.header)
1919
coinbase_transactions_sum = sum_coinbase_transactions(new_block)
2020

2121
cond do

apps/aecore/lib/aecore/utils/serialization.ex

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,36 @@ defmodule Aecore.Utils.Serialization do
55

66
alias Aecore.Structures.Block
77
alias Aecore.Structures.SignedTx
8-
alias Aecore.Structures.TxData
98

109
@spec block(%Block{}, :serialize | :deserialize) :: %Block{}
1110
def block(block, direction) do
1211
new_header = %{block.header |
1312
chain_state_hash: hex_binary(block.header.chain_state_hash, direction),
1413
prev_hash: hex_binary(block.header.prev_hash, direction),
1514
txs_hash: hex_binary(block.header.txs_hash, direction)}
16-
new_txs = for tx <- block.txs do
17-
from_acc = if(tx.data.from_acc != nil) do
18-
hex_binary(tx.data_from_acc, direction)
19-
else
20-
nil
21-
end
22-
new_data = %{tx.data |
23-
from_acc: from_acc,
24-
to_acc: hex_binary(tx.data.to_acc, direction)}
25-
new_signature = if(tx.signature != nil) do
26-
hex_binary(tx.signature, direction)
27-
else
28-
nil
29-
end
30-
%SignedTx{data: new_data, signature: new_signature}
31-
end
15+
new_txs = Enum.map(block.txs, fn(tx) -> tx(tx,direction) end)
3216
%{block | header: new_header, txs: new_txs}
3317
end
3418

35-
3619
@spec tx(map(), :serialize | :deserialize) :: map() | {:error, term()}
3720
def tx(tx, direction) do
38-
new_data = %{tx.data |
39-
from_acc: hex_binary(tx.data.from_acc, direction),
40-
to_acc: hex_binary(tx.data.to_acc, direction)}
41-
new_signature = hex_binary(tx.signature, direction)
42-
%SignedTx{data: TxData.new(new_data), signature: new_signature}
21+
new_data = %{tx.data |
22+
from_acc: hex_binary(tx.data.from_acc, direction),
23+
to_acc: hex_binary(tx.data.to_acc, direction)}
24+
new_signature = hex_binary(tx.signature, direction)
25+
%SignedTx{data: new_data, signature: new_signature}
4326
end
4427

4528
def hex_binary(data, direction) do
46-
case(direction) do
47-
:serialize ->
48-
Base.encode16(data)
49-
:deserialize ->
50-
Base.decode16!(data)
29+
if(data != nil) do
30+
case(direction) do
31+
:serialize ->
32+
Base.encode16(data)
33+
:deserialize ->
34+
Base.decode16!(data)
35+
end
36+
else
37+
nil
5138
end
5239
end
5340
end

apps/aecore/test/aecore_keys_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,18 @@ defmodule AecoreKeysTest do
2222
{:ok, to_account} = Keys.pubkey()
2323
assert {:ok, _} = Keys.sign_tx(to_account, 5, Map.get(Chain.chain_state(), to_account, %{nonce: 0}).nonce + 1)
2424
end
25+
26+
test "check pubkey length" do
27+
pub_key_str = "041A470AE9831B61D9951A10D49663419CE087DF1BD7DB06578971767F032D389CB283AD4DD4E3532F3A5F3C89B006092CB6CECE39CAC3B06C2CB6DF8B51C73675"
28+
pub_key_bin = pub_key_str |> Base.decode16!()
29+
assert false == Keys.verify("", "", pub_key_bin)
30+
end
31+
32+
test "wrong key verification" do
33+
pub_key_str = "041A470AE9831B61D9951A10D49663419CE087DF1BD7DB06578971767F032D389CB283AD4DD4E3"
34+
pub_key_bin = pub_key_str |> Base.decode16!()
35+
36+
assert {:error, _} = Keys.verify("", "", pub_key_bin)
37+
end
38+
2539
end

apps/aehttpclient/lib/client.ex

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule Aehttpclient.Client do
44
"""
55

66
alias Aecore.Structures.Block
7+
alias Aecore.Structures.SignedTx
8+
alias Aecore.Structures.TxData
79
alias Aecore.Peers.Worker, as: Peers
810

911
@spec get_info(term()) :: {:ok, map()} | :error
@@ -27,10 +29,16 @@ defmodule Aehttpclient.Client do
2729
Enum.each(peers, fn{peer, _} -> Peers.add_peer(peer) end)
2830
end
2931

32+
@spec get_account_balance({term(), term()}) :: {:ok, map()} | :error
3033
def get_account_balance({uri,acc}) do
3134
get(uri <> "/balance/#{acc}", :balance)
3235
end
3336

37+
@spec get_account_txs({term(), term()}) :: {:ok, list()} | :error
38+
def get_account_txs({uri,acc}) do
39+
get(uri <> "/tx_pool/#{acc}", :acc_txs)
40+
end
41+
3442
def get(uri, identifier) do
3543
case(HTTPoison.get(uri)) do
3644
{:ok, %{body: body, status_code: 200}} ->
@@ -41,10 +49,12 @@ defmodule Aehttpclient.Client do
4149
:info ->
4250
response = Poison.decode!(body, keys: :atoms!)
4351
{:ok, response}
44-
:peers ->
45-
standard_response(body)
46-
:balance ->
47-
standard_response(body)
52+
:acc_txs ->
53+
response = Poison.decode!(body,
54+
as: [%SignedTx{data: %TxData{}}], keys: :atoms!)
55+
{:ok, response}
56+
_ ->
57+
json_response(body)
4858
end
4959
{:ok, %HTTPoison.Response{status_code: 404}} ->
5060
:error
@@ -62,7 +72,7 @@ defmodule Aehttpclient.Client do
6272
[{"Content-Type", "application/json"}]
6373
end
6474

65-
def standard_response(body) do
75+
def json_response(body) do
6676
response = Poison.decode!(body)
6777
{:ok,response}
6878
end

apps/aehttpclient/test/aehttpclient_test.exs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,31 @@ defmodule AehttpclientTest do
22

33
use ExUnit.Case
44

5+
alias Aecore.Structures.Block
6+
alias Aecore.Chain.Worker, as: Chain
7+
alias Aecore.Txs.Pool.Worker, as: Pool
8+
alias Aecore.Keys.Worker, as: Keys
9+
alias Aehttpclient.Client
10+
11+
test "Client functions" do
12+
account = Keys.pubkey() |> elem(1) |> Base.encode16()
13+
add_txs_to_pool()
14+
genesis_block = Block.genesis_block()
15+
assert {:ok, _} = Client.get_info("localhost:4000")
16+
assert {:ok, genesis_block} = Client.get_block({"localhost:4000",
17+
"C061E48A6F7FB2634E0C012B168D41F4773A38BD9E5EA28E5BE7D04186127BA0"})
18+
assert {:ok, _} = Client.get_peers("localhost:4000")
19+
assert Enum.count(Client.get_account_txs({"localhost:4000", account})
20+
|> elem(1)) == 2
21+
end
22+
23+
def add_txs_to_pool() do
24+
{:ok, to_account} = Keys.pubkey()
25+
{:ok, tx1} = Keys.sign_tx(to_account, 5,
26+
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
27+
{:ok, tx2} = Keys.sign_tx(to_account, 5,
28+
Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
29+
Pool.add_transaction(tx1)
30+
Pool.add_transaction(tx2)
31+
end
532
end
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
defmodule Aehttpserver.NewTxController do
22
use Aehttpserver.Web, :controller
33

4-
alias Aecore.Structures.Block
5-
alias Aecore.Structures.TxData
6-
alias Aecore.Structures.SignedTx
7-
alias Aecore.Chain.Worker, as: Chain
8-
alias Aecore.Utils.Blockchain.BlockValidation
9-
alias Aecore.Utils.Serialization
10-
alias Aecore.Keys.Worker, as: Keys
11-
4+
alias Aecore.Utils.Serialization
5+
alias Aecore.Txs.Pool.Worker, as: Pool
126

137
def new_tx(conn, _params) do
148
conn.body_params["_json"]
159
|> Poison.decode!([keys: :atoms])
1610
|> Serialization.tx(:deserialize)
17-
|> Aecore.Txs.Pool.Worker.add_transaction()
11+
|> Pool.add_transaction()
1812
json conn, %{:status => :new_tx_added}
1913
end
2014
end

0 commit comments

Comments
 (0)