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

Fix payment_queryInfo #2188

Merged
merged 15 commits into from
Aug 21, 2024
7 changes: 7 additions & 0 deletions core/api/jrpc/value_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ namespace kagome::api {
return ret;
}

inline jsonrpc::Value makeValue(const primitives::Weight &val) {
jStruct data;
data["ref_time"] = static_cast<int64_t>(*val.ref_time);
data["proof_size"] = static_cast<int64_t>(*val.proof_size);
return data;
}

inline jsonrpc::Value makeValue(const primitives::OldWeight &val) {
jsonrpc::Value ret(static_cast<int64_t>(*val));
return ret;
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/payment/impl/payment_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace kagome::api {
BOOST_ASSERT(block_tree_);
}

outcome::result<primitives::RuntimeDispatchInfo<primitives::OldWeight>>
outcome::result<primitives::RuntimeDispatchInfo<primitives::Weight>>
PaymentApiImpl::queryInfo(const primitives::Extrinsic &extrinsic,
uint32_t len,
OptionalHashRef at) const {
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/payment/impl/payment_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace kagome::api {
std::shared_ptr<const blockchain::BlockTree> block_tree);
~PaymentApiImpl() override = default;

outcome::result<primitives::RuntimeDispatchInfo<primitives::OldWeight>>
outcome::result<primitives::RuntimeDispatchInfo<primitives::Weight>>
queryInfo(const primitives::Extrinsic &extrinsic,
uint32_t len,
OptionalHashRef at) const override;
Expand Down
3 changes: 1 addition & 2 deletions core/api/service/payment/payment_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ namespace kagome::api {

virtual ~PaymentApi() = default;

virtual outcome::result<
primitives::RuntimeDispatchInfo<primitives::OldWeight>>
virtual outcome::result<primitives::RuntimeDispatchInfo<primitives::Weight>>
queryInfo(const primitives::Extrinsic &extrinsic,
uint32_t len,
OptionalHashRef at) const = 0;
Expand Down
14 changes: 7 additions & 7 deletions core/api/service/payment/requests/query_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ namespace kagome::api::payment::request {

class QueryInfo final
: public details::RequestType<
primitives::RuntimeDispatchInfo<primitives::OldWeight>,
primitives::RuntimeDispatchInfo<primitives::Weight>,
std::string,
std::string> {
std::optional<std::string>> {
public:
explicit QueryInfo(std::shared_ptr<PaymentApi> api) : api_(std::move(api)) {
BOOST_ASSERT(api_);
};
}

outcome::result<primitives::RuntimeDispatchInfo<primitives::OldWeight>>
outcome::result<primitives::RuntimeDispatchInfo<primitives::Weight>>
execute() override {
auto ext_hex = getParam<0>();
OUTCOME_TRY(ext_bytes, common::unhexWith0x(ext_hex));

OUTCOME_TRY(extrinsic, scale::decode<primitives::Extrinsic>(ext_bytes));

auto at_hex = getParam<1>();
if (at_hex.empty()) {
auto at_hex_opt = getParam<1>();
if (not at_hex_opt.has_value()) {
return api_->queryInfo(extrinsic, ext_bytes.size(), std::nullopt);
}
common::Hash256 at_hash;
OUTCOME_TRY(at, common::unhexWith0x(at_hex));
OUTCOME_TRY(at, common::unhexWith0x(at_hex_opt.value()));
BOOST_ASSERT(at.size() == common::Hash256::size());
std::copy_n(at.cbegin(), common::Hash256::size(), at_hash.begin());
return api_->queryInfo(extrinsic, ext_bytes.size(), std::cref(at_hash));
Expand Down
4 changes: 2 additions & 2 deletions core/api/transport/impl/ws/ws_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/config.hpp>
#include <libp2p/outcome/outcome.hpp>

namespace boost::beast {
template <class NextLayer, class DynamicBuffer>
Expand Down Expand Up @@ -138,6 +137,7 @@ namespace kagome::api {
}

void WsSession::respond(std::string_view response) {
SL_DEBUG(logger_, "Responding: {}", response);
post([self{shared_from_this()}, response{std::string{response}}] {
if (not self->is_ws_) {
self->sessionClose();
Expand Down Expand Up @@ -204,7 +204,7 @@ namespace kagome::api {
sessionMake();

asyncRead();
};
}

void WsSession::onRead(boost::system::error_code ec,
std::size_t bytes_transferred) {
Expand Down
3 changes: 3 additions & 0 deletions core/api/transport/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <boost/signals2/signal.hpp>

#include "api/transport/rpc_io_context.hpp"
#include "log/logger.hpp"

namespace kagome::api {
enum struct SessionType {
Expand Down Expand Up @@ -69,6 +70,7 @@ namespace kagome::api {
*/
void processRequest(std::string_view request,
std::shared_ptr<Session> session) {
SL_DEBUG(logger_, "Processing request: {}", request);
on_request_(request, std::move(session));
}

Expand Down Expand Up @@ -118,6 +120,7 @@ namespace kagome::api {
private:
std::function<OnRequestSignature> on_request_; ///< `on request` callback
OnCloseHandler on_close_;
log::Logger logger_ = log::createLogger("Session", "rpc_transport");
};

} // namespace kagome::api
16 changes: 0 additions & 16 deletions core/host_api/impl/offchain_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ namespace kagome::host_api {
storage_type = StorageType::Persistent;
} else if (kind == 2) {
storage_type = StorageType::Local;
} else if (kind == 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Под шумок

// TODO(xDimon): Remove this if-branch when it will be fixed it substrate
// issue: https://github.com/soramitsu/kagome/issues/997
storage_type = StorageType::Persistent;
} else {
throw std::invalid_argument(
"Method was called with unknown kind of storage");
Expand All @@ -137,10 +133,6 @@ namespace kagome::host_api {
storage_type = StorageType::Persistent;
} else if (kind == 2) {
storage_type = StorageType::Local;
} else if (kind == 0) {
// TODO(xDimon): Remove this if-branch when it will be fixed it substrate
// issue: https://github.com/soramitsu/kagome/issues/997
storage_type = StorageType::Persistent;
} else {
throw std::invalid_argument(
"Method was called with unknown kind of storage");
Expand All @@ -166,10 +158,6 @@ namespace kagome::host_api {
storage_type = StorageType::Persistent;
} else if (kind == 2) {
storage_type = StorageType::Local;
} else if (kind == 0) {
// TODO(xDimon): Remove this if-branch when it will be fixed it substrate
// issue: https://github.com/soramitsu/kagome/issues/997
storage_type = StorageType::Persistent;
} else {
throw std::invalid_argument(
"Method was called with unknown kind of storage");
Expand Down Expand Up @@ -208,10 +196,6 @@ namespace kagome::host_api {
storage_type = StorageType::Persistent;
} else if (kind == 2) {
storage_type = StorageType::Local;
} else if (kind == 0) {
// TODO(xDimon): Remove this if-branch when it will be fixed it substrate
// issue: https://github.com/soramitsu/kagome/issues/997
storage_type = StorageType::Persistent;
} else {
throw std::invalid_argument(
"Method was called with unknown kind of storage");
Expand Down
9 changes: 6 additions & 3 deletions core/primitives/runtime_dispatch_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#pragma once

#include <scale/scale.hpp>
#include <scale/tie.hpp>

#include "common/unused.hpp"
#include "scale/big_fixed_integers.hpp"

Expand All @@ -16,7 +18,9 @@ namespace kagome::primitives {
using OldWeight = scale::Compact<uint64_t>;

struct Weight {
// NOLINTBEGIN
SCALE_TIE(2);
// NOLINTEND
Weight() = default;

explicit Weight(OldWeight w) : ref_time{w}, proof_size{0} {}
Expand Down Expand Up @@ -58,7 +62,6 @@ namespace kagome::primitives {
template <typename Stream,
typename = std::enable_if_t<Stream::is_decoder_stream>>
Stream &operator>>(Stream &stream, DispatchClass &dispatch_class) {
std::ignore = stream.nextByte();
uint8_t dispatch_class_byte;
stream >> dispatch_class_byte;
dispatch_class = static_cast<DispatchClass>(dispatch_class_byte);
Expand All @@ -68,7 +71,7 @@ namespace kagome::primitives {
template <typename Stream,
typename = std::enable_if_t<Stream::is_decoder_stream>>
Stream &operator<<(Stream &stream, DispatchClass dispatch_class) {
return stream << uint8_t{0} << dispatch_class;
return stream << dispatch_class;
}

struct Balance : public scale::Fixed<scale::uint128_t> {};
Expand All @@ -78,7 +81,7 @@ namespace kagome::primitives {
*/
template <typename Weight>
struct RuntimeDispatchInfo {
SCALE_TIE(3)
SCALE_TIE(3); // NOLINT

Weight weight;
DispatchClass dispatch_class;
Expand Down
15 changes: 6 additions & 9 deletions core/runtime/runtime_api/impl/transaction_payment_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ OUTCOME_CPP_DEFINE_CATEGORY(kagome::runtime,
switch (err) {
case E::TRANSACTION_PAYMENT_API_NOT_FOUND:
return "Transaction payment runtime API is not found in the runtime";
case TransactionPaymentApiImpl::Error::API_BELOW_VERSION_2_NOT_SUPPORTED:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case TransactionPaymentApiImpl::Error::API_BELOW_VERSION_2_NOT_SUPPORTED:
case E::API_BELOW_VERSION_2_NOT_SUPPORTED:

return "API below version 2 is not supported";
}
return "Unknown TransactionPaymentApiImpl error";
}
Expand All @@ -32,7 +34,7 @@ namespace kagome::runtime {
BOOST_ASSERT(hasher_);
}

outcome::result<primitives::RuntimeDispatchInfo<primitives::OldWeight>>
outcome::result<primitives::RuntimeDispatchInfo<primitives::Weight>>
TransactionPaymentApiImpl::query_info(const primitives::BlockHash &block,
const primitives::Extrinsic &ext,
uint32_t len) {
Expand All @@ -56,19 +58,14 @@ namespace kagome::runtime {
auto api_version = res->second;
OUTCOME_TRY(ctx, executor_->ctx().ephemeralAt(block));
if (api_version < 2) {
return executor_
->call<primitives::RuntimeDispatchInfo<primitives::OldWeight>>(
ctx, "TransactionPaymentApi_query_info", ext.data, len);
return Error::API_BELOW_VERSION_2_NOT_SUPPORTED;
}
OUTCOME_TRY(
result,
executor_->call<primitives::RuntimeDispatchInfo<primitives::Weight>>(
ctx, "TransactionPaymentApi_query_info", ext.data, len));
primitives::RuntimeDispatchInfo<primitives::OldWeight> old_format_result;
old_format_result.dispatch_class = result.dispatch_class;
old_format_result.partial_fee = result.partial_fee;
old_format_result.weight = result.weight.ref_time;
return old_format_result;

return result;
}

} // namespace kagome::runtime
4 changes: 3 additions & 1 deletion core/runtime/runtime_api/impl/transaction_payment_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ namespace kagome::runtime {
enum class Error {
// Transaction payment runtime API is not found in the runtime
TRANSACTION_PAYMENT_API_NOT_FOUND = 1,
// API below version 2 is not supported (needed for query_info)
API_BELOW_VERSION_2_NOT_SUPPORTED = 2,
};

explicit TransactionPaymentApiImpl(std::shared_ptr<Executor> executor,
std::shared_ptr<Core> core_api,
std::shared_ptr<crypto::Hasher> hasher);

outcome::result<primitives::RuntimeDispatchInfo<primitives::OldWeight>>
outcome::result<primitives::RuntimeDispatchInfo<primitives::Weight>>
query_info(const primitives::BlockHash &block,
const primitives::Extrinsic &ext,
uint32_t len) override;
Expand Down
2 changes: 1 addition & 1 deletion core/runtime/runtime_api/transaction_payment_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace kagome::runtime {
virtual ~TransactionPaymentApi() = default;

virtual outcome::result<
primitives::RuntimeDispatchInfo<primitives::OldWeight>>
primitives::RuntimeDispatchInfo<primitives::Weight>>
query_info(const primitives::BlockHash &block,
const primitives::Extrinsic &ext,
uint32_t len) = 0;
Expand Down
Loading