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

Add apply_discount_factor to gas_parameters #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions lib/evmone/execution_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ struct gas_parameters {
uint64_t G_codedeposit = 200;
uint64_t G_sset = 20000;

static gas_parameters apply_discount_factor(const intx::uint256& inclusion_price, const uint64_t base_fee_per_gas, const uint64_t storage_price, const evmone::gas_parameters& g) {
//storage_gas_discount_factor = (storage_price)/(base_price + inclusion_price)
const intx::uint256 num(storage_price);
const intx::uint256 den(intx::uint256(base_fee_per_gas) + intx::uint256(inclusion_price));
gas_parameters out;
out.G_txnewaccount = static_cast<uint64_t>((num*g.G_txnewaccount)/den);
out.G_newaccount = static_cast<uint64_t>((num*g.G_newaccount)/den);
out.G_txcreate = static_cast<uint64_t>((num*g.G_txcreate)/den);
out.G_codedeposit = static_cast<uint64_t>((num*g.G_codedeposit)/den);
out.G_sset = static_cast<uint64_t>((num*g.G_sset)/den);
return out;
};

const storage_cost_t& get_storage_cost(uint64_t version) {
if(!storage_cost.has_value()) {
storage_cost = generate_storage_cost_table(version);
Expand All @@ -162,7 +175,7 @@ struct gas_parameters {
storage_cost_t st;
if( version >= 3) {
int64_t cpu_gas_to_change_slot = reset - warm_access; //cpu cost of adding or removing or mutating a slot in the db
int64_t storage_gas_to_add_slot = set - reset; //storage cost of adding a new slot into the db
int64_t storage_gas_to_add_slot = set; //storage cost of adding a new slot into the db

st[EVMC_STORAGE_ASSIGNED] = { 0, 0};
st[EVMC_STORAGE_ADDED] = { cpu_gas_to_change_slot, storage_gas_to_add_slot};
Expand Down Expand Up @@ -223,8 +236,8 @@ struct gas_state_t {
int64_t apply_storage_gas_delta(int64_t storage_gas_delta){
if (eos_evm_version_ >= 3) {
int64_t d = storage_gas_delta - storage_gas_refund_;
storage_gas_refund_ = std::max(-d, 0l);
const auto gas_consumed = std::max(d, 0l);
storage_gas_refund_ = std::max(-d, decltype(d){0});
const auto gas_consumed = std::max(d, decltype(d){0});
storage_gas_consumed_ += gas_consumed;
return gas_consumed;
}
Expand All @@ -234,8 +247,8 @@ struct gas_state_t {
int64_t apply_speculative_cpu_gas_delta(int64_t cpu_gas_delta) {
if (eos_evm_version_ >= 3) {
int64_t d = cpu_gas_delta - cpu_gas_refund_;
cpu_gas_refund_ = std::max(-d, 0l);
const auto gas_consumed = std::max(d, 0l);
cpu_gas_refund_ = std::max(-d, decltype(d){0});
const auto gas_consumed = std::max(d, decltype(d){0});
speculative_cpu_gas_consumed_ += gas_consumed;
return gas_consumed;
}
Expand All @@ -262,13 +275,13 @@ struct gas_state_t {
int64_t collapse() {
const auto s = std::min(storage_gas_consumed_, storage_gas_refund_);
const auto x = storage_gas_consumed_ - storage_gas_refund_;
storage_gas_consumed_ = std::max(x, 0l);
storage_gas_refund_ = std::max(-x, 0l);
storage_gas_consumed_ = std::max(x, decltype(x){0});
storage_gas_refund_ = std::max(-x, decltype(x){0});

const auto c = std::min(speculative_cpu_gas_consumed_, cpu_gas_refund_);
const auto y = speculative_cpu_gas_consumed_ - cpu_gas_refund_;
speculative_cpu_gas_consumed_ = std::max(y, 0l);
cpu_gas_refund_ = std::max(-y, 0l);
speculative_cpu_gas_consumed_ = std::max(y, decltype(y){0});
cpu_gas_refund_ = std::max(-y, decltype(y){0});

return s + c;
}
Expand Down