Skip to content

Commit 09b187d

Browse files
[ETCM-912] Implement gas calculation changes in BALANCE
1 parent 16703e8 commit 09b187d

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/main/scala/io/iohk/ethereum/vm/OpCode.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,18 @@ case object SHA3 extends OpCode(0x20, 2, 1, _.G_sha3) {
386386

387387
case object ADDRESS extends ConstOp(0x30)(_.env.ownerAddr.toUInt256)
388388

389-
case object BALANCE extends OpCode(0x31, 1, 1, _.G_balance) with ConstGas {
389+
case object BALANCE extends OpCode(0x31, 1, 1, _.G_balance) with AddrAccessGas with ConstGas {
390390
protected def exec[W <: WorldStateProxy[W, S], S <: Storage[S]](state: ProgramState[W, S]): ProgramState[W, S] = {
391391
val (accountAddress, stack1) = state.stack.pop
392-
val accountBalance = state.world.getBalance(Address(accountAddress))
392+
val addr = Address(accountAddress)
393+
val accountBalance = state.world.getBalance(addr)
393394
val stack2 = stack1.push(accountBalance)
394-
state.withStack(stack2).step()
395+
state.withStack(stack2).addAccessedAddress(addr).step()
396+
}
397+
398+
protected def address[W <: WorldStateProxy[W, S], S <: Storage[S]](state: ProgramState[W, S]): Address = {
399+
val (accountAddress, _) = state.stack.pop
400+
Address(accountAddress)
395401
}
396402
}
397403

src/test/scala/io/iohk/ethereum/vm/OpCodeFunSpec.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,14 @@ class OpCodeFunSpec extends AnyFunSuite with OpCodeTesting with Matchers with Sc
156156

157157
test(BALANCE) { op =>
158158
forAll(getProgramStateGen(), getUInt256Gen()) { (stateIn, accountBalance) =>
159+
val (addr, _) = stateIn.stack.pop
159160
val stateOut = executeOp(op, stateIn)
160161
withStackVerification(op, stateIn, stateOut) {
161162
val (_, stack1) = stateIn.stack.pop
162-
stateOut shouldEqual stateIn.withStack(stack1.push(UInt256.Zero)).step()
163+
stateOut shouldEqual stateIn.addAccessedAddress(Address(addr)).withStack(stack1.push(UInt256.Zero)).step()
163164
}
164165

165-
val (addr, stack1) = stateIn.stack.pop
166+
val (_, stack1) = stateIn.stack.pop
166167

167168
val account = Account(balance = accountBalance)
168169
val world1 = stateIn.world.saveAccount(Address(addr.mod(UInt256(BigInt(2).pow(160)))), account)
@@ -172,7 +173,7 @@ class OpCodeFunSpec extends AnyFunSuite with OpCodeTesting with Matchers with Sc
172173

173174
withStackVerification(op, stateInWithAccount, stateOutWithAccount) {
174175
val stack2 = stack1.push(accountBalance)
175-
stateOutWithAccount shouldEqual stateInWithAccount.withStack(stack2).step()
176+
stateOutWithAccount shouldEqual stateInWithAccount.addAccessedAddress(Address(addr)).withStack(stack2).step()
176177
}
177178
}
178179
}

src/test/scala/io/iohk/ethereum/vm/OpCodeGasSpecPostEip2929.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class OpCodeGasSpecPostEip2929 extends AnyFunSuite with OpCodeTesting with Match
1717

1818
import config.feeSchedule._
1919

20-
test(EXTCODESIZE, EXTCODEHASH) { op =>
20+
test(EXTCODESIZE, EXTCODEHASH, BALANCE) { op =>
2121
val stateGen = getProgramStateGen(
2222
evmConfig = config,
2323
stackGen = getStackGen(elems = 1),

0 commit comments

Comments
 (0)