Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lupin012 committed May 8, 2024
1 parent c8dfc90 commit fca0c8d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
5 changes: 2 additions & 3 deletions silkworm/rpc/types/call.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct Call {
std::optional<uint64_t> nonce;
AccessList access_list;

[[nodiscard]] silkworm::Transaction to_transaction(const std::optional<intx::uint256>& override_gas_price = std::nullopt,
[[nodiscard]] silkworm::Transaction to_transaction(const std::optional<intx::uint256>& override_gas_price = std::nullopt,
const std::optional<AccessList>& override_access_list = std::nullopt,
const std::optional<uint64_t> override_nonce = std::nullopt) const {
silkworm::Transaction txn{};
Expand All @@ -55,8 +55,7 @@ struct Call {
txn.to = to;
if (override_nonce) {
txn.nonce = *override_nonce;
}
else if (nonce) {
} else if (nonce) {
txn.nonce = *nonce;
}

Expand Down
40 changes: 40 additions & 0 deletions silkworm/rpc/types/call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,44 @@ TEST_CASE("create call with AccessList", "[rpc][types][call]") {
CHECK(txn.access_list == access_list);
}

TEST_CASE("create call with no AccessList and pass it to_transaction", "[rpc][types][call]") {
Call call{
std::nullopt,
std::nullopt,
235, // gas
21000, // gas_price
std::nullopt, // max_priority_fee_per_gas
std::nullopt, // max_fee_per_gas
31337, // value
{}, // data
1}; // nonce
silkworm::Transaction txn = call.to_transaction(std::nullopt, access_list);
CHECK(txn.gas_limit == 235);
CHECK(txn.max_fee_per_gas == 21000);
CHECK(txn.max_priority_fee_per_gas == 21000);
CHECK(txn.nonce == 1);
CHECK(!txn.access_list.empty());
CHECK(txn.access_list == access_list);
}

TEST_CASE("create call with no nonce and pass it to_transaction", "[rpc][types][call]") {
uint64_t nonce = 5;
Call call{
std::nullopt,
std::nullopt,
235, // gas
21000, // gas_price
std::nullopt, // max_priority_fee_per_gas
std::nullopt, // max_fee_per_gas
31337, // value
{}, // data
std::nullopt}; // nonce
silkworm::Transaction txn = call.to_transaction(std::nullopt, std::nullopt, nonce);
CHECK(txn.gas_limit == 235);
CHECK(txn.max_fee_per_gas == 21000);
CHECK(txn.max_priority_fee_per_gas == 21000);
CHECK(txn.nonce == nonce);
CHECK(txn.access_list.empty());
}

} // namespace silkworm::rpc

0 comments on commit fca0c8d

Please sign in to comment.