Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Greg/initial state && The Great Refactor #73

Merged
merged 21 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
node_modules/
test:ethereum
test:ethereum
beaconChain/node_modules/
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 4 additions & 1 deletion beaconChain/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
// Misc
export const SHARD_COUNT = 2 ** 10; // 1024 shards
export const TARGET_COMMITTEE_SIZE = 2 ** 8; // 256 validators
export const MIN_BALANCE = 2 ** 4; // 16 ETH
export const EJECTION_BALANCE = 2 ** 4; // 16 ETH
export const MAX_BALANCE_CHURN_QUOTIENT = 2 ** 5; // 32
export const GWEI_PER_ETH = 10 ** 9; // 1B Gwei/ETH
export const BEACON_CHAIN_SHARD_NUMBER = 2 ** 64 - 1;
export const BLS_WITHDRAWAL_PREFIX_BYTE = 0x00;
export const MAX_CASPER_VOTES = 2 ** 10; // 1024 votes
export const LATEST_BLOCK_ROOTS_LENGTH = 2 ** 13; // 8192 block roots
export const LATEST_RANDAO_MIXES_LENGTH = 2 ** 13; // 8192 randao mixes
export const EMPTY_SIGNATURE = [new Uint8Array(48), new Uint8Array(48)]; // [bytes48(0), bytes48(0)]

// Deposit contract
export const DEPOSIT_CONTRACT_ADDRESS = "TBD";
Expand Down
16 changes: 16 additions & 0 deletions beaconChain/helpers/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Takes two arrays and combines them similar to the Array.zip() form Python.
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
* a = [1,2,3] b = [4,5,6]
* c = zip(a,b) // [[1,4], [2,5], [3,6]]
* NOTE: Lodash would be an alternative, although theres no need *YET* to import lodash for one function.
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
* That being said there is one main caveat: if the arrays are not 1-1 in length it will return undefined
* but, this is fine for our use case since validatorBalances and validators must be 1-1 in length.
* @param {Array<T>} a
* @param {Array<X>} b
* @returns {(T | X)[][]}
*/
function zip<T, X>(a: T[], b: X[]): Array<Array<T|X>> {
return a.map((e, i) => [e, b[i]]);
}

export { zip };
23 changes: 13 additions & 10 deletions beaconChain/helpers/stateTransitionHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { keccakAsU8a } from "@polkadot/util-crypto";
// Helper functions related to state transition functions
import { EPOCH_LENGTH, MAX_DEPOSIT, SHARD_COUNT, TARGET_COMMITTEE_SIZE } from "../constants/constants";
import {
EPOCH_LENGTH, GWEI_PER_ETH, LATEST_BLOCK_ROOTS_LENGTH, MAX_DEPOSIT, SHARD_COUNT,
TARGET_COMMITTEE_SIZE,
} from "../constants/constants";
import { ValidatorStatusCodes } from "../constants/enums";
import {AttestationData, BeaconBlock} from "../interfaces/blocks";
import {BeaconState, ShardCommittee, ValidatorRecord} from "../interfaces/state";
Expand Down Expand Up @@ -186,12 +189,11 @@ function getShardCommitteesAtSlot(state: BeaconState, slot: int): ShardCommittee
* @param {int} slot
* @returns {hash32}
*/
function getBlockHash(state: BeaconState, slot: int): hash32 {
const earliestSlotInArray = state.slot - state.latestBlockHashes.length;
if (earliestSlotInArray <= slot && slot < state.slot) {
throw new Error();
}
return state.latestBlockHashes[slot - earliestSlotInArray];
function getBlockRoot(state: BeaconState, slot: int): hash32 {
// Returns the block root at a recent ``slot``.
if (state.slot <= slot + LATEST_BLOCK_ROOTS_LENGTH) { throw new Error(); }
if (slot < state.slot) { throw new Error(); }
return state.latestBlockRoots[slot % LATEST_BLOCK_ROOTS_LENGTH];
}

/**
Expand All @@ -200,7 +202,7 @@ function getBlockHash(state: BeaconState, slot: int): hash32 {
* @param {int} slot
* @returns {int}
*/
function getBeaconProposerIndex(state: BeaconState, slot: int): int {
export function getBeaconProposerIndex(state: BeaconState, slot: int): int {
const firstCommittee = getShardCommitteesAtSlot(state, slot)[0].committee;
return firstCommittee[slot % firstCommittee.length];
}
Expand Down Expand Up @@ -231,8 +233,9 @@ function getAttestationParticipants(state: BeaconState, attestationData: Attesta
* @returns {int}
*/
// TODO Math.min requires int, validator.record is a uint64
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
function getEffectiveBalance(validator: ValidatorRecord): int {
return Math.min(validator.balance, MAX_DEPOSIT);
export function getEffectiveBalance(state: BeaconState, index: int): int {
// Returns the effective balance (also known as "balance at stake") for a ``validator`` with the given ``index``.
return Math.min(state.validatorBalances[index], MAX_DEPOSIT * GWEI_PER_ETH);
}

// TODO figure out what bytes1() does in python
Expand Down
12 changes: 7 additions & 5 deletions beaconChain/interfaces/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type bytes = number;
type uint24 = number;
type uint64 = number;
type uint384 = number;
type hash32 = number;
type hash32 = Uint8Array;

// Beacon chain operations

Expand Down Expand Up @@ -82,22 +82,24 @@ export interface Deposit {

export interface DepositData {
// Deposit parameters
depositParameters: DepositParameters;
depositInput: DepositInput;
// Value in Gwei
value: uint64;
// Timestamp from deposit contract
timestamp: uint64;
}

export interface DepositParameters {
export interface DepositInput {
// BLS pubkey
pubkey: uint384;
// BLS proof of possession (a BLS signature)
proofOfPossession: uint384[];
// Withdrawal credentials
withdrawalCredentials: hash32;
// Initial RANDAO commitment
randaoCommitment: hash32;
// Initial custody commitment
custodyCommitment: hash32;
// BLS proof of possession (a BLS signature)
proofOfPossession: Uint8Array[];
}

export interface Exit {
Expand Down
44 changes: 27 additions & 17 deletions beaconChain/interfaces/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ type uint384 = number;
type hash32 = Uint8Array;

export interface BeaconState {
// Misc
slot: uint64;
genesisTime: uint64;
// For versioning hard forks
forkData: ForkData;
forkData: ForkData; // For versioning hard forks

// Validator registry
validatorRegistry: ValidatorRecord[];
validatorBalances: uint64[];
validatorRegistryLatestChangeSlot: uint64;
validatorRegistryExitCount: uint64;
// For light clients to track deltas
validatorRegistryDeltaChainTip: hash32;
validatorRegistryDeltaChainTip: hash32; // For light clients to track deltas

// Randomness and committees
randaoMix: hash32;
nextSeed: hash32;
latestRandaoMixes: hash32[];
latestVdfOutputs: hash32[];
shardCommitteesAtSlots: ShardCommittee[][];
persistentCommittees: uint24[][];
persistentCommitteeReassignments: ShardReassignmentRecord[];

// Custody Challenges
custodyChallenges: CustodyChallenge[];

// Finality
previousJustifiedSlot: uint64;
Expand All @@ -38,11 +39,10 @@ export interface BeaconState {

// Recent state
latestCrosslinks: CrosslinkRecord[];
// Needed to process attestations; older to newer
latestBlockHashes: hash32[];
// Balances penalized at every withdrawal period
latestPenalizedExitBalances: uint64[];
latestBlockRoots: hash32[]; // Needed to process attestations; older to newer
latestPenalizedExitBalances: uint64[]; // Balances penalized at every withdrawal period
latestAttestations: PendingAttestationRecord[];
batchedBlockRoots: hash32[];

// PoW receipt root
processedPowReceiptRoot: hash32;
Expand All @@ -56,16 +56,20 @@ export interface ValidatorRecord {
withdrawalCredentials: hash32;
// RANDAO commitment
randaoCommitment: hash32;
// Slot the proposer has skipped (ie. layers of RANDAO expected)
randaoSkips: uint64;
// Balance in Gwei
balance: uint64;
// Slots the proposer has skipped (i.e. layers of RANDAO expected)
randaoLayers: uint64;
// Status code
status: uint64;
// Slot when validator last changed status (or 0)
lastStatusChangeSlot: uint64;
latestStatusChangeSlot: uint64;
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
// Exit counter when validator exited (or 0)
exitCount: uint64;
// Custody Commitment
custodyCommitment: hash32;
// Slot of latest custody reseed
latestCustodyReseedSlot: uint64;
// Slotof second-latest custody reseed
penultimateCustodyResseedSlot: uint64;
}

export interface CrosslinkRecord {
Expand Down Expand Up @@ -118,3 +122,9 @@ export interface ForkData {
// Fork slot number
forkSlot: uint64;
}

/* tslint:disable:no-empty-interface*/
// Empty for Phase 0
export interface CustodyReseed {}
export interface CustodyChallenge {}
export interface CustodyResponse {}
Loading