Skip to content

Commit

Permalink
Rename latest_penalized_exit_balances (Consensys#239)
Browse files Browse the repository at this point in the history
* Rename `latest_penalized_exit_balances`

Brings into line with 1.0 spec.
Addresses part of Consensys#235.

* rebase from master
  • Loading branch information
benjaminion authored and dangerousfood committed Feb 17, 2019
1 parent 7b5422e commit 0d9821c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 61 deletions.
Expand Up @@ -87,7 +87,7 @@ public class BeaconState {
// Recent state
private ArrayList<CrosslinkRecord> latest_crosslinks;
private ArrayList<Bytes32> latest_block_roots = new ArrayList<>();
private ArrayList<Double> latest_penalized_exit_balances;
private ArrayList<Double> latest_penalized_balances;
private ArrayList<PendingAttestationRecord> latest_attestations;
private ArrayList<Bytes32> batched_block_roots = new ArrayList<>();

Expand Down Expand Up @@ -132,7 +132,7 @@ public BeaconState(
// Recent state
ArrayList<CrosslinkRecord> latest_crosslinks,
ArrayList<Bytes32> latest_block_roots,
ArrayList<Double> latest_penalized_exit_balances,
ArrayList<Double> latest_penalized_balances,
ArrayList<PendingAttestationRecord> latest_attestations,
ArrayList<Bytes32> batched_block_roots,
// Ethereum 1.0 chain data
Expand Down Expand Up @@ -164,7 +164,7 @@ public BeaconState(
// Recent state
this.latest_crosslinks = latest_crosslinks;
this.latest_block_roots = latest_block_roots;
this.latest_penalized_exit_balances = latest_penalized_exit_balances;
this.latest_penalized_balances = latest_penalized_balances;
this.latest_attestations = latest_attestations;
this.batched_block_roots = batched_block_roots;

Expand Down Expand Up @@ -495,9 +495,9 @@ public void exit_validator(BeaconState state, int index, int new_status) {

if (new_status == EXITED_WITH_PENALTY) {
int lpeb_index = toIntExact(state.getSlot()) / COLLECTIVE_PENALTY_CALCULATION_PERIOD;
latest_penalized_exit_balances.set(
latest_penalized_balances.set(
lpeb_index,
latest_penalized_exit_balances.get(lpeb_index) + get_effective_balance(state, index));
latest_penalized_balances.get(lpeb_index) + get_effective_balance(state, index));

int whistleblower_index = get_beacon_proposer_index(state, toIntExact(state.getSlot()));
double whistleblower_reward =
Expand Down Expand Up @@ -919,12 +919,12 @@ public ArrayList<ArrayList<ShardCommittee>> getShard_committees_at_slots() {
return shard_committees_at_slots;
}

public ArrayList<Double> getLatest_penalized_exit_balances() {
return latest_penalized_exit_balances;
public ArrayList<Double> getLatest_penalized_balances() {
return latest_penalized_balances;
}

public void setLatest_penalized_exit_balances(ArrayList<Double> latest_penalized_exit_balances) {
this.latest_penalized_exit_balances = latest_penalized_exit_balances;
public void setLatest_penalized_balances(ArrayList<Double> latest_penalized_balances) {
this.latest_penalized_balances = latest_penalized_balances;
}

public ArrayList<Bytes32> getLatest_randao_mixes() {
Expand Down
Expand Up @@ -111,12 +111,12 @@ public static void update_validator_registry(BeaconState state) {

int period_index =
Math.toIntExact(state.getSlot() / Constants.COLLECTIVE_PENALTY_CALCULATION_PERIOD);
ArrayList<Double> latest_penalized_exit_balances = state.getLatest_penalized_exit_balances();
ArrayList<Double> latest_penalized_balances = state.getLatest_penalized_balances();

double total_penalties =
latest_penalized_exit_balances.get(period_index)
+ latest_penalized_exit_balances.get(period_index - 1 < 0 ? period_index - 1 : 0)
+ latest_penalized_exit_balances.get(period_index - 2 < 0 ? period_index - 2 : 0);
latest_penalized_balances.get(period_index)
+ latest_penalized_balances.get(period_index - 1 < 0 ? period_index - 1 : 0)
+ latest_penalized_balances.get(period_index - 2 < 0 ? period_index - 2 : 0);

ArrayList<Validator> to_penalize = to_penalize(active_validators);
}
Expand Down Expand Up @@ -182,54 +182,56 @@ private static ArrayList<Validator> to_penalize(ArrayList<Validator> validator_r
}

private static void process_penalties_and_exits(BeaconState state) {
// todo after the spec refactor status, penalized_slot no longer exists
// Validators active_validators =
// ValidatorsUtil.get_active_validators(state.getValidator_registry());
// double total_balance = ValidatorsUtil.get_effective_balance(active_validators);
// ListIterator<Validator> itr =
// (ListIterator<Validator>) active_validators.iterator();
//
// while (itr.hasNext()) {
// int index = itr.nextIndex();
// Validator validator = itr.next();
//
// if (Math.floor(state.getSlot() / Constants.EPOCH_LENGTH)
// == Math.floor(validator.getPenalized_slot() / Constants.EPOCH_LENGTH)
// + Math.floor(Constants.LATEST_PENALIZED_EXIT_LENGTH / 2)) {
// int e =
// (int) Math.floor(state.getSlot() / Constants.EPOCH_LENGTH)
// % Constants.LATEST_PENALIZED_EXIT_LENGTH;
// ;
// double total_at_start =
// state
// .getLatest_penalized_exit_balances()
// .get((e + 1) % Constants.LATEST_PENALIZED_EXIT_LENGTH);
// double total_at_end = state.getLatest_penalized_exit_balances().get(e);
// double total_penalties = total_at_end - total_at_start;
// double penalty =
// BeaconStateUtil.get_effective_balance(state, validator)
// * Math.min(total_penalties * 3, total_balance); // total_balance
// state
// .getValidator_balances()
// .set(index, state.getValidator_balances().get(index) - penalty);
// }
// }
// Validators eligible_validators = new Validators();
// for (Validator validator : active_validators) {
// if (eligible(state, validator)) eligible_validators.add(validator);
// }
// Collections.sort(
// eligible_validators,
// (a, b) -> {
// return a.getExit_count().compareTo(b.getExit_count());
// });
//
// int withdrawn_so_far = 0;
// for (Validator validator : eligible_validators) {
// validator.setStatus(UnsignedLong.valueOf(Constants.WITHDRAWABLE));
// withdrawn_so_far += 1;
// if (withdrawn_so_far >= Constants.MAX_WITHDRAWALS_PER_EPOCH) break;
// }
// todo: penalized slot no longer exists in 0.1 spec
/*
Validators active_validators =
ValidatorsUtil.get_active_validators(state.getValidator_registry());
double total_balance = ValidatorsUtil.get_effective_balance(active_validators);
ListIterator<Validator> itr =
(ListIterator<Validator>) active_validators.iterator();
while (itr.hasNext()) {
int index = itr.nextIndex();
Validator validator = itr.next();
if (Math.floor(state.getSlot() / Constants.EPOCH_LENGTH)
== Math.floor(validator.getPenalized_slot() / Constants.EPOCH_LENGTH)
+ Math.floor(Constants.LATEST_PENALIZED_EXIT_LENGTH / 2)) {
int e =
(int) Math.floor(state.getSlot() / Constants.EPOCH_LENGTH)
% Constants.LATEST_PENALIZED_EXIT_LENGTH;
;
double total_at_start =
state
.getLatest_penalized_balances()
.get((e + 1) % Constants.LATEST_PENALIZED_EXIT_LENGTH);
double total_at_end = state.getLatest_penalized_balances().get(e);
double total_penalties = total_at_end - total_at_start;
double penalty =
BeaconStateUtil.get_effective_balance(state, validator)
* Math.min(total_penalties * 3, total_balance); // total_balance
state
.getValidator_balances()
.set(index, state.getValidator_balances().get(index) - penalty);
}
}
Validators eligible_validators = new Validators();
for (Validator validator : active_validators) {
if (eligible(state, validator)) eligible_validators.add(validator);
}
Collections.sort(
eligible_validators,
(a, b) -> {
return a.getExit_count().compareTo(b.getExit_count());
});
int withdrawn_so_far = 0;
for (Validator validator : eligible_validators) {
validator.setStatus(UnsignedLong.valueOf(Constants.WITHDRAWABLE));
withdrawn_so_far += 1;
if (withdrawn_so_far >= Constants.MAX_WITHDRAWALS_PER_EPOCH) break;
}
*/
}

private static boolean eligible(BeaconState state, Validator validator) {
Expand Down

0 comments on commit 0d9821c

Please sign in to comment.