Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
[PAN-2829] Add accountVersion to MessageFrame (#1675)
Browse files Browse the repository at this point in the history
* [PAN-2829] Add accountVersion to MessageFrame

Add the current contract's version to the message frame.
  • Loading branch information
Danno Ferrin authored Jul 13, 2019
1 parent 210d260 commit 4d37101
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public interface Account {
long DEFAULT_NONCE = 0L;
Wei DEFAULT_BALANCE = Wei.ZERO;
int DEFAULT_VERSION = 0;
int ISTANBUL_VERSION = 1;

/**
* The Keccak-256 hash of the account address.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import tech.pegasys.pantheon.ethereum.MainnetBlockValidator;
import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.core.Account;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.MutableAccount;
Expand Down Expand Up @@ -97,7 +98,8 @@ public static ProtocolSpecBuilder<Void> frontierDefinition(
contractCreationProcessor,
messageCallProcessor,
false,
stackSizeLimit))
stackSizeLimit,
Account.DEFAULT_VERSION))
.privateTransactionProcessorBuilder(
(gasCalculator,
transactionValidator,
Expand All @@ -109,7 +111,8 @@ public static ProtocolSpecBuilder<Void> frontierDefinition(
contractCreationProcessor,
messageCallProcessor,
false,
stackSizeLimit))
stackSizeLimit,
Account.DEFAULT_VERSION))
.difficultyCalculator(MainnetDifficultyCalculators.FRONTIER)
.blockHeaderValidatorBuilder(MainnetBlockHeaderValidator::create)
.ommerHeaderValidatorBuilder(MainnetBlockHeaderValidator::createOmmerValidator)
Expand Down Expand Up @@ -215,7 +218,8 @@ public static ProtocolSpecBuilder<Void> spuriousDragonDefinition(
contractCreationProcessor,
messageCallProcessor,
true,
stackSizeLimit))
stackSizeLimit,
Account.DEFAULT_VERSION))
.privateTransactionProcessorBuilder(
(gasCalculator,
transactionValidator,
Expand All @@ -227,7 +231,8 @@ public static ProtocolSpecBuilder<Void> spuriousDragonDefinition(
contractCreationProcessor,
messageCallProcessor,
false,
stackSizeLimit))
stackSizeLimit,
Account.DEFAULT_VERSION))
.name("SpuriousDragon");
}

Expand Down Expand Up @@ -280,8 +285,35 @@ public static ProtocolSpecBuilder<Void> istanbulDefinition(
final boolean enableRevertReason) {
final int contractSizeLimit =
configContractSizeLimit.orElse(SPURIOUS_DRAGON_CONTRACT_SIZE_LIMIT);
final int stackSizeLimit = configStackSizeLimit.orElse(DEFAULT_MAX_STACK_SIZE);
return constantinopleFixDefinition(
chainId, configContractSizeLimit, configStackSizeLimit, enableRevertReason)
.transactionProcessorBuilder(
(gasCalculator,
transactionValidator,
contractCreationProcessor,
messageCallProcessor) ->
new MainnetTransactionProcessor(
gasCalculator,
transactionValidator,
contractCreationProcessor,
messageCallProcessor,
true,
stackSizeLimit,
Account.ISTANBUL_VERSION))
.privateTransactionProcessorBuilder(
(gasCalculator,
transactionValidator,
contractCreationProcessor,
messageCallProcessor) ->
new PrivateTransactionProcessor(
gasCalculator,
transactionValidator,
contractCreationProcessor,
messageCallProcessor,
false,
stackSizeLimit,
Account.ISTANBUL_VERSION))
.contractCreationProcessorBuilder(
(gasCalculator, evm) ->
new MainnetContractCreationProcessor(
Expand All @@ -291,7 +323,7 @@ public static ProtocolSpecBuilder<Void> istanbulDefinition(
Collections.singletonList(MaxCodeSizeRule.of(contractSizeLimit)),
1,
SPURIOUS_DRAGON_FORCE_DELETE_WHEN_EMPTY_ADDRESSES,
1))
Account.ISTANBUL_VERSION))
.name("Istanbul");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public class MainnetTransactionProcessor implements TransactionProcessor {
private final AbstractMessageProcessor contractCreationProcessor;

private final AbstractMessageProcessor messageCallProcessor;

private final int maxStackSize;

private final int createContractAccountVersion;

public static class Result implements TransactionProcessor.Result {

private final Status status;
Expand Down Expand Up @@ -150,13 +153,15 @@ public MainnetTransactionProcessor(
final AbstractMessageProcessor contractCreationProcessor,
final AbstractMessageProcessor messageCallProcessor,
final boolean clearEmptyAccounts,
final int maxStackSize) {
final int maxStackSize,
final int createContractAccountVersion) {
this.gasCalculator = gasCalculator;
this.transactionValidator = transactionValidator;
this.contractCreationProcessor = contractCreationProcessor;
this.messageCallProcessor = messageCallProcessor;
this.clearEmptyAccounts = clearEmptyAccounts;
this.maxStackSize = maxStackSize;
this.createContractAccountVersion = createContractAccountVersion;
}

@Override
Expand Down Expand Up @@ -229,6 +234,7 @@ public Result processTransaction(
.address(contractAddress)
.originator(senderAddress)
.contract(contractAddress)
.contractAccountVersion(createContractAccountVersion)
.gasPrice(transaction.getGasPrice())
.inputData(BytesValue.EMPTY)
.sender(senderAddress)
Expand Down Expand Up @@ -258,6 +264,8 @@ public Result processTransaction(
.address(to)
.originator(senderAddress)
.contract(to)
.contractAccountVersion(
contract != null ? contract.getVersion() : Account.DEFAULT_VERSION)
.gasPrice(transaction.getGasPrice())
.inputData(transaction.getPayload())
.sender(senderAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class PrivateTransactionProcessor {

private final int maxStackSize;

private final int createContractAccountVersion;

public static class Result implements TransactionProcessor.Result {

private final Status status;
Expand Down Expand Up @@ -160,13 +162,15 @@ public PrivateTransactionProcessor(
final AbstractMessageProcessor contractCreationProcessor,
final AbstractMessageProcessor messageCallProcessor,
final boolean clearEmptyAccounts,
final int maxStackSize) {
final int maxStackSize,
final int createContractAccountVersion) {
this.gasCalculator = gasCalculator;
this.transactionValidator = transactionValidator;
this.contractCreationProcessor = contractCreationProcessor;
this.messageCallProcessor = messageCallProcessor;
this.clearEmptyAccounts = clearEmptyAccounts;
this.maxStackSize = maxStackSize;
this.createContractAccountVersion = createContractAccountVersion;
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -227,6 +231,7 @@ public Result processTransaction(
.address(privateContractAddress)
.originator(senderAddress)
.contract(privateContractAddress)
.contractAccountVersion(createContractAccountVersion)
.initialGas(Gas.MAX_VALUE)
.gasPrice(transaction.getGasPrice())
.inputData(BytesValue.EMPTY)
Expand Down Expand Up @@ -255,6 +260,8 @@ public Result processTransaction(
.address(to)
.originator(senderAddress)
.contract(to)
.contractAccountVersion(
contract != null ? contract.getVersion() : Account.DEFAULT_VERSION)
.initialGas(Gas.MAX_VALUE)
.gasPrice(transaction.getGasPrice())
.inputData(transaction.getPayload())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ public void execute(final MessageFrame frame) {
.address(address(frame))
.originator(frame.getOriginatorAddress())
.contract(to)
.contractAccountVersion(
contract != null ? contract.getVersion() : Account.DEFAULT_VERSION)
.gasPrice(frame.getGasPrice())
.inputData(inputData)
.sender(sender(frame))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package tech.pegasys.pantheon.ethereum.vm;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import tech.pegasys.pantheon.ethereum.chain.Blockchain;
Expand Down Expand Up @@ -206,6 +207,7 @@ public enum Type {
private final Address recipient;
private final Address originator;
private final Address contract;
private final int contractAccountVersion;
private final Wei gasPrice;
private final BytesValue inputData;
private final Address sender;
Expand Down Expand Up @@ -238,6 +240,7 @@ private MessageFrame(
final Address recipient,
final Address originator,
final Address contract,
final int contractAccountVersion,
final Wei gasPrice,
final BytesValue inputData,
final Address sender,
Expand Down Expand Up @@ -271,6 +274,7 @@ private MessageFrame(
this.recipient = recipient;
this.originator = originator;
this.contract = contract;
this.contractAccountVersion = contractAccountVersion;
this.gasPrice = gasPrice;
this.inputData = inputData;
this.sender = sender;
Expand Down Expand Up @@ -846,6 +850,10 @@ public void setCurrentOperation(final Operation currentOperation) {
this.currentOperation = currentOperation;
}

public int getContractAccountVersion() {
return contractAccountVersion;
}

public static class Builder {

private Type type;
Expand All @@ -856,6 +864,7 @@ public static class Builder {
private Address address;
private Address originator;
private Address contract;
private int contractAccountVersion = -1;
private Wei gasPrice;
private BytesValue inputData;
private Address sender;
Expand Down Expand Up @@ -912,6 +921,12 @@ public Builder contract(final Address contract) {
return this;
}

public Builder contractAccountVersion(final int contractAccountVersion) {
checkArgument(contractAccountVersion >= 0, "Contract account version cannot be negative");
this.contractAccountVersion = contractAccountVersion;
return this;
}

public Builder gasPrice(final Wei gasPrice) {
this.gasPrice = gasPrice;
return this;
Expand Down Expand Up @@ -1008,6 +1023,7 @@ private void validate() {
checkState(miningBeneficiary != null, "Missing mining beneficiary");
checkState(blockHashLookup != null, "Missing block hash lookup");
checkState(isPersistingState != null, "Missing isPersistingState");
checkState(contractAccountVersion != -1, "Missing contractAccountVersion");
}

public MessageFrame build() {
Expand All @@ -1022,6 +1038,7 @@ public MessageFrame build() {
address,
originator,
contract,
contractAccountVersion,
gasPrice,
inputData,
sender,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private void spawnChildMessage(final MessageFrame frame) {
.address(contractAddress)
.originator(frame.getOriginatorAddress())
.contract(contractAddress)
.contractAccountVersion(frame.getContractAccountVersion())
.gasPrice(frame.getGasPrice())
.inputData(BytesValue.EMPTY)
.sender(frame.getRecipientAddress())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class MessageFrameTestFixture {
private Address sender = DEFAUT_ADDRESS;
private Address originator = DEFAUT_ADDRESS;
private Address contract = DEFAUT_ADDRESS;
private int contractAccountVersion = Account.DEFAULT_VERSION;
private Wei gasPrice = Wei.ZERO;
private Wei value = Wei.ZERO;
private BytesValue inputData = BytesValue.EMPTY;
Expand Down Expand Up @@ -108,6 +109,11 @@ public MessageFrameTestFixture contract(final Address contract) {
return this;
}

public MessageFrameTestFixture contractAccountVersion(final int contractAccountVersion) {
this.contractAccountVersion = contractAccountVersion;
return this;
}

public MessageFrameTestFixture gasPrice(final Wei gasPrice) {
this.gasPrice = gasPrice;
return this;
Expand Down Expand Up @@ -167,6 +173,7 @@ public MessageFrame build() {
.value(value)
.apparentValue(value)
.contract(contract)
.contractAccountVersion(contractAccountVersion)
.code(code)
.blockHeader(blockHeader)
.depth(depth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public TestCodeExecutor(final ProtocolSchedule<Void> protocolSchedule) {
}

public MessageFrame executeCode(
final String code, final long gasLimit, final Consumer<MutableAccount> accountSetup) {
final String code,
final int accountVersion,
final long gasLimit,
final Consumer<MutableAccount> accountSetup) {
final ProtocolSpec<Void> protocolSpec = fixture.getProtocolSchedule().getByBlockNumber(0);
final WorldUpdater worldState =
createInitialWorldState(accountSetup, fixture.getStateArchive());
Expand Down Expand Up @@ -68,6 +71,7 @@ public MessageFrame executeCode(
.address(SENDER_ADDRESS)
.originator(SENDER_ADDRESS)
.contract(SENDER_ADDRESS)
.contractAccountVersion(accountVersion)
.gasPrice(transaction.getGasPrice())
.inputData(transaction.getPayload())
.sender(SENDER_ADDRESS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static tech.pegasys.pantheon.ethereum.mainnet.ValidationResult.valid;

import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.core.Account;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.ProcessableBlockHeader;
import tech.pegasys.pantheon.ethereum.core.Transaction;
Expand Down Expand Up @@ -61,7 +62,8 @@ public void before() {
contractCreationProcessor,
messageCallProcessor,
false,
MAX_STACK_SIZE);
MAX_STACK_SIZE,
Account.DEFAULT_VERSION);
}

@Test
Expand Down
Loading

0 comments on commit 4d37101

Please sign in to comment.