Skip to content

Commit

Permalink
merge e24 branch: A lot of fixes with xchain, tpic etc
Browse files Browse the repository at this point in the history
  • Loading branch information
cleverfox committed Dec 26, 2022
2 parents c40e807 + 7586ed8 commit a86406f
Show file tree
Hide file tree
Showing 21 changed files with 877 additions and 464 deletions.
44 changes: 33 additions & 11 deletions apps/tpic2/src/tpic2_tls.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,39 @@ loop1(State=#{socket:=Socket,role:=Role,opts:=Opts,transport:=Transport}) ->
undefined
end,
IsItMe=tpecdsa:cmp_pubkey(Pubkey)==tpecdsa:cmp_pubkey(nodekey:get_pub()),
IsOurNode=chainsettings:is_our_node(Pubkey),
?LOG_INFO("Peer PubKey ~s ~p",[hex:encode(Pubkey),
try
chainsettings:is_our_node(Pubkey)
catch _:_ -> unkn0wn
end]),
case {IsItMe,Role} of
{true, server} ->
?LOG_NOTICE("Looks like I received connection from myself, dropping session"),
try IsOurNode catch _:_ -> unkn0wn end]),
if IsItMe andalso Role==server ->
?LOG_NOTICE("I received connection from myself, dropping session"),
timer:sleep(1000),
Transport:close(Socket),
done;
{true, _} ->
?LOG_NOTICE("Looks like I received connected to myself, dropping session"),

IsItMe ->
?LOG_NOTICE("I connected to myself, dropping session"),
timer:sleep(1000),
Transport:close(Socket),
done;

Role == server andalso IsOurNode == false -> %server, unknonwn peer
tpic2_tls:send_msg(unknown_node, State),
timer:sleep(1000),
Transport:close(Socket),
done;
{false, server} ->

Role == server -> %server, known peer
{ok,PPID}=gen_server:call(tpic2_cmgr, {peer,Pubkey, {register, undefined, in, self()}}),
?MODULE:loop(State#{pubkey=>Pubkey,peerpid=>PPID});
{false, _} ->

IsOurNode == false -> %client unknown node
{IP, Port} = maps:get(address, State),
gen_server:call(tpic2_cmgr,{peer, Pubkey, {del, IP, Port}, unknown_node}),
timer:sleep(5000),
Transport:close(Socket),
done;

true -> %client, known node
Stream=maps:get(stream, Opts, 0),
{IP, Port} = maps:get(address, State),
gen_server:call(tpic2_cmgr,{peer, Pubkey, {add, IP, Port}}),
Expand Down Expand Up @@ -208,6 +225,11 @@ send_msg(hello, #{socket:=Socket, opts:=Opts}) ->
?LOG_DEBUG("Hello ~p",[Hello]),
ssl:send(Socket,msgpack:pack(Hello));

send_msg(unknown_node, #{socket:=Socket, opts:=Opts}) ->
?LOG_DEBUG("send unknown_node and close conn to ~p",[Opts]),
Hello=#{null=><<"unknown_node">>},
ssl:send(Socket,msgpack:pack(Hello));

send_msg(Msg, #{socket:=Socket}) when is_map(Msg) ->
ssl:send(Socket,msgpack:pack(Msg)).

Expand Down
18 changes: 12 additions & 6 deletions apps/tpnode/src/block.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ prepack(Block) ->
maps:from_list(
lists:map(
fun({TxID, T}) ->
{TxID, tx:pack(T)}
{TxID, pack_patch(T)}
end, Txs)
);
(inbound_blocks, Blocks) ->
Expand Down Expand Up @@ -372,6 +372,8 @@ verify(#{ header:=#{parent:=Parent, %blkv2
{settings_hash, SH};
({<<"settings_hash">>, SH}) ->
{settings_hash, SH};
({<<"log_hash">>, SH}) ->
{log_hash, SH};
({Key, Value}) ->
?LOG_INFO("Unknown root ~p",[Key]),
{Key, Value}
Expand Down Expand Up @@ -507,11 +509,15 @@ verify(#{ header:=#{parent:=_,


binarize_settings([]) -> [];
binarize_settings([{TxID, #{ kind:=patch, ver:=2, patches:=_ }=Patch}|Rest]) ->
[{TxID, tx:pack(Patch)}|binarize_settings(Rest)];
binarize_settings([{TxID, #{ patch:=_LPatch }=Patch}|Rest]) ->
[{TxID, tx:pack(Patch)}|binarize_settings(Rest)].

binarize_settings([{TxID, Patch}|Rest]) ->
[{TxID, pack_patch(Patch)}|binarize_settings(Rest)].

%pack_patch(#{ patch:=P }) ->
% tx:pack(tx:construct_tx(#{patches=>P, kind=>patch, ver=>2}));
pack_patch(#{ kind:=patch, ver:=2, patches:=_, body:=_ }=Patch) ->
tx:pack(Patch);
pack_patch(#{ kind:=patch, ver:=2, patches:=_ }=Patch) ->
tx:pack(tx:construct_tx(Patch)).

mkblock2(#{ txs:=Txs, parent:=Parent,
height:=H, bals:=Bals0,
Expand Down
41 changes: 28 additions & 13 deletions apps/tpnode/src/blockchain_sync.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
%% API Function Exports
%% ------------------------------------------------------------------

-export([start_link/0,chainstate/0,bbyb_sync/3,receive_block/2,tpiccall/3]).
-export([start_link/0,chainstate/0,bbyb_sync/3,receive_block/2,tpiccall/3,sort_rnd/1]).

%% ------------------------------------------------------------------
%% gen_server Function Exports
Expand All @@ -31,17 +31,27 @@ chainstate() ->
))++[{self,gen_server:call(blockchain_reader,sync_req)}],
% io:format("Cand ~p~n",[Candidates]),
ChainState=lists:foldl( %first suitable will be the quickest
fun({_, #{chain:=_HisChain,
fun({PK0, #{chain:=_HisChain,
%null:=<<"sync_available">>,
last_hash:=Hash,
last_temp:=Tmp,
%prev_hash:=PHash,
last_height:=Heig
}=A
}, Acc) ->
PK=case PK0 of
self -> nodekey:node_name();
{HisKey,_,_} ->
case chainsettings:is_our_node(HisKey) of
false ->
hex:encode(HisKey);
OurName ->
OurName
end
end,
PHash=maps:get(prev_hash,A,<<0,0,0,0,0,0,0,0>>),
maps:put({Heig, Hash, PHash, Tmp},
maps:get({Heig, Hash, PHash, Tmp}, Acc, 0)+1, Acc);
[PK|maps:get({Heig, Hash, PHash, Tmp}, Acc, [])], Acc);
({_, _}, Acc) ->
Acc
end, #{}, Candidates),
Expand All @@ -54,7 +64,7 @@ chainstate() ->
":",blkid(Has),
"/",blkid(PHas),":",
integer_to_list(if Tmp==false -> 0; true -> Tmp end)
]),V,Acc)
]),length(V),Acc)
end, #{}, ChainState)),
ChainState.

Expand Down Expand Up @@ -243,11 +253,10 @@ handle_info(runsync, State) ->
Candidates = case maps:get(sync_candidates, State, []) of
[] ->
?LOG_DEBUG("use default list of candidates"),
lists:reverse(
tpiccall(<<"blockchain">>,
#{null=><<"sync_request">>},
[last_hash, last_height, chain, last_temp]
));
sort_rnd(tpiccall(<<"blockchain">>,
#{null=><<"sync_request">>},
[last_hash, last_height, chain, last_temp]
));
SavedCandidates ->
?LOG_DEBUG("use saved list of candidates"),
SavedCandidates
Expand All @@ -262,15 +271,15 @@ handle_info({runsync, Candidates}, State) ->
}, hash:=MyLastHash}=MyLast=blockchain:last_meta(),

B=tpiccall(<<"blockchain">>,
#{null=><<"sync_request">>},
[last_hash, last_height, chain, last_temp]
),
#{null=><<"sync_request">>},
[last_hash, last_height, chain, last_temp]
),
Hack_Candidates=lists:foldl(
fun({{A1,B1,_},_}=Elem, Acc) ->
maps:put({A1,B1},Elem,Acc)
end, #{}, B),

Candidate=lists:foldl( %first suitable will be the quickest
Candidate=lists:foldl(
fun
({{A2,B2,_}, undefined}, undefined) ->
case maps:find({A2,B2},Hack_Candidates) of
Expand Down Expand Up @@ -589,4 +598,10 @@ skip_candidate(default)->
skip_candidate(Candidates) when is_list(Candidates) ->
tl(Candidates).

sort_rnd(List) ->
List0=[ {rand:uniform(), I} || I <- List ],
List1=lists:keysort(1, List0),
[ E || {_,E} <- List1 ].


%% ------------------------------------------------------------------
5 changes: 2 additions & 3 deletions apps/tpnode/src/bsig.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ encode_edval(N, PK) when is_binary(N) andalso is_binary(PK) ->
end;
encode_edval(_, _) -> <<>>.

splitsig(<<255, SLen:8/integer, Rest/binary>>) ->
<<Signature:SLen/binary, Extradata/binary>>=Rest,
{Signature, Extradata}.
splitsig(<<255, SLen:8/integer, Signature:SLen/binary, Rest/binary>>) ->
{Signature, Rest}.

unpacksig(HSig) when is_map(HSig) ->
HSig;
Expand Down
37 changes: 37 additions & 0 deletions apps/tpnode/src/chainsettings.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
is_our_node/2,
settings_to_ets/1,
all/0, by_path/1]).
-export([contacts/1,contacts/2]).
-export([checksum/0]).

is_our_node(PubKey, Settings) ->
Expand Down Expand Up @@ -93,6 +94,42 @@ settings_to_ets(NewSettings) ->
blockchain_updater:store_mychain(MyName, ChainNodes, MyChain),
NewSettings.

contacts(Name, Protocol) when is_binary(Protocol) ->
contacts(Name, [Protocol]);

contacts(Name, Protocols) when is_list(Protocols) ->
C=contacts(Name),
lists:foldl(
fun(URL,A) ->
try
#{scheme:=P}=uri_string:parse(URL),
case lists:member(P,Protocols) of
true ->
[URL|A];
false ->
A
end
catch _:_ ->
A
end
end, [], C).

contacts(Name) ->
Config = application:get_env(tpnode, contacts, #{}),
R=case maps:get(Name, Config, undefined) of
Value when is_list(Value) ->
Value;
undefined ->
chainsettings:by_path([<<"contacts">>,<<"default">>,Name]);
Any ->
logger:notice("Error value for contact ~p in config: ~p",[Name,Any]),
chainsettings:by_path([<<"contacts">>,<<"default">>,Name])
end,
if R==#{} ->
[];
true ->
R
end.

get_val(Name) ->
get_val(Name, undefined).
Expand Down
19 changes: 15 additions & 4 deletions apps/tpnode/src/generate_block.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ generate_block(PreTXL, {Parent_Height, Parent_Hash}, GetSettings, GetAddr, Extra
%file:write_file("tmp/tx.txt", io_lib:format("~p.~n", [PreTXL])),
_T1=erlang:system_time(),
_T2=erlang:system_time(),
TXL=sort_txs(PreTXL),
XSettings=GetSettings(settings),
PreTXL1=lists:filter(
fun({TxID,#{not_before:=DT}}) ->
L=settings:get([<<"current">>,<<"delaytx">>,DT], XSettings),
if is_list(L) ->
lists:member(TxID,L);
true ->
false
end;
(_) -> true
end, PreTXL),

TXL=sort_txs(PreTXL1),
Addrs0=lists:foldl(
fun(default, Acc) ->
BinAddr=naddress:construct_private(0, 0),
Expand Down Expand Up @@ -441,7 +452,7 @@ update_aalloc(#{
IncAddr=#{<<"t">> => <<"set">>,
<<"p">> => [<<"current">>, <<"allocblock">>, <<"last">>],
<<"v">> => CA},
AAlloc={<<"aalloc">>, #{sig=>[], patch=>[IncAddr]}},
AAlloc={<<"aalloc">>, #{sig=>[], ver=>2, kind=>patch, patches=>[IncAddr]}},
SS1=settings:patch(AAlloc, SetState),

Acc#{new_settings=>SS1,
Expand Down Expand Up @@ -497,7 +508,7 @@ cleanup_delayed(#{delayed:=DTX, new_settings:=NS, settings:=Sets} = Acc) ->
Cleanup=[#{<<"p">>=>[<<"current">>,<<"delaytx">>],
<<"t">>=><<"lists_cleanup">>,
<<"v">>=><<"empty_list">>}],
DelPatch={<<"cleanjob">>, #{sig=>[], patch=>ToDel++Cleanup}},
DelPatch={<<"cleanjob">>, #{sig=>[], ver=>2, kind=>patch, patches=>ToDel++Cleanup}},
?LOG_INFO("Patches ~p~n",[ToDel]),
Acc#{
settings=>[DelPatch|Sets]
Expand Down Expand Up @@ -536,7 +547,7 @@ process_delayed_txs(#{emit:=Emit, settings:=Settings, parent:=P,
_ ->
Patches=[#{<<"p">> => [<<"current">>,<<"delaytx">>,Timestamp],
<<"t">> => <<"list_add">>,<<"v">> => [H,P,TxID]} || {TxID, Timestamp} <- ToSet ],
SyncPatch={<<"delayjob">>, #{sig=>[], patch=>Patches}},
SyncPatch={<<"delayjob">>, #{sig=>[], ver=>2, kind=>patch, patches=>Patches}},

?LOG_INFO("Emit ~p~n",[NewEmit]),
?LOG_INFO("Patches ~p~n",[Patches]),
Expand Down
Loading

0 comments on commit a86406f

Please sign in to comment.