Skip to content

Commit a86406f

Browse files
committed
merge e24 branch: A lot of fixes with xchain, tpic etc
2 parents c40e807 + 7586ed8 commit a86406f

21 files changed

+877
-464
lines changed

apps/tpic2/src/tpic2_tls.erl

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,39 @@ loop1(State=#{socket:=Socket,role:=Role,opts:=Opts,transport:=Transport}) ->
5555
undefined
5656
end,
5757
IsItMe=tpecdsa:cmp_pubkey(Pubkey)==tpecdsa:cmp_pubkey(nodekey:get_pub()),
58+
IsOurNode=chainsettings:is_our_node(Pubkey),
5859
?LOG_INFO("Peer PubKey ~s ~p",[hex:encode(Pubkey),
59-
try
60-
chainsettings:is_our_node(Pubkey)
61-
catch _:_ -> unkn0wn
62-
end]),
63-
case {IsItMe,Role} of
64-
{true, server} ->
65-
?LOG_NOTICE("Looks like I received connection from myself, dropping session"),
60+
try IsOurNode catch _:_ -> unkn0wn end]),
61+
if IsItMe andalso Role==server ->
62+
?LOG_NOTICE("I received connection from myself, dropping session"),
63+
timer:sleep(1000),
64+
Transport:close(Socket),
6665
done;
67-
{true, _} ->
68-
?LOG_NOTICE("Looks like I received connected to myself, dropping session"),
66+
67+
IsItMe ->
68+
?LOG_NOTICE("I connected to myself, dropping session"),
69+
timer:sleep(1000),
70+
Transport:close(Socket),
71+
done;
72+
73+
Role == server andalso IsOurNode == false -> %server, unknonwn peer
74+
tpic2_tls:send_msg(unknown_node, State),
75+
timer:sleep(1000),
76+
Transport:close(Socket),
6977
done;
70-
{false, server} ->
78+
79+
Role == server -> %server, known peer
7180
{ok,PPID}=gen_server:call(tpic2_cmgr, {peer,Pubkey, {register, undefined, in, self()}}),
7281
?MODULE:loop(State#{pubkey=>Pubkey,peerpid=>PPID});
73-
{false, _} ->
82+
83+
IsOurNode == false -> %client unknown node
84+
{IP, Port} = maps:get(address, State),
85+
gen_server:call(tpic2_cmgr,{peer, Pubkey, {del, IP, Port}, unknown_node}),
86+
timer:sleep(5000),
87+
Transport:close(Socket),
88+
done;
89+
90+
true -> %client, known node
7491
Stream=maps:get(stream, Opts, 0),
7592
{IP, Port} = maps:get(address, State),
7693
gen_server:call(tpic2_cmgr,{peer, Pubkey, {add, IP, Port}}),
@@ -208,6 +225,11 @@ send_msg(hello, #{socket:=Socket, opts:=Opts}) ->
208225
?LOG_DEBUG("Hello ~p",[Hello]),
209226
ssl:send(Socket,msgpack:pack(Hello));
210227

228+
send_msg(unknown_node, #{socket:=Socket, opts:=Opts}) ->
229+
?LOG_DEBUG("send unknown_node and close conn to ~p",[Opts]),
230+
Hello=#{null=><<"unknown_node">>},
231+
ssl:send(Socket,msgpack:pack(Hello));
232+
211233
send_msg(Msg, #{socket:=Socket}) when is_map(Msg) ->
212234
ssl:send(Socket,msgpack:pack(Msg)).
213235

apps/tpnode/src/block.erl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ prepack(Block) ->
6868
maps:from_list(
6969
lists:map(
7070
fun({TxID, T}) ->
71-
{TxID, tx:pack(T)}
71+
{TxID, pack_patch(T)}
7272
end, Txs)
7373
);
7474
(inbound_blocks, Blocks) ->
@@ -372,6 +372,8 @@ verify(#{ header:=#{parent:=Parent, %blkv2
372372
{settings_hash, SH};
373373
({<<"settings_hash">>, SH}) ->
374374
{settings_hash, SH};
375+
({<<"log_hash">>, SH}) ->
376+
{log_hash, SH};
375377
({Key, Value}) ->
376378
?LOG_INFO("Unknown root ~p",[Key]),
377379
{Key, Value}
@@ -507,11 +509,15 @@ verify(#{ header:=#{parent:=_,
507509

508510

509511
binarize_settings([]) -> [];
510-
binarize_settings([{TxID, #{ kind:=patch, ver:=2, patches:=_ }=Patch}|Rest]) ->
511-
[{TxID, tx:pack(Patch)}|binarize_settings(Rest)];
512-
binarize_settings([{TxID, #{ patch:=_LPatch }=Patch}|Rest]) ->
513-
[{TxID, tx:pack(Patch)}|binarize_settings(Rest)].
514-
512+
binarize_settings([{TxID, Patch}|Rest]) ->
513+
[{TxID, pack_patch(Patch)}|binarize_settings(Rest)].
514+
515+
%pack_patch(#{ patch:=P }) ->
516+
% tx:pack(tx:construct_tx(#{patches=>P, kind=>patch, ver=>2}));
517+
pack_patch(#{ kind:=patch, ver:=2, patches:=_, body:=_ }=Patch) ->
518+
tx:pack(Patch);
519+
pack_patch(#{ kind:=patch, ver:=2, patches:=_ }=Patch) ->
520+
tx:pack(tx:construct_tx(Patch)).
515521

516522
mkblock2(#{ txs:=Txs, parent:=Parent,
517523
height:=H, bals:=Bals0,

apps/tpnode/src/blockchain_sync.erl

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
%% API Function Exports
88
%% ------------------------------------------------------------------
99

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

1212
%% ------------------------------------------------------------------
1313
%% gen_server Function Exports
@@ -31,17 +31,27 @@ chainstate() ->
3131
))++[{self,gen_server:call(blockchain_reader,sync_req)}],
3232
% io:format("Cand ~p~n",[Candidates]),
3333
ChainState=lists:foldl( %first suitable will be the quickest
34-
fun({_, #{chain:=_HisChain,
34+
fun({PK0, #{chain:=_HisChain,
3535
%null:=<<"sync_available">>,
3636
last_hash:=Hash,
3737
last_temp:=Tmp,
3838
%prev_hash:=PHash,
3939
last_height:=Heig
4040
}=A
4141
}, Acc) ->
42+
PK=case PK0 of
43+
self -> nodekey:node_name();
44+
{HisKey,_,_} ->
45+
case chainsettings:is_our_node(HisKey) of
46+
false ->
47+
hex:encode(HisKey);
48+
OurName ->
49+
OurName
50+
end
51+
end,
4252
PHash=maps:get(prev_hash,A,<<0,0,0,0,0,0,0,0>>),
4353
maps:put({Heig, Hash, PHash, Tmp},
44-
maps:get({Heig, Hash, PHash, Tmp}, Acc, 0)+1, Acc);
54+
[PK|maps:get({Heig, Hash, PHash, Tmp}, Acc, [])], Acc);
4555
({_, _}, Acc) ->
4656
Acc
4757
end, #{}, Candidates),
@@ -54,7 +64,7 @@ chainstate() ->
5464
":",blkid(Has),
5565
"/",blkid(PHas),":",
5666
integer_to_list(if Tmp==false -> 0; true -> Tmp end)
57-
]),V,Acc)
67+
]),length(V),Acc)
5868
end, #{}, ChainState)),
5969
ChainState.
6070

@@ -243,11 +253,10 @@ handle_info(runsync, State) ->
243253
Candidates = case maps:get(sync_candidates, State, []) of
244254
[] ->
245255
?LOG_DEBUG("use default list of candidates"),
246-
lists:reverse(
247-
tpiccall(<<"blockchain">>,
248-
#{null=><<"sync_request">>},
249-
[last_hash, last_height, chain, last_temp]
250-
));
256+
sort_rnd(tpiccall(<<"blockchain">>,
257+
#{null=><<"sync_request">>},
258+
[last_hash, last_height, chain, last_temp]
259+
));
251260
SavedCandidates ->
252261
?LOG_DEBUG("use saved list of candidates"),
253262
SavedCandidates
@@ -262,15 +271,15 @@ handle_info({runsync, Candidates}, State) ->
262271
}, hash:=MyLastHash}=MyLast=blockchain:last_meta(),
263272

264273
B=tpiccall(<<"blockchain">>,
265-
#{null=><<"sync_request">>},
266-
[last_hash, last_height, chain, last_temp]
267-
),
274+
#{null=><<"sync_request">>},
275+
[last_hash, last_height, chain, last_temp]
276+
),
268277
Hack_Candidates=lists:foldl(
269278
fun({{A1,B1,_},_}=Elem, Acc) ->
270279
maps:put({A1,B1},Elem,Acc)
271280
end, #{}, B),
272281

273-
Candidate=lists:foldl( %first suitable will be the quickest
282+
Candidate=lists:foldl(
274283
fun
275284
({{A2,B2,_}, undefined}, undefined) ->
276285
case maps:find({A2,B2},Hack_Candidates) of
@@ -589,4 +598,10 @@ skip_candidate(default)->
589598
skip_candidate(Candidates) when is_list(Candidates) ->
590599
tl(Candidates).
591600

601+
sort_rnd(List) ->
602+
List0=[ {rand:uniform(), I} || I <- List ],
603+
List1=lists:keysort(1, List0),
604+
[ E || {_,E} <- List1 ].
605+
606+
592607
%% ------------------------------------------------------------------

apps/tpnode/src/bsig.erl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ encode_edval(N, PK) when is_binary(N) andalso is_binary(PK) ->
105105
end;
106106
encode_edval(_, _) -> <<>>.
107107

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

112111
unpacksig(HSig) when is_map(HSig) ->
113112
HSig;

apps/tpnode/src/chainsettings.erl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
is_our_node/2,
99
settings_to_ets/1,
1010
all/0, by_path/1]).
11+
-export([contacts/1,contacts/2]).
1112
-export([checksum/0]).
1213

1314
is_our_node(PubKey, Settings) ->
@@ -93,6 +94,42 @@ settings_to_ets(NewSettings) ->
9394
blockchain_updater:store_mychain(MyName, ChainNodes, MyChain),
9495
NewSettings.
9596

97+
contacts(Name, Protocol) when is_binary(Protocol) ->
98+
contacts(Name, [Protocol]);
99+
100+
contacts(Name, Protocols) when is_list(Protocols) ->
101+
C=contacts(Name),
102+
lists:foldl(
103+
fun(URL,A) ->
104+
try
105+
#{scheme:=P}=uri_string:parse(URL),
106+
case lists:member(P,Protocols) of
107+
true ->
108+
[URL|A];
109+
false ->
110+
A
111+
end
112+
catch _:_ ->
113+
A
114+
end
115+
end, [], C).
116+
117+
contacts(Name) ->
118+
Config = application:get_env(tpnode, contacts, #{}),
119+
R=case maps:get(Name, Config, undefined) of
120+
Value when is_list(Value) ->
121+
Value;
122+
undefined ->
123+
chainsettings:by_path([<<"contacts">>,<<"default">>,Name]);
124+
Any ->
125+
logger:notice("Error value for contact ~p in config: ~p",[Name,Any]),
126+
chainsettings:by_path([<<"contacts">>,<<"default">>,Name])
127+
end,
128+
if R==#{} ->
129+
[];
130+
true ->
131+
R
132+
end.
96133

97134
get_val(Name) ->
98135
get_val(Name, undefined).

apps/tpnode/src/generate_block.erl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ generate_block(PreTXL, {Parent_Height, Parent_Hash}, GetSettings, GetAddr, Extra
1313
%file:write_file("tmp/tx.txt", io_lib:format("~p.~n", [PreTXL])),
1414
_T1=erlang:system_time(),
1515
_T2=erlang:system_time(),
16-
TXL=sort_txs(PreTXL),
1716
XSettings=GetSettings(settings),
17+
PreTXL1=lists:filter(
18+
fun({TxID,#{not_before:=DT}}) ->
19+
L=settings:get([<<"current">>,<<"delaytx">>,DT], XSettings),
20+
if is_list(L) ->
21+
lists:member(TxID,L);
22+
true ->
23+
false
24+
end;
25+
(_) -> true
26+
end, PreTXL),
27+
28+
TXL=sort_txs(PreTXL1),
1829
Addrs0=lists:foldl(
1930
fun(default, Acc) ->
2031
BinAddr=naddress:construct_private(0, 0),
@@ -441,7 +452,7 @@ update_aalloc(#{
441452
IncAddr=#{<<"t">> => <<"set">>,
442453
<<"p">> => [<<"current">>, <<"allocblock">>, <<"last">>],
443454
<<"v">> => CA},
444-
AAlloc={<<"aalloc">>, #{sig=>[], patch=>[IncAddr]}},
455+
AAlloc={<<"aalloc">>, #{sig=>[], ver=>2, kind=>patch, patches=>[IncAddr]}},
445456
SS1=settings:patch(AAlloc, SetState),
446457

447458
Acc#{new_settings=>SS1,
@@ -497,7 +508,7 @@ cleanup_delayed(#{delayed:=DTX, new_settings:=NS, settings:=Sets} = Acc) ->
497508
Cleanup=[#{<<"p">>=>[<<"current">>,<<"delaytx">>],
498509
<<"t">>=><<"lists_cleanup">>,
499510
<<"v">>=><<"empty_list">>}],
500-
DelPatch={<<"cleanjob">>, #{sig=>[], patch=>ToDel++Cleanup}},
511+
DelPatch={<<"cleanjob">>, #{sig=>[], ver=>2, kind=>patch, patches=>ToDel++Cleanup}},
501512
?LOG_INFO("Patches ~p~n",[ToDel]),
502513
Acc#{
503514
settings=>[DelPatch|Sets]
@@ -536,7 +547,7 @@ process_delayed_txs(#{emit:=Emit, settings:=Settings, parent:=P,
536547
_ ->
537548
Patches=[#{<<"p">> => [<<"current">>,<<"delaytx">>,Timestamp],
538549
<<"t">> => <<"list_add">>,<<"v">> => [H,P,TxID]} || {TxID, Timestamp} <- ToSet ],
539-
SyncPatch={<<"delayjob">>, #{sig=>[], patch=>Patches}},
550+
SyncPatch={<<"delayjob">>, #{sig=>[], ver=>2, kind=>patch, patches=>Patches}},
540551

541552
?LOG_INFO("Emit ~p~n",[NewEmit]),
542553
?LOG_INFO("Patches ~p~n",[Patches]),

0 commit comments

Comments
 (0)