Skip to content

Commit

Permalink
Minor fixes to build errors in codebase (#577)
Browse files Browse the repository at this point in the history
* Updated get_initial_beacon_state instances to correct name get_genesis_beacon_state.

* Update constant name

* Fixes BeaconStateTest

* Update Fork value types and tests.
  • Loading branch information
Akhila Raju authored Apr 11, 2019
1 parent bad1baa commit 2a481b2
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

public class Fork {

private UnsignedLong previous_version;
private UnsignedLong current_version;
private Bytes previous_version;
private Bytes current_version;
private UnsignedLong epoch;

public Fork(UnsignedLong previous_version, UnsignedLong current_version, UnsignedLong epoch) {
public Fork(Bytes previous_version, Bytes current_version, UnsignedLong epoch) {
this.previous_version = previous_version;
this.current_version = current_version;
this.epoch = epoch;
Expand All @@ -41,16 +41,16 @@ public static Fork fromBytes(Bytes bytes) {
bytes,
reader ->
new Fork(
UnsignedLong.fromLongBits(reader.readUInt64()),
UnsignedLong.fromLongBits(reader.readUInt64()),
Bytes.wrap(reader.readBytes()),
Bytes.wrap(reader.readBytes()),
UnsignedLong.fromLongBits(reader.readUInt64())));
}

public Bytes toBytes() {
return SSZ.encode(
writer -> {
writer.writeUInt64(previous_version.longValue());
writer.writeUInt64(current_version.longValue());
writer.writeBytes(previous_version);
writer.writeBytes(current_version);
writer.writeUInt64(epoch.longValue());
});
}
Expand Down Expand Up @@ -81,19 +81,19 @@ public boolean equals(Object obj) {
}

/** ******************* * GETTERS & SETTERS * * ******************* */
public UnsignedLong getPrevious_version() {
public Bytes getPrevious_version() {
return previous_version;
}

public void setPrevious_version(UnsignedLong previous_version) {
public void setPrevious_version(Bytes previous_version) {
this.previous_version = previous_version;
}

public UnsignedLong getCurrent_version() {
public Bytes getCurrent_version() {
return current_version;
}

public void setCurrent_version(UnsignedLong current_version) {
public void setCurrent_version(Bytes current_version) {
this.current_version = current_version;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,8 @@ public static UnsignedLong max(UnsignedLong value1, UnsignedLong value2) {
*/
public static UnsignedLong get_domain(Fork fork, UnsignedLong epoch, int domain_type) {
// Below error is to be fixed in changes being made in Steven's open PR.
return bytes_to_int(Bytes.wrap(get_fork_version(fork, epoch), int_to_bytes(domain_type, 4)));
return UnsignedLong.valueOf(bytes_to_int(
Bytes.wrap(get_fork_version(fork, epoch), int_to_bytes(domain_type, 4))));
}

/**
Expand Down Expand Up @@ -1186,7 +1187,7 @@ public static boolean verify_slashable_attestation(
* href="https://github.com/ethereum/eth2.0-specs/blob/v0.4.0/specs/core/0_beacon-chain.md#get_fork_version">get_fork_version
* - Spec v0.4</a>
*/
public static UnsignedLong get_fork_version(Fork fork, UnsignedLong epoch) {
public static Bytes get_fork_version(Fork fork, UnsignedLong epoch) {
if (epoch.compareTo(fork.getEpoch()) < 0) {
return fork.getPrevious_version();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.generate_seed;
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.get_active_index_root;
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.get_current_epoch;
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.get_initial_beacon_state;
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.get_genesis_beacon_state;
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.get_randao_mix;
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.int_to_bytes32;
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.split;
Expand Down Expand Up @@ -57,15 +57,15 @@ private BeaconState newState(int numDeposits) {

// Initialize state
BeaconStateWithCache state = new BeaconStateWithCache();
get_initial_beacon_state(
get_genesis_beacon_state(
state,
randomDeposits(numDeposits),
UnsignedLong.ZERO,
new Eth1Data(Bytes32.ZERO, Bytes32.ZERO));

return state;
} catch (Exception e) {
fail("get_initial_beacon_state() failed");
fail("get_genesis_beacon_state() failed");
return null;
}
}
Expand All @@ -77,12 +77,12 @@ void activateValidator() {
UnsignedLong activation_epoch;

BeaconStateUtil.activate_validator(
state, state.getValidator_registry().get(validator_index), true);
state, validator_index, true);
activation_epoch = state.getValidator_registry().get(validator_index).getActivation_epoch();
assertThat(activation_epoch).isEqualTo(UnsignedLong.valueOf(GENESIS_EPOCH));

BeaconStateUtil.activate_validator(
state, state.getValidator_registry().get(validator_index), false);
state, validator_index, false);
activation_epoch = state.getValidator_registry().get(validator_index).getActivation_epoch();
assertThat(activation_epoch)
.isEqualTo(UnsignedLong.valueOf(GENESIS_EPOCH + 1 + ACTIVATION_EXIT_DELAY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import static tech.pegasys.artemis.datastructures.Constants.GENESIS_EPOCH;
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.get_initial_beacon_state;
import static tech.pegasys.artemis.datastructures.util.BeaconStateUtil.get_genesis_beacon_state;
import static tech.pegasys.artemis.datastructures.util.DataStructureUtil.randomDeposits;

import com.google.common.primitives.UnsignedLong;
Expand All @@ -38,15 +38,15 @@ private BeaconState newState(int numDeposits) {

// Initialize state
BeaconStateWithCache state = new BeaconStateWithCache();
get_initial_beacon_state(
get_genesis_beacon_state(
state,
randomDeposits(numDeposits),
UnsignedLong.ZERO,
new Eth1Data(Bytes32.ZERO, Bytes32.ZERO));

return state;
} catch (Exception e) {
fail("get_initial_beacon_state() failed");
fail("get_genesis_beacon_state() failed");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ void getForkVersionReturnsPreviousVersionWhenGivenEpochIsLessThanForkEpoch() {
// Setup Fork Versions
// The values of these don't really matter, it just makes sense that
// previous version is less than current version.
UnsignedLong previousVersion = UnsignedLong.ZERO;
UnsignedLong currentVersion = previousVersion.plus(UnsignedLong.valueOf(1L));
Bytes previousVersion = Bytes.ofUnsignedInt(0);
Bytes currentVersion = previousVersion.and(Bytes.ofUnsignedInt(1));

// Setup Epochs
// It is necessary for this test that givenEpoch is less than forkEpoch.
Expand All @@ -122,8 +122,8 @@ void getForkVersionReturnsCurrentVersionWhenGivenEpochIsGreaterThanForkEpoch() {
// Setup Fork Versions
// The values of these don't really matter, it just makes sense that
// previous version is less than current version.
UnsignedLong previousVersion = UnsignedLong.ZERO;
UnsignedLong currentVersion = previousVersion.plus(UnsignedLong.valueOf(1L));
Bytes previousVersion = Bytes.ofUnsignedInt(0);
Bytes currentVersion = previousVersion.and(Bytes.ofUnsignedInt(1));

// Setup Epochs
// It is necessary for this test that givenEpoch is greater than forkEpoch.
Expand All @@ -141,8 +141,8 @@ void getDomainReturnsAsExpectedForAllSignatureDomainTypesWithPreviousVersionFork
// Setup Fork Versions
// The values of these don't really matter, it just makes sense that
// previous version is less than current version.
UnsignedLong previousVersion = UnsignedLong.ZERO;
UnsignedLong currentVersion = previousVersion.plus(UnsignedLong.valueOf(1L));
Bytes previousVersion = Bytes.ofUnsignedInt(0);
Bytes currentVersion = previousVersion.and(Bytes.ofUnsignedInt(1));

// Setup Epochs
UnsignedLong givenEpoch = UnsignedLong.valueOf(100L);
Expand All @@ -161,7 +161,7 @@ void getDomainReturnsAsExpectedForAllSignatureDomainTypesWithPreviousVersionFork
assertEquals(
BeaconStateUtil.get_domain(fork, givenEpoch, domain),
UnsignedLong.valueOf(
(BeaconStateUtil.get_fork_version(fork, givenEpoch).longValue() << 32) + domain));
(BeaconStateUtil.get_fork_version(fork, givenEpoch).toLong() << 32) + domain));
}
}

Expand All @@ -170,8 +170,8 @@ void getDomainReturnsAsExpectedForAllSignatureDomainTypesWithCurrentVersionFork(
// Setup Fork Versions
// The values of these don't really matter, it just makes sense that
// previous version is less than current version.
UnsignedLong previousVersion = UnsignedLong.ZERO;
UnsignedLong currentVersion = previousVersion.plus(UnsignedLong.valueOf(1L));
Bytes previousVersion = Bytes.ofUnsignedInt(0);
Bytes currentVersion = previousVersion.and(Bytes.ofUnsignedInt(1));

// Setup Epochs
UnsignedLong forkEpoch = UnsignedLong.valueOf(100L);
Expand All @@ -190,7 +190,7 @@ void getDomainReturnsAsExpectedForAllSignatureDomainTypesWithCurrentVersionFork(
assertEquals(
BeaconStateUtil.get_domain(fork, givenEpoch, domain),
UnsignedLong.valueOf(
(BeaconStateUtil.get_fork_version(fork, givenEpoch).longValue() << 32) + domain));
(BeaconStateUtil.get_fork_version(fork, givenEpoch).toLong() << 32) + domain));
}
}
// *************** END Fork Tests ***************
Expand All @@ -204,8 +204,8 @@ void validateProofOfPosessionReturnsTrueIfTheBLSSignatureIsValidForGivenDepositI
UnsignedLong domain =
BeaconStateUtil.get_domain(
new Fork(
UnsignedLong.valueOf(Constants.GENESIS_FORK_VERSION),
UnsignedLong.valueOf(Constants.GENESIS_FORK_VERSION),
Bytes.ofUnsignedInt(Constants.GENESIS_FORK_VERSION),
Bytes.ofUnsignedInt(Constants.GENESIS_FORK_VERSION),
UnsignedLong.valueOf(Constants.GENESIS_EPOCH)),
UnsignedLong.fromLongBits(Constants.GENESIS_EPOCH),
Constants.DOMAIN_DEPOSIT);
Expand All @@ -227,8 +227,8 @@ void validateProofOfPosessionReturnsFalseIfTheBLSSignatureIsNotValidForGivenDepo
UnsignedLong domain =
BeaconStateUtil.get_domain(
new Fork(
UnsignedLong.valueOf(Constants.GENESIS_FORK_VERSION),
UnsignedLong.valueOf(Constants.GENESIS_FORK_VERSION),
Bytes.ofUnsignedInt(Constants.GENESIS_FORK_VERSION),
Bytes.ofUnsignedInt(Constants.GENESIS_FORK_VERSION),
UnsignedLong.valueOf(Constants.GENESIS_EPOCH)),
UnsignedLong.fromLongBits(Constants.GENESIS_EPOCH),
Constants.DOMAIN_DEPOSIT);
Expand Down Expand Up @@ -496,8 +496,8 @@ private BeaconState createBeaconState(
beaconState.setSlot(randomUnsignedLong());
beaconState.setFork(
new Fork(
UnsignedLong.valueOf(Constants.GENESIS_FORK_VERSION),
UnsignedLong.valueOf(Constants.GENESIS_FORK_VERSION),
Bytes.ofUnsignedInt(Constants.GENESIS_FORK_VERSION),
Bytes.ofUnsignedInt(Constants.GENESIS_FORK_VERSION),
UnsignedLong.valueOf(Constants.GENESIS_EPOCH)));

List<Validator> validatorList =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ private BeaconState newState() {
try {
// Initialize state
BeaconStateWithCache state = new BeaconStateWithCache();
BeaconStateUtil.get_initial_beacon_state(
BeaconStateUtil.get_genesis_beacon_state(
state, randomDeposits(5), UnsignedLong.ZERO, new Eth1Data(Bytes32.ZERO, Bytes32.ZERO));

state.setLatest_block_roots(
new ArrayList<Bytes32>(
Collections.nCopies(Constants.LATEST_BLOCK_ROOTS_LENGTH, Constants.ZERO_HASH)));
new ArrayList<>(
Collections.nCopies(Constants.SLOTS_PER_HISTORICAL_ROOT, Constants.ZERO_HASH)));

return state;
} catch (Exception e) {
fail("get_initial_beacon_state() failed");
fail("get_genesis_beacon_state() failed");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ BeaconState createArbitraryBeaconState(int numValidators) {
try {
// get initial state
BeaconStateWithCache state = new BeaconStateWithCache();
// BeaconStateUtil.get_initial_beacon_state(
// BeaconStateUtil.get_genesis_beacon_state(
// state, deposits, UnsignedLong.valueOf(Constants.GENESIS_SLOT), randomEth1Data());
UnsignedLong currentEpoch = BeaconStateUtil.get_current_epoch(state);

Expand Down

0 comments on commit 2a481b2

Please sign in to comment.