From aeb7cad0bf38616252d3afa9b3af4800788c9e02 Mon Sep 17 00:00:00 2001 From: decanus Date: Sun, 10 Mar 2019 01:04:06 +0100 Subject: [PATCH 1/2] started working ons tate factory --- .../BeaconChain/Factories/StateFactory.swift | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Sources/BeaconChain/Factories/StateFactory.swift diff --git a/Sources/BeaconChain/Factories/StateFactory.swift b/Sources/BeaconChain/Factories/StateFactory.swift new file mode 100644 index 0000000..3a34c0c --- /dev/null +++ b/Sources/BeaconChain/Factories/StateFactory.swift @@ -0,0 +1,72 @@ +import Foundation + +class StateFactory { + + static func createInitialBeaconState( + genesisValidatorDeposits: [Deposit], + genesisTime: UInt64, + latestEth1Data: Eth1Data + ) -> BeaconState { + + var state = genesisState( + genesisTime: genesisTime, + latestEth1Data: latestEth1Data, + depositLength: genesisValidatorDeposits.count + ) + + for deposit in genesisValidatorDeposits { + processDeposit(state: &state, deposit: deposit) + } + + for (i, _) in state.validatorRegistry.enumerated() { + if getEffectiveBalance(state: state, index: ValidatorIndex(i)) >= MAX_DEPOSIT_AMOUNT { + state.validatorRegistry[i].activate(state: state, genesis: true) + } + } + + let genesisActiveIndexRoot = hashTreeRoot(state.validatorRegistry.activeIndices(epoch: GENESIS_EPOCH)) + + for i in 0.. BeaconState { + return BeaconState( + slot: GENESIS_SLOT, + genesisTime: genesisTime, + fork: Fork( + previousVersion: GENESIS_FORK_VERSION, + currentVersion: GENESIS_FORK_VERSION, + epoch: GENESIS_EPOCH + ), + validatorRegistry: [Validator](), + validatorBalances: [UInt64](), + validatorRegistryUpdateEpoch: GENESIS_EPOCH, + latestRandaoMixes: [Data](repeating: EMPTY_SIGNATURE, count: Int(LATEST_RANDAO_MIXES_LENGTH)), + previousShufflingStartShard: GENESIS_START_SHARD, + currentShufflingStartShard: GENESIS_START_SHARD, + previousShufflingEpoch: GENESIS_EPOCH, + currentShufflingEpoch: GENESIS_EPOCH, + previousShufflingSeed: ZERO_HASH, + currentShufflingSeed: ZERO_HASH, + previousJustifiedEpoch: GENESIS_EPOCH, + justifiedEpoch: GENESIS_EPOCH, + justificationBitfield: 0, + finalizedEpoch: GENESIS_EPOCH, + latestCrosslinks: [Crosslink](repeating: Crosslink(epoch: GENESIS_EPOCH, crosslinkDataRoot: ZERO_HASH), count: Int(SHARD_COUNT)), + latestBlockRoots: [Data](repeating: ZERO_HASH, count: Int(LATEST_BLOCK_ROOTS_LENGTH)), + latestActiveIndexRoots: [Data](repeating: ZERO_HASH, count: Int(LATEST_ACTIVE_INDEX_ROOTS_LENGTH)), + latestSlashedBalances: [UInt64](repeating: 0, count: Int(LATEST_SLASHED_EXIT_LENGTH)), + latestAttestations: [PendingAttestation](), + batchedBlockRoots: [Data](), + latestEth1Data: latestEth1Data, + eth1DataVotes: [Eth1DataVote](), + depositIndex: UInt64(depositLength) + ) + } +} \ No newline at end of file From b276e7e6a3809d656fa04d79312ffbe02beb9a39 Mon Sep 17 00:00:00 2001 From: decanus Date: Tue, 12 Mar 2019 18:57:55 +0100 Subject: [PATCH 2/2] moved --- Sources/BeaconChain/Factories/StateFactory.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/BeaconChain/Factories/StateFactory.swift b/Sources/BeaconChain/Factories/StateFactory.swift index 3a34c0c..8631db5 100644 --- a/Sources/BeaconChain/Factories/StateFactory.swift +++ b/Sources/BeaconChain/Factories/StateFactory.swift @@ -15,22 +15,22 @@ class StateFactory { ) for deposit in genesisValidatorDeposits { - processDeposit(state: &state, deposit: deposit) + BeaconChain.processDeposit(state: &state, deposit: deposit) } for (i, _) in state.validatorRegistry.enumerated() { - if getEffectiveBalance(state: state, index: ValidatorIndex(i)) >= MAX_DEPOSIT_AMOUNT { + if BeaconChain.getEffectiveBalance(state: state, index: ValidatorIndex(i)) >= MAX_DEPOSIT_AMOUNT { state.validatorRegistry[i].activate(state: state, genesis: true) } } - let genesisActiveIndexRoot = hashTreeRoot(state.validatorRegistry.activeIndices(epoch: GENESIS_EPOCH)) + let genesisActiveIndexRoot = BeaconChain.hashTreeRoot(state.validatorRegistry.activeIndices(epoch: GENESIS_EPOCH)) for i in 0..