Skip to content

Commit 2d68665

Browse files
committed
Merge branch 'e24': efecciently get smartcontract data from ledger
2 parents a86406f + 294568c commit 2d68665

4 files changed

Lines changed: 82 additions & 272 deletions

File tree

apps/tpnode/src/mledger.erl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-export([dbmtfun/3]).
88
-export([mb2item/1]).
99
-export([get_kv/1]).
10+
-export([get_kpv/3, get_kpvs/3]).
1011
-export([addr_proof/1, bi_decode/1]).
1112
-export([rollback/2]).
1213
-export([apply_backup/1]).
@@ -134,6 +135,34 @@ get_vers(Address, trans) ->
134135
mb2item( #mb{ address=A, key=K, path=P, value=V, version=Ver, introduced=Int }) ->
135136
bi_create(A,Ver,K,P,Int,V).
136137

138+
get_kpvs(Address, Key, Path) ->
139+
case rockstable:get(mledger,
140+
undefined,
141+
#bal_items{address=Address,
142+
version=latest,
143+
key=Key,
144+
path=Path,
145+
_='_'}) of
146+
not_found -> [];
147+
[] -> [];
148+
List ->
149+
[ {K, P, V} || #bal_items{key=K,path=P,value=V} <- List]
150+
end.
151+
152+
get_kpv(Address, Key, Path) ->
153+
case rockstable:get(mledger,
154+
undefined,
155+
#bal_items{address=Address,
156+
version=latest,
157+
key=Key,
158+
path=Path,
159+
_='_'}) of
160+
not_found -> undefined;
161+
[] -> undefined;
162+
[#bal_items{value=V}] ->
163+
{ok, V}
164+
end.
165+
137166
get(Address) ->
138167
get(Address,trans).
139168

apps/tpnode/src/tpnode_httpapi.erl

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -375,38 +375,45 @@ h(<<"GET">>, [<<"nodes">>, Chain], _Req) ->
375375
chain_nodes => get_nodes(binary_to_integer(Chain, 10))
376376
});
377377

378-
h(<<"GET">>, [<<"address">>, TAddr, <<"statekeys">>], _Req) ->
378+
h(<<"GET">>, [<<"address">>, TAddr, <<"statekeys">>], Req) ->
379379
try
380380
Addr=case TAddr of
381381
<<"0x", Hex/binary>> -> hex:parse(Hex);
382382
_ -> naddress:decode(TAddr)
383383
end,
384-
Ledger=mledger:get(Addr),
385-
case Ledger =/= undefined of
386-
false ->
387-
err(
388-
10003,
389-
<<"Not found">>,
390-
#{result => <<"not_found">>},
391-
#{http_code => 404}
392-
);
393-
true ->
394-
State=maps:get(state, Ledger, #{}),
395-
S1=maps:fold(
396-
fun(K,_,Acc) ->
397-
[<<"0x",(hex:encode(K))/binary>>|Acc]
398-
end, [], State),
384+
case mledger:get_kpv(Addr,vm,'_') of
385+
undefined ->
386+
err(
387+
10003,
388+
<<"Not found">>,
389+
#{result => <<"not_found">>},
390+
#{http_code => 404}
391+
);
392+
{ok,_VM} ->
393+
RawKeys=mledger:get_kpvs(Addr,state,'_'),
394+
395+
S1=case maps:get(req_format,Req) of
396+
<<"mp">> ->
397+
lists:foldl(
398+
fun({state,K,_},Acc) ->
399+
[K|Acc]
400+
end, [], RawKeys);
401+
_ -> %default hex encode
402+
lists:foldl(
403+
fun({state,K,_},Acc) ->
404+
[<<"0x",(hex:encode(K))/binary>>|Acc]
405+
end, [], RawKeys)
406+
end,
399407
{200, [{"Content-Type","application/json"}],
400408
#{
401409
notice => <<"Only for debugging. Do not use it in scripts!!!">>,
402410
keys => S1
403411
}
404412
}
405-
406413
end
407414
catch
408415
throw:{error, address_crc} ->
409-
err(
416+
err(
410417
10004,
411418
<<"Invalid address">>,
412419
#{result => <<"error">>},
@@ -428,41 +435,47 @@ h(<<"GET">>, [<<"address">>, TAddr, <<"state",F/binary>>|Path], _Req) ->
428435
<<"0x", Hex/binary>> -> hex:parse(Hex);
429436
_ -> naddress:decode(TAddr)
430437
end,
431-
Ledger=mledger:get(Addr),
432-
case Ledger =/= undefined of
433-
false ->
438+
case mledger:get_kpv(Addr,vm,'_') of
439+
undefined ->
434440
err(
435441
10003,
436442
<<"Not found">>,
437443
#{result => <<"not_found">>},
438444
#{http_code => 404}
439445
);
440-
true ->
441-
State=maps:get(state, Ledger, #{}),
446+
{ok,_VM} ->
442447
case Path of
443448
[] ->
444-
?LOG_INFO("F ~p",[F]),
449+
RawKeys=mledger:get_kpvs(Addr,state,'_'),
445450
case F of <<>> ->
446-
S1=msgpack:pack(State),
451+
KVs=lists:foldl(
452+
fun({state,K,V}, A) ->
453+
maps:put(K,V,A)
454+
end,#{},RawKeys
455+
),
456+
S1=msgpack:pack(KVs),
447457
{200, [{"Content-Type","binary/octet-stream"}], S1};
448458
<<"json">> ->
449-
S1=maps:fold(
450-
fun(K,V,Acc) ->
451-
maps:put(
452-
base64:encode(K),
453-
base64:encode(V),
454-
Acc)
455-
end, #{
456-
notice => <<"Only for Sasha">>
457-
}, State),
459+
S1=lists:foldl(
460+
fun({state,K,V},Acc) ->
461+
maps:put(
462+
base64:encode(K),
463+
base64:encode(V),
464+
Acc)
465+
end, #{
466+
notice => <<"Only for Sasha">>
467+
}, RawKeys),
458468
{200, [{"Content-Type","application/json"}], S1}
459469
end;
460470
[Key] ->
461471
K=case Key of
462472
<<"0x", HexK/binary>> -> hex:parse(HexK);
463473
_ -> base64:decode(Key)
464474
end,
465-
Val=maps:get(K,State,<<>>),
475+
Val=case mledger:get_kpv(Addr,state,K) of
476+
undefined -> <<>>;
477+
{ok,V1} -> V1
478+
end,
466479
{200, [{"Content-Type","binary/octet-stream"}], Val}
467480
end
468481
end
@@ -490,17 +503,16 @@ h(<<"GET">>, [<<"address">>, TAddr, <<"code">>], _Req) ->
490503
<<"0x", Hex/binary>> -> hex:parse(Hex);
491504
_ -> naddress:decode(TAddr)
492505
end,
493-
Ledger=mledger:get(Addr),
494-
case Ledger =/= undefined of
495-
false ->
506+
Ledger=mledger:get_kpv(Addr, code, []),
507+
case Ledger of
508+
undefined ->
496509
err(
497510
10003,
498511
<<"Not found">>,
499512
#{result => <<"not_found">>},
500513
#{http_code => 404}
501514
);
502-
true ->
503-
Code=maps:get(code, Ledger, <<>>),
515+
{ok, Code} ->
504516
{200, [{"Content-Type","binary/octet-stream"}], Code}
505517
end
506518
catch

0 commit comments

Comments
 (0)