From a8f6bca48e05983dc9a78df6b09e2961f7de0c3c Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Tue, 31 Jan 2023 23:22:13 +0400 Subject: [PATCH] Add ACI to /compile output (#106) --- CHANGELOG.md | 1 + apps/aesophia_http/src/aesophia_http_handler.erl | 10 ++++++---- apps/aesophia_http/test/aesophia_http_SUITE.erl | 16 +++++++++------- config/swagger.yaml | 11 ++++++++--- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfb91a6..cca291f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/apps/aesophia_http/src/aesophia_http_handler.erl b/apps/aesophia_http/src/aesophia_http_handler.erl index 5ab440c..3278c7f 100644 --- a/apps/aesophia_http/src/aesophia_http_handler.erl +++ b/apps/aesophia_http/src/aesophia_http_handler.erl @@ -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) -> @@ -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 -> diff --git a/apps/aesophia_http/test/aesophia_http_SUITE.erl b/apps/aesophia_http/test/aesophia_http_SUITE.erl index 737cb8c..846c763 100644 --- a/apps/aesophia_http/test/aesophia_http_SUITE.erl +++ b/apps/aesophia_http/test/aesophia_http_SUITE.erl @@ -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. @@ -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. @@ -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()), @@ -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(), @@ -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!">>), @@ -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, #{}}, @@ -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. diff --git a/config/swagger.yaml b/config/swagger.yaml index 72879fc..ed50273 100644 --- a/config/swagger.yaml +++ b/config/swagger.yaml @@ -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: @@ -56,7 +56,7 @@ paths: '200': description: Byte code response schema: - $ref: '#/definitions/ByteCode' + $ref: '#/definitions/CompileResult' '400': description: Invalid data schema: @@ -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: