Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Add get types api #30

Merged
merged 2 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ add_library( eos_chain
block_log.cpp
BlockchainConfiguration.cpp

types.cpp

${HEADERS}
)

Expand Down
3 changes: 3 additions & 0 deletions libraries/chain/include/eos/chain/type_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace eos { namespace chain {
TypeName base;
shared_vector<Field> fields;
};
using type_id_type = type_object::id_type;

struct by_scope_name;
using type_index = chainbase::shared_multi_index_container<
Expand All @@ -57,4 +58,6 @@ namespace eos { namespace chain {

CHAINBASE_SET_INDEX_TYPE(eos::chain::type_object, eos::chain::type_index)

FC_REFLECT(chainbase::oid<eos::chain::type_object>, (_id))

FC_REFLECT(eos::chain::type_object, (id)(scope)(name)(base_scope)(base)(fields) )
5 changes: 5 additions & 0 deletions libraries/chain/include/eos/chain/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ namespace eos { namespace chain {

} } // eos::chain

namespace fc {
void to_variant(const eos::chain::shared_vector<eos::types::Field>& c, fc::variant& v);
void from_variant(const fc::variant& v, eos::chain::shared_vector<eos::types::Field>& fields);
}

FC_REFLECT_ENUM(eos::chain::object_type,
(null_object_type)
(account_object_type)
Expand Down
41 changes: 41 additions & 0 deletions libraries/chain/types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2017, Respective Authors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <eos/chain/types.hpp>

namespace fc {
using eos::chain::shared_vector;
void to_variant(const shared_vector<eos::types::Field>& c, fc::variant& v) {
fc::mutable_variant_object mvo; mvo.reserve(c.size());
for(const auto& f : c) {
mvo.set(f.name, eos::types::String(f.type));
}
v = std::move(mvo);
}
void from_variant(const fc::variant& v, shared_vector<eos::types::Field>& fields) {
const auto& obj = v.get_object();
fields.reserve(obj.size());
for(const auto& f : obj)
fields.emplace_back(eos::types::Field{ f.key(), f.value().get_string() });
}
}
1 change: 1 addition & 0 deletions plugins/chain_api_plugin/chain_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void chain_api_plugin::plugin_startup() {
app().get_plugin<http_plugin>().add_api({
CHAIN_RO_CALL(get_info),
CHAIN_RO_CALL(get_block),
CHAIN_RO_CALL(get_types),
CHAIN_RW_CALL(push_block),
CHAIN_RW_CALL(push_transaction)
});
Expand Down
20 changes: 20 additions & 0 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <eos/native_contract/native_contract_chain_initializer.hpp>

#include <fc/io/json.hpp>
#include <fc/variant.hpp>

namespace eos {

Expand All @@ -16,6 +17,8 @@ using fc::flat_map;
using chain::block_id_type;
using chain::fork_database;
using chain::block_log;
using chain::type_index;
using chain::by_scope_name;

class chain_plugin_impl {
public:
Expand Down Expand Up @@ -162,6 +165,23 @@ read_only::get_block_results read_only::get_block(const read_only::get_block_par
"Could not find block: ${block}", ("block", params.block_num_or_id));
}

read_only::get_types_results read_only::get_types(const get_types_params& params) const {

auto& _db = app().get_plugin<database_plugin>().db();
auto& index = _db.get_index<type_index, by_scope_name>();
auto range = index.equal_range( boost::make_tuple( params.account_name ) );

get_types_results res;

for( const auto& to : boost::make_iterator_range( range.first, range.second ) ) {
fc::variant v;
fc::to_variant(to, v);
res.emplace_back(v);
}

return res;
}

read_write::push_block_results read_write::push_block(const read_write::push_block_params& params) {
db.push_block(params);
return read_write::push_block_results();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <eos/database_plugin/database_plugin.hpp>

namespace fc { class variant; }

namespace eos {
using eos::chain::chain_controller;
using std::unique_ptr;
Expand Down Expand Up @@ -35,6 +37,12 @@ class read_only {
};
using get_block_results = chain::signed_block;
get_block_results get_block(const get_block_params& params) const;

struct get_types_params {
string account_name;
};
using get_types_results = std::vector<fc::variant>;
get_types_results get_types(const get_types_params& params) const;
};

class read_write {
Expand Down Expand Up @@ -89,3 +97,4 @@ FC_REFLECT(eos::chain_apis::read_only::get_info_results,
(head_block_num)(head_block_id)(head_block_time)(head_block_producer)
(recent_slots)(participation_rate))
FC_REFLECT(eos::chain_apis::read_only::get_block_params, (block_num_or_id))
FC_REFLECT(eos::chain_apis::read_only::get_types_params, (account_name))