Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
add consensus_failure member roles (#218)
Browse files Browse the repository at this point in the history
* add consensus_failure member roles

Introduces `consensus_failure_member` and `consensus_failure_failed_member` as actor roles and uses them in `blockchain_txn_consensus_group_failure_v1` transactions as actors to distinguish from the normal `consensus_member` role.

A `backfill consensus_failure_members`  is provided to adjust the existing actor role entries, it:

* Fixes up the fields for the affected transactions
* Removes all actors associated with the affected transaction
* Inserts the new/fixed actors for the affected transactions
  • Loading branch information
madninja authored Aug 4, 2021
1 parent 5916e7f commit d186895
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 32 deletions.
5 changes: 5 additions & 0 deletions migrations/1628080839-consensus_failure_member.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- migrations/1628080839-consensus_failure_member.sql
-- :up

ALTER TYPE transaction_actor_role ADD VALUE IF NOT EXISTS 'consensus_failure_member';

4 changes: 4 additions & 0 deletions migrations/1628082883-consensus_failure_failed_member.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- migrations/1628082883-consensus_failure_failed_member.sql
-- :up

ALTER TYPE transaction_actor_role ADD VALUE IF NOT EXISTS 'consensus_failure_failed_member';
4 changes: 3 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
{telemetry, "0.4.1"},
{psql_migration, {git, "https://github.com/helium/psql-migration.git", {branch, "master"}}},
{clique, {git, "https://github.com/helium/clique.git", {branch, "develop"}}},
{blockchain, {git, "https://github.com/helium/blockchain-core.git", {tag, "2021.07.22.0"}}},
{blockchain,
{git, "https://github.com/helium/blockchain-core.git",
{ref, "c1de76cc43cb518021bfe8adccd48b62d24b6c1f"}}},
{envloader, {git, "https://github.com/nuex/envloader.git", {branch, "master"}}}
]}.

Expand Down
4 changes: 2 additions & 2 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{<<"base64url">>,{pkg,<<"base64url">>,<<"1.0.1">>},0},
{<<"blockchain">>,
{git,"https://github.com/helium/blockchain-core.git",
{ref,"75c9941165559ee32f31cc45f1441d368f77ce68"}},
{ref,"c1de76cc43cb518021bfe8adccd48b62d24b6c1f"}},
0},
{<<"certifi">>,{pkg,<<"certifi">>,<<"2.7.0">>},1},
{<<"clique">>,
Expand Down Expand Up @@ -67,7 +67,7 @@
{<<"lager">>,{pkg,<<"lager">>,<<"3.9.2">>},0},
{<<"libp2p">>,
{git,"https://github.com/helium/erlang-libp2p.git",
{ref,"fc1069010371c4a3571e61a20b4fe4285fce7432"}},
{ref,"166dcb375aec51816702208d8c790209f0a3645b"}},
1},
{<<"libp2p_crypto">>,{pkg,<<"libp2p_crypto">>,<<"1.4.0">>},2},
{<<"merkerl">>,
Expand Down
51 changes: 40 additions & 11 deletions src/be_cli_backfill.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ register_all_usage() ->
backfill_reward_gateways_usage(),
backfill_gateway_location_hex_usage(),
backfill_dc_burn_usage(),
backfill_gateway_payers_usage()
backfill_gateway_payers_usage(),
backfill_consensus_failure_members_usage()
]
).

Expand All @@ -46,7 +47,8 @@ register_all_cmds() ->
backfill_reward_gateways_cmd(),
backfill_gateway_location_hex_cmd(),
backfill_dc_burn_cmd(),
backfill_gateway_payers_cmd()
backfill_gateway_payers_cmd(),
backfill_consensus_failure_members_cmd()
]
).

Expand All @@ -59,15 +61,16 @@ backfill_usage() ->
["backfill"],
[
"backfill commands\n\n",
" backfill receipts_challenger - Backfill the challenger for receipts transactions.\n",
" backfill reversed_receipts_path - Backfill fix reversed poc receipts paths.\n",
" backfill gateway_names - Backfill names in gateway_inventory.\n"
" backfill oui_subnets - Backfill OUI inventory subnets.\n"
" backfill location_geometry - Backfill location postgis geometries.\n"
" backfill reward_gateways - Backfill reward gateways.\n"
" backfill gateway_location_hex - Backfill location hex in gateway_inventory.\n"
" backfill dc_burn - Backfill DC burn table.\n"
" backfill gateway_payers - Backfill payers in gateway_inventory.\n"
" backfill receipts_challenger - Backfill the challenger for receipts transactions.\n",
" backfill reversed_receipts_path - Backfill fix reversed poc receipts paths.\n",
" backfill gateway_names - Backfill names in gateway_inventory.\n"
" backfill oui_subnets - Backfill OUI inventory subnets.\n"
" backfill location_geometry - Backfill location postgis geometries.\n"
" backfill reward_gateways - Backfill reward gateways.\n"
" backfill gateway_location_hex - Backfill location hex in gateway_inventory.\n"
" backfill dc_burn - Backfill DC burn table.\n"
" backfill gateway_payers - Backfill payers in gateway_inventory.\n"
" backfill consensus_failure_members - Backfill consensus failure actors.\n"
]
].

Expand Down Expand Up @@ -410,3 +413,29 @@ backfill_gateway_payers_usage() ->
backfill_gateway_payers(_CmdBase, [], []) ->
Updated = be_db_backfill:gateway_payers(),
[clique_status:text(io_lib:format("Updated ~p", [Updated]))].

%%
%% backfill consensus_failure_members
%%

backfill_consensus_failure_members_cmd() ->
[
[
["backfill", "consensus_failure_members"],
[],
[],
fun backfill_consensus_failure_members/3
]
].

backfill_consensus_failure_members_usage() ->
[
["backfill", "consensus_failure_members"],
[
"backfill transaction actors table with failed consensus member roles.\n\n"
]
].

backfill_consensus_failure_members(_CmdBase, [], []) ->
Updated = be_db_backfill:consensus_failure_members(),
[clique_status:text(io_lib:format("Updated ~p", [Updated]))].
65 changes: 64 additions & 1 deletion src/be_db_backfill.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
gateway_location_hex/0,
dc_burn/2,
oracle_price_at/1,
gateway_payers/0
gateway_payers/0,
consensus_failure_members/0
]).

-define(INSERT_RECEIPTS_CHALLENGERS, [
Expand Down Expand Up @@ -329,3 +330,65 @@ gateway_payers() ->
[]
),
Updated.

%%
%% Fix in consensus_group_failure_v1 actors
%%

consensus_failure_members() ->
{ok, _, Blocks} = ?EQUERY(
[
"select block from transactions ",
"where type = 'consensus_group_failure_v1'"
],
[]
),
Chain = blockchain_worker:blockchain(),
Ledger = blockchain:ledger(Chain),
lists:foldl(
fun({Height}, UpdatedAcc) ->
{ok, Block} = blockchain:get_block(Height, Chain),
Txns = lists:filter(
fun(Txn) ->
blockchain_txn:type(Txn) == blockchain_txn_consensus_group_failure_v1
end,
blockchain_block_v1:transactions(Block)
),
?WITH_TRANSACTION(fun(Conn) ->
lists:foreach(
fun(Txn) ->
%% Update fields for each affected txn
TxnHash = ?BIN_TO_B64(blockchain_txn:hash(Txn)),
{ok, _} = ?EQUERY(
[
"update transactions ",
" set fields = $3 ",
"where hash = $1 and block = $2"
],
[
TxnHash,
Height,
be_txn:to_json(Txn, Ledger, Chain)
]
),
%% Remove old actors
?EQUERY(
[
"delete from transaction_actors ",
"where transaction_hash = $1 and block = $2"
],
[TxnHash, Height]
),
%% Re-insert actors
ActorQueries =
be_db_txn_actor:q_insert_transaction_actors(Height, Txn, []),
ok = ?BATCH_QUERY(Conn, ActorQueries)
end,
Txns
),
UpdatedAcc + length(Txns)
end)
end,
0,
Blocks
).
37 changes: 20 additions & 17 deletions src/be_db_txn_actor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
%% be_block_handler
-export([init/1, load_block/6]).
%% api
-export([to_actors/1]).
-export([to_actors/1, q_insert_transaction_actors/3]).

-define(S_INSERT_ACTOR, "insert_actor").

Expand Down Expand Up @@ -48,26 +48,29 @@ init(_) ->
{ok, #state{}}.

load_block(Conn, _Hash, Block, _Sync, _Ledger, State = #state{}) ->
Queries = q_insert_transaction_actors(Block, [], State),
Queries = q_insert_block_transaction_actors(Block, []),
ok = ?BATCH_QUERY(Conn, Queries),
{ok, State}.

q_insert_transaction_actors(Block, Query, #state{}) ->
q_insert_transaction_actors(Height, Txn, Acc) ->
TxnHash = ?BIN_TO_B64(blockchain_txn:hash(Txn)),
lists:foldl(
fun({Role, Key}, ActorAcc) ->
[
{?S_INSERT_ACTOR, [Height, ?BIN_TO_B58(Key), Role, TxnHash]}
| ActorAcc
]
end,
Acc,
to_actors(Txn)
).

q_insert_block_transaction_actors(Block, Query) ->
Height = blockchain_block_v1:height(Block),
Txns = blockchain_block_v1:transactions(Block),
lists:foldl(
fun(T, Acc) ->
TxnHash = ?BIN_TO_B64(blockchain_txn:hash(T)),
lists:foldl(
fun({Role, Key}, ActorAcc) ->
[
{?S_INSERT_ACTOR, [Height, ?BIN_TO_B58(Key), Role, TxnHash]}
| ActorAcc
]
end,
Acc,
to_actors(T)
)
fun(Txn, Acc) ->
q_insert_transaction_actors(Height, Txn, Acc)
end,
Query,
Txns
Expand Down Expand Up @@ -358,11 +361,11 @@ to_actors(blockchain_txn_validator_heartbeat_v1, T) ->
];
to_actors(blockchain_txn_consensus_group_failure_v1, T) ->
Members = [
{"consensus_member", M}
{"consensus_failure_member", M}
|| M <- blockchain_txn_consensus_group_failure_v1:members(T)
],
FailedMembers = [
{"consensus_member", M}
{"consensus_failure_failed_member", M}
|| M <- blockchain_txn_consensus_group_failure_v1:failed_members(T)
],
Members ++ FailedMembers.

0 comments on commit d186895

Please sign in to comment.