Skip to content

Commit 0445686

Browse files
Added exception handling for miner and add_block and small refactoring
1 parent 26bdad0 commit 0445686

File tree

2 files changed

+84
-81
lines changed

2 files changed

+84
-81
lines changed

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

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,6 @@ defmodule Aecore.Chain.Worker do
7373
{:reply, latest_block_chain_state, state}
7474
end
7575

76-
def handle_call(:get_prior_blocks_for_validity_check, _from, state) do
77-
chain = elem(state, 0)
78-
79-
if length(chain) == 1 do
80-
[lb | _] = chain
81-
{:reply, {lb, nil}, state}
82-
else
83-
[lb, prev | _] = chain
84-
{:reply, {lb, prev}, state}
85-
end
86-
end
87-
8876
def handle_call({:get_block, block_hash}, _from, state) do
8977
{block_map, _} = state
9078
block = block_map[block_hash]
@@ -97,7 +85,8 @@ defmodule Aecore.Chain.Worker do
9785
end
9886

9987
def handle_call({:get_block_by_hex_hash, hash}, _from, state) do
100-
case(Enum.find(elem(state, 0), fn{block_hash, _block} ->
88+
{chain, _} = state
89+
case(Enum.find(chain, fn{block_hash, _block} ->
10190
block_hash |> Base.encode16() == hash end)) do
10291
{_, block} ->
10392
{:reply, block, state}
@@ -112,41 +101,49 @@ defmodule Aecore.Chain.Worker do
112101
new_block_state = ChainState.calculate_block_state(block.txs)
113102
new_chain_state = ChainState.calculate_chain_state(new_block_state, prev_block_chain_state)
114103

115-
BlockValidation.validate_block!(block, chain[block.header.prev_hash], new_chain_state)
104+
try do
105+
BlockValidation.validate_block!(block, chain[block.header.prev_hash], new_chain_state)
116106

117-
Enum.each(block.txs, fn(tx) -> Pool.remove_transaction(tx) end)
107+
Enum.each(block.txs, fn(tx) -> Pool.remove_transaction(tx) end)
118108

119-
{block_map, latest_block_chain_state} = state
120-
block_hash = BlockValidation.block_header_hash(block.header)
121-
updated_block_map = Map.put(block_map, block_hash, block)
122-
has_prev_block = Map.has_key?(latest_block_chain_state, block.header.prev_hash)
109+
{block_map, latest_block_chain_state} = state
110+
block_hash = BlockValidation.block_header_hash(block.header)
111+
updated_block_map = Map.put(block_map, block_hash, block)
112+
has_prev_block = Map.has_key?(latest_block_chain_state, block.header.prev_hash)
123113

124-
{deleted_latest_chain_state, prev_chain_state} = case has_prev_block do
125-
true ->
126-
prev_chain_state = Map.get(latest_block_chain_state, block.header.prev_hash)
127-
{Map.delete(latest_block_chain_state, block.header.prev_hash), prev_chain_state}
128-
false ->
129-
{latest_block_chain_state, %{}}
130-
end
131-
132-
new_block_state = ChainState.calculate_block_state(block.txs)
133-
new_chain_state = ChainState.calculate_chain_state(new_block_state, prev_chain_state)
114+
{deleted_latest_chain_state, prev_chain_state} = case has_prev_block do
115+
true ->
116+
prev_chain_state = Map.get(latest_block_chain_state, block.header.prev_hash)
117+
{Map.delete(latest_block_chain_state, block.header.prev_hash), prev_chain_state}
118+
false ->
119+
{latest_block_chain_state, %{}}
120+
end
134121

135-
updated_latest_block_chainstate = Map.put(deleted_latest_chain_state, block_hash, new_chain_state)
122+
new_block_state = ChainState.calculate_block_state(block.txs)
123+
new_chain_state = ChainState.calculate_chain_state(new_block_state, prev_chain_state)
136124

137-
total_tokens = ChainState.calculate_total_tokens(new_chain_state)
125+
updated_latest_block_chainstate = Map.put(deleted_latest_chain_state, block_hash, new_chain_state)
138126

139-
Logger.info(
140-
fn ->
141-
"Added block ##{block.header.height} with hash #{
142-
block.header
143-
|> BlockValidation.block_header_hash()
144-
|> Base.encode16()
145-
}, total tokens: #{total_tokens}"
146-
end
147-
)
127+
total_tokens = ChainState.calculate_total_tokens(new_chain_state)
148128

149-
{:reply, :ok, {updated_block_map, updated_latest_block_chainstate}}
129+
Logger.info(
130+
fn ->
131+
"Added block ##{block.header.height} with hash #{
132+
block.header
133+
|> BlockValidation.block_header_hash()
134+
|> Base.encode16()
135+
}, total tokens: #{total_tokens}"
136+
end
137+
)
138+
139+
{:reply, :ok, {updated_block_map, updated_latest_block_chainstate}}
140+
catch
141+
{:error, message} ->
142+
Logger.error(fn ->
143+
"Failed to add block: #{message}"
144+
end)
145+
{:reply, :error, state}
146+
end
150147
end
151148

152149
def handle_call({:chain_state, latest_block_hash}, _from, state) do

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

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule Aecore.Miner.Worker do
4141

4242
## Idle ##
4343
def idle({:call, from}, :start, _data) do
44-
IO.puts("Mining resuming by user")
44+
IO.puts("Mining resumed by user")
4545
GenStateMachine.cast(__MODULE__, :mine)
4646
{:next_state, :running, 0, [{:reply, from, :ok}]}
4747
end
@@ -78,7 +78,7 @@ defmodule Aecore.Miner.Worker do
7878
end
7979

8080
def running({:call, from}, :suspend, data) do
81-
IO.puts("Mined stop by user")
81+
IO.puts("Mining stopped by user")
8282
{:next_state, :idle, data, [{:reply, from, :ok}]}
8383
end
8484

@@ -120,45 +120,51 @@ defmodule Aecore.Miner.Worker do
120120
blocks = Chain.get_blocks(latest_block_hash, 2)
121121
Enum.at(blocks, 1)
122122
end
123-
124-
BlockValidation.validate_block!(latest_block, previous_block, chain_state)
125-
126-
valid_txs = BlockValidation.filter_invalid_transactions_chainstate(ordered_txs_list, chain_state)
127-
{_, pubkey} = Keys.pubkey()
128-
valid_txs = [get_coinbase_transaction(pubkey) | valid_txs]
129-
root_hash = BlockValidation.calculate_root_hash(valid_txs)
130-
131-
new_block_state = ChainState.calculate_block_state(valid_txs)
132-
new_chain_state = ChainState.calculate_chain_state(new_block_state, chain_state)
133-
chain_state_hash = ChainState.calculate_chain_state_hash(new_chain_state)
134-
135-
latest_block_hash = BlockValidation.block_header_hash(latest_block.header)
136-
137-
difficulty = Difficulty.calculate_next_difficulty(blocks_for_difficulty_calculation)
138-
139-
unmined_header =
140-
Header.create(
141-
latest_block.header.height + 1,
142-
latest_block_hash,
143-
root_hash,
144-
chain_state_hash,
145-
difficulty,
146-
0, #start from nonce 0, will be incremented in mining
147-
Block.current_block_version()
148-
)
149-
Logger.debug("start nonce #{start_nonce}. Final nonce = #{start_nonce + @nonce_per_cycle}")
150-
case Cuckoo.generate(%{unmined_header
151-
| nonce: start_nonce + @nonce_per_cycle}) do
152-
{:ok, mined_header} ->
153-
block = %Block{header: mined_header, txs: valid_txs}
154-
Chain.add_block(block)
155-
Logger.info(fn ->
156-
"Mined block ##{block.header.height}, difficulty target #{block.header.difficulty_target}, nonce #{block.header.nonce}"
157-
end)
158-
{:block_found, 0}
159-
123+
try do
124+
BlockValidation.validate_block!(latest_block, previous_block, chain_state)
125+
126+
valid_txs = BlockValidation.filter_invalid_transactions_chainstate(ordered_txs_list, chain_state)
127+
{_, pubkey} = Keys.pubkey()
128+
valid_txs = [get_coinbase_transaction(pubkey) | valid_txs]
129+
root_hash = BlockValidation.calculate_root_hash(valid_txs)
130+
131+
new_block_state = ChainState.calculate_block_state(valid_txs)
132+
new_chain_state = ChainState.calculate_chain_state(new_block_state, chain_state)
133+
chain_state_hash = ChainState.calculate_chain_state_hash(new_chain_state)
134+
135+
latest_block_hash = BlockValidation.block_header_hash(latest_block.header)
136+
137+
difficulty = Difficulty.calculate_next_difficulty(blocks_for_difficulty_calculation)
138+
139+
unmined_header =
140+
Header.create(
141+
latest_block.header.height + 1,
142+
latest_block_hash,
143+
root_hash,
144+
chain_state_hash,
145+
difficulty,
146+
0, #start from nonce 0, will be incremented in mining
147+
Block.current_block_version()
148+
)
149+
Logger.debug("start nonce #{start_nonce}. Final nonce = #{start_nonce + @nonce_per_cycle}")
150+
case Cuckoo.generate(%{unmined_header
151+
| nonce: start_nonce + @nonce_per_cycle}) do
152+
{:ok, mined_header} ->
153+
block = %Block{header: mined_header, txs: valid_txs}
154+
Logger.info(fn ->
155+
"Mined block ##{block.header.height}, difficulty target #{block.header.difficulty_target}, nonce #{block.header.nonce}"
156+
end)
157+
Chain.add_block(block)
158+
{:block_found, 0}
159+
160+
{:error, _message} ->
161+
{:no_block_found, start_nonce + @nonce_per_cycle}
162+
end
163+
catch
160164
{:error, _message} ->
161-
{:no_block_found, start_nonce + @nonce_per_cycle}
165+
Logger.error(fn ->
166+
"Failed to mine block"
167+
end)
162168
end
163169
end
164170
end

0 commit comments

Comments
 (0)