Skip to content

Commit

Permalink
remove signature_type, replace reward_key/owner with identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
samantehrani committed Jan 2, 2025
1 parent fae59da commit 5b1a7c1
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 153 deletions.
13 changes: 4 additions & 9 deletions apps/arweave/include/ar.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
-define(EDDSA_SIGN_ALG, eddsa).
-define(EDDSA_TYPE_BYTE, <<3>>).

%% The default key type used by transactions that do not specify a signature type.
%% The default key type
-define(DEFAULT_KEY_TYPE, {?RSA_SIGN_ALG, 65537}).

%% The difficulty a new weave is started with.
Expand Down Expand Up @@ -572,8 +572,7 @@
recall_byte2,
%% The block signature.
signature = <<>>,
%% {KeyType, PubKey} - the public key the block was signed with.
%% The only supported KeyType is currently {rsa, 65537}.
%% Identifier - The identifier of the key the block was signed with.
reward_key,
%% The estimated number of Winstons it costs the network to store one gibibyte
%% for one minute.
Expand Down Expand Up @@ -696,7 +695,7 @@
%% the same wallet or the identifier of one of the
%% last ?MAX_TX_ANCHOR_DEPTH blocks.
last_tx = <<>>,
%% The public key the transaction is signed with.
%% Identifier - The identifier of the key the block was signed with.
owner = <<>>,
%% A list of arbitrary key-value pairs. Keys and values are binaries.
tags = [],
Expand Down Expand Up @@ -736,11 +735,7 @@
%% blocks preceding the redenomination block.
%%
%% Transaction denomination code must not exceed the block's denomination code.
denomination = 0,

%% The type of signature this transaction was signed with. A system field,
%% not used by the protocol yet.
signature_type = ?DEFAULT_KEY_TYPE
denomination = 0
}).

%% A macro to convert AR into Winstons.
Expand Down
13 changes: 6 additions & 7 deletions apps/arweave/src/ar_block.erl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ generate_signed_hash(#block{ previous_block = PrevH, timestamp = TS,
weave_size = WeaveSize, tx_root = TXRoot, wallet_list = WalletList,
hash_list_merkle = HashListMerkle, reward_pool = RewardPool,
packing_2_5_threshold = Packing_2_5_Threshold, reward_addr = Addr,
reward_key = RewardKey, strict_data_split_threshold = StrictChunkThreshold,
reward_key = Identifier, strict_data_split_threshold = StrictChunkThreshold,
usd_to_ar_rate = {RateDividend, RateDivisor},
scheduled_usd_to_ar_rate = {ScheduledRateDividend, ScheduledRateDivisor},
tags = Tags, txs = TXs,
Expand Down Expand Up @@ -330,7 +330,6 @@ generate_signed_hash(#block{ previous_block = PrevH, timestamp = TS,
%% The only block where reward_address may be unclaimed
%% is the genesis block of a new weave.
Addr2 = case Addr of unclaimed -> <<>>; _ -> Addr end,
RewardKey2 = case RewardKey of undefined -> undefined; {_Type, Pub} -> Pub end,
#nonce_limiter_info{ output = Output, global_step_number = N, seed = Seed,
next_seed = NextSeed, partition_upper_bound = PartitionUpperBound,
next_partition_upper_bound = NextPartitionUpperBound,
Expand Down Expand Up @@ -391,7 +390,7 @@ generate_signed_hash(#block{ previous_block = PrevH, timestamp = TS,
(encode_bin_list([GetTXID(TX) || TX <- TXs], 16, 8))/binary,
(encode_int(Reward, 8))/binary,
(encode_int(RecallByte, 16))/binary, (encode_bin(HashPreimage, 8))/binary,
(encode_int(RecallByte2, 16))/binary, (encode_bin(RewardKey2, 16))/binary,
(encode_int(RecallByte2, 16))/binary, (encode_bin(Identifier, 16))/binary,
(encode_int(PartitionNumber, 8))/binary, Output:32/binary, N:64,
Seed:48/binary, NextSeed:48/binary, PartitionUpperBound:256,
NextPartitionUpperBound:256, (encode_bin(PrevOutput, 8))/binary,
Expand Down Expand Up @@ -428,15 +427,15 @@ indep_hash(BDS, B) ->

%% @doc Verify the block signature.
verify_signature(BlockPreimage, PrevCDiff,
#block{ signature = Signature, reward_key = {?DEFAULT_KEY_TYPE, Pub} = RewardKey,
#block{ signature = Signature, reward_key = Identifier,
reward_addr = RewardAddr, previous_solution_hash = PrevSolutionH,
cumulative_diff = CDiff })
when byte_size(Signature) == 512, byte_size(Pub) == 512 ->
when byte_size(Signature) == 512 ->
SignaturePreimage = << (ar_serialize:encode_int(CDiff, 16))/binary,
(ar_serialize:encode_int(PrevCDiff, 16))/binary, PrevSolutionH/binary,
BlockPreimage/binary >>,
ar_wallet:to_address(RewardKey) == RewardAddr andalso
ar_wallet:verify(RewardKey, SignaturePreimage, Signature);
ar_wallet:to_address(Identifier) == RewardAddr andalso
ar_wallet:verify(Identifier, SignaturePreimage, Signature);
verify_signature(_BlockPreimage, _PrevCDiff, _B) ->
false.

Expand Down
12 changes: 6 additions & 6 deletions apps/arweave/src/ar_block_pre_validator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ handle_cast(pre_validate, #state{ pqueue = Q, size = Size, ip_timestamps = IPTim
false ->
{{_, {B, PrevB, SolutionResigned, Peer}},
Q2} = gb_sets:take_largest(Q),
BlockSize = byte_size(term_to_binary(B)),
BlockSize = byte_size(term_to_binary(B)),
Size2 = Size - BlockSize,
case ar_ignore_registry:permanent_member(B#block.indep_hash) of
true ->
Expand Down Expand Up @@ -430,10 +430,10 @@ pre_validate_existing_solution_hash(B, PrevB, Peer) ->
end.

may_be_report_double_signing(B, B2) ->
#block{ reward_key = {_, Key}, signature = Signature1, cumulative_diff = CDiff1,
#block{ reward_key = Identifier, signature = Signature1, cumulative_diff = CDiff1,
previous_solution_hash = PreviousSolutionH1,
previous_cumulative_diff = PrevCDiff } = B,
#block{ reward_key = {_, Key}, signature = Signature2, cumulative_diff = CDiff2,
#block{ reward_key = Identifier, signature = Signature2, cumulative_diff = CDiff2,
previous_cumulative_diff = PrevCDiff2,
previous_solution_hash = PreviousSolutionH2 } = B2,
case CDiff1 == CDiff2 orelse (CDiff1 > PrevCDiff2 andalso CDiff2 > PrevCDiff) of
Expand All @@ -442,10 +442,10 @@ may_be_report_double_signing(B, B2) ->
(ar_block:generate_signed_hash(B))/binary >>,
Preimage2 = << PreviousSolutionH2/binary,
(ar_block:generate_signed_hash(B2))/binary >>,
Proof = {Key, Signature1, CDiff1, PrevCDiff, Preimage1, Signature2, CDiff2,
Proof = {Identifier, Signature1, CDiff1, PrevCDiff, Preimage1, Signature2, CDiff2,
PrevCDiff2, Preimage2},
?LOG_INFO([{event, report_double_signing},
{key, ar_util:encode(Key)},
{key, ar_util:encode(Identifier)},
{block1, ar_util:encode(B#block.indep_hash)},
{block2, ar_util:encode(B2#block.indep_hash)},
{height1, B#block.height}, {height2, B2#block.height}]),
Expand Down Expand Up @@ -767,7 +767,7 @@ pre_validate_nonce_limiter(B, PrevB, Peer) ->

accept_block(B, Peer, Gossip) ->
ar_ignore_registry:add(B#block.indep_hash),
ar_events:send(block, {new, B,
ar_events:send(block, {new, B,
#{ source => {peer, Peer}, gossip => Gossip }}),
?LOG_INFO([{event, accepted_block}, {height, B#block.height},
{indep_hash, ar_util:encode(B#block.indep_hash)}]),
Expand Down
18 changes: 9 additions & 9 deletions apps/arweave/src/ar_mempool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-include_lib("arweave/include/ar.hrl").

-export([reset/0, load_from_disk/0, add_tx/2, drop_txs/1, drop_txs/3,
get_map/0, get_all_txids/0, take_chunk/2, get_tx/1, has_tx/1,
get_map/0, get_all_txids/0, take_chunk/2, get_tx/1, has_tx/1,
get_priority_set/0, get_last_tx_map/0, get_origin_tx_map/0,
get_propagation_queue/0, del_from_propagation_queue/2]).

Expand All @@ -21,7 +21,7 @@ load_from_disk() ->
{ok, {SerializedTXs, _MempoolSize}} ->
TXs = maps:map(fun(_, {TX, St}) -> {deserialize_tx(TX), St} end, SerializedTXs),

{MempoolSize2, PrioritySet2, PropagationQueue2, LastTXMap2, OriginTXMap2} =
{MempoolSize2, PrioritySet2, PropagationQueue2, LastTXMap2, OriginTXMap2} =
maps:fold(
fun(TXID, {TX, Status}, {MempoolSize, PrioritySet, PropagationQueue, LastTXMap, OriginTXMap}) ->
MetaData = {_, _, Timestamp} = init_tx_metadata(TX, Status),
Expand Down Expand Up @@ -79,7 +79,7 @@ add_tx(#tx{ id = TXID } = TX, Status) ->
add_to_last_tx_map(get_last_tx_map(), TX),
add_to_origin_tx_map(get_origin_tx_map(), TX)
};
{TX, PrevStatus, Timestamp} ->
{TX, PrevStatus, Timestamp} ->
{
{TX, Status, Timestamp},
get_mempool_size(),
Expand All @@ -98,7 +98,7 @@ add_tx(#tx{ id = TXID } = TX, Status) ->
{last_tx_map, LastTXMap},
{origin_tx_map, OriginTXMap}
]),

case ar_node:is_joined() of
true ->
% 1. Drop unconfirmable transactions:
Expand Down Expand Up @@ -332,7 +332,7 @@ del_from_last_tx_map(LastTXMap, TX) ->
%% when resolving overspends.
add_to_origin_tx_map(OriginTXMap, TX) ->
Element = unconfirmed_tx(TX),
Origin = ar_wallet:to_address(TX#tx.owner, TX#tx.signature_type),
Origin = ar_wallet:to_address(TX#tx.owner),
Set2 = case maps:get(Origin, OriginTXMap, not_found) of
not_found ->
gb_sets:from_list([Element]);
Expand All @@ -343,7 +343,7 @@ add_to_origin_tx_map(OriginTXMap, TX) ->

del_from_origin_tx_map(OriginTXMap, TX) ->
Element = unconfirmed_tx(TX),
Origin = ar_wallet:to_address(TX#tx.owner, TX#tx.signature_type),
Origin = ar_wallet:to_address(TX#tx.owner),
case maps:get(Origin, OriginTXMap, not_found) of
not_found ->
OriginTXMap;
Expand All @@ -353,7 +353,7 @@ del_from_origin_tx_map(OriginTXMap, TX) ->

unconfirmed_tx(TX = #tx{}) ->
{ar_tx:utility(TX), TX#tx.id}.


increase_mempool_size(
_MempoolSize = {MempoolHeaderSize, MempoolDataSize}, TX = #tx{}) ->
Expand Down Expand Up @@ -460,7 +460,7 @@ filter_clashing_txs(ClashingTXIDs) ->
%% confirmed)
%%
%% Note: when doing the overspend calculation any unconfirmed deposit
%% transactions are ignored. This is to prevent a second potentially
%% transactions are ignored. This is to prevent a second potentially
%% malicious scenario like the following:
%%
%% Peer A: receives deposit TX and several spend TXs,
Expand All @@ -479,7 +479,7 @@ find_overspent_txs(<<>>) ->
[];
find_overspent_txs(TX)
when TX#tx.reward > 0 orelse TX#tx.quantity > 0 ->
Origin = ar_wallet:to_address(TX#tx.owner, TX#tx.signature_type),
Origin = ar_wallet:to_address(TX#tx.owner),
SpentTXIDs = maps:get(Origin, get_origin_tx_map(), gb_sets:new()),
% We only care about the origin wallet since we aren't tracking
% unconfirmed deposits
Expand Down
33 changes: 15 additions & 18 deletions apps/arweave/src/ar_node_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

%% @doc Update the given accounts by applying a transaction.
apply_tx(Accounts, Denomination, TX) ->
#tx{ owner = From, signature_type = SigType } = TX,
Addr = ar_wallet:to_address(From, SigType),
#tx{ owner = From} = TX,
Addr = ar_wallet:to_address(From),
case maps:get(Addr, Accounts, not_found) of
not_found ->
Accounts;
Expand Down Expand Up @@ -154,12 +154,11 @@ update_sender_balance(Accounts, Denomination,
#tx{
id = ID,
owner = From,
signature_type = SigType,
quantity = Qty,
reward = Reward,
denomination = TXDenomination
}) ->
Addr = ar_wallet:to_address(From, SigType),
Addr = ar_wallet:to_address(From),
case maps:get(Addr, Accounts, not_found) of
{Balance, _LastTX} ->
Balance2 = ar_pricing:redenominate(Balance, 1, Denomination),
Expand Down Expand Up @@ -249,14 +248,13 @@ may_be_apply_double_signing_proof(B, PrevB, Accounts) ->
end.

may_be_apply_double_signing_proof2(B, PrevB, Accounts) ->
{Pub, _Signature1, _CDiff1, _PrevCDiff1, _Preimage1, _Signature2, _CDiff2, _PrevCDiff2,
{Identifier, _Signature1, _CDiff1, _PrevCDiff1, _Preimage1, _Signature2, _CDiff2, _PrevCDiff2,
_Preimage2} = B#block.double_signing_proof,
Key = {?DEFAULT_KEY_TYPE, Pub},
case B#block.reward_key == Key of
case B#block.reward_key == Identifier of
true ->
{error, invalid_double_signing_proof_same_address};
false ->
Addr = ar_wallet:to_address(Key),
Addr = ar_wallet:to_address(Identifier),
case is_account_banned(Addr, Accounts) of
true ->
{error, invalid_double_signing_proof_already_banned};
Expand All @@ -272,23 +270,22 @@ may_be_apply_double_signing_proof2(B, PrevB, Accounts) ->
end.

may_be_apply_double_signing_proof3(B, PrevB, Accounts) ->
{Pub, Signature1, CDiff1, PrevCDiff1, Preimage1, Signature2, CDiff2, PrevCDiff2,
{Identifier, Signature1, CDiff1, PrevCDiff1, Preimage1, Signature2, CDiff2, PrevCDiff2,
Preimage2} = B#block.double_signing_proof,
EncodedCDiff1 = ar_serialize:encode_int(CDiff1, 16),
EncodedPrevCDiff1 = ar_serialize:encode_int(PrevCDiff1, 16),
SignaturePreimage1 = << EncodedCDiff1/binary, EncodedPrevCDiff1/binary,
Preimage1/binary >>,
Key = {?DEFAULT_KEY_TYPE, Pub},
Addr = ar_wallet:to_address(Key),
case ar_wallet:verify(Key, SignaturePreimage1, Signature1) of
Addr = ar_wallet:to_address(Identifier),
case ar_wallet:verify(Identifier, SignaturePreimage1, Signature1) of
false ->
{error, invalid_double_signing_proof_invalid_signature};
true ->
EncodedCDiff2 = ar_serialize:encode_int(CDiff2, 16),
EncodedPrevCDiff2 = ar_serialize:encode_int(PrevCDiff2, 16),
SignaturePreimage2 = << EncodedCDiff2/binary,
EncodedPrevCDiff2/binary, Preimage2/binary >>,
case ar_wallet:verify(Key, SignaturePreimage2, Signature2) of
case ar_wallet:verify(Identifier, SignaturePreimage2, Signature2) of
false ->
{error, invalid_double_signing_proof_invalid_signature};
true ->
Expand Down Expand Up @@ -320,7 +317,7 @@ update_accounts4(B, PrevB, Accounts, Args) ->
update_accounts5(B, Accounts, Args);
Proof ->
Denomination = PrevB#block.denomination,
BannedAddr = ar_wallet:to_address({?DEFAULT_KEY_TYPE, element(1, Proof)}),
BannedAddr = ar_wallet:to_address(element(1, Proof)),
Sum = ar_rewards:get_total_reward_for_address(BannedAddr, PrevB) - 1,
{Dividend, Divisor} = ?DOUBLE_SIGNING_PROVER_REWARD_SHARE,
LockedRewards = ar_rewards:get_locked_rewards(PrevB),
Expand Down Expand Up @@ -525,7 +522,7 @@ validate_block(next_vdf_difficulty, {NewB, OldB, Wallets, BlockAnchors, RecentTX
RecentTXMap});
true ->
ExpectedNextVDFDifficulty = ar_block:compute_next_vdf_difficulty(OldB),
#nonce_limiter_info{ next_vdf_difficulty = NextVDFDifficulty } =
#nonce_limiter_info{ next_vdf_difficulty = NextVDFDifficulty } =
NewB#block.nonce_limiter_info,
case ExpectedNextVDFDifficulty == NextVDFDifficulty of
false ->
Expand Down Expand Up @@ -638,7 +635,7 @@ validate_block(merkle_rebase_support_threshold, {NewB, OldB}) ->
-ifdef(DEBUG).
is_wallet_invalid(#tx{ signature = <<>> }, _Wallets) ->
false;
is_wallet_invalid(#tx{ owner = Owner, signature_type = SigType }, Wallets) ->
is_wallet_invalid(#tx{ owner = Owner}, Wallets) ->
Address = ar_wallet:to_address(Owner, SigType),
case maps:get(Address, Wallets, not_found) of
{Balance, LastTX} when Balance >= 0 ->
Expand All @@ -659,8 +656,8 @@ is_wallet_invalid(#tx{ owner = Owner, signature_type = SigType }, Wallets) ->
true
end.
-else.
is_wallet_invalid(#tx{ owner = Owner, signature_type = SigType }, Wallets) ->
Address = ar_wallet:to_address(Owner, SigType),
is_wallet_invalid(#tx{ owner = Owner }, Wallets) ->
Address = ar_wallet:to_address(Owner),
case maps:get(Address, Wallets, not_found) of
{Balance, LastTX} when Balance >= 0 ->
case Balance of
Expand Down
Loading

0 comments on commit 5b1a7c1

Please sign in to comment.