WIP
This commit is contained in:
+39
-5
@@ -792,11 +792,20 @@ contract_code(ID) ->
|
|||||||
|
|
||||||
contract_source(ID) ->
|
contract_source(ID) ->
|
||||||
case request(["/v3/contracts/", ID, "/source"]) of
|
case request(["/v3/contracts/", ID, "/source"]) of
|
||||||
{ok, #{"source" := Source}} -> {ok, Source};
|
{ok, #{"source" := Source}} -> extract(Source);
|
||||||
{ok, #{"reason" := Reason}} -> {error, Reason};
|
{ok, #{"reason" := Reason}} -> {error, Reason};
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
extract(TarBaby) ->
|
||||||
|
case erl_tar:extract({binary, TarBaby}, [memory, compressed]) of
|
||||||
|
{ok, Project} ->
|
||||||
|
{ok, Project};
|
||||||
|
Error ->
|
||||||
|
tell(info, "non-compressed thingy returned: ~tp", [Error]),
|
||||||
|
{ok, TarBaby}
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
-spec contract_poi(ID) -> {ok, Bytecode} | {error, Reason}
|
-spec contract_poi(ID) -> {ok, Bytecode} | {error, Reason}
|
||||||
when ID :: contract_id(),
|
when ID :: contract_id(),
|
||||||
@@ -1049,6 +1058,7 @@ contract_create(CreatorID, Path, InitArgs) ->
|
|||||||
contract_create(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Path, InitArgs) ->
|
contract_create(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Path, InitArgs) ->
|
||||||
case file:read_file(Path) of
|
case file:read_file(Path) of
|
||||||
{ok, Source} ->
|
{ok, Source} ->
|
||||||
|
Name = filename:basename(Path),
|
||||||
Dir = filename:dirname(Path),
|
Dir = filename:dirname(Path),
|
||||||
{ok, CWD} = file:get_cwd(),
|
{ok, CWD} = file:get_cwd(),
|
||||||
SrcDir = so_utils:canonical_dir(Path),
|
SrcDir = so_utils:canonical_dir(Path),
|
||||||
@@ -1058,17 +1068,18 @@ contract_create(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Path, InitArgs) ->
|
|||||||
{src_dir, SrcDir},
|
{src_dir, SrcDir},
|
||||||
{include, {file_system, [CWD, so_utils:canonical_dir(Dir)]}}],
|
{include, {file_system, [CWD, so_utils:canonical_dir(Dir)]}}],
|
||||||
contract_create2(CreatorID, Nonce, Gas, GasPrice, Amount, TTL,
|
contract_create2(CreatorID, Nonce, Gas, GasPrice, Amount, TTL,
|
||||||
Source, Options, InitArgs);
|
Name, Source, Options, InitArgs);
|
||||||
Error ->
|
Error ->
|
||||||
Error
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
contract_create2(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Source, Options, InitArgs) ->
|
contract_create2(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Name, Source, Options, InitArgs) ->
|
||||||
case so_compiler:from_string(Source, Options) of
|
case so_compiler:from_string(Source, Options) of
|
||||||
{ok, Compiled} ->
|
{ok, Compiled} ->
|
||||||
|
Named = maps:put(contract_name, Name, Compiled),
|
||||||
contract_create_built(CreatorID, Nonce, Gas, GasPrice, Amount, TTL,
|
contract_create_built(CreatorID, Nonce, Gas, GasPrice, Amount, TTL,
|
||||||
Compiled, InitArgs);
|
Named, InitArgs);
|
||||||
Error ->
|
Error ->
|
||||||
Error
|
Error
|
||||||
end.
|
end.
|
||||||
@@ -1146,7 +1157,7 @@ assemble_calldata(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Compiled, CallDa
|
|||||||
|
|
||||||
assemble_calldata2(OwnerID, Nonce, Gas, GasPrice, Amount, TTL, Compiled, CallData) ->
|
assemble_calldata2(OwnerID, Nonce, Gas, GasPrice, Amount, TTL, Compiled, CallData) ->
|
||||||
Code = gmser_contract_code:serialize(Compiled),
|
Code = gmser_contract_code:serialize(Compiled),
|
||||||
Source = unicode:characters_to_binary(maps:get(contract_source, Compiled, <<>>)),
|
Source = bundle_source(Compiled),
|
||||||
VM = 1,
|
VM = 1,
|
||||||
ABI = 1,
|
ABI = 1,
|
||||||
<<CTVersion:32>> = <<VM:16, ABI:16>>,
|
<<CTVersion:32>> = <<VM:16, ABI:16>>,
|
||||||
@@ -1183,6 +1194,29 @@ assemble_calldata2(OwnerID, Nonce, Gas, GasPrice, Amount, TTL, Compiled, CallDat
|
|||||||
error:Reason -> {error, Reason}
|
error:Reason -> {error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
bundle_source(Compiled) ->
|
||||||
|
case maps:find(contract_source, Compiled) of
|
||||||
|
{ok, Source} -> bundle_source2(unicode:characters_to_binary(Source), Compiled);
|
||||||
|
error -> <<>>
|
||||||
|
end.
|
||||||
|
|
||||||
|
bundle_source2(Source, Compiled) ->
|
||||||
|
File = unicode:characters_to_binary(maps:get(contract_name, Compiled, <<"contract.aes">>)),
|
||||||
|
<<RN:(8 * 8)>> = crypto:strong_rand_bytes(8),
|
||||||
|
Rand = integer_to_binary(RN, 36),
|
||||||
|
TmpDir = filename:join(zx_lib:path(tmp), Rand),
|
||||||
|
TgzName = <<File, ".tgz">>,
|
||||||
|
TarGzPath = filename:join(TmpDir, TgzName),
|
||||||
|
ok = filelib:ensure_dir(TarGzPath),
|
||||||
|
{ok, CWD} = file:get_cwd(),
|
||||||
|
ok = file:set_cwd(TmpDir),
|
||||||
|
ok = file:write_file(File, Source),
|
||||||
|
ok = erl_tar:create(TarGzPath, [File], [compressed]),
|
||||||
|
{ok, TgzBin} = file:read_file(TarGzPath),
|
||||||
|
ok = file:set_cwd(CWD),
|
||||||
|
ok = file:del_dir_r(TmpDir),
|
||||||
|
TgzBin.
|
||||||
|
|
||||||
|
|
||||||
-spec read_aci(Path) -> Result
|
-spec read_aci(Path) -> Result
|
||||||
when Path :: file:filename(),
|
when Path :: file:filename(),
|
||||||
|
|||||||
Reference in New Issue
Block a user