Skip to content

Commit

Permalink
Add ACI to /compile output (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk authored Jan 31, 2023
1 parent 5d97cb2 commit a8f6bca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- `aci` property to the result of CompileContract with ACI formatted the same way as in aesophia_cli
### Changed
### Removed

Expand Down
10 changes: 6 additions & 4 deletions apps/aesophia_http/src/aesophia_http_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ handle_request('CompileContract', Req, _Context) ->
#{ <<"code">> := Code } = Json } ->
Options = maps:get(<<"options">>, Json, #{}),
case compile_contract(Code, Options) of
{ok, ByteCode} ->
{200, [], #{bytecode => aeser_api_encoder:encode(contract_bytearray, ByteCode)}};
{ok, ByteCode, Aci} ->
ByteCodeEncoded = aeser_api_encoder:encode(contract_bytearray, ByteCode),
{200, [], #{bytecode => ByteCodeEncoded, aci => Aci}};
{error, Errors} when is_list(Errors) ->
{400, [], mk_errors(Errors)};
{error, Msg} when is_binary(Msg) ->
Expand Down Expand Up @@ -290,9 +291,10 @@ generate_aci(Contract, Options) ->

compile_contract(Contract, Options) ->
Opts = compile_options(Options),
try aeso_compiler:from_string(binary_to_list(Contract), Opts) of
try aeso_compiler:from_string(binary_to_list(Contract), [{aci, json} | Opts]) of
{ok, Map} ->
{ok, aeser_contract_code:serialize(Map)};
#{ aci := Aci } = Map,
{ok, aeser_contract_code:serialize(Map), Aci};
Err = {error, _} ->
Err
catch _:R:S ->
Expand Down
16 changes: 9 additions & 7 deletions apps/aesophia_http/test/aesophia_http_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ end_per_testcase(_Case, _Config) ->

identity_contract(_Config) ->
%% Compile test contract "identity.aes"
{ok, _Code} = compile_test_contract("identity"),
{ok, _Code, Aci} = compile_test_contract("identity"),

?assertMatch([_C], Aci),

ok.

Expand Down Expand Up @@ -131,7 +133,7 @@ include_contract(_Config) ->
end || Name <- Files ]),
Opts = #{file_system => ExplicitFileSystem, src_file => <<"include.aes">>},

{ok, _Code} = compile_test_contract(Dir, "include", Opts),
{ok, _Code, _Aci} = compile_test_contract(Dir, "include", Opts),

ok.

Expand Down Expand Up @@ -187,7 +189,7 @@ encode_calldata(_Config) ->
decode_calldata_bytecode(_Config) ->
{ok, ContractSrcBin} = read_test_contract("calldata"),
ContractSrc = binary_to_list(ContractSrcBin),
{ok, Contract} = compile_test_contract("calldata"),
{ok, Contract, _Aci} = compile_test_contract("calldata"),

DoEnc = fun(F, V) -> encode_calldata(ContractSrc, binary_to_list(F), [V]) end,
Datas = maps:map(DoEnc, test_data()),
Expand Down Expand Up @@ -292,7 +294,7 @@ decode_call_result(_Config) ->
[ Check(K) || K <- maps:keys(Expects) ].

decode_call_result_bytecode(_Config) ->
{ok, Contract} = compile_test_contract("callresult"),
{ok, Contract, _Aci} = compile_test_contract("callresult"),

Values = bin_test_data(),

Expand All @@ -306,7 +308,7 @@ decode_call_result_bytecode(_Config) ->
[ Check(K) || K <- maps:keys(Expects) ].

decode_call_result_bytecode_not_ok(_Config) ->
{ok, Contract} = compile_test_contract("callresult"),
{ok, Contract, _Aci} = compile_test_contract("callresult"),
Map0 = #{bytecode => Contract, function => <<"foo">>},

ErrVal = aeser_api_encoder:encode(contract_bytearray, <<"An error happened!">>),
Expand All @@ -324,7 +326,7 @@ decode_call_result_bytecode_not_ok(_Config) ->
ok.

validate_byte_code(_Config) ->
{ok, IdByteCode} = compile_test_contract("identity"),
{ok, IdByteCode, _Aci} = compile_test_contract("identity"),
{ok, IdSource} = read_test_contract("identity"),
{ok, NotIdSource} = read_test_contract("callresult"),
?assertMatch({ok, 200, #{}},
Expand Down Expand Up @@ -407,7 +409,7 @@ compile_test_contract(Dir, Name, Opts) ->
FileName = filename:join(Dir, Name ++ ".aes"),
{ok, SophiaCode} = file:read_file(FileName),
case get_contract_bytecode(SophiaCode, Opts) of
{ok, 200, #{<<"bytecode">> := Code}} -> {ok, Code};
{ok, 200, #{<<"bytecode">> := Code, <<"aci">> := Aci}} -> {ok, Code, Aci};
{ok, 400, Errors} -> {error, Errors}
end.

Expand Down
11 changes: 8 additions & 3 deletions config/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ paths:
/compile:
post:
operationId: 'CompileContract'
description: 'Compile a sophia contract from source and return byte code'
description: 'Compile a sophia contract from source and return byte code and ACI'
consumes:
- application/json
produces:
Expand All @@ -56,7 +56,7 @@ paths:
'200':
description: Byte code response
schema:
$ref: '#/definitions/ByteCode'
$ref: '#/definitions/CompileResult'
'400':
description: Invalid data
schema:
Expand Down Expand Up @@ -428,13 +428,18 @@ definitions:
API:
description: 'Swagger API description'
type: object
ByteCode:
CompileResult:
type: object
properties:
bytecode:
$ref: '#/definitions/EncodedByteArray'
aci:
type: array
items:
type: object
required:
- bytecode
- aci
SophiaCallResultInput:
type: object
properties:
Expand Down

0 comments on commit a8f6bca

Please sign in to comment.