Skip to content

Commit

Permalink
When checking whether the contract exists we need to compare the curr…
Browse files Browse the repository at this point in the history
…ent nonce against initialNonce constant instead of ZERO
  • Loading branch information
Nashatyrev committed Sep 7, 2017
1 parent ab3696a commit 5a07de1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.ethereum.core;

import org.ethereum.config.BlockchainConfig;
import org.ethereum.config.SystemProperties;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.FastByteComparisons;
Expand Down Expand Up @@ -136,8 +137,9 @@ public byte[] getEncoded() {
return rlpEncoded;
}

public boolean isContractExist() {
return !FastByteComparisons.equal(codeHash, EMPTY_DATA_HASH) || !BigInteger.ZERO.equals(nonce);
public boolean isContractExist(BlockchainConfig blockchainConfig) {
return !FastByteComparisons.equal(codeHash, EMPTY_DATA_HASH) ||
!blockchainConfig.getConstants().getInitialNonce().equals(nonce);
}

public boolean isEmpty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigInteger;
import java.util.List;
Expand Down Expand Up @@ -254,7 +253,7 @@ private void create() {
byte[] newContractAddress = tx.getContractAddress();

AccountState existingAddr = cacheTrack.getAccountState(newContractAddress);
if (existingAddr != null && existingAddr.isContractExist()) {
if (existingAddr != null && existingAddr.isContractExist(blockchainConfig)) {
execError("Trying to create a contract with existing contract address: 0x" + Hex.toHexString(newContractAddress));
m_endGas = BigInteger.ZERO;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,17 +415,17 @@ public void createContract(DataWord value, DataWord memStart, DataWord memSize)
if (logger.isInfoEnabled())
logger.info("creating a new contract inside contract run: [{}]", Hex.toHexString(senderAddress));

BlockchainConfig blockchainConfig = config.getBlockchainConfig().getConfigForBlock(getNumber().longValue());
// actual gas subtract
DataWord gasLimit = config.getBlockchainConfig().getConfigForBlock(getNumber().longValue()).
getCreateGas(getGas());
DataWord gasLimit = blockchainConfig.getCreateGas(getGas());
spendGas(gasLimit.longValue(), "internal call");

// [2] CREATE THE CONTRACT ADDRESS
byte[] nonce = getStorage().getNonce(senderAddress).toByteArray();
byte[] newAddress = HashUtil.calcNewAddr(getOwnerAddress().getLast20Bytes(), nonce);

AccountState existingAddr = getStorage().getAccountState(newAddress);
boolean contractAlreadyExists = existingAddr != null && existingAddr.isContractExist();
boolean contractAlreadyExists = existingAddr != null && existingAddr.isContractExist(blockchainConfig);

if (byTestingSuite()) {
// This keeps track of the contracts created for a test
Expand Down Expand Up @@ -500,7 +500,7 @@ this, new DataWord(newAddress), getOwnerAddress(), value, gasLimit,
long storageCost = getLength(code) * getBlockchainConfig().getGasCost().getCREATE_DATA();
long afterSpend = programInvoke.getGas().longValue() - storageCost - result.getGasUsed();
if (afterSpend < 0) {
if (!config.getBlockchainConfig().getConfigForBlock(getNumber().longValue()).getConstants().createEmptyContractOnOOG()) {
if (!blockchainConfig.getConstants().createEmptyContractOnOOG()) {
result.setException(Program.Exception.notEnoughSpendingGas("No gas to return just created contract",
storageCost, this));
} else {
Expand Down

0 comments on commit 5a07de1

Please sign in to comment.