-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
%%%----------------------------------------------------------------------------- | ||
%%% @copyright (C) 2024, Aeternity Anstalt | ||
%%% @doc | ||
%%% Module for various Hypoerchain-related encoding tasks | ||
%%% @end | ||
%%%----------------------------------------------------------------------------- | ||
|
||
-module(aeser_hc). | ||
|
||
-export([encode_parent_pin_payload/1, | ||
decode_parent_pin_payload/1, | ||
encode_child_pin_payload/1, | ||
decode_child_pin_payload/1]). | ||
|
||
%%%============================================================================= | ||
%%% Global Definitions | ||
%%%============================================================================= | ||
|
||
%%%============================================================================= | ||
%%% Pinning | ||
%%%============================================================================= | ||
|
||
%%============================================================================= | ||
%% Encode an Epoch info map to a <80 bytes binary to use in payload when pinning | ||
%% Hyperchain state to the parent chain | ||
%% ============================================================================= | ||
-spec encode_parent_pin_payload(#{epoch => integer(), height => integer(), block_hash => binary()}) -> binary(). | ||
encode_parent_pin_payload(#{epoch := Epoch, height := Height, block_hash := BlockHash}) -> | ||
EpochHex = list_to_binary(erlang:integer_to_list(Epoch, 16)), | ||
HeightHex = list_to_binary(erlang:integer_to_list(Height, 16)), | ||
EncBlockHash = aeser_api_encoder:encode(key_block_hash, BlockHash), | ||
<<EpochHex/binary, ":", HeightHex/binary, " ", EncBlockHash/binary>>. | ||
|
||
%%============================================================================= | ||
%% Decode a bionary payload to an Epoch info map when fetching pinning info | ||
%% from the parent chain | ||
%% ============================================================================= | ||
-spec decode_parent_pin_payload(binary()) -> #{epoch => integer(), height => integer(), block_hash => binary()}. | ||
decode_parent_pin_payload(Binary) -> | ||
try | ||
[HexEpoch, HexHeight, EncBlockHash] = binary:split(Binary, [<<":">>, <<" ">>], [global]), | ||
Epoch = erlang:list_to_integer(binary_to_list(HexEpoch), 16), | ||
Height = erlang:list_to_integer(binary_to_list(HexHeight), 16), | ||
{ok, BlockHash} = aeser_api_encoder:safe_decode(key_block_hash, EncBlockHash), | ||
{ok, #{epoch => Epoch, height => Height, block_hash => BlockHash}} | ||
catch | ||
_ -> {error, {bad_parent_pin_payload, Binary}} | ||
end. | ||
|
||
%%============================================================================= | ||
%% Encode the transaction hash returned when pinning to the parent chain to be | ||
%% easily findable when notarized (in a spend tx to the leader of the last | ||
%% epoch block) to the hyper chain. | ||
%%============================================================================= | ||
encode_child_pin_payload(TxHash) -> | ||
<<"pin", TxHash/binary>>. | ||
|
||
%%============================================================================= | ||
%% Decode a pinning transacation notarization payload | ||
%% ============================================================================= | ||
decode_child_pin_payload(<<"pin", TxHash/binary>>) -> | ||
{ok, TxHash}; | ||
decode_child_pin_payload(BadTxHash) -> | ||
{error, {bad_child_pin_payload, BadTxHash}}. |