Skip to content

Commit

Permalink
Temporary EIP-86 partial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Apr 10, 2018
1 parent 6760e96 commit 179888a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@
*/
package org.ethereum.casper.config.net;

import org.ethereum.casper.core.CasperFacade;
import org.ethereum.config.BlockchainConfig;
import org.ethereum.config.Constants;
import org.ethereum.config.ConstantsAdapter;
import org.ethereum.config.blockchain.ByzantiumConfig;
import org.ethereum.config.blockchain.Eip150HFConfig;
import org.ethereum.config.blockchain.FrontierConfig;
import org.ethereum.config.net.BaseNetConfig;
import org.ethereum.core.Transaction;
import org.ethereum.crypto.ECKey;
import org.ethereum.vm.GasCost;
import org.spongycastle.util.encoders.Hex;

import java.math.BigInteger;
import java.util.Objects;

import static org.ethereum.config.blockchain.HomesteadConfig.SECP256K1N_HALF;

public class CasperTestNetConfig extends BaseNetConfig {

Expand All @@ -38,6 +43,7 @@ public class CasperTestNetConfig extends BaseNetConfig {
public static final double BASE_INTEREST_FACTOR = 0.1;
public static final double BASE_PENALTY_FACTOR = 0.0001;
public static final int MIN_DEPOSIT_ETH = 1500;
private byte[] casperAddress = null;

public final static ECKey NULL_SIGN_SENDER = ECKey.fromPrivate(Hex.decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));

Expand Down Expand Up @@ -85,9 +91,25 @@ public boolean eip161() {
public Constants getConstants() {
return constants;
}

@Override
public boolean acceptTransactionSignature(Transaction tx) {
if (tx.getSignature() != null) {
// Homestead-like check
if (!tx.getSignature().validateComponents() ||
tx.getSignature().s.compareTo(SECP256K1N_HALF) > 0) return false;
} else {
if (casperAddress == null || !CasperFacade.isVote(tx, casperAddress)) return false;

This comment has been minimized.

Copy link
@mkalinin

mkalinin Apr 11, 2018

Contributor

IMO no need for extra casperAddress copy here. CasperFacade can handle this check

}
return tx.getChainId() == null || Objects.equals(getChainId(), tx.getChainId());
}
}

public CasperTestNetConfig() {
add(0, new CasperConfig(new FrontierConfig()));
}

public void setCasperAddress(byte[] casperAddress) {
this.casperAddress = casperAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.ethereum.casper.config.CasperProperties;
import org.ethereum.casper.config.net.CasperTestNetConfig;
import org.ethereum.config.BlockchainNetConfig;
import org.ethereum.config.SystemProperties;
import org.ethereum.core.Block;
import org.ethereum.core.CallTransaction;
Expand Down Expand Up @@ -81,6 +83,10 @@ private void init() {
byte[] casperAddress = res.getKey();
this.initTxs = res.getValue();
systemProperties.setCasperAddress(casperAddress);
BlockchainNetConfig netConfig = systemProperties.getBlockchainConfig();
if (netConfig instanceof CasperTestNetConfig) {
((CasperTestNetConfig) netConfig).setCasperAddress(casperAddress);
}
this.contractAddress = Hex.toHexString(casperAddress);
logger.info("Casper contract address set to [0x{}]", contractAddress);
String casperAbi = systemProperties.getCasperAbi();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,12 @@ public void init() {
}
}

@Override
protected boolean isSignatureValid() {
return isCasperVote() || super.isSignatureValid();
}

@Override
public void execute() {

if (!readyToExecute) return;

if (!localCall && !isCasperVote()) {
if (!localCall && !isCasperServiceTx()) {
track.increaseNonce(tx.getSender());

BigInteger txGasLimit = toBI(tx.getGasLimit());
Expand All @@ -106,10 +101,6 @@ private boolean isCasperServiceTx() {
return CasperFacade.isServiceTx(tx, ((CasperProperties) config).getCasperAddress());
}

private boolean isCasperVote() {
return CasperFacade.isVote(tx, ((CasperProperties) config).getCasperAddress());
}

@Override
protected void payRewards(final TransactionExecutionSummary summary) {
if (isCasperServiceTx()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
public class HomesteadConfig extends FrontierConfig {

static final BigInteger SECP256K1N_HALF = Constants.getSECP256K1N().divide(BigInteger.valueOf(2));
public static final BigInteger SECP256K1N_HALF = Constants.getSECP256K1N().divide(BigInteger.valueOf(2));

public static class HomesteadConstants extends FrontierConstants {
@Override
Expand Down Expand Up @@ -68,7 +68,6 @@ public long getTransactionCost(Transaction tx) {
@Override
public boolean acceptTransactionSignature(Transaction tx) {
if (!super.acceptTransactionSignature(tx)) return false;
if (tx.getSignature() == null) return false;
return tx.getSignature().s.compareTo(SECP256K1N_HALF) <= 0;
}
}
11 changes: 7 additions & 4 deletions ethereumj-core/src/main/java/org/ethereum/core/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ public Transaction(byte[] nonce, byte[] gasPrice, byte[] gasLimit, byte[] receiv
}


private Integer extractChainIdFromV(BigInteger bv) {
private Integer extractChainIdFromRawSignature(BigInteger bv, byte[] r, byte[] s) {
if (r == null && s == null) return bv.intValue(); // EIP 86
if (bv.bitLength() > 31) return Integer.MAX_VALUE; // chainId is limited to 31 bits, longer are not valid for now
long v = bv.longValue();
if (v == LOWER_REAL_V || v == (LOWER_REAL_V + 1)) return null;
Expand Down Expand Up @@ -213,10 +214,12 @@ public synchronized void rlpParse() {
if (transaction.get(6).getRLPData() != null) {
byte[] vData = transaction.get(6).getRLPData();
BigInteger v = ByteUtil.bytesToBigInteger(vData);
this.chainId = extractChainIdFromV(v);
byte[] r = transaction.get(7).getRLPData();
byte[] s = transaction.get(8).getRLPData();
this.signature = ECDSASignature.fromComponents(r, s, getRealV(v));
this.chainId = extractChainIdFromRawSignature(v, r, s);
if (r != null && s != null) {
this.signature = ECDSASignature.fromComponents(r, s, getRealV(v));
}
} else {
logger.debug("RLP encoded tx is not signed!");
}
Expand Down Expand Up @@ -381,7 +384,7 @@ public synchronized byte[] getSender() {
if (sendAddress == null && getSignature() != null) {
sendAddress = ECKey.signatureToAddress(getRawHash(), getSignature());
}
// FIXME: Casper votes, we shouldn't do it this for Transaction itself
// TODO: check for side-effects
return sendAddress == null ? NULL_SENDER : sendAddress;
} catch (SignatureException e) {
logger.error(e.getMessage(), e);
Expand Down

0 comments on commit 179888a

Please sign in to comment.