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

Commit

Permalink
Test the lineage trace
Browse files Browse the repository at this point in the history
  • Loading branch information
xandkar committed May 14, 2022
1 parent 1c0344e commit a3021f3
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions src/blockchain.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1466,19 +1466,8 @@ build(Height, Blockchain, N, Acc) ->
blockchain(),
rocksdb:cf_handle()
) ->
[H] when H :: blockchain_block:hash().
[H, ...] when H :: blockchain_block:hash().
build_hash_chain(StopHash, StartBlock, #blockchain{db=DB}, CF) ->
StartHash = blockchain_block:hash_block(StartBlock),
build_hash_chain_in_parallel(DB, CF, StopHash, StartHash).

-spec build_hash_chain_in_parallel(
rocksdb:db_handle(),
rocksdb:cf_handle(),
H,
H
) ->
[H] when H :: blockchain_block:hash().
build_hash_chain_in_parallel(DB, CF, Oldest, Youngest) ->
Parents =
maps:from_list(
data_stream:pmap_to_bag(
Expand All @@ -1490,11 +1479,16 @@ build_hash_chain_in_parallel(DB, CF, Oldest, Youngest) ->
end
)
),
(fun TraceBack ([Child | _]=Lineage) ->
StartHash = blockchain_block:hash_block(StartBlock),
trace_lineage(Parents, StopHash, StartHash).

-spec trace_lineage(#{A => A}, A, A) -> [A, ...].
trace_lineage(Parents, Oldest, Youngest) ->
(fun Trace ([Child | _]=Lineage) ->
case maps:find(Child, Parents) of
error -> Lineage;
{ok, Oldest} -> Lineage;
{ok, Parent} -> TraceBack([Parent | Lineage])
{ok, Parent} -> Trace([Parent | Lineage])
end
end)([Youngest]).

Expand Down Expand Up @@ -3403,4 +3397,32 @@ block_info_upgrade_test() ->
V2BlockInfo = upgrade_block_info(V1BlockInfo, Block, Chain),
?assertMatch(ExpV2BlockInfo, V2BlockInfo).

trace_lineage_test_() ->
[
?_assertEqual(
[middle, youngest],
trace_lineage(
#{
youngest => middle,
middle => oldest
},
oldest,
youngest
)
),
?_assertEqual(
[b, c, d, e],
trace_lineage(
#{
e => d,
d => c,
c => b,
b => a
},
a,
e
)
)
].

-endif.

0 comments on commit a3021f3

Please sign in to comment.