Skip to content

Commit

Permalink
Merge pull request #425 from ethereum/instruction_metrics
Browse files Browse the repository at this point in the history
instructions: Set gas cost for undefined instructions to 0
  • Loading branch information
chfast authored Sep 19, 2019
2 parents 3379a94 + e87b24d commit b831a16
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions include/evmc/instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ enum evmc_opcode
/**
* Metrics for an EVM 1 instruction.
*
* Small integer types are used here to make the tables of metrics cache friendly.
* Small integer types are used here to make the tables of metrics smaller.
*/
struct evmc_instruction_metrics
{
/** The instruction gas cost. Value -1 indicates an undefined instruction. */
/** The instruction gas cost. */
int16_t gas_cost;

/** The number of items the instruction pops from the EVM stack before execution. */
Expand Down
21 changes: 13 additions & 8 deletions lib/instructions/instruction_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@

#include <evmc/instructions.h>

/** Marks an instruction as undefined. */
#define UNDEFINED -1
/**
* Marks an instruction as undefined.
*
* The gas cost for undefined instructions is 0 because this is the cost of executing them
* in practice in EVM implementations.
*/
#define UNDEFINED 0

/**
* Gas price tiers, names from Yellow Paper.
Expand Down Expand Up @@ -277,7 +282,7 @@ static struct evmc_instruction_metrics istanbul_metrics[256] = {
/* = 0xfc */ {UNDEFINED, 0, 0},
/* REVERT = 0xfd */ {ZERO, 2, 0},
/* INVALID = 0xfe */ {ZERO, 0, 0},
/* SUICIDE = 0xff */ {5000, 1, 0},
/* SELFDESTRUCT = 0xff */ {5000, 1, 0},
};

static struct evmc_instruction_metrics constantinople_metrics[256] = {
Expand Down Expand Up @@ -536,7 +541,7 @@ static struct evmc_instruction_metrics constantinople_metrics[256] = {
/* = 0xfc */ {UNDEFINED, 0, 0},
/* REVERT = 0xfd */ {ZERO, 2, 0},
/* INVALID = 0xfe */ {ZERO, 0, 0},
/* SUICIDE = 0xff */ {5000, 1, 0},
/* SELFDESTRUCT = 0xff */ {5000, 1, 0},
};

static struct evmc_instruction_metrics byzantium_metrics[256] = {
Expand Down Expand Up @@ -795,7 +800,7 @@ static struct evmc_instruction_metrics byzantium_metrics[256] = {
/* = 0xfc */ {UNDEFINED, 0, 0},
/* REVERT = 0xfd */ {ZERO, 2, 0},
/* INVALID = 0xfe */ {ZERO, 0, 0},
/* SUICIDE = 0xff */ {5000, 1, 0},
/* SELFDESTRUCT = 0xff */ {5000, 1, 0},
};

static struct evmc_instruction_metrics tangerine_whistle_metrics[256] = {
Expand Down Expand Up @@ -1054,7 +1059,7 @@ static struct evmc_instruction_metrics tangerine_whistle_metrics[256] = {
/* = 0xfc */ {UNDEFINED, 0, 0},
/* = 0xfd */ {UNDEFINED, 0, 0},
/* INVALID = 0xfe */ {ZERO, 0, 0},
/* SUICIDE = 0xff */ {5000, 1, 0},
/* SELFDESTRUCT = 0xff */ {5000, 1, 0},
};

static struct evmc_instruction_metrics homestead_metrics[256] = {
Expand Down Expand Up @@ -1313,7 +1318,7 @@ static struct evmc_instruction_metrics homestead_metrics[256] = {
/* = 0xfc */ {UNDEFINED, 0, 0},
/* = 0xfd */ {UNDEFINED, 0, 0},
/* INVALID = 0xfe */ {ZERO, 0, 0},
/* SUICIDE = 0xff */ {ZERO, 1, 0},
/* SELFDESTRUCT = 0xff */ {ZERO, 1, 0},
};

static struct evmc_instruction_metrics frontier_metrics[256] = {
Expand Down Expand Up @@ -1572,7 +1577,7 @@ static struct evmc_instruction_metrics frontier_metrics[256] = {
/* = 0xfc */ {UNDEFINED, 0, 0},
/* = 0xfd */ {UNDEFINED, 0, 0},
/* INVALID = 0xfe */ {ZERO, 0, 0},
/* SUICIDE = 0xff */ {ZERO, 1, 0},
/* SELFDESTRUCT = 0xff */ {ZERO, 1, 0},
};

const struct evmc_instruction_metrics* evmc_get_instruction_metrics_table(
Expand Down
20 changes: 10 additions & 10 deletions test/unittests/test_instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TEST(instructions, name_gas_cost_equivalence)
if (name != nullptr)
EXPECT_GE(gas_cost, 0);
else
EXPECT_EQ(gas_cost, -1);
EXPECT_EQ(gas_cost, 0);
}
}
}
Expand All @@ -53,7 +53,7 @@ TEST(instructions, homestead_hard_fork)
}
}

EXPECT_EQ(f[OP_DELEGATECALL].gas_cost, -1);
EXPECT_EQ(f[OP_DELEGATECALL].gas_cost, 0);
EXPECT_EQ(h[OP_DELEGATECALL].gas_cost, 40);
EXPECT_TRUE(fn[OP_DELEGATECALL] == nullptr);
EXPECT_EQ(hn[OP_DELEGATECALL], std::string{"DELEGATECALL"});
Expand Down Expand Up @@ -161,22 +161,22 @@ TEST(instructions, byzantium_hard_fork)
EXPECT_EQ(b[OP_REVERT].gas_cost, 0);
EXPECT_EQ(b[OP_REVERT].num_stack_arguments, 2);
EXPECT_EQ(b[OP_REVERT].num_stack_returned_items, 0);
EXPECT_EQ(sd[OP_REVERT].gas_cost, -1);
EXPECT_EQ(sd[OP_REVERT].gas_cost, 0);
EXPECT_EQ(bn[OP_REVERT], std::string{"REVERT"});
EXPECT_TRUE(sdn[OP_REVERT] == nullptr);

EXPECT_EQ(b[OP_RETURNDATACOPY].gas_cost, 3);
EXPECT_EQ(sd[OP_RETURNDATACOPY].gas_cost, -1);
EXPECT_EQ(sd[OP_RETURNDATACOPY].gas_cost, 0);
EXPECT_EQ(bn[OP_RETURNDATACOPY], std::string{"RETURNDATACOPY"});
EXPECT_TRUE(sdn[OP_RETURNDATACOPY] == nullptr);

EXPECT_EQ(b[OP_RETURNDATASIZE].gas_cost, 2);
EXPECT_EQ(sd[OP_RETURNDATASIZE].gas_cost, -1);
EXPECT_EQ(sd[OP_RETURNDATASIZE].gas_cost, 0);
EXPECT_EQ(bn[OP_RETURNDATASIZE], std::string{"RETURNDATASIZE"});
EXPECT_TRUE(sdn[OP_RETURNDATASIZE] == nullptr);

EXPECT_EQ(b[OP_STATICCALL].gas_cost, 700);
EXPECT_EQ(sd[OP_STATICCALL].gas_cost, -1);
EXPECT_EQ(sd[OP_STATICCALL].gas_cost, 0);
EXPECT_EQ(bn[OP_STATICCALL], std::string{"STATICCALL"});
EXPECT_TRUE(sdn[OP_STATICCALL] == nullptr);
}
Expand Down Expand Up @@ -216,14 +216,14 @@ TEST(instructions, constantinople_hard_fork)
EXPECT_EQ(c[OP_CREATE2].gas_cost, 32000);
EXPECT_EQ(c[OP_CREATE2].num_stack_arguments, 4);
EXPECT_EQ(c[OP_CREATE2].num_stack_returned_items, 1);
EXPECT_EQ(b[OP_CREATE2].gas_cost, -1);
EXPECT_EQ(b[OP_CREATE2].gas_cost, 0);
EXPECT_EQ(cn[OP_CREATE2], std::string{"CREATE2"});
EXPECT_TRUE(bn[OP_CREATE2] == nullptr);

EXPECT_EQ(c[OP_EXTCODEHASH].gas_cost, 400);
EXPECT_EQ(c[OP_EXTCODEHASH].num_stack_arguments, 1);
EXPECT_EQ(c[OP_EXTCODEHASH].num_stack_returned_items, 1);
EXPECT_EQ(b[OP_EXTCODEHASH].gas_cost, -1);
EXPECT_EQ(b[OP_EXTCODEHASH].gas_cost, 0);
EXPECT_EQ(cn[OP_EXTCODEHASH], std::string{"EXTCODEHASH"});
EXPECT_TRUE(bn[OP_EXTCODEHASH] == nullptr);
}
Expand Down Expand Up @@ -269,14 +269,14 @@ TEST(instructions, istanbul_hard_fork)
EXPECT_EQ(i[OP_CHAINID].gas_cost, 2);
EXPECT_EQ(i[OP_CHAINID].num_stack_arguments, 0);
EXPECT_EQ(i[OP_CHAINID].num_stack_returned_items, 1);
EXPECT_EQ(p[OP_CHAINID].gas_cost, -1);
EXPECT_EQ(p[OP_CHAINID].gas_cost, 0);
EXPECT_EQ(in[OP_CHAINID], std::string{"CHAINID"});
EXPECT_TRUE(pn[OP_CHAINID] == nullptr);

EXPECT_EQ(i[OP_SELFBALANCE].gas_cost, 5);
EXPECT_EQ(i[OP_SELFBALANCE].num_stack_arguments, 0);
EXPECT_EQ(i[OP_SELFBALANCE].num_stack_returned_items, 1);
EXPECT_EQ(p[OP_SELFBALANCE].gas_cost, -1);
EXPECT_EQ(p[OP_SELFBALANCE].gas_cost, 0);
EXPECT_EQ(in[OP_SELFBALANCE], std::string{"SELFBALANCE"});
EXPECT_TRUE(pn[OP_SELFBALANCE] == nullptr);

Expand Down

0 comments on commit b831a16

Please sign in to comment.