Skip to content

Commit

Permalink
Store EVM revert value in log, and in case of text value - in tx
Browse files Browse the repository at this point in the history
  • Loading branch information
cleverfox committed Oct 1, 2022
1 parent 0a81f2b commit 9f20bb2
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 9 deletions.
7 changes: 4 additions & 3 deletions apps/tpnode/src/contract_evm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ encode_arg(_,_) ->


handle_tx(#{to:=To,from:=From}=Tx, #{code:=Code}=Ledger,
GasLimit, GetFun, Opaque) ->
GasLimit, GetFun, #{log:=PreLog}=Opaque) ->
State=case maps:get(state,Ledger,#{}) of
BinState when is_binary(BinState) ->
{ok,MapState}=msgpack:unpack(BinState),
Expand Down Expand Up @@ -347,11 +347,12 @@ handle_tx(#{to:=To,from:=From}=Tx, #{code:=Code}=Ledger,
"state"=>unchanged,
"gas"=>0,
"txs"=>[]}, Opaque};
{done, {revert, _}, #{ gas:=GasLeft}} ->
{done, {revert, Revert}, #{ gas:=GasLeft}} ->
Log1=[([evm,revert,Revert])|PreLog],
{ok, #{null=>"exec",
"state"=>unchanged,
"gas"=>GasLeft,
"txs"=>[]}, Opaque};
"txs"=>[]}, Opaque#{"revert"=>Revert, log=>Log1}};
{error, nogas, #{storage:=NewStorage}} ->
io:format("St ~w keys~n",[maps:size(NewStorage)]),
io:format("St ~p~n",[(NewStorage)]),
Expand Down
22 changes: 17 additions & 5 deletions apps/tpnode/src/generate_block_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ try_process_inbound([{TxID,
end,

TxExt=maps:get(extdata,Tx,#{}),
NewExt=maps:mege(
NewExt=maps:merge(
TxExt#{
<<"orig_bhei">>=>OriginHeight,
<<"orig_bhash">>=>OriginHash,
Expand Down Expand Up @@ -1233,11 +1233,23 @@ deposit(TxID, Address, Addresses0, #{ver:=2}=Tx, GasLimit,
{Addresses4, EmitTxs, GasLeft, Acc2#{aalloc=>AAlloc2, log=>EmitLog++Logs},
case maps:get("return",OpaqueState2,undefined) of
<<Int:256/big>> when Int < 16#10000000000000000 ->
[{retval,Int}];
[{retval, Int}];
<<Bin:32/big>> ->
[{retval,Bin}];
_ ->
[]
[{retval, Bin}];
RetVal when is_binary(RetVal) ->
[{retval, other}];
undefined ->
case maps:get("revert",OpaqueState2,undefined) of
<<16#08C379A0:32/big,
16#20:256/big,
Len:256/big,
Str:Len/binary,_/binary>> when 32>=Len ->
[{revert, Str}];
Revert when is_binary(Revert) ->
[{revert, other}];
undefined ->
[]
end
end
}
end.
Expand Down
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@


{relx, [
{release, { thepower, "0.13.4" }, [tpnode,
{release, { thepower, "0.13.5" }, [tpnode,
sasl,
runtime_tools
]},
Expand Down
136 changes: 136 additions & 0 deletions test/mkblock_evm_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,142 @@ mapval(N,Key) when is_binary(Key) ->
{ok,Hash}=ksha3:hash(256, <<NKey:256/big, N:256/big>>),
Hash.

evm_revert_test() ->
OurChain=150,
Pvt1= <<194, 124, 65, 109, 233, 236, 108, 24, 50, 151, 189, 216, 23, 42, 215, 220, 24, 240,
248, 115, 150, 54, 239, 58, 218, 221, 145, 246, 158, 15, 210, 165>>,
Addr1=naddress:construct_public(1, OurChain, 1),
SkAddr1=naddress:construct_public(1, OurChain, 4),
SkAddr2=naddress:construct_public(1, OurChain, 5),
Code1=eevm_asm:asm(
[{push,1,0},
sload,
{push,1,1},
add,
{dup,1},
{push,1,0},
sstore,
{push,1,0},
mstore,

{push,1,50}, %2
{push,1,32},
{push,1,0},
{log,1},

{push,1,32},
{push,1,0},
revert]
),
Code2=eevm_asm:asm(
[{push,32,16#08c379a0},
{push,1,224},
shl,
{push,1,0},
mstore,

{push,1,32},
{push,1,4},
mstore,

{push,1,6},
{push,1,4+32},
mstore,

{push,32,0},
{push,1,4+64},
mstore,

{push,32,binary:decode_unsigned(<<"preved">>)},
{push,1,(256-6)*8},
shl,
{push,1,4+64},
mstore,

{push,1,4+64+32},
{push,1,0},
revert]
),

TX1=tx:sign(
tx:construct_tx(#{
ver=>2,
kind=>generic,
from=>Addr1,
to=>SkAddr1,
call=>#{
},
payload=>[
#{purpose=>gas, amount=>3300, cur=><<"FTT">>},
#{purpose=>srcfee, amount=>2, cur=><<"FTT">>}
],
seq=>3,
t=>os:system_time(millisecond)
}), Pvt1),


TX2=tx:sign(
tx:construct_tx(#{
ver=>2,
kind=>generic,
from=>Addr1,
to=>SkAddr2,
call=>#{
},
payload=>[
#{purpose=>gas, amount=>3300, cur=><<"FTT">>},
#{purpose=>srcfee, amount=>2, cur=><<"FTT">>}
],
seq=>4,
t=>os:system_time(millisecond)
}), Pvt1),

TxList1=[
{<<"1log">>, maps:put(sigverify,#{valid=>1},TX1)},
{<<"2log">>, maps:put(sigverify,#{valid=>1},TX2)}
],
TestFun=fun(#{block:=_Block=#{txs:=Txs1},
emit:=_Emit,
log:=Log,
failed:=Failed}) ->
io:format("Failed ~p~n",[Failed]),
?assertMatch([],Failed),
{ok,Log,Txs1}
end,
Ledger=[
{Addr1,
#{amount => #{ <<"FTT">> => 1000000, <<"SK">> => 3, <<"TST">> => 26 }}
},
{SkAddr1,
#{amount => #{},
code => Code1,
vm => <<"evm">>,
state => #{ <<0>> => <<2,0,0>> }
}
},
{SkAddr2,
#{amount => #{},
code => Code2,
vm => <<"evm">>,
state => #{}
}
}
],
{ok,Log,BlockTx}=extcontract_template(OurChain, TxList1, Ledger, TestFun),
io:format("Logs ~p~n",[BlockTx]),
ReadableLog=lists:map(
fun(Bin) ->
{ok,LogEntry} = msgpack:unpack(Bin),
io:format("- ~p~n",[LogEntry]),
LogEntry
end, Log),
[
?assertMatch([
[<<"1log">>,<<"evm">>, <<"revert">>, <<131073:256/big>>]
], ReadableLog),
?assertMatch(true,false)
].

evm_log_test() ->
OurChain=150,
Pvt1= <<194, 124, 65, 109, 233, 236, 108, 24, 50, 151, 189, 216, 23, 42, 215, 220, 24, 240,
Expand Down

0 comments on commit 9f20bb2

Please sign in to comment.