Skip to content

Commit

Permalink
BeaconState SSZ fixes, and run spotless. (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroedingerscode authored May 6, 2019
1 parent d2f190e commit a735c19
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import net.consensys.cava.ssz.SSZ;
import tech.pegasys.artemis.util.bls.BLSSignature;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil;
import tech.pegasys.artemis.util.hashtree.Merkleizable;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil.SSZTypes;
import tech.pegasys.artemis.util.hashtree.Merkleizable;

public class Attestation implements Merkleizable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import net.consensys.cava.ssz.SSZ;
import tech.pegasys.artemis.datastructures.Constants;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil;
import tech.pegasys.artemis.util.hashtree.Merkleizable;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil.SSZTypes;
import tech.pegasys.artemis.util.hashtree.Merkleizable;

public class Deposit implements Merkleizable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import net.consensys.cava.ssz.SSZ;
import tech.pegasys.artemis.datastructures.blocks.BeaconBlockHeader;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil;
import tech.pegasys.artemis.util.hashtree.Merkleizable;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil.SSZTypes;
import tech.pegasys.artemis.util.hashtree.Merkleizable;

public class ProposerSlashing implements Merkleizable {

Expand Down Expand Up @@ -110,7 +110,8 @@ public void setHeader_2(BeaconBlockHeader header_2) {
public Bytes32 hash_tree_root() {
return HashTreeUtil.merkleize(
Arrays.asList(
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(proposer_index.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(proposer_index.longValue())),
header_1.hash_tree_root(),
header_2.hash_tree_root()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public void setCustody_bitfield(Bytes custody_bitfield) {
public Bytes32 hash_tree_root() {
return HashTreeUtil.merkleize(
Arrays.asList(
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_BASIC,
HashTreeUtil.hash_tree_root(
SSZTypes.LIST_OF_BASIC,
validator_indices.stream()
.map(item -> SSZ.encodeUInt64(item.longValue()))
.collect(Collectors.toList())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import tech.pegasys.artemis.util.bls.BLSPublicKey;
import tech.pegasys.artemis.util.bls.BLSSignature;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil;
import tech.pegasys.artemis.util.hashtree.Merkleizable;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil.SSZTypes;
import tech.pegasys.artemis.util.hashtree.Merkleizable;

public class Transfer implements Merkleizable {
private UnsignedLong sender;
Expand Down Expand Up @@ -174,7 +174,8 @@ public Bytes32 signed_root(String truncation_param) {
HashTreeUtil.merkleize(
Arrays.asList(
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(sender.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(recipient.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(recipient.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(amount.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(fee.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(slot.longValue())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import net.consensys.cava.ssz.SSZ;
import tech.pegasys.artemis.util.bls.BLSSignature;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil;
import tech.pegasys.artemis.util.hashtree.Merkleizable;
import tech.pegasys.artemis.util.hashtree.HashTreeUtil.SSZTypes;
import tech.pegasys.artemis.util.hashtree.Merkleizable;

public class VoluntaryExit implements Merkleizable {

Expand Down Expand Up @@ -115,16 +115,17 @@ public Bytes32 signed_root(String truncation_param) {
HashTreeUtil.merkleize(
Arrays.asList(
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(validator_index.longValue())))));
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(validator_index.longValue())))));
}

@Override
public Bytes32 hash_tree_root() {
return HashTreeUtil.merkleize(
Arrays.asList(
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(validator_index.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(validator_index.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.TUPLE_OF_BASIC, signature.toBytes())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -722,79 +722,64 @@ public Bytes32 hash_tree_root() {
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(genesis_time.longValue())),
fork.hash_tree_root(),
// Validator registry
HashTreeUtil.mix_in_length(
HashTreeUtil.merkleize(
validator_registry.stream()
.map(item -> item.hash_tree_root())
.collect(Collectors.toList())),
validator_registry.size()),
HashTreeUtil.hash_tree_root_list_of_basic_type(
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_COMPOSITE, validator_registry),
HashTreeUtil.hash_tree_root(
SSZTypes.LIST_OF_BASIC,
validator_balances.stream()
.map(item -> SSZ.encodeUInt64(item.longValue()))
.collect(Collectors.toList()),
validator_balances.size()),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(validator_registry_update_epoch.longValue())),
.collect(Collectors.toList())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(validator_registry_update_epoch.longValue())),
// Randomness and committees
HashTreeUtil.hash_tree_root_basic_type(latest_randao_mixes.toArray(new Bytes32[0])),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(previous_shuffling_start_shard.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(current_shuffling_start_shard.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(previous_shuffling_epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(current_shuffling_epoch.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.TUPLE_OF_COMPOSITE, latest_randao_mixes.toArray(new Bytes32[0])),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(previous_shuffling_start_shard.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(current_shuffling_start_shard.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(previous_shuffling_epoch.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(current_shuffling_epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.TUPLE_OF_BASIC, previous_shuffling_seed),
HashTreeUtil.hash_tree_root(SSZTypes.TUPLE_OF_BASIC, current_shuffling_seed),
// Finality
HashTreeUtil.mix_in_length(
HashTreeUtil.merkleize(
previous_epoch_attestations.stream()
.map(item -> item.hash_tree_root())
.collect(Collectors.toList())),
previous_epoch_attestations.size()),
HashTreeUtil.mix_in_length(
HashTreeUtil.merkleize(
current_epoch_attestations.stream()
.map(item -> item.hash_tree_root())
.collect(Collectors.toList())),
current_epoch_attestations.size()),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(previous_justified_epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(current_justified_epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_COMPOSITE, previous_epoch_attestations),
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_COMPOSITE, current_epoch_attestations),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(previous_justified_epoch.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(current_justified_epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.TUPLE_OF_BASIC, previous_justified_root),
HashTreeUtil.hash_tree_root(SSZTypes.TUPLE_OF_BASIC, current_justified_root),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(justification_bitfield.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(finalized_epoch.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(justification_bitfield.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(finalized_epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.TUPLE_OF_BASIC, finalized_root),
// Recent state
HashTreeUtil.merkleize(
latest_crosslinks.stream()
.map(item -> item.hash_tree_root())
.collect(Collectors.toList())),
HashTreeUtil.hash_tree_root_basic_type(latest_block_roots.toArray(new Bytes32[0])),
HashTreeUtil.hash_tree_root_basic_type(latest_state_roots.toArray(new Bytes32[0])),
HashTreeUtil.hash_tree_root_basic_type(
latest_active_index_roots.toArray(new Bytes32[0])),
HashTreeUtil.hash_tree_root_basic_type(
HashTreeUtil.hash_tree_root(
SSZTypes.TUPLE_OF_COMPOSITE, latest_block_roots.toArray(new Bytes32[0])),
HashTreeUtil.hash_tree_root(
SSZTypes.TUPLE_OF_COMPOSITE, latest_state_roots.toArray(new Bytes32[0])),
HashTreeUtil.hash_tree_root(
SSZTypes.TUPLE_OF_COMPOSITE, latest_active_index_roots.toArray(new Bytes32[0])),
HashTreeUtil.hash_tree_root(
SSZTypes.TUPLE_OF_BASIC,
latest_slashed_balances.stream()
.map(item -> SSZ.encodeUInt64(item.longValue()))
.collect(Collectors.toList())
.toArray(new Bytes[0])),
latest_block_header.hash_tree_root(),
HashTreeUtil.hash_tree_root_list_of_basic_type(
historical_roots, historical_roots.size()),
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_COMPOSITE, historical_roots),
// Ethereum 1.0 chain data
latest_eth1_data.hash_tree_root(),
HashTreeUtil.mix_in_length(
HashTreeUtil.merkleize(
eth1_data_votes.stream()
.map(item -> item.hash_tree_root())
.collect(Collectors.toList())),
eth1_data_votes.size()),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(deposit_index.longValue()))));
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_COMPOSITE, eth1_data_votes),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(deposit_index.longValue()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public Bytes32 hash_tree_root() {
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_BASIC, aggregation_bitfield),
data.hash_tree_root(),
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_BASIC, custody_bitfield),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(inclusion_slot.longValue()))));
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(inclusion_slot.longValue()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ public Bytes32 hash_tree_root() {
Arrays.asList(
HashTreeUtil.hash_tree_root(SSZTypes.TUPLE_OF_BASIC, pubkey.toBytes()),
HashTreeUtil.hash_tree_root(SSZTypes.TUPLE_OF_BASIC, withdrawal_credentials),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(activation_epoch.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(activation_epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(exit_epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeUInt64(withdrawable_epoch.longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(withdrawable_epoch.longValue())),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeBoolean(initiated_exit)),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC, SSZ.encodeBoolean(slashed))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public static BeaconStateWithCache get_genesis_beacon_state(
ValidatorsUtil.get_active_validator_indices(
state.getValidator_registry(), UnsignedLong.valueOf(GENESIS_EPOCH));
Bytes32 genesis_active_index_root =
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_BASIC,
HashTreeUtil.hash_tree_root(
SSZTypes.LIST_OF_BASIC,
active_validator_indices.stream()
.map(item -> SSZ.encodeUInt64(item))
.collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public static void process_randao(BeaconState state, BeaconBlock block) {
checkArgument(
bls_verify(
proposer.getPubkey(),
HashTreeUtil.hash_tree_root(SSZTypes.BASIC,
SSZ.encodeUInt64(get_current_epoch(state).longValue())),
HashTreeUtil.hash_tree_root(
SSZTypes.BASIC, SSZ.encodeUInt64(get_current_epoch(state).longValue())),
block.getBody().getRandao_reveal(),
get_domain(state.getFork(), get_current_epoch(state), DOMAIN_RANDAO)),
"Provided randao value is invalid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,8 @@ public static void finish_epoch_update(BeaconState state) {
.getLatest_active_index_roots()
.set(
index_root_position,
HashTreeUtil.hash_tree_root(SSZTypes.LIST_OF_BASIC,
HashTreeUtil.hash_tree_root(
SSZTypes.LIST_OF_BASIC,
active_validator_indices.stream()
.map(item -> SSZ.encodeUInt64(item))
.collect(Collectors.toList())));
Expand Down
Loading

0 comments on commit a735c19

Please sign in to comment.