Skip to content

Commit

Permalink
Merge pull request #710 from eosnetworkfoundation/elmato/cache-front-…
Browse files Browse the repository at this point in the history
…block-queue

Add queue front block in runtime configuration
  • Loading branch information
elmato committed May 10, 2024
2 parents 3c80d1b + b9e821b commit 9cdedae
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/evm_runtime/config_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ struct config_wrapper {
uint64_t get_minimum_natively_representable() const;

private:
void set_queue_front_block(uint32_t block_num);

bool is_dirty()const;
void set_dirty();
void clear_dirty();
Expand Down
4 changes: 2 additions & 2 deletions include/evm_runtime/tables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ struct [[eosio::table]] [[eosio::contract("evm_contract")]] config
uint32_t status = 0; // <- bit mask values from status_flags
binary_extension<evm_version_type> evm_version;
binary_extension<consensus_parameter_type> consensus_parameter;

binary_extension<eosio::name> token_contract; // <- default(unset) means eosio.token
binary_extension<uint32_t> queue_front_block;

EOSLIB_SERIALIZE(config, (version)(chainid)(genesis_time)(ingress_bridge_fee)(gas_price)(miner_cut)(status)(evm_version)(consensus_parameter)(token_contract));
EOSLIB_SERIALIZE(config, (version)(chainid)(genesis_time)(ingress_bridge_fee)(gas_price)(miner_cut)(status)(evm_version)(consensus_parameter)(token_contract)(queue_front_block));
};

struct [[eosio::table]] [[eosio::contract("evm_contract")]] price_queue
Expand Down
20 changes: 18 additions & 2 deletions src/config_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ config_wrapper::config_wrapper(eosio::name self) : _self(self), _config(self, se
_cached_config.token_contract = default_token_account;
// Don't set dirty because action can be read-only.
}
if (!_cached_config.queue_front_block.has_value()) {
_cached_config.queue_front_block = 0;
}
}

config_wrapper::~config_wrapper() {
Expand Down Expand Up @@ -105,19 +108,32 @@ void config_wrapper::enqueue_gas_price(uint64_t gas_price) {
el.price = gas_price;
});

if( _cached_config.queue_front_block.value() == 0 ) {
set_queue_front_block(activation_block_num);
}
}

void config_wrapper::set_queue_front_block(uint32_t block_num) {
_cached_config.queue_front_block = block_num;
set_dirty();
}

void config_wrapper::process_price_queue() {
eosevm::block_mapping bm(get_genesis_time().sec_since_epoch());
auto current_block_num = bm.timestamp_to_evm_block_num(get_current_time().time_since_epoch().count());

auto queue_front_block = _cached_config.queue_front_block.value();
if( queue_front_block == 0 || current_block_num < queue_front_block ) {
return;
}

price_queue_table queue(_self, _self.value);
auto it = queue.begin();
while( it != queue.end() && current_block_num >= it->block ) {
set_gas_price(it->price);
it = queue.erase(it);
set_queue_front_block(it != queue.end() ? it->block : 0);
}

}

uint32_t config_wrapper::get_miner_cut()const {
Expand Down Expand Up @@ -312,4 +328,4 @@ uint64_t config_wrapper::get_minimum_natively_representable() const {
return pow10_const(evm_precision - _cached_config.ingress_bridge_fee.symbol.precision());
}

} //namespace evm_runtime
} //namespace evm_runtime
10 changes: 10 additions & 0 deletions tests/basic_evm_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ namespace fc { namespace raw {
fc::raw::unpack(ds, consensus_parameter);
tmp.consensus_parameter.emplace(consensus_parameter);
}
if(ds.remaining()) {
name token_contract;
fc::raw::unpack(ds, token_contract);
tmp.token_contract.emplace(token_contract);
}
if(ds.remaining()) {
uint32_t queue_front_block;
fc::raw::unpack(ds, queue_front_block);
tmp.queue_front_block.emplace(queue_front_block);
}
} FC_RETHROW_EXCEPTIONS(warn, "error unpacking partial_account_table_row") }
}}

Expand Down
2 changes: 2 additions & 0 deletions tests/basic_evm_tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ struct config_table_row
uint32_t status;
std::optional<evm_version_type> evm_version;
std::optional<consensus_parameter_type> consensus_parameter;
std::optional<name> token_contract;
std::optional<uint32_t> queue_front_block;
};

struct config2_table_row
Expand Down
14 changes: 14 additions & 0 deletions tests/gas_fee_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ try {
init();

auto cfg = get_config();
BOOST_CHECK_EQUAL(cfg.queue_front_block.value(), 0);

eosevm::block_mapping bm(cfg.genesis_time.sec_since_epoch());

Expand Down Expand Up @@ -342,6 +343,9 @@ try {
BOOST_CHECK_EQUAL(q[0].block, b1);
BOOST_CHECK_EQUAL(q[0].price, ten_gwei);

cfg = get_config();
BOOST_CHECK_EQUAL(cfg.queue_front_block.value(), b1);

produce_blocks(100);

// Queue change of gas_price to 30Gwei
Expand All @@ -356,6 +360,9 @@ try {
BOOST_CHECK_EQUAL(q[1].block, b2);
BOOST_CHECK_EQUAL(q[1].price, 3*ten_gwei);

cfg = get_config();
BOOST_CHECK_EQUAL(cfg.queue_front_block.value(), b1);

// Overwrite queue change (same block) 20Gwei
setfeeparams({.gas_price = 2*ten_gwei});

Expand All @@ -366,6 +373,9 @@ try {
BOOST_CHECK_EQUAL(q[1].block, b2);
BOOST_CHECK_EQUAL(q[1].price, 2*ten_gwei);

cfg = get_config();
BOOST_CHECK_EQUAL(cfg.queue_front_block.value(), b1);

while(bm.timestamp_to_evm_block_num(control->pending_block_time().time_since_epoch().count()) != b1) {
produce_blocks(1);
}
Expand All @@ -379,6 +389,8 @@ try {
BOOST_CHECK_EQUAL(q[0].block, b2);
BOOST_CHECK_EQUAL(q[0].price, 2*ten_gwei);

BOOST_CHECK_EQUAL(cfg.queue_front_block.value(), b2);

while(bm.timestamp_to_evm_block_num(control->pending_block_time().time_since_epoch().count()) != b2) {
produce_blocks(1);
}
Expand All @@ -389,6 +401,8 @@ try {

q = get_price_queue();
BOOST_CHECK_EQUAL(q.size(), 0);

BOOST_CHECK_EQUAL(cfg.queue_front_block.value(), 0);
}
FC_LOG_AND_RETHROW()

Expand Down

0 comments on commit 9cdedae

Please sign in to comment.