Skip to content

Commit

Permalink
fix cbor test
Browse files Browse the repository at this point in the history
  • Loading branch information
lupin012 committed Mar 31, 2024
1 parent 8f11001 commit 976edef
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
1 change: 0 additions & 1 deletion silkworm/rpc/core/rawdb/chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <silkworm/infra/concurrency/task.hpp>

#include <boost/asio/thread_pool.hpp>

#include <evmc/evmc.hpp>
#include <intx/intx.hpp>
#include <nlohmann/json.hpp>
Expand Down
3 changes: 1 addition & 2 deletions silkworm/rpc/core/receipts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

#pragma once

#include <boost/asio/thread_pool.hpp>

#include <silkworm/infra/concurrency/task.hpp>

#include <boost/asio/thread_pool.hpp>
#include <evmc/evmc.hpp>

#include <silkworm/core/types/block.hpp>
Expand Down
2 changes: 0 additions & 2 deletions silkworm/rpc/ethdb/cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <boost/asio/compose.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/use_awaitable.hpp>

#include <cbor/cbor.h>
#include <cbor/listener.h>
#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -197,7 +196,6 @@ Task<bool> cbor_decode(boost::asio::thread_pool& workers, const silkworm::Bytes&
co_await boost::asio::async_compose<decltype(boost::asio::use_awaitable), void(bool)>(
[&](auto& self) {
boost::asio::post(workers, [&, self = std::move(self)]() mutable {

bool operation_result = true;
auto json = nlohmann::json::from_cbor(bytes);
SILK_TRACE << "cbor_decode<std::vector<Receipt>> json: " << json.dump();
Expand Down
3 changes: 2 additions & 1 deletion silkworm/rpc/ethdb/cbor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

#include <vector>

#include <silkworm/infra/concurrency/task.hpp>

#include <boost/asio/thread_pool.hpp>

#include <silkworm/core/common/util.hpp>
#include <silkworm/infra/concurrency/task.hpp>
#include <silkworm/rpc/types/log.hpp>
#include <silkworm/rpc/types/receipt.hpp>

Expand Down
44 changes: 33 additions & 11 deletions silkworm/rpc/ethdb/cbor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

#include <vector>

#include <boost/asio/co_spawn.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/asio/use_future.hpp>
#include <catch2/catch.hpp>
#include <evmc/evmc.hpp>

Expand Down Expand Up @@ -124,21 +127,30 @@ TEST_CASE("decode logs from incorrect bytes", "[rpc][ethdb][cbor]") {
TEST_CASE("decode receipts from empty bytes", "[rpc][ethdb][cbor]") {
test_util::SetLogVerbosityGuard log_guard{log::Level::kNone};
Receipts receipts{};
CHECK_NOTHROW(cbor_decode(*silkworm::from_hex(""), receipts));
boost::asio::thread_pool pool{1};
boost::asio::thread_pool workers{1};
auto result = boost::asio::co_spawn(pool, cbor_decode(workers, *silkworm::from_hex(""), receipts), boost::asio::use_future);
CHECK_NOTHROW(result.get());
CHECK(receipts.empty());
}

TEST_CASE("decode receipts from empty array", "[rpc][ethdb][cbor]") {
test_util::SetLogVerbosityGuard log_guard{log::Level::kNone};
Receipts receipts{};
CHECK_NOTHROW(cbor_decode(*silkworm::from_hex("80"), receipts));
boost::asio::thread_pool pool{1};
boost::asio::thread_pool workers{1};
auto result = boost::asio::co_spawn(pool, cbor_decode(workers, *silkworm::from_hex("80"), receipts), boost::asio::use_future);
CHECK_NOTHROW(result.get());
CHECK(receipts.empty());
}

TEST_CASE("decode receipts from CBOR 1", "[rpc][ethdb][cbor]") {
test_util::SetLogVerbosityGuard log_guard{log::Level::kNone};
Receipts receipts{};
CHECK_NOTHROW(cbor_decode(*silkworm::from_hex("818400f60101"), receipts));
boost::asio::thread_pool pool{1};
boost::asio::thread_pool workers{1};
auto result = boost::asio::co_spawn(pool, cbor_decode(workers, *silkworm::from_hex("818400f60101"), receipts), boost::asio::use_future);
CHECK_NOTHROW(result.get());
CHECK(receipts.size() == 1);
CHECK(receipts[0].type == 0);
CHECK(receipts[0].success == 1);
Expand All @@ -148,11 +160,14 @@ TEST_CASE("decode receipts from CBOR 1", "[rpc][ethdb][cbor]") {
TEST_CASE("decode receipts from CBOR 2", "[rpc][ethdb][cbor]") {
test_util::SetLogVerbosityGuard log_guard{log::Level::kNone};
Receipts receipts{};
CHECK_NOTHROW(cbor_decode(*silkworm::from_hex(
"82"
"8400f60101"
"8400f60101"),
receipts));
boost::asio::thread_pool pool{1};
boost::asio::thread_pool workers{1};
auto bytes = *silkworm::from_hex(
"82"
"8400f60101"
"8400f60101");
auto result = boost::asio::co_spawn(pool, cbor_decode(workers, bytes, receipts), boost::asio::use_future);
CHECK_NOTHROW(result.get());
CHECK(receipts.size() == 2);
CHECK(receipts[0].type == 0);
CHECK(receipts[0].success == 1);
Expand All @@ -165,8 +180,11 @@ TEST_CASE("decode receipts from CBOR 2", "[rpc][ethdb][cbor]") {
TEST_CASE("decode receipts from CBOR 3", "[rpc][ethdb][cbor]") {
test_util::SetLogVerbosityGuard log_guard{log::Level::kNone};
Receipts receipts{};
boost::asio::thread_pool pool{1};
boost::asio::thread_pool workers{1};
auto bytes = *silkworm::from_hex("838400f601196d398400f6011a00371b0b8400f6011a003947f4");
CHECK_NOTHROW(cbor_decode(bytes, receipts));
auto result = boost::asio::co_spawn(pool, cbor_decode(workers, bytes, receipts), boost::asio::use_future);
CHECK_NOTHROW(result.get());
CHECK(receipts.size() == 3);
CHECK(receipts[0].success == true);
CHECK(receipts[0].cumulative_gas_used == 0x6d39);
Expand All @@ -179,10 +197,14 @@ TEST_CASE("decode receipts from CBOR 3", "[rpc][ethdb][cbor]") {
TEST_CASE("decode receipts from incorrect bytes", "[rpc][ethdb][cbor]") {
test_util::SetLogVerbosityGuard log_guard{log::Level::kNone};
Receipts receipts{};
boost::asio::thread_pool pool{1};
boost::asio::thread_pool workers{1};
const auto b1 = *silkworm::from_hex("81");
CHECK_THROWS(cbor_decode(b1, receipts));
auto result = boost::asio::co_spawn(pool, cbor_decode(workers, b1, receipts), boost::asio::use_future);
CHECK_THROWS(result.get());
const auto b2 = *silkworm::from_hex("83808040");
CHECK_THROWS_MATCHES(cbor_decode(b2, receipts), std::system_error, Message("Receipt CBOR: missing entries: "s + invalidArgumentMessage));
result = boost::asio::co_spawn(pool, cbor_decode(workers, b2, receipts), boost::asio::use_future);
CHECK_THROWS_MATCHES(result.get(), std::system_error, Message("Receipt CBOR: missing entries: "s + invalidArgumentMessage));
}

} // namespace silkworm::rpc

0 comments on commit 976edef

Please sign in to comment.