Skip to content

Commit

Permalink
Bump Casper contract to commit #251a1a349fddd90b40febf9781ffc92e8c6a4a0e
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Apr 9, 2018
1 parent 84a3254 commit 6760e96
Show file tree
Hide file tree
Showing 12 changed files with 782 additions and 3,259 deletions.
Expand Up @@ -34,6 +34,7 @@ public class CasperTestNetConfig extends BaseNetConfig {

public static final int EPOCH_LENGTH = 50;
public static final int WITHDRAWAL_DELAY = 5;
public static final int DYNASTY_LOGOUT_DELAY = 5;
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;
Expand Down
Expand Up @@ -121,7 +121,7 @@ private boolean switchRevertsFinalizedBlock(final Block block) {
}

private BigInteger getScore(final Block block) {
Object[] res = casper.constCall(block, "get_last_justified_epoch");
Object[] res = casper.constCall(block, "last_justified_epoch");
return ((BigInteger) res[0]).multiply(PRETTY_BIG).add(getPoWDifficulty(block));
}

Expand Down Expand Up @@ -210,19 +210,19 @@ private synchronized ImportResult casperConnect(final Block block) {
* Finalizes Casper epoch checkpoint if needed
*/
private void finalizeCheckpoint(final Block block) {
Object[] res = casper.constCall(block, "get_last_finalized_epoch");
Object[] res = casper.constCall(block, "last_finalized_epoch");
long finalizedEpoch = ((BigInteger) res[0]).longValue();
Object[] res2 = casper.constCall(block, "get_current_epoch");
Object[] res2 = casper.constCall(block, "current_epoch");
long currentEpoch = ((BigInteger) res2[0]).longValue();
if (finalizedEpoch == currentEpoch - 1) {
// Actually one hash per epoch, just the getter for array
Object[] res3 = casper.constCall(block, "get_checkpoint_hashes", finalizedEpoch);
Object[] res3 = casper.constCall(block, "checkpoint_hashes", finalizedEpoch);
byte[] checkpointHash = (byte[]) res3[0];
if (!Arrays.areEqual(checkpointHash, new byte[32])) { // new byte[32] == 00-filled
Block histBlock = getBlockByHash(checkpointHash);
Object[] res4 = casper.constCall(histBlock, "get_total_curdyn_deposits");
Object[] res4 = casper.constCall(histBlock, "total_curdyn_deposits_scaled");
BigInteger curDeposits = (BigInteger) res4[0];
Object[] res5 = casper.constCall(histBlock, "get_total_prevdyn_deposits");
Object[] res5 = casper.constCall(histBlock, "total_prevdyn_deposits_scaled");
BigInteger prevDeposits = (BigInteger) res5[0];
if (curDeposits.compareTo(NON_REVERT_MIN_DEPOSIT) > 0 &&
prevDeposits.compareTo(NON_REVERT_MIN_DEPOSIT) > 0) {
Expand Down
Expand Up @@ -43,6 +43,7 @@

import static org.ethereum.casper.config.net.CasperTestNetConfig.BASE_INTEREST_FACTOR;
import static org.ethereum.casper.config.net.CasperTestNetConfig.BASE_PENALTY_FACTOR;
import static org.ethereum.casper.config.net.CasperTestNetConfig.DYNASTY_LOGOUT_DELAY;
import static org.ethereum.casper.config.net.CasperTestNetConfig.MIN_DEPOSIT_ETH;
import static org.ethereum.casper.config.net.CasperTestNetConfig.NULL_SIGN_SENDER;
import static org.ethereum.casper.config.net.CasperTestNetConfig.WITHDRAWAL_DELAY;
Expand Down Expand Up @@ -223,6 +224,7 @@ private Pair<byte[], List<Transaction>> makeInitTxes() {
byte[] casperInit = contract.getConstructor().encodeArguments(
systemProperties.getCasperEpochLength(), // Epoch length
WITHDRAWAL_DELAY, // Withdrawal delay
DYNASTY_LOGOUT_DELAY, // Logout delay
ECKey.fromPrivate(sha3("0".getBytes())).getAddress(), // Owner
sigHasherContract, // Signature hasher contract
purityCheckerContract, // Purity checker contract
Expand Down
Expand Up @@ -299,13 +299,13 @@ private Transaction makeTx(byte[] receiveAddress, BigInteger value, byte[] data,


private long getValidatorIndex() {
return constCallCasperForLong("get_validator_indexes", new ByteArrayWrapper(coinbase.getAddress()));
return constCallCasperForLong("validator_indexes", new ByteArrayWrapper(coinbase.getAddress()));
}

private void initValContractAddress() { // Actually it's not used after deposit
if (valContractAddress != null)
return;
byte[] address = (byte[]) casper.constCall("get_validators__addr", getValidatorIndex())[0];
byte[] address = (byte[]) casper.constCall("validators__addr", getValidatorIndex())[0];
if (!Arrays.equals(address, new byte[20])) {
logger.info("Valcode contract found at {}", address);
this.valContractAddress = address;
Expand Down Expand Up @@ -380,9 +380,9 @@ private Transaction makeWithdrawTx(long validatorIndex) {
}

private boolean isLoggedIn(long targetEpoch, long validatorIndex) {
long startDynasty = constCallCasperForLong("get_validators__start_dynasty", validatorIndex);
long endDynasty = constCallCasperForLong("get_validators__end_dynasty", validatorIndex);
long currentDynasty = constCallCasperForLong("get_dynasty_in_epoch", targetEpoch);
long startDynasty = constCallCasperForLong("validators__start_dynasty", validatorIndex);
long endDynasty = constCallCasperForLong("validators__end_dynasty", validatorIndex);
long currentDynasty = constCallCasperForLong("dynasty_in_epoch", targetEpoch);
long pastDynasty = currentDynasty - 1;
boolean inCurrentDynasty = ((startDynasty <= currentDynasty) &&
(currentDynasty < endDynasty));
Expand Down Expand Up @@ -426,7 +426,7 @@ private long getEpoch() {
}

private long getCurrentEpoch() { // FIXME: WHY there are 2 methods for the same thing???
return constCallCasperForLong("get_current_epoch");
return constCallCasperForLong("current_epoch");
}

private boolean vote() {
Expand Down Expand Up @@ -492,12 +492,12 @@ private void checkWithdrawable() {
setState(LOGGED_OUT);
return;
}
long validatorEndDynasty = constCallCasperForLong("get_validators__end_dynasty", validatorIndex);
long endEpoch = constCallCasperForLong("get_dynasty_start_epoch", validatorEndDynasty + 1);
long validatorEndDynasty = constCallCasperForLong("validators__end_dynasty", validatorIndex);
long endEpoch = constCallCasperForLong("dynasty_start_epoch", validatorEndDynasty + 1);

// Check Casper to see if we can withdraw
long curEpoch = getCurrentEpoch();
long withdrawalDelay = constCallCasperForLong("get_withdrawal_delay");
long withdrawalDelay = constCallCasperForLong("withdrawal_delay");
if (curEpoch >= (endEpoch + withdrawalDelay)) {
// Make withdraw tx & broadcast
Transaction withdrawTx = makeWithdrawTx(validatorIndex);
Expand All @@ -513,44 +513,44 @@ private void checkWithdrawable() {

private void checkWithdrawn() {
// Check that we have been withdrawn--validator index will now be zero
if (constCallCasperForLong("get_validator_indexes", new ByteArrayWrapper(coinbase.getAddress())) == 0) {
if (constCallCasperForLong("validator_indexes", new ByteArrayWrapper(coinbase.getAddress())) == 0) {
setState(LOGGED_OUT);
}
}

private void logCasperInfo() {
long curEpoch = getCurrentEpoch();
long expectedSourceEpoch = constCallCasperForLong("get_expected_source_epoch");
BigInteger curDeposits = (BigInteger) casper.constCall("get_total_curdyn_deposits")[0];
BigInteger prevDeposits = (BigInteger) casper.constCall("get_total_prevdyn_deposits")[0];
BigDecimal curVotes = (BigDecimal) casper.constCall("get_votes__cur_dyn_votes", curEpoch, expectedSourceEpoch)[0];
BigDecimal prevVotes = (BigDecimal) casper.constCall("get_votes__prev_dyn_votes", curEpoch, expectedSourceEpoch)[0];
BigDecimal scaleFactor = (BigDecimal) casper.constCall("get_deposit_scale_factor", curEpoch)[0];
long expectedSourceEpoch = constCallCasperForLong("expected_source_epoch");
BigInteger curDepositsScaled = (BigInteger) casper.constCall("total_curdyn_deposits_scaled")[0];
BigInteger prevDepositsScaled = (BigInteger) casper.constCall("total_prevdyn_deposits_scaled")[0];
BigDecimal curVotes = (BigDecimal) casper.constCall("votes__cur_dyn_votes", curEpoch, expectedSourceEpoch)[0];
BigDecimal prevVotes = (BigDecimal) casper.constCall("votes__prev_dyn_votes", curEpoch, expectedSourceEpoch)[0];
BigDecimal scaleFactor = (BigDecimal) casper.constCall("deposit_scale_factor", curEpoch)[0];
BigDecimal curVotesScaled = curVotes.multiply(scaleFactor);
BigDecimal prevVotesScaled = prevVotes.multiply(scaleFactor);
BigDecimal curVotesPct = BigDecimal.ZERO;
BigDecimal prevVotesPct = BigDecimal.ZERO;
if (curDeposits.compareTo(BigInteger.ZERO) > 0 ) {
curVotesPct = curVotesScaled.multiply(BigDecimal.valueOf(100)).divide(new BigDecimal(curDeposits), MathContext.DECIMAL32);
if (curDepositsScaled.compareTo(BigInteger.ZERO) > 0 ) {
curVotesPct = curVotesScaled.multiply(BigDecimal.valueOf(100)).divide(new BigDecimal(curDepositsScaled), MathContext.DECIMAL32);
}
if (prevDeposits.compareTo(BigInteger.ZERO) > 0 ) {
prevVotesPct = prevVotesScaled.multiply(BigDecimal.valueOf(100)).divide(new BigDecimal(prevDeposits), MathContext.DECIMAL32);
if (prevDepositsScaled.compareTo(BigInteger.ZERO) > 0 ) {
prevVotesPct = prevVotesScaled.multiply(BigDecimal.valueOf(100)).divide(new BigDecimal(prevDepositsScaled), MathContext.DECIMAL32);
}

long lastFinalizedEpoch = constCallCasperForLong("get_last_finalized_epoch");
long lastJustifiedEpoch = constCallCasperForLong("get_last_justified_epoch");
BigDecimal lastNonvoterRescale = (BigDecimal) casper.constCall("get_last_nonvoter_rescale")[0];
BigDecimal lastVoterRescale = (BigDecimal) casper.constCall("get_last_voter_rescale")[0];
long lastFinalizedEpoch = constCallCasperForLong("last_finalized_epoch");
long lastJustifiedEpoch = constCallCasperForLong("last_justified_epoch");
BigDecimal lastNonvoterRescale = (BigDecimal) casper.constCall("last_nonvoter_rescale")[0];
BigDecimal lastVoterRescale = (BigDecimal) casper.constCall("last_voter_rescale")[0];
String logStr = String.format(
"CASPER STATUS: epoch %d, %.3f / %.3f ETH (%.2f %%) voted from current dynasty, " +
"%.3f / %.3f ETH (%.2f %%) voted from previous dynasty, last finalized epoch %d justified %d " +
"expected source %d. Nonvoter deposits last rescaled %.5fx, voter deposits %.5fx",
curEpoch,
curVotesScaled.divide(BigDecimal.TEN.pow(18), MathContext.DECIMAL32),
new BigDecimal(curDeposits).divide(BigDecimal.TEN.pow(18), MathContext.DECIMAL32),
new BigDecimal(curDepositsScaled).divide(BigDecimal.TEN.pow(18), MathContext.DECIMAL32),
curVotesPct,
prevVotesScaled.divide(BigDecimal.TEN.pow(18), MathContext.DECIMAL32),
new BigDecimal(prevDeposits).divide(BigDecimal.TEN.pow(18), MathContext.DECIMAL32),
new BigDecimal(prevDepositsScaled).divide(BigDecimal.TEN.pow(18), MathContext.DECIMAL32),
prevVotesPct,
lastFinalizedEpoch,
lastJustifiedEpoch,
Expand All @@ -560,7 +560,7 @@ private void logCasperInfo() {
logger.info(logStr);

long valIndex = getValidatorIndex();
BigDecimal myDeposit = (BigDecimal) casper.constCall("get_validators__deposit", valIndex)[0];
BigDecimal myDeposit = (BigDecimal) casper.constCall("validators__deposit", valIndex)[0];
BigDecimal myDepositScaled = myDeposit.multiply(scaleFactor);
String myStr = String.format(
"MY VALIDATOR STATUS: epoch %d, index #%d, deposit: %.3f ETH",
Expand All @@ -581,8 +581,8 @@ private byte[] getRecommendedVoteData(long validatorIndex) {
// target_hash = self.epoch_blockhash(current_epoch)
// ANSWER: Though, I'll try

byte[] targetHash = (byte[]) casper.constCall("get_recommended_target_hash")[0];
long sourceEpoch = constCallCasperForLong("get_recommended_source_epoch");
byte[] targetHash = (byte[]) casper.constCall("recommended_target_hash")[0];
long sourceEpoch = constCallCasperForLong("recommended_source_epoch");

if (targetHash == null) {
return null;
Expand Down
Expand Up @@ -26,6 +26,7 @@
import static org.ethereum.util.ByteUtil.longToBytesNoLeadZeroes;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -100,6 +101,7 @@ public enum FunctionType {
fallback
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Function {
public boolean anonymous;
public boolean constant;
Expand Down

0 comments on commit 6760e96

Please sign in to comment.