Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support serialization of fixed-size arrays in abi_serializer. #1918

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions libraries/chain/abi_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,15 @@ namespace eosio { namespace chain {
auto h = ctx.enter_scope();
auto rtype = resolve_type(type);
auto ftype = fundamental_type(rtype);
auto btype = built_in_types.find(ftype );
if( btype != built_in_types.end() ) {
bool var_is_array = is_szarray(rtype);
heifner marked this conversation as resolved.
Show resolved Hide resolved
if( auto btype = built_in_types.find(ftype ); !var_is_array && btype != built_in_types.end() ) {
try {
return btype->second.first(stream, is_array(rtype), is_optional(rtype), ctx.get_yield_function());
heifner marked this conversation as resolved.
Show resolved Hide resolved
} EOS_RETHROW_EXCEPTIONS( unpack_exception, "Unable to unpack ${class} type '${type}' while processing '${p}'",
("class", is_array(rtype) ? "array of built-in" : is_optional(rtype) ? "optional of built-in" : "built-in")
("type", impl::limit_size(ftype))("p", ctx.get_path_string()) )
}
if ( is_array(rtype) ) {
if ( is_array(rtype) || var_is_array ) {
ctx.hint_array_type_if_in_array();
fc::unsigned_int size;
try {
Expand Down Expand Up @@ -496,10 +496,11 @@ namespace eosio { namespace chain {
auto v_itr = variants.end();
auto s_itr = structs.end();

auto btype = built_in_types.find(fundamental_type(rtype));
if( btype != built_in_types.end() ) {
bool var_is_array = is_szarray(rtype);
heifner marked this conversation as resolved.
Show resolved Hide resolved

if( auto btype = built_in_types.find(fundamental_type(rtype)); !var_is_array && btype != built_in_types.end() ) {
btype->second.second(var, ds, is_array(rtype), is_optional(rtype), ctx.get_yield_function());
heifner marked this conversation as resolved.
Show resolved Hide resolved
} else if ( is_array(rtype) ) {
} else if ( var_is_array || is_array(rtype) ) {
ctx.hint_array_type_if_in_array();
vector<fc::variant> vars = var.get_array();
fc::raw::pack(ds, (fc::unsigned_int)vars.size());
Expand Down
38 changes: 38 additions & 0 deletions unittests/abi_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,44 @@ fc::variant verify_type_round_trip_conversion( const abi_serializer& abis, const
}
)=====";

BOOST_AUTO_TEST_CASE(std_array_types)
{ try {

const char* currency_abi = R"=====(
heifner marked this conversation as resolved.
Show resolved Hide resolved
{
"version": "eosio::abi/1.0",
"types": [],
"structs": [{
"name": "test",
"base": "",
"fields": [{
"name": "a",
"type": "uint8[5]"
}]
}],
"actions": [],
"tables": [],
"ricardian_clauses": []
}
)=====";

auto abi = fc::json::from_string(currency_abi).as<abi_def>();

abi_serializer abis(eosio_contract_abi(abi), abi_serializer::create_yield_function( max_serialization_time ));

const char* test_data = R"=====(
{
"a" : [1, 2, 3, 4, 5]
}
)=====";


auto var = fc::json::from_string(test_data);
verify_byte_round_trip_conversion(abi_serializer{std::move(abi), abi_serializer::create_yield_function( max_serialization_time )}, "test", var);

} FC_LOG_AND_RETHROW() }


BOOST_AUTO_TEST_CASE(uint_types)
{ try {

Expand Down