Skip to content

Commit

Permalink
Add tests for gas price queue processing
Browse files Browse the repository at this point in the history
  • Loading branch information
elmato committed Apr 18, 2024
1 parent 9dd689c commit 51f6cdf
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 3 deletions.
10 changes: 10 additions & 0 deletions tests/basic_evm_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ namespace fc { namespace raw {
fc::raw::unpack(ds, version);
tmp.evm_version.emplace(version);
}
if(ds.remaining()) {
evm_test::consensus_parameter_type consensus_parameter;
fc::raw::unpack(ds, consensus_parameter);
tmp.consensus_parameter.emplace(consensus_parameter);
}
if(ds.remaining()) {
std::deque<evm_test::price_time> base_price_queue;
fc::raw::unpack(ds, base_price_queue);
tmp.base_price_queue.emplace(base_price_queue);
}
} FC_RETHROW_EXCEPTIONS(warn, "error unpacking partial_account_table_row") }
}}

Expand Down
41 changes: 39 additions & 2 deletions tests/basic_evm_tester.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once

#include <deque>
#include <eosio/chain/abi_serializer.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/fixed_bytes.hpp>
Expand Down Expand Up @@ -66,7 +66,36 @@ struct evm_version_type {
std::optional<pending> pending_version;
uint64_t cached_version=0;
};
struct gas_parameter_type {
uint64_t gas_txnewaccount = 0;
uint64_t gas_newaccount = 25000;
uint64_t gas_txcreate = 32000;
uint64_t gas_codedeposit = 200;
uint64_t gas_sset = 20000;
};
struct consensus_parameter_data_v0 {
gas_parameter_type gas_parameter;
};
using consensus_parameter_data_type = std::variant<consensus_parameter_data_v0>;
struct pending_consensus_parameter_data_type {
consensus_parameter_data_type data;
fc::time_point pending_time;
};
struct consensus_parameter_type {
consensus_parameter_data_type current;
std::optional<pending_consensus_parameter_data_type> pending;
};
struct price_time {
uint64_t price;
fc::time_point time;
bool operator==(const price_time& o) const { return price == o.price && time == o.time; }
};

inline std::ostream& operator<<(std::ostream& ds, const price_time& pt)
{
ds << "{" << pt.price << "," << pt.time.to_iso_string() << "}";
return ds;
}
struct config_table_row
{
unsigned_int version;
Expand All @@ -77,6 +106,8 @@ struct config_table_row
uint32_t miner_cut;
uint32_t status;
std::optional<evm_version_type> evm_version;
std::optional<consensus_parameter_type> consensus_parameter;
std::optional<std::deque<price_time>> base_price_queue;
};

struct config2_table_row
Expand Down Expand Up @@ -179,7 +210,6 @@ using bridge_message = std::variant<bridge_message_v0>;
} // namespace evm_test


FC_REFLECT(evm_test::config_table_row, (version)(chainid)(genesis_time)(ingress_bridge_fee)(gas_price)(miner_cut)(status)(evm_version))
FC_REFLECT(evm_test::evm_version_type, (pending_version)(cached_version))
FC_REFLECT(evm_test::evm_version_type::pending, (version)(time))
FC_REFLECT(evm_test::config2_table_row,(next_account_id))
Expand All @@ -198,6 +228,12 @@ FC_REFLECT(evm_test::gcstore, (id)(storage_id));
FC_REFLECT(evm_test::account_code, (id)(ref_count)(code)(code_hash));
FC_REFLECT(evm_test::evmtx_v0, (eos_evm_version)(rlptx)(base_fee_per_gas));

FC_REFLECT(evm_test::price_time, (price)(time));
FC_REFLECT(evm_test::consensus_parameter_type, (current)(pending));
FC_REFLECT(evm_test::pending_consensus_parameter_data_type, (data)(pending_time));
FC_REFLECT(evm_test::consensus_parameter_data_v0, (gas_parameter));
FC_REFLECT(evm_test::gas_parameter_type, (gas_txnewaccount)(gas_newaccount)(gas_txcreate)(gas_codedeposit)(gas_sset));

namespace evm_test {
class evm_eoa
{
Expand Down Expand Up @@ -353,6 +389,7 @@ class basic_evm_tester : public evm_validating_tester
static constexpr uint64_t suggested_gas_price = 150'000'000'000; // 150 gwei
static constexpr uint32_t suggested_miner_cut = 10'000; // 10%
static constexpr uint64_t suggested_ingress_bridge_fee_amount = 70; // 0.0070 EOS
static constexpr uint64_t price_queue_grace_period = 180; // 180 seconds

const symbol native_symbol;

Expand Down
77 changes: 77 additions & 0 deletions tests/gas_fee_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,81 @@ try {
}
FC_LOG_AND_RETHROW()


BOOST_FIXTURE_TEST_CASE(set_gas_price_v1, gas_fee_evm_tester)
try {
init();

setversion(1, evm_account_name);
produce_blocks(2);

const auto ten_gwei = 10'000'000'000ull;

// Queue change of gas_price to 10Gwei
setfeeparams({.gas_price = ten_gwei});

auto conf1 = get_config();
BOOST_REQUIRE(conf1.base_price_queue.has_value());

auto& q = conf1.base_price_queue.value();

auto gp1 = price_time{
.price=ten_gwei,
.time=control->pending_block_time()+fc::seconds(price_queue_grace_period)
};

BOOST_CHECK_EQUAL(q.size(), 1);
BOOST_CHECK_EQUAL(q.front(), gp1);

produce_blocks(2);

// different time, same price => no effect
setfeeparams({.gas_price = ten_gwei});
conf1 = get_config();;
q = conf1.base_price_queue.value();
BOOST_CHECK_EQUAL(q.size(), 1);
BOOST_CHECK_EQUAL(q.front(), gp1);

produce_blocks(2);

// different time, different price => [ok]
setfeeparams({.gas_price = ten_gwei+1});
conf1 = get_config();;
q = conf1.base_price_queue.value();
auto gp2 = price_time{
.price=ten_gwei+1,
.time=control->pending_block_time()+fc::seconds(price_queue_grace_period)
};

BOOST_CHECK_EQUAL(q.size(), 2);
BOOST_CHECK_EQUAL(q.front(), gp1);
BOOST_CHECK_EQUAL(q.back(), gp2);

// Process price queue
conf1 = get_config();
BOOST_CHECK_EQUAL(conf1.gas_price, suggested_gas_price);

auto trigger_price_queue_processing = [&](){
transfer_token("alice"_n, evm_account_name, make_asset(1), evm_account_name.to_string());
};

while(control->pending_block_time() != gp1.time) {
produce_blocks(1);
}
trigger_price_queue_processing();
conf1 = get_config();
BOOST_CHECK_EQUAL(conf1.gas_price, gp1.price);

while(control->pending_block_time() != gp2.time) {
produce_blocks(1);
}
trigger_price_queue_processing();
conf1 = get_config();
BOOST_CHECK_EQUAL(conf1.gas_price, gp2.price);

q = conf1.base_price_queue.value();
BOOST_CHECK_EQUAL(q.size(), 0);
}
FC_LOG_AND_RETHROW()

BOOST_AUTO_TEST_SUITE_END()
10 changes: 9 additions & 1 deletion tests/gas_param_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,17 @@ BOOST_FIXTURE_TEST_CASE(basic, gas_param_evm_tester) try {
eosio_assert_message_exception,
eosio_assert_message_is("gas_sset too small"));

// This change in the gas price now takes effect immediately
// Change in the gas parameters now takes effect immediately, but not gas price
updtgasparam(asset(10'0000, native_symbol), 1'000'000'000, evm_account_name);

auto t0 = control->pending_block_time() + fc::seconds(price_queue_grace_period);
while(control->pending_block_time() != t0) {
produce_blocks(1);
}

// Process price queue
transfer_token("alice"_n, evm_account_name, make_asset(1), evm_account_name.to_string());

setgasparam(21000,21000,21000,21000,2900, evm_account_name);

{
Expand Down

0 comments on commit 51f6cdf

Please sign in to comment.