Skip to content

Commit

Permalink
Merge branch 'e24'
Browse files Browse the repository at this point in the history
  • Loading branch information
cleverfox committed May 6, 2024
2 parents 4ed8dc1 + c0c3d59 commit e5308c9
Show file tree
Hide file tree
Showing 48 changed files with 2,980 additions and 1,598 deletions.
2 changes: 1 addition & 1 deletion apps/tpnode/src/apixiom.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ handle_request(Method, Path, Req, Target, Format, _Opts) ->
api_call,
[{path, cowboy_req:path(Req)}, {client_addr, get_client_ip_headers(Req)}]
),
?LOG_INFO("api: ~p ~p", [cowboy_req:path(Req), get_client_ip_headers(Req)]),
?LOG_DEBUG("api: ~p ~p", [cowboy_req:path(Req), get_client_ip_headers(Req)]),

Req1 =
case Format of
Expand Down
2 changes: 1 addition & 1 deletion apps/tpnode/src/block.erl
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ mkblock2(#{ txs:=Txs, parent:=Parent,
[]
end,

?LOG_INFO("ER ~p",[ExtraRoots]),
?LOG_DEBUG("ExtraRoots ~p",[ExtraRoots]),
TempRoot=if TempID == false -> ExtraRoots;
true -> [{tmp, <<TempID:64/big>>}|ExtraRoots]
end,
Expand Down
20 changes: 18 additions & 2 deletions apps/tpnode/src/blockchain_reader.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
-export([start_link/0]).
-export([blkid/1,
mychain/0,
get_block/2,
get_block/1,
send_block_real/4]).
%-export([mychain0/0]).

Expand Down Expand Up @@ -132,6 +134,15 @@ handle_call({block_exists, BlockHash}, _From, #{ldb:=LDB} = State)
end,
{reply, Exists, State};

handle_call({get_block, Height}, _From, #{ldb:=LDB} = State) when is_integer(Height) ->
case ldb:read_key(LDB, <<"h:", Height:64/big>>, undefined) of
undefined ->
{reply, not_found, State};
Hash ->
Res=block_rel(LDB, Hash, self),
{reply, Res, State}
end;

handle_call({get_hash, Height}, _From, #{ldb:=LDB} = State) when is_integer(Height) ->
R=
case ldb:read_key(LDB, <<"h:", Height:64/big>>, undefined) of
Expand Down Expand Up @@ -578,8 +589,8 @@ mychain() ->
?LOG_ERROR("Can't parse node key ~p -> ~p",[Key, Val]),
Acc
end, #{}, KeyDB),
?LOG_NOTICE("NodesDB ~p", [KeyDB]),
?LOG_NOTICE("Nodes ~p", [ChainNodes0]),
?LOG_DEBUG("NodesDB ~p", [KeyDB]),
?LOG_INFO("Nodes ~p", [maps:values(ChainNodes0)]),
MyName=maps:get(PubKey, ChainNodes0, undefined),
MyChain=maps:get(MyName, NodeChain, 0),
ChainNodes=maps:filter(
Expand All @@ -606,3 +617,8 @@ mychain(State) ->
State
end.

get_block(BlockHash, Rel) ->
gen_server:call(?SERVER,{get_block, BlockHash, Rel}).
get_block(H) when is_integer(H) ->
gen_server:call(?SERVER,{get_block, H}).

32 changes: 19 additions & 13 deletions apps/tpnode/src/blockchain_updater.erl
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,14 @@ handle_call({new_block, #{hash:=BlockHash,
end,
MBlk=case maps:get(BlockHash, Candidates, undefined) of
undefined ->
Blk;
Blk#{
<<"_install_t">> => os:system_time(microsecond)
};
#{sign:=BSig}=ExBlk ->
NewSigs=lists:usort(BSig ++ Success),
ExBlk#{
sign=>NewSigs
sign=>NewSigs,
<<"_install_t">> => os:system_time(microsecond)
}
end,
SigLen=length(maps:get(sign, MBlk)),
Expand Down Expand Up @@ -357,6 +360,7 @@ handle_call({new_block, #{hash:=BlockHash,
}),
gen_server:cast(blockchain_reader,{update,MBlk}),
gen_server:cast(tpnode_ws_dispatcher, {new_block, MBlk}),
gen_server:cast(tpnode_reporter, {new_block, maps:get(height, maps:get(header, Blk)), maps:get(temporary,Blk,false)}),

{reply, ok, State#{
tmpblock=>MBlk
Expand Down Expand Up @@ -388,7 +392,7 @@ handle_call({new_block, #{hash:=BlockHash,
_ ->
{checkput, BlockLedgerHash}
end, MBlk),
?LOG_INFO("LApply ~p",[LApply]),
?LOG_DEBUG("LApply ~p",[LApply]),

LHash=case LApply of
{ok, LH11} ->
Expand Down Expand Up @@ -424,6 +428,8 @@ handle_call({new_block, #{hash:=BlockHash,
end
end,

gen_server:cast(tpnode_reporter, {new_block, maps:get(height, maps:get(header, Blk)), maps:get(temporary,Blk,false)}),

SendSuccess=lists:map(
fun({TxID, #{register:=_, address:=Addr}}) ->
{TxID, #{address=>Addr, block=>BlockHash}};
Expand Down Expand Up @@ -866,7 +872,7 @@ apply_ledger(Action, #{bals:=S, hash:=BlockHash, header:=#{height:=Height}}) ->
put ->
{ok,mledger:apply_patch(mledger:bals2patch(Patch), {commit, {Height, BlockHash}})}
end,
?LOG_INFO("Apply ~p ~p", [Action, LR]),
?LOG_DEBUG("Apply ~p ~p", [Action, LR]),
LR.

apply_block_conf_meta(#{hash:=Hash}=Block, Conf0) ->
Expand Down Expand Up @@ -923,10 +929,10 @@ mychain(#{lastblock:=#{header:=#{chain:=MyChain}}, settings:=Sets}=State) ->
chainnodes=>ChainNodes,
mychain=>MyChain
}),
SK=fun(SS) ->
Req=[ candidates, ldb, settings, btable, lastblock, mychain ],
[ {T,maps:is_key(T,SS)} || T <- Req ]
end,
%SK=fun(SS) ->
% Req=[ candidates, ldb, settings, btable, lastblock, mychain ],
% [ {T,maps:is_key(T,SS)} || T <- Req ]
% end,
%logger:error("MYCHAIN old ~p new ~p",[SK(State), SK(S1)]),
S1;

Expand All @@ -940,11 +946,11 @@ mychain(#{lastblock:=#{header:=_ChainLess}, settings:=Sets}=State) ->
chainnodes=>ChainNodes,
mychain=>0
}),
SK=fun(SS) ->
Req=[ candidates, ldb, settings, btable, lastblock, mychain ],
[ {T,maps:is_key(T,SS)} || T <- Req ]
end,
%logger:error("MYCHAIN old ~p new ~p",[SK(State), SK(S1)]),
%SK=fun(SS) ->
% Req=[ candidates, ldb, settings, btable, lastblock, mychain ],
% [ {T,maps:is_key(T,SS)} || T <- Req ]
% end,
%?LOG_DEBUG("MYCHAIN old ~p new ~p",[SK(State), SK(S1)]),
S1.

replace(Field, Value) ->
Expand Down
10 changes: 6 additions & 4 deletions apps/tpnode/src/blockvote.erl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ handle_cast({tpic, _From, #{
<<"sign">> := Sigs
}},
#{mychain:=MyChain}=State) when MyChain==MsgChain ->
handle_cast({signature, BlockHash, Sigs}, State);
LD = << 64 , (os:system_time(microsecond)):64/big >>,
Sigs2 = [ bsig:set_localdata(E, LD) || E <- Sigs ],
handle_cast({signature, BlockHash, Sigs2}, State);

handle_cast({tpic, _From, #{
null:=<<"blockvote">>,
Expand All @@ -116,7 +118,7 @@ handle_cast({tpic, _From, #{
handle_cast({signature, BlockHash, Sigs}=WholeSig,
#{lastblock:=#{hash:=LBH}}=State) when LBH==BlockHash->
?LOG_DEBUG("BV Got extra sig for ~s ~p", [blkid(BlockHash), WholeSig]),
?LOG_INFO("BV Got extra sig for ~s", [blkid(BlockHash)]),
%?LOG_DEBUG("BV Got extra sig for ~s", [blkid(BlockHash)]),

stout:log(
bv_gotsig,
Expand All @@ -129,7 +131,7 @@ handle_cast({signature, BlockHash, Sigs}=WholeSig,

handle_cast({signature, BlockHash, Sigs},
#{candidatesig:=Candidatesig, candidatets:=CandidateTS}=State) ->
?LOG_INFO("BV Got sig for ~s", [blkid(BlockHash)]),
?LOG_DEBUG("BV Got sig for ~s", [blkid(BlockHash)]),
CSig0=maps:get(BlockHash, Candidatesig, #{}),
CSig=checksig(BlockHash, Sigs, CSig0),
%?LOG_DEBUG("BV S CS2 ~p", [maps:keys(CSig)]),
Expand Down Expand Up @@ -357,7 +359,7 @@ is_block_ready(BlockHash, #{extras:=Extras}=State) ->
blockchain_updater:new_block(Blk),
Extra=maps:get(BlockHash, Extras, #{}),
if Extra =/= #{} andalso Extra =/= #{log=>[]} ->
?LOG_INFO("Extra for blk ~w ~s: ~p",[Height, blkid(BlockHash), Extra]);
?LOG_INFO("Extra for blk ~w ~s: ~p",[Height, blkid(BlockHash), maps:keys(Extra)]);
true -> ok
end,
case maps:is_key(log, Extra) of
Expand Down
65 changes: 55 additions & 10 deletions apps/tpnode/src/bsig.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
-export([checksig1/3, checksig1/2]).
-export([signhash/3, signhash1/3]).
-export([packsig/1, unpacksig/1]).
-export([add_localdata/2]).
-export([set_localdata/2]).
-export([get_localdata/1]).
-export([pack_sign_ed/1, unpack_sign_ed/1]).
-export([add_sig/2]).
-export([extract_pubkey/1, extract_pubkeys/1]).
Expand Down Expand Up @@ -118,8 +121,9 @@ decode_edval(240, <<KL:8/integer, Rest/binary>>=Raw) ->
{240, Raw}
end;

decode_edval(254, Bin) -> {purpose, Bin};
%decode_edval(254, Bin) -> {purpose, Bin};
decode_edval(255, Bin) -> {signature, Bin};
decode_edval(254, Bin) -> {local_data, Bin};
decode_edval(Key, BinVal) -> {Key, BinVal}.

encode_edval(timestamp, Integer) -> <<1, 8, Integer:64/big>>;
Expand All @@ -134,7 +138,7 @@ encode_edval(baldep, {Address,Seq}) ->
8=size(Address),
<<7, 16, Address/binary, Seq:64/big>>;
encode_edval(signature, PK) -> <<255, (size(PK)):8/integer, PK/binary>>;
encode_edval(purpose, PK) -> <<254, (size(PK)):8/integer, PK/binary>>;
%encode_edval(purpose, PK) -> <<254, (size(PK)):8/integer, PK/binary>>;
encode_edval(N, PK) when is_binary(N) andalso is_binary(PK) ->
TS=size(N)+size(PK)+1,
if TS>=64 ->
Expand All @@ -144,23 +148,64 @@ encode_edval(N, PK) when is_binary(N) andalso is_binary(PK) ->
end;
encode_edval(_, _) -> <<>>.

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

splitsig(<<255, SLen:8/integer, Signature:SLen/binary, Rest/binary>>,A) ->
A#{signature => Signature,
binextra => Rest};

splitsig(<<254, LLen:8/integer, LocalData:LLen/binary, Rest/binary>>,A) ->
splitsig(Rest,A#{local_data => LocalData}).

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

unpacksig(BSig) when is_binary(BSig) ->
{Signature, Hdr}=splitsig(BSig),
#{ binextra => (Hdr),
signature => (Signature),
extra => unpack_sign_ed(Hdr)
}.
#{binextra:=BE}=Split=splitsig(BSig),
Split#{extra=> unpack_sign_ed(BE)}.

get_localdata(<<254, LLen:8/integer, LD0:LLen/binary, _Rest/binary>>) ->
LD0;
get_localdata(#{local_data:=LD0}) ->
LD0;
get_localdata(_) ->
<<>>.

add_localdata(<<255,_/binary>> = Sig, LD) ->
<<254, (size(LD)):8/integer, LD/binary, Sig/binary>>;

add_localdata(#{local_data:=LD0}=Sig,LD1) ->
Sig#{
local_data => <<LD0/binary,LD1/binary>>
};

add_localdata(#{}=Sig,LD1) ->
Sig#{
local_data => <<LD1/binary>>
}.

set_localdata(<<255,_/binary>> = Sig, LD) ->
<<254, (size(LD)):8/integer, LD/binary, Sig/binary>>;

set_localdata(<<254, LLen:8/integer, _LD0:LLen/binary, Rest/binary>>, LD1) ->
<<254, (size(LD1)):8/integer, LD1/binary, Rest/binary>>;

set_localdata(#{}=Sig,LD1) ->
Sig#{ local_data => <<LD1/binary>> }.

packsig(BinSig) when is_binary(BinSig) ->
BinSig;

packsig(#{local_data:=LD, signature:=Signature, binextra:=BinExtra}) ->
<<254, (size(LD)):8/integer, LD/binary,
255, (size(Signature)):8/integer, Signature/binary,
BinExtra/binary>>;

packsig(#{signature:=Signature, binextra:=BinExtra}) ->
<<255, (size(Signature)):8/integer, Signature/binary, BinExtra/binary>>.
<<255, (size(Signature)):8/integer, Signature/binary,
BinExtra/binary>>.


add_sig(OldSigs, NewSigs) ->
Apply=fun(#{extra:=EPL}=Sig, Acc) ->
Expand Down
16 changes: 8 additions & 8 deletions apps/tpnode/src/chainkeeper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ handle_cast({tpic, NodeName, _From, Payload}, #{lookaround_timer := Timer} = Sta
{ok, Decode} when is_map(Decode) ->
case Decode of
#{null := <<"block_installed">>,<<"blk">> := ReceivedBlk} ->
?LOG_INFO("got ck_beacon from ~s, block: ~p", [NodeName, Payload]),
?LOG_DEBUG("got ck_beacon from ~s ~p", [NodeName, ReceivedBlk]),
block:unpack(ReceivedBlk);
_ ->
?LOG_INFO("got ck_beacon from ~s, unpacked payload: ~p", [NodeName, Decode]),
Expand All @@ -149,13 +149,13 @@ handle_cast({tpic, NodeName, _From, Payload}, #{lookaround_timer := Timer} = Sta
#{}
end,

?LOG_INFO("Blk: ~p", [Blk]),
case Blk of
#{hash:=H} ->
Sig=gen_server:call(blockvote,{signatures, H}),
?LOG_INFO("got signatures: ~p", [Sig]);
_ -> ok
end,
?LOG_DEBUG("Blk: ~p", [Blk]),
%case Blk of
% #{hash:=H} ->
% Sig=gen_server:call(blockvote,{signatures, H}),
% ?LOG_INFO("got new signatures from node ~s: ~p", [NodeName,Sig]);
% _ -> ok
%end,


stout:log(ck_beacon,
Expand Down
Loading

0 comments on commit e5308c9

Please sign in to comment.