|
1 | 1 | package io.iohk.ethereum.vm
|
2 | 2 |
|
3 | 3 | import org.scalacheck.Arbitrary
|
| 4 | +import org.scalacheck.Gen |
4 | 5 | import org.scalatest.funsuite.AnyFunSuite
|
5 | 6 | import org.scalatest.matchers.should.Matchers
|
6 | 7 | import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
|
@@ -196,4 +197,51 @@ class OpCodeGasSpecPostEip2929 extends AnyFunSuite with OpCodeTesting with Match
|
196 | 197 | }
|
197 | 198 | }
|
198 | 199 | }
|
| 200 | + |
| 201 | + test(SSTORE) { op => |
| 202 | + val storage = MockStorage.Empty.store(Zero, One) |
| 203 | + val table = Table[UInt256, UInt256, Boolean, BigInt, BigInt]( |
| 204 | + ("offset", "value", "alreadyAccessed", "startGas", "expectedGasConsumption"), |
| 205 | + (0, 1, true, G_callstipend + 1, G_sload), |
| 206 | + (0, 1, false, G_sload + G_cold_account_access, G_sload + G_cold_account_access), |
| 207 | + (0, 0, true, G_callstipend + 1, G_sload), |
| 208 | + (0, 0, false, G_sload + G_cold_account_access, G_sload + G_cold_account_access), |
| 209 | + (1, 0, true, G_callstipend + 1, G_sload), |
| 210 | + (1, 0, false, G_sload + G_cold_account_access, G_sload + G_cold_account_access), |
| 211 | + (1, 1, true, G_sset, G_sset), |
| 212 | + (1, 1, false, G_sset + G_cold_account_access, G_sset + G_cold_account_access) |
| 213 | + ) |
| 214 | + |
| 215 | + forAll(table) { (offset, value, alreadyAccessed, startGas, expectedGasConsumption) => |
| 216 | + val stackIn = Stack.empty().push(value).push(offset) |
| 217 | + val stateIn = getProgramStateGen( |
| 218 | + blockNumberGen = getUInt256Gen(Fixtures.MagnetoBlockNumber), |
| 219 | + evmConfig = config |
| 220 | + ).sample.get.withStack(stackIn).withStorage(storage).copy(gas = startGas) |
| 221 | + |
| 222 | + val stateOut = |
| 223 | + if (alreadyAccessed) op.execute(stateIn.addAccessedStorageKey(stateIn.ownAddress, offset)) |
| 224 | + else op.execute(stateIn) |
| 225 | + verifyGas(expectedGasConsumption, stateIn, stateOut, allowOOG = false) |
| 226 | + } |
| 227 | + |
| 228 | + // test G_sreset |
| 229 | + forAll(Arbitrary.arbitrary[Boolean]) { alreadyAccessed => |
| 230 | + val offset = 0 |
| 231 | + val value = 0 |
| 232 | + val expectedGasConsumption = if (alreadyAccessed) G_sreset else G_sreset + G_cold_account_access |
| 233 | + |
| 234 | + val stackIn = Stack.empty().push(value).push(offset) |
| 235 | + val stateIn = getProgramStateGen( |
| 236 | + blockNumberGen = getUInt256Gen(Fixtures.MagnetoBlockNumber), |
| 237 | + evmConfig = config, |
| 238 | + storageGen = Gen.const(storage) |
| 239 | + ).sample.get.withStack(stackIn).copy(gas = expectedGasConsumption) |
| 240 | + |
| 241 | + val stateOut = |
| 242 | + if (alreadyAccessed) op.execute(stateIn.addAccessedStorageKey(stateIn.ownAddress, offset)) |
| 243 | + else op.execute(stateIn) |
| 244 | + verifyGas(expectedGasConsumption, stateIn, stateOut, allowOOG = false) |
| 245 | + } |
| 246 | + } |
199 | 247 | }
|
0 commit comments