abi_serializer.cpp 14.7 KB
Newer Older
D
Daniel Larimer 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
#include <eosio/chain/abi_serializer.hpp>
#include <eosio/chain/contract_types.hpp>
#include <eosio/chain/authority.hpp>
#include <eosio/chain/chain_config.hpp>
#include <eosio/chain/transaction.hpp>
#include <eosio/chain/asset.hpp>
#include <fc/io/raw.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <fc/io/varint.hpp>

using namespace boost;

18
namespace eosio { namespace chain {
D
Daniel Larimer 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

   using boost::algorithm::ends_with;
   using std::string;

   template <typename T>
   inline fc::variant variant_from_stream(fc::datastream<const char*>& stream) {
      T temp;
      fc::raw::unpack( stream, temp );
      return fc::variant(temp);
   }

   template <typename T>
   auto pack_unpack() {
      return std::make_pair<abi_serializer::unpack_function, abi_serializer::pack_function>(
         []( fc::datastream<const char*>& stream, bool is_array, bool is_optional) -> fc::variant  {
            if( is_array )
               return variant_from_stream<vector<T>>(stream);
            else if ( is_optional )
               return variant_from_stream<optional<T>>(stream);
            return variant_from_stream<T>(stream);
         },
         []( const fc::variant& var, fc::datastream<char*>& ds, bool is_array, bool is_optional ){
            if( is_array )
               fc::raw::pack( ds, var.as<vector<T>>() );
            else if ( is_optional )
               fc::raw::pack( ds, var.as<optional<T>>() );
            else
               fc::raw::pack( ds,  var.as<T>());
         }
      );
   }

   abi_serializer::abi_serializer( const abi_def& abi ) {
      configure_built_in_types();
      set_abi(abi);
   }

   void abi_serializer::configure_built_in_types() {
      //public_key.hpp
      built_in_types.emplace("public_key",                pack_unpack<public_key_type>());

      //symbol.hpp
      built_in_types.emplace("symbol",                    pack_unpack<symbol>());
      built_in_types.emplace("symbol_code",               pack_unpack<symbol_code>());

      //asset.hpp
      built_in_types.emplace("asset",                     pack_unpack<asset>());
M
Matias Romeo 已提交
66
      built_in_types.emplace("extended_asset",            pack_unpack<extended_asset>());
D
Daniel Larimer 已提交
67 68 69 70

      //native.hpp
      built_in_types.emplace("string",                    pack_unpack<string>());
      built_in_types.emplace("clause_pair",               pack_unpack<clause_pair>());
B
Bucky Kittinger 已提交
71 72 73
      built_in_types.emplace("time_point",                pack_unpack<fc::time_point>());
      built_in_types.emplace("time_point_sec",            pack_unpack<fc::time_point_sec>());
      built_in_types.emplace("block_timestamp_type",      pack_unpack<block_timestamp_type>());
D
Daniel Larimer 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
      built_in_types.emplace("signature",                 pack_unpack<signature_type>());
      built_in_types.emplace("checksum160",               pack_unpack<checksum160_type>());
      built_in_types.emplace("checksum256",               pack_unpack<checksum256_type>());
      built_in_types.emplace("checksum512",               pack_unpack<checksum512_type>());
      built_in_types.emplace("transaction_id_type",       pack_unpack<checksum256_type>());
      built_in_types.emplace("field_name",                pack_unpack<field_name>());
      built_in_types.emplace("fixed_string32",            pack_unpack<fixed_string32>());
      built_in_types.emplace("fixed_string16",            pack_unpack<fixed_string16>());
      built_in_types.emplace("type_name",                 pack_unpack<type_name>());
      built_in_types.emplace("bytes",                     pack_unpack<bytes>());
      built_in_types.emplace("uint128",                   pack_unpack<boost::multiprecision::uint128_t>());
      built_in_types.emplace("uint256",                   pack_unpack<boost::multiprecision::uint256_t>());
      built_in_types.emplace("varuint32",                 pack_unpack<fc::unsigned_int>());
87
      built_in_types.emplace("bool",                      pack_unpack<uint8_t>());
D
Daniel Larimer 已提交
88 89 90 91
      built_in_types.emplace("int8",                      pack_unpack<int8_t>());
      built_in_types.emplace("int16",                     pack_unpack<int16_t>());
      built_in_types.emplace("int32",                     pack_unpack<int32_t>());
      built_in_types.emplace("int64",                     pack_unpack<int64_t>());
92 93 94 95
      built_in_types.emplace("uint8",                     pack_unpack<uint8_t>());
      built_in_types.emplace("uint16",                    pack_unpack<uint16_t>());
      built_in_types.emplace("uint32",                    pack_unpack<uint32_t>());
      built_in_types.emplace("uint64",                    pack_unpack<uint64_t>());
D
Daniel Larimer 已提交
96 97 98 99 100 101 102
      built_in_types.emplace("varint32",                  pack_unpack<fc::signed_int>());
      built_in_types.emplace("float64",                   pack_unpack<double>());
      built_in_types.emplace("name",                      pack_unpack<name>());
      built_in_types.emplace("account_name",              pack_unpack<account_name>());
      built_in_types.emplace("permission_name",           pack_unpack<permission_name>());
      built_in_types.emplace("action_name",               pack_unpack<action_name>());
      built_in_types.emplace("scope_name",                pack_unpack<scope_name>());
103
      built_in_types.emplace("permission_level",          pack_unpack<permission_level>());
D
Daniel Larimer 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
      built_in_types.emplace("producer_schedule",         pack_unpack<producer_schedule_type>());
      built_in_types.emplace("newaccount",                pack_unpack<newaccount>());
   }

   void abi_serializer::set_abi(const abi_def& abi) {
      typedefs.clear();
      structs.clear();
      actions.clear();
      tables.clear();

      for( const auto& st : abi.structs )
         structs[st.name] = st;

      for( const auto& td : abi.types ) {
         FC_ASSERT(is_type(td.type), "invalid type", ("type",td.type));
         typedefs[td.new_type_name] = td.type;
      }

      for( const auto& a : abi.actions )
         actions[a.name] = a.type;

      for( const auto& t : abi.tables )
         tables[t.name] = t.type;

      /**
       *  The ABI vector may contain duplicates which would make it
       *  an invalid ABI
       */
      FC_ASSERT( typedefs.size() == abi.types.size() );
      FC_ASSERT( structs.size() == abi.structs.size() );
      FC_ASSERT( actions.size() == abi.actions.size() );
      FC_ASSERT( tables.size() == abi.tables.size() );
   }

   bool abi_serializer::is_builtin_type(const type_name& type)const {
      return built_in_types.find(type) != built_in_types.end();
   }

   bool abi_serializer::is_integer(const type_name& type) const {
      string stype = type;
      return boost::starts_with(stype, "uint") || boost::starts_with(stype, "int");
   }

   int abi_serializer::get_integer_size(const type_name& type) const {
      string stype = type;
      FC_ASSERT( is_integer(type), "${stype} is not an integer type", ("stype",stype));
      if( boost::starts_with(stype, "uint") ) {
         return boost::lexical_cast<int>(stype.substr(4));
      } else {
         return boost::lexical_cast<int>(stype.substr(3));
      }
   }

   bool abi_serializer::is_struct(const type_name& type)const {
      return structs.find(resolve_type(type)) != structs.end();
   }

   bool abi_serializer::is_array(const type_name& type)const {
      return ends_with(string(type), "[]");
   }

   bool abi_serializer::is_optional(const type_name& type)const {
      return ends_with(string(type), "?");
   }

   type_name abi_serializer::fundamental_type(const type_name& type)const {
      if( is_array(type) ) {
         return type_name(string(type).substr(0, type.size()-2));
      } else if ( is_optional(type) ) {
         return type_name(string(type).substr(0, type.size()-1));
      } else {
       return type;
      }
   }

   bool abi_serializer::is_type(const type_name& rtype)const {
      auto type = fundamental_type(rtype);
      if( built_in_types.find(type) != built_in_types.end() ) return true;
      if( typedefs.find(type) != typedefs.end() ) return is_type(typedefs.find(type)->second);
      if( structs.find(type) != structs.end() ) return true;
      return false;
   }

   const struct_def& abi_serializer::get_struct(const type_name& type)const {
      auto itr = structs.find(resolve_type(type) );
      FC_ASSERT( itr != structs.end(), "Unknown struct ${type}", ("type",type) );
      return itr->second;
   }

   void abi_serializer::validate()const {
      for( const auto& t : typedefs ) { try {
         vector<type_name> types_seen{t.first, t.second};
         auto itr = typedefs.find(t.second);
         while( itr != typedefs.end() ) {
            FC_ASSERT( find(types_seen.begin(), types_seen.end(), itr->second) == types_seen.end(), "Circular reference in type ${type}", ("type",t.first) );
            types_seen.emplace_back(itr->second);
            itr = typedefs.find(itr->second);
         }
      } FC_CAPTURE_AND_RETHROW( (t) ) }
      for( const auto& t : typedefs ) { try {
         FC_ASSERT(is_type(t.second), "", ("type",t.second) );
      } FC_CAPTURE_AND_RETHROW( (t) ) }
      for( const auto& s : structs ) { try {
         if( s.second.base != type_name() ) {
            struct_def current = s.second;
            vector<type_name> types_seen{current.name};
            while( current.base != type_name() ) {
               const auto& base = get_struct(current.base); //<-- force struct to inherit from another struct
               FC_ASSERT( find(types_seen.begin(), types_seen.end(), base.name) == types_seen.end(), "Circular reference in struct ${type}", ("type",s.second.name) );
               types_seen.emplace_back(base.name);
               current = base;
            }
         }
         for( const auto& field : s.second.fields ) { try {
            FC_ASSERT(is_type(field.type) );
         } FC_CAPTURE_AND_RETHROW( (field) ) }
      } FC_CAPTURE_AND_RETHROW( (s) ) }
      for( const auto& a : actions ) { try {
        FC_ASSERT(is_type(a.second), "", ("type",a.second) );
      } FC_CAPTURE_AND_RETHROW( (a)  ) }

      for( const auto& t : tables ) { try {
        FC_ASSERT(is_type(t.second), "", ("type",t.second) );
      } FC_CAPTURE_AND_RETHROW( (t)  ) }
   }

   type_name abi_serializer::resolve_type(const type_name& type)const  {
      auto itr = typedefs.find(type);
      if( itr != typedefs.end() )
         return resolve_type(itr->second);
      return type;
   }

   void abi_serializer::binary_to_variant(const type_name& type, fc::datastream<const char *>& stream,
                                          fc::mutable_variant_object& obj)const {
      const auto& st = get_struct(type);
      if( st.base != type_name() ) {
         binary_to_variant(resolve_type(st.base), stream, obj);
      }
      for( const auto& field : st.fields ) {
         obj( field.name, binary_to_variant(resolve_type(field.type), stream) );
      }
   }

   fc::variant abi_serializer::binary_to_variant(const type_name& type, fc::datastream<const char *>& stream)const
   {
      type_name rtype = resolve_type(type);
      auto ftype = fundamental_type(rtype);
      auto btype = built_in_types.find(ftype );
      if( btype != built_in_types.end() ) {
         return btype->second.first(stream, is_array(rtype), is_optional(rtype));
      }
      if ( is_array(rtype) ) {
        fc::unsigned_int size;
        fc::raw::unpack(stream, size);
        vector<fc::variant> vars;
        vars.resize(size);
        for (auto& var : vars) {
           var = binary_to_variant(ftype, stream);
        }
        return fc::variant( std::move(vars) );
      } else if ( is_optional(rtype) ) {
        char flag;
        fc::raw::unpack(stream, flag);
        return flag ? binary_to_variant(ftype, stream) : fc::variant();
      }

      fc::mutable_variant_object mvo;
      binary_to_variant(rtype, stream, mvo);
      return fc::variant( std::move(mvo) );
   }

   fc::variant abi_serializer::binary_to_variant(const type_name& type, const bytes& binary)const{
      fc::datastream<const char*> ds( binary.data(), binary.size() );
      return binary_to_variant(type, ds);
   }

   void abi_serializer::variant_to_binary(const type_name& type, const fc::variant& var, fc::datastream<char *>& ds)const
   { try {
      auto rtype = resolve_type(type);

      auto btype = built_in_types.find(fundamental_type(rtype));
      if( btype != built_in_types.end() ) {
         btype->second.second(var, ds, is_array(rtype), is_optional(rtype));
      } else if ( is_array(rtype) ) {
         vector<fc::variant> vars = var.get_array();
         fc::raw::pack(ds, (fc::unsigned_int)vars.size());
         for (const auto& var : vars) {
           variant_to_binary(fundamental_type(rtype), var, ds);
         }
      } else {
         const auto& st = get_struct(rtype);

         if( var.is_object() ) {
            const auto& vo = var.get_object();

            if( st.base != type_name() ) {
               variant_to_binary(resolve_type(st.base), var, ds);
            }
            for( const auto& field : st.fields ) {
               if( vo.contains( string(field.name).c_str() ) ) {
                  variant_to_binary(field.type, vo[field.name], ds);
               }
               else {
                  variant_to_binary(field.type, fc::variant(), ds);
                  /// TODO: default construct field and write it out
                  FC_THROW( "Missing '${f}' in variant object", ("f",field.name) );
               }
            }
         } else if( var.is_array() ) {
            const auto& va = var.get_array();

            FC_ASSERT( st.base == type_name(), "support for base class as array not yet implemented" );
            /*if( st.base != type_name() ) {
               variant_to_binary(resolve_type(st.base), var, ds);
            }
            */
            uint32_t i = 0;
            if (va.size() > 0) {
               for( const auto& field : st.fields ) {
                  if( va.size() > i )
                     variant_to_binary(field.type, va[i], ds);
                  else
                     variant_to_binary(field.type, fc::variant(), ds);
                  ++i;
               }
            }
         }
      }
   } FC_CAPTURE_AND_RETHROW( (type)(var) ) }

   bytes abi_serializer::variant_to_binary(const type_name& type, const fc::variant& var)const { try {
      if( !is_type(type) ) {
         return var.as<bytes>();
      }

      bytes temp( 1024*1024 );
      fc::datastream<char*> ds(temp.data(), temp.size() );
      variant_to_binary(type, var, ds);
      temp.resize(ds.tellp());
      return temp;
   } FC_CAPTURE_AND_RETHROW( (type)(var) ) }

   type_name abi_serializer::get_action_type(name action)const {
      auto itr = actions.find(action);
      if( itr != actions.end() ) return itr->second;
      return type_name();
   }
   type_name abi_serializer::get_table_type(name action)const {
      auto itr = tables.find(action);
      if( itr != tables.end() ) return itr->second;
      return type_name();
   }

} }