Skip to content

[ETCM-25/26] enable byzantium and constantinople on atlantis/agharta #643

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

Merged
merged 2 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/main/resources/chains/base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
# Doc: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-161.md
eip161-block-number = "1000000000000000000"

# EIP-170 max code size (Eth only)
# EIP-170 max code size (Enabled from Atlantis fork block number)
# Doc: https://github.com/ethereum/EIPs/issues/170
# null value indicates there's no max code size for the contract code
max-code-size = null
# TODO improve this configuration format as currently it is not obvious that this is enabled only from some block number
max-code-size = "24576"

# Difficulty bomb pause block number
# Doc: https://github.com/ethereumproject/ECIPs/blob/master/ECIPs/ECIP-1010.md
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/chains/eth.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# https://github.com/ethereum/pm/issues/53
constantinople-block-number = "7080000"

# EIP-170 max code size (Eth only)
# EIP-170 max code size (Enabled from eip161-block-number)
max-code-size = "24576"

# Setting both values to zero disables difficulty bomb delay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EthashDifficultyCalculator(blockchainConfig: BlockchainConfig) extends Dif
val c: BigInt =
if (blockNumber < homesteadBlockNumber) {
if (blockTimestamp < parentHeader.unixTimestamp + 13) 1 else -1
} else if (blockNumber >= byzantiumBlockNumber) {
} else if (blockNumber >= byzantiumBlockNumber || blockNumber >= blockchainConfig.atlantisBlockNumber) {
val parentUncleFactor = if (parentHeader.ommersHash == BlockHeader.emptyOmmerHash) 1 else 2
math.max(parentUncleFactor - (timestampDiff / 9), FrontierTimestampDiffLimit)
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/io/iohk/ethereum/extvm/VMServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ class VMServer(messageHandler: MessageHandler)
byzantiumBlockNumber = BigInt(4370000), //TODO include byzantium block number in protobuf
constantinopleBlockNumber = BigInt(10000000), //TODO include constantinople block number in protobuf
maxCodeSize = if (conf.maxCodeSize.isEmpty) None else Some(bigintFromGByteString(conf.maxCodeSize)),
accountStartNonce = conf.accountStartNonce
accountStartNonce = conf.accountStartNonce,
atlantisBlockNumber = BigInt(8772000), //TODO include atlantis block number in protobuf
aghartaBlockNumber = BigInt(9573000), //TODO include agharta block number in protobuf
phoenixBlockNumber = BigInt(10500839) //TODO include phoenix block number in protobuf
)
}
}
5 changes: 4 additions & 1 deletion src/main/scala/io/iohk/ethereum/ledger/BlockPreparator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ class BlockPreparator(
val TxResult(newWorld, gasUsed, logs, _, vmError) = executeTransaction(stx, address, blockHeader, world.saveAccount(address, account))

// spec: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-658.md
val transactionOutcome = if (blockHeader.number >= blockchainConfig.byzantiumBlockNumber) {
val transactionOutcome = if (
blockHeader.number >= blockchainConfig.byzantiumBlockNumber ||
blockHeader.number >= blockchainConfig.atlantisBlockNumber
) {
if (vmError.isDefined) FailureOutcome else SuccessOutcome
} else {
HashOutcome(newWorld.stateRootHash)
Expand Down
10 changes: 8 additions & 2 deletions src/main/scala/io/iohk/ethereum/vm/BlockchainConfigForEvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ case class BlockchainConfigForEvm(
byzantiumBlockNumber: BigInt,
constantinopleBlockNumber: BigInt,
maxCodeSize: Option[BigInt],
accountStartNonce: UInt256
accountStartNonce: UInt256,
atlantisBlockNumber: BigInt,
aghartaBlockNumber: BigInt,
phoenixBlockNumber: BigInt
)

object BlockchainConfigForEvm {
Expand All @@ -32,7 +35,10 @@ object BlockchainConfigForEvm {
byzantiumBlockNumber = byzantiumBlockNumber,
constantinopleBlockNumber = constantinopleBlockNumber,
maxCodeSize = maxCodeSize,
accountStartNonce = accountStartNonce
accountStartNonce = accountStartNonce,
atlantisBlockNumber = atlantisBlockNumber,
aghartaBlockNumber = aghartaBlockNumber,
phoenixBlockNumber = phoenixBlockNumber
)
}

Expand Down
23 changes: 22 additions & 1 deletion src/main/scala/io/iohk/ethereum/vm/EvmConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ object EvmConfig {
blockchainConfig.eip160BlockNumber -> PostEIP160ConfigBuilder,
blockchainConfig.eip161BlockNumber -> PostEIP161ConfigBuilder,
blockchainConfig.byzantiumBlockNumber -> ByzantiumConfigBuilder,
blockchainConfig.constantinopleBlockNumber -> ConstantinopleConfigBuilder
blockchainConfig.constantinopleBlockNumber -> ConstantinopleConfigBuilder,
blockchainConfig.atlantisBlockNumber -> AtlantisConfigBuilder,
blockchainConfig.aghartaBlockNumber -> AghartaConfigBuilder
)

// highest transition block that is less/equal to `blockNumber`
Expand All @@ -48,7 +50,9 @@ object EvmConfig {
val FrontierOpCodes = OpCodeList(OpCodes.FrontierOpCodes)
val HomesteadOpCodes = OpCodeList(OpCodes.HomesteadOpCodes)
val ByzantiumOpCodes = OpCodeList(OpCodes.ByzantiumOpCodes)
val AtlantisOpCodes = ByzantiumOpCodes
val ConstantinopleOpCodes = OpCodeList(OpCodes.ConstantinopleOpCodes)
val AghartaOpCodes = ConstantinopleOpCodes

val FrontierConfigBuilder: EvmConfigBuilder = config => EvmConfig(
blockchainConfig = config,
Expand Down Expand Up @@ -89,6 +93,18 @@ object EvmConfig {
opCodeList = ConstantinopleOpCodes
)

// Ethereum classic forks only
val AtlantisConfigBuilder: EvmConfigBuilder = config => PostEIP160ConfigBuilder(config).copy(
feeSchedule = new FeeSchedule.AtlantisFeeSchedule,
opCodeList = AtlantisOpCodes,
noEmptyAccounts = true
)

val AghartaConfigBuilder: EvmConfigBuilder = config => AtlantisConfigBuilder(config).copy(
feeSchedule = new vm.FeeSchedule.ConstantionopleFeeSchedule,
opCodeList = AghartaOpCodes
)

case class OpCodeList(opCodes: List[OpCode]) {
val byteToOpCode: Map[Byte, OpCode] =
opCodes.map(op => op.code -> op).toMap
Expand Down Expand Up @@ -232,6 +248,11 @@ object FeeSchedule {
class ByzantiumFeeSchedule extends PostEIP160FeeSchedule

class ConstantionopleFeeSchedule extends ByzantiumFeeSchedule

class AtlantisFeeSchedule extends PostEIP160FeeSchedule

class AghartaFeeSchedule extends ByzantiumFeeSchedule

}

trait FeeSchedule {
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/io/iohk/ethereum/vm/PrecompiledContracts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ object PrecompiledContracts {

private def getContract(context: ProgramContext[_, _]): Option[PrecompiledContract] = {
context.recipientAddr.flatMap{ addr =>
if (context.blockHeader.number >= context.evmConfig.blockchainConfig.byzantiumBlockNumber)
if (context.blockHeader.number >= context.evmConfig.blockchainConfig.byzantiumBlockNumber ||
context.blockHeader.number >= context.evmConfig.blockchainConfig.atlantisBlockNumber) {
// byzantium and atlantis hard fork introduce the same set of precompiled contracts
byzantiumContracts.get(addr)
else
} else
contracts.get(addr)
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/scala/io/iohk/ethereum/vm/VM.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,23 @@ class VM[W <: WorldStateProxy[W, S], S <: Storage[S]] extends Logger {
private def invalidCallResult(context: PC): PR =
ProgramResult(ByteString.empty, context.startGas, context.world, Set(), Nil, Nil, 0, Some(InvalidCall))


private def exceedsMaxContractSize(context: PC, config: EvmConfig, contractCode: ByteString): Boolean = {
lazy val maxCodeSizeExceeded = config.maxCodeSize.exists(codeSizeLimit => contractCode.size > codeSizeLimit)
val currentBlock = context.blockHeader.number
// Max code size was enabled on eip161 block number on eth network, and on atlantis block number on etc
(currentBlock >= config.blockchainConfig.eip161BlockNumber || currentBlock >= config.blockchainConfig.atlantisBlockNumber) &&
maxCodeSizeExceeded
}

private def saveNewContract(context: PC, address: Address, result: PR, config: EvmConfig): PR = {
if(result.error.isDefined) {
if (result.error.contains(RevertOccurs)) result else result.copy(gasRemaining = 0)
} else {
val contractCode = result.returnData
val codeDepositCost = config.calcCodeDepositCost(contractCode)

val maxCodeSizeExceeded = config.maxCodeSize.exists(codeSizeLimit => contractCode.size > codeSizeLimit)
val maxCodeSizeExceeded = exceedsMaxContractSize(context, config, contractCode)
val codeStoreOutOfGas = result.gasRemaining < codeDepositCost

if (maxCodeSizeExceeded || (codeStoreOutOfGas && config.exceptionalFailedCodeDeposit)) {
Expand Down
8 changes: 6 additions & 2 deletions src/test/scala/io/iohk/ethereum/extvm/VMClientSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class VMClientSpec extends FlatSpec with Matchers with MockFactory {
eip150BlockNumber = blockchainConfig.eip150BlockNumber,
eip160BlockNumber = blockchainConfig.eip160BlockNumber,
eip161BlockNumber = blockchainConfig.eip161BlockNumber,
maxCodeSize = ByteString(),
maxCodeSize = blockchainConfig.maxCodeSize.get,
accountStartNonce = blockchainConfig.accountStartNonce)
val expectedHelloConfigMsg = msg.Hello.Config.EthereumConfig(expectedEthereumConfig)
val expectedHelloMsg = msg.Hello(version = "testVersion", config = expectedHelloConfigMsg)
Expand All @@ -174,7 +174,11 @@ class VMClientSpec extends FlatSpec with Matchers with MockFactory {
byzantiumBlockNumber = 0,
constantinopleBlockNumber = 0,
maxCodeSize = None,
accountStartNonce = 0)
accountStartNonce = 0,
atlantisBlockNumber = 0,
aghartaBlockNumber = 0,
phoenixBlockNumber = 0
)
val evmConfig = EvmConfig.FrontierConfigBuilder(blockchainConfigForEvm)

val senderAddress = Address("0x01")
Expand Down
5 changes: 4 additions & 1 deletion src/test/scala/io/iohk/ethereum/vm/Fixtures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ object Fixtures {
byzantiumBlockNumber = 0,
constantinopleBlockNumber = ConstantinopleBlockNumber,
maxCodeSize = None,
accountStartNonce = 0
accountStartNonce = 0,
atlantisBlockNumber = 0,
aghartaBlockNumber = 0,
phoenixBlockNumber = 0
)

}
16 changes: 13 additions & 3 deletions src/test/scala/io/iohk/ethereum/vm/VMSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,21 @@ class VMSpec extends WordSpec with PropertyChecks with Matchers {
result2.world.getStorage(expectedNewAddress).load(storageOffset) shouldEqual secondStoredValue
}

"go OOG if new contract's code size exceeds limit" in new ContractCreation {
"go OOG if new contract's code size exceeds limit and block is after atlantis or eip161" in new ContractCreation {
val codeSize = evmBlockchainConfig.maxCodeSize.get.toInt + 1
val contractCode = ByteString(Array.fill(codeSize)(-1.toByte))

val context = getContext(inputData = initCode(contractCode))
val context = getContext(inputData = initCode(contractCode),
evmConfig = homesteadConfig.copy(blockchainConfig = homesteadConfig.blockchainConfig.copy(eip161BlockNumber = 1)))
val result = vm.run(context)

result.error shouldBe Some(OutOfGas)

val context1 = getContext(inputData = initCode(contractCode),
evmConfig = homesteadConfig.copy(blockchainConfig = homesteadConfig.blockchainConfig.copy(atlantisBlockNumber = 1)))
val result1 = vm.run(context1)

result1.error shouldBe Some(OutOfGas)
}

"fail to create contract in case of address conflict (non-empty code)" in new ContractCreation {
Expand Down Expand Up @@ -147,7 +154,10 @@ class VMSpec extends WordSpec with PropertyChecks with Matchers {
byzantiumBlockNumber = Long.MaxValue,
constantinopleBlockNumber = Long.MaxValue,
maxCodeSize = Some(16),
accountStartNonce = 0
accountStartNonce = 0,
atlantisBlockNumber = Long.MaxValue,
aghartaBlockNumber = Long.MaxValue,
phoenixBlockNumber = Long.MaxValue
)

val homesteadConfig = EvmConfig.forBlock(0, evmBlockchainConfig.copy(homesteadBlockNumber = 0))
Expand Down