Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 3a6208b

Browse files
committed
EIP-1884 Implement SELFBALANCE opcode in LegacyVM
1 parent a052892 commit 3a6208b

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

libethcore/EVMSchedule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct EVMSchedule
4141
bool haveStaticCall = false;
4242
bool haveCreate2 = false;
4343
bool haveExtcodehash = false;
44+
bool haveSelfbalance = false;
4445
std::array<unsigned, 8> tierStepGas;
4546
unsigned expGas = 10;
4647
unsigned expByteGas = 10;
@@ -153,6 +154,7 @@ static const EVMSchedule IstanbulSchedule = [] {
153154
schedule.sloadGas = 800;
154155
schedule.balanceGas = 700;
155156
schedule.extcodehashGas = 700;
157+
schedule.haveSelfbalance = true;
156158
return schedule;
157159
}();
158160

libevm/Instruction.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace dev
2222
{
2323
namespace eth
2424
{
25-
25+
// clang-format off
2626
static const std::map<Instruction, InstructionInfo> c_instructionInfo =
2727
{ // Args, Ret, GasPriceTier
2828
{ Instruction::STOP, { "STOP", 0, 0, Tier::Zero } },
@@ -74,6 +74,7 @@ static const std::map<Instruction, InstructionInfo> c_instructionInfo =
7474
{ Instruction::NUMBER, { "NUMBER", 0, 1, Tier::Base } },
7575
{ Instruction::DIFFICULTY, { "DIFFICULTY", 0, 1, Tier::Base } },
7676
{ Instruction::GASLIMIT, { "GASLIMIT", 0, 1, Tier::Base } },
77+
{ Instruction::SELFBALANCE, { "SELFBALANCE", 0, 1, Tier::Low } },
7778
{ Instruction::POP, { "POP", 1, 0, Tier::Base } },
7879
{ Instruction::MLOAD, { "MLOAD", 1, 1, Tier::VeryLow } },
7980
{ Instruction::MSTORE, { "MSTORE", 2, 0, Tier::VeryLow } },
@@ -215,8 +216,9 @@ static const std::map<Instruction, InstructionInfo> c_instructionInfo =
215216
{ Instruction::PUSHC, { "PUSHC", 0, 1, Tier::VeryLow } },
216217
{ Instruction::JUMPC, { "JUMPC", 1, 0, Tier::Mid } },
217218
{ Instruction::JUMPCI, { "JUMPCI", 2, 0, Tier::High } },
218-
};
219-
219+
};
220+
// clang-format on
221+
220222
InstructionInfo instructionInfo(Instruction _inst)
221223
{
222224
auto it = c_instructionInfo.find(_inst);

libevm/Instruction.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ enum class Instruction : uint8_t
7575
RETURNDATACOPY = 0x3e, ///< copy data returned from previous call to memory
7676
EXTCODEHASH = 0x3f, ///< get external code hash
7777

78-
BLOCKHASH = 0x40, ///< get hash of most recent complete block
79-
COINBASE, ///< get the block's coinbase address
80-
TIMESTAMP, ///< get the block's timestamp
81-
NUMBER, ///< get the block's number
82-
DIFFICULTY, ///< get the block's difficulty
83-
GASLIMIT, ///< get the block's gas limit
78+
BLOCKHASH = 0x40, ///< get hash of most recent complete block
79+
COINBASE, ///< get the block's coinbase address
80+
TIMESTAMP, ///< get the block's timestamp
81+
NUMBER, ///< get the block's number
82+
DIFFICULTY, ///< get the block's difficulty
83+
GASLIMIT, ///< get the block's gas limit
84+
SELFBALANCE = 0x47, ///< get balance of the current address
8485

8586
POP = 0x50, ///< remove item from stack
8687
MLOAD, ///< load word from memory

libevm/LegacyVM.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,19 @@ void LegacyVM::interpretCases()
14391439
}
14401440
NEXT
14411441

1442+
CASE(SELFBALANCE)
1443+
{
1444+
ON_OP();
1445+
1446+
if (!m_schedule->haveSelfbalance)
1447+
throwBadInstruction();
1448+
1449+
updateIOGas();
1450+
1451+
m_SPP[0] = m_ext->balance(m_ext->myAddress);
1452+
}
1453+
NEXT
1454+
14421455
CASE(POP)
14431456
{
14441457
ON_OP();

libevm/LegacyVMConfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ namespace dev
226226
&&EXTCODECOPY, \
227227
&&RETURNDATASIZE, \
228228
&&RETURNDATACOPY, \
229-
&&EXTCODEHASH, \
229+
&&EXTCODEHASH, \
230230
&&BLOCKHASH, /* 40, */ \
231231
&&COINBASE, \
232232
&&TIMESTAMP, \
233233
&&NUMBER, \
234234
&&DIFFICULTY, \
235235
&&GASLIMIT, \
236236
&&INVALID, \
237-
&&INVALID, \
237+
&&SELFBALANCE, \
238238
&&INVALID, \
239239
&&INVALID, \
240240
&&INVALID, \

0 commit comments

Comments
 (0)