Skip to content

Commit

Permalink
EIP150 implementation
Browse files Browse the repository at this point in the history
Resolves #571
(cherry picked from commit d6b7d83)
  • Loading branch information
Nashatyrev committed Oct 17, 2016
1 parent 119f60a commit 075b881
Show file tree
Hide file tree
Showing 13 changed files with 666 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import org.ethereum.db.BlockStore;
import org.ethereum.db.RepositoryTrack;
import org.ethereum.mine.MinerIfc;
import org.ethereum.vm.DataWord;
import org.ethereum.vm.GasCost;
import org.ethereum.vm.OpCode;
import org.ethereum.vm.program.Program;

import java.math.BigInteger;
import java.util.List;
Expand Down Expand Up @@ -63,5 +67,30 @@ String validateTransactionChanges(BlockStore blockStore, Block curBlock, Transac
*/
void hardForkTransfers(Block block, Repository repo);

/**
* Hardcode the block hashes. I.e. if the block #1920000 should have the hash 0x1111
* the this method should return [{1920000, 0x1111}]
*/
List<Pair<Long, byte[]>> blockHashConstraints();

/**
* EVM operations costs
*/
GasCost getGasCost();

/**
* Calculates available gas to be passed for callee
* Since EIP150
* @param op Opcode
* @param requestedGas amount of gas requested by the program
* @param availableGas available gas
* @throws Program.OutOfGasException If passed args doesn't conform to limitations
*/
DataWord getCallGas(OpCode op, DataWord requestedGas, DataWord availableGas) throws Program.OutOfGasException;

/**
* Calculates available gas to be passed for contract constructor
* Since EIP150
*/
DataWord getCreateGas(DataWord availableGas);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import org.ethereum.db.RepositoryTrack;
import org.ethereum.mine.EthashMiner;
import org.ethereum.mine.MinerIfc;
import org.ethereum.vm.DataWord;
import org.ethereum.vm.GasCost;
import org.ethereum.vm.OpCode;
import org.ethereum.vm.program.Program;

import java.math.BigInteger;
import java.util.Collections;
Expand All @@ -27,6 +31,8 @@
* Created by Anton Nashatyrev on 25.02.2016.
*/
public abstract class AbstractConfig implements BlockchainConfig, BlockchainNetConfig {
private static final GasCost GAS_COST = new GasCost();

protected Constants constants;
protected MinerIfc miner;

Expand Down Expand Up @@ -98,4 +104,22 @@ public void hardForkTransfers(Block block, Repository repo) {}
public List<Pair<Long, byte[]>> blockHashConstraints() {
return Collections.emptyList();
}

@Override
public GasCost getGasCost() {
return GAS_COST;
}

@Override
public DataWord getCallGas(OpCode op, DataWord requestedGas, DataWord availableGas) throws Program.OutOfGasException {
if (requestedGas.compareTo(availableGas) > 0) {
throw Program.Exception.notEnoughOpGas(op, requestedGas, availableGas);
}
return requestedGas.clone();
}

@Override
public DataWord getCreateGas(DataWord availableGas) {
return availableGas;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.ethereum.config.blockchain;

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.lang3.tuple.Pair;
import org.ethereum.config.BlockchainConfig;
import org.ethereum.config.BlockchainNetConfig;
import org.ethereum.config.Constants;
import org.ethereum.config.SystemProperties;
import org.ethereum.core.Block;
import org.ethereum.core.BlockHeader;
import org.ethereum.core.Repository;
import org.ethereum.core.Transaction;
import org.ethereum.db.BlockStore;
import org.ethereum.db.RepositoryTrack;
import org.ethereum.mine.MinerIfc;
import org.ethereum.util.Utils;
import org.ethereum.vm.DataWord;
import org.ethereum.vm.GasCost;
import org.ethereum.vm.OpCode;
import org.ethereum.vm.program.Program;

import java.math.BigInteger;
import java.util.List;

/**
* Created by Anton Nashatyrev on 14.10.2016.
*/
public class Eip150HFConfig implements BlockchainConfig, BlockchainNetConfig {
private BlockchainConfig parent;


private static final GasCost NEW_GAS_COST = new GasCost() {
public int getBALANCE() { return 400; }
public int getEXT_CODE_SIZE() { return 700; }
public int getEXT_CODE_COPY() { return 700; }
public int getSLOAD() { return 200; }
public int getCALL() { return 700; }
public int getSUICIDE() { return 5000; }
public int getNEW_ACCT_SUICIDE() { return 25000; }
};

public Eip150HFConfig(BlockchainConfig parent) {
this.parent = parent;
}

@Override
public DataWord getCallGas(OpCode op, DataWord requestedGas, DataWord availableGas) throws Program.OutOfGasException {
DataWord maxAllowed = Utils.allButOne64th(availableGas);
return requestedGas.compareTo(maxAllowed) > 0 ? maxAllowed : requestedGas;
}

@Override
public DataWord getCreateGas(DataWord availableGas) {
return Utils.allButOne64th(availableGas);
}

@Override
public Constants getConstants() {
return parent.getConstants();
}

@Override
public MinerIfc getMineAlgorithm(SystemProperties config) {
return parent.getMineAlgorithm(config);
}

@Override
public BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent) {
return this.parent.calcDifficulty(curBlock, parent);
}

@Override
public long getTransactionCost(Transaction tx) {
return parent.getTransactionCost(tx);
}

@Override
public boolean acceptTransactionSignature(Transaction tx) {
return parent.acceptTransactionSignature(tx);
}

@Override
public String validateTransactionChanges(BlockStore blockStore, Block curBlock, Transaction tx, RepositoryTrack repositoryTrack) {
return parent.validateTransactionChanges(blockStore, curBlock, tx, repositoryTrack);
}

@Override
public void hardForkTransfers(Block block, Repository repo) {
parent.hardForkTransfers(block, repo);
}

@Override
public List<Pair<Long, byte[]>> blockHashConstraints() {
return parent.blockHashConstraints();
}

@Override
public GasCost getGasCost() {
return NEW_GAS_COST;
}

@Override
public BlockchainConfig getConfigForBlock(long blockNumber) {
return this;
}

@Override
public Constants getCommonConstants() {
return getConstants();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.ethereum.config.Constants;
import org.ethereum.core.BlockHeader;
import org.ethereum.core.Transaction;
import org.ethereum.vm.GasCost;

import java.math.BigInteger;

Expand Down Expand Up @@ -45,8 +44,8 @@ public long getTransactionCost(Transaction tx) {
long nonZeroes = tx.nonZeroDataBytes();
long zeroVals = ArrayUtils.getLength(tx.getData()) - nonZeroes;

return (tx.isContractCreation() ? GasCost.TRANSACTION_CREATE_CONTRACT : GasCost.TRANSACTION)
+ zeroVals * GasCost.TX_ZERO_DATA + nonZeroes * GasCost.TX_NO_ZERO_DATA;
return (tx.isContractCreation() ? getGasCost().getTRANSACTION_CREATE_CONTRACT() : getGasCost().getTRANSACTION())
+ zeroVals * getGasCost().getTX_ZERO_DATA() + nonZeroes * getGasCost().getTX_NO_ZERO_DATA();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.ethereum.config.Constants;
import org.ethereum.core.BlockHeader;
import org.ethereum.core.Transaction;
import org.ethereum.vm.GasCost;

import java.math.BigInteger;

Expand All @@ -31,6 +30,7 @@ public long getTransactionCost(Transaction tx) {
long nonZeroes = tx.nonZeroDataBytes();
long zeroVals = ArrayUtils.getLength(tx.getData()) - nonZeroes;

return GasCost.TRANSACTION + zeroVals * GasCost.TX_ZERO_DATA + nonZeroes * GasCost.TX_NO_ZERO_DATA;
return getGasCost().getTRANSACTION() + zeroVals * getGasCost().getTX_ZERO_DATA() +
nonZeroes * getGasCost().getTX_NO_ZERO_DATA();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ethereum.config.net;

import org.ethereum.config.blockchain.DaoHFConfig;
import org.ethereum.config.blockchain.Eip150HFConfig;
import org.ethereum.config.blockchain.FrontierConfig;
import org.ethereum.config.blockchain.HomesteadConfig;

Expand All @@ -14,5 +15,6 @@ public MainNetConfig() {
add(0, new FrontierConfig());
add(1_150_000, new HomesteadConfig());
add(1_920_000, new DaoHFConfig());
add(2_457_000, new Eip150HFConfig(new DaoHFConfig()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ public void go() {

if (tx.isContractCreation()) {

int returnDataGasValue = getLength(program.getResult().getHReturn()) * GasCost.CREATE_DATA;
int returnDataGasValue = getLength(program.getResult().getHReturn()) *
config.getBlockchainConfig().getConfigForBlock(currentBlock.getNumber()).getGasCost().getCREATE_DATA();
if (m_endGas.compareTo(BigInteger.valueOf(returnDataGasValue)) >= 0) {

m_endGas = m_endGas.subtract(BigInteger.valueOf(returnDataGasValue));
Expand Down Expand Up @@ -319,7 +320,8 @@ public TransactionExecutionSummary finalization() {

if (result != null) {
// Accumulate refunds for suicides
result.addFutureRefund(result.getDeleteAccounts().size() * GasCost.SUICIDE_REFUND);
result.addFutureRefund(result.getDeleteAccounts().size() * config.getBlockchainConfig().
getConfigForBlock(currentBlock.getNumber()).getGasCost().getSUICIDE_REFUND());
long gasRefund = Math.min(result.getFutureRefund(), result.getGasUsed() / 2);
byte[] addr = tx.isContractCreation() ? tx.getContractAddress() : tx.getReceiveAddress();
m_endGas = m_endGas.add(BigInteger.valueOf(gasRefund));
Expand Down
12 changes: 12 additions & 0 deletions ethereumj-core/src/main/java/org/ethereum/util/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.ethereum.util;

import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.vm.DataWord;
import org.spongycastle.util.encoders.DecoderException;
import org.spongycastle.util.encoders.Hex;

Expand All @@ -21,6 +24,7 @@
import javax.swing.*;

public class Utils {
private static final DataWord DIVISOR = new DataWord(64);

private static SecureRandom random = new SecureRandom();

Expand Down Expand Up @@ -183,4 +187,12 @@ public static String repeat(String s, int n) {
return ret.toString();
}
}

public static DataWord allButOne64th(DataWord dw) {
DataWord ret = dw.clone();
DataWord d = dw.clone();
d.div(DIVISOR);
ret.sub(d);
return ret;
}
}
Loading

0 comments on commit 075b881

Please sign in to comment.