From 8286fb3a397b9fe832564bdc366eee1723da9f2e Mon Sep 17 00:00:00 2001 From: decanus Date: Sun, 10 Mar 2019 00:53:57 +0100 Subject: [PATCH 1/4] moved block transitions --- Sources/BeaconChain/StateTransition.swift | 34 +---------------- .../Blocks/BlockTransitions.swift | 6 +++ .../StateTransitions/Blocks/Transfers.swift | 37 +++++++++++++++++++ 3 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 Sources/BeaconChain/StateTransitions/Blocks/BlockTransitions.swift create mode 100644 Sources/BeaconChain/StateTransitions/Blocks/Transfers.swift diff --git a/Sources/BeaconChain/StateTransition.swift b/Sources/BeaconChain/StateTransition.swift index 16587eb..8f5b8fa 100644 --- a/Sources/BeaconChain/StateTransition.swift +++ b/Sources/BeaconChain/StateTransition.swift @@ -27,6 +27,7 @@ extension StateTransition { attestations(state: &state, block: block) deposits(state: &state, block: block) voluntaryExits(state: &state, block: block) + Transfers.transition(state: &state, block: block) } static func blockSignature(state: inout BeaconState, block: BeaconBlock) { @@ -265,39 +266,6 @@ extension StateTransition { } } - static func transfers(state: inout BeaconState, block: BeaconBlock) { - assert(block.body.transfers.count <= MAX_TRANSFERS) - - for transfer in block.body.transfers { - assert(state.validatorBalances[Int(transfer.from)] >= transfer.amount) - assert(state.validatorBalances[Int(transfer.from)] >= transfer.fee) - assert( - state.validatorBalances[Int(transfer.from)] == transfer.amount + transfer.fee - || state.validatorBalances[Int(transfer.from)] >= transfer.amount + transfer.fee + MIN_DEPOSIT_AMOUNT - ) - - assert(state.slot == transfer.slot) - assert( - BeaconChain.getCurrentEpoch(state: state) >= state.validatorRegistry[Int(transfer.from)].withdrawableEpoch - || state.validatorRegistry[Int(transfer.from)].activationEpoch == FAR_FUTURE_EPOCH - ) - assert(state.validatorRegistry[Int(transfer.from)].withdrawalCredentials == BLS_WITHDRAWAL_PREFIX_BYTE + BeaconChain.hash(transfer.pubkey).suffix(from: 1)) - - assert( - BLS.verify( - pubkey: transfer.pubkey, - message: BeaconChain.signedRoot(transfer, field: "signature"), - signature: transfer.signature, - domain: state.fork.domain(epoch: transfer.slot.toEpoch(), type: .transfer) - ) - ) - - state.validatorBalances[Int(transfer.from)] -= transfer.amount + transfer.fee - state.validatorBalances[Int(transfer.to)] += transfer.amount - state.validatorBalances[Int(BeaconChain.getBeaconProposerIndex(state: state, slot: state.slot))] += transfer.fee - } - } - static func verifyMerkleBranch(leaf: Bytes32, branch: [Bytes32], depth: Int, index: Int, root: Bytes32) -> Bool { var value = leaf for i in 0..= transfer.amount) + assert(state.validatorBalances[Int(transfer.from)] >= transfer.fee) + assert( + state.validatorBalances[Int(transfer.from)] == transfer.amount + transfer.fee + || state.validatorBalances[Int(transfer.from)] >= transfer.amount + transfer.fee + MIN_DEPOSIT_AMOUNT + ) + + assert(state.slot == transfer.slot) + assert( + BeaconChain.getCurrentEpoch(state: state) >= state.validatorRegistry[Int(transfer.from)].withdrawableEpoch + || state.validatorRegistry[Int(transfer.from)].activationEpoch == FAR_FUTURE_EPOCH + ) + assert(state.validatorRegistry[Int(transfer.from)].withdrawalCredentials == BLS_WITHDRAWAL_PREFIX_BYTE + BeaconChain.hash(transfer.pubkey).suffix(from: 1)) + + assert( + BLS.verify( + pubkey: transfer.pubkey, + message: BeaconChain.signedRoot(transfer, field: "signature"), + signature: transfer.signature, + domain: state.fork.domain(epoch: transfer.slot.toEpoch(), type: .transfer) + ) + ) + + state.validatorBalances[Int(transfer.from)] -= transfer.amount + transfer.fee + state.validatorBalances[Int(transfer.to)] += transfer.amount + state.validatorBalances[Int(BeaconChain.getBeaconProposerIndex(state: state, slot: state.slot))] += transfer.fee + } + } +} From 1a632c7232ab39a86d71857fc56fa25cfd78738d Mon Sep 17 00:00:00 2001 From: decanus Date: Sun, 10 Mar 2019 00:56:22 +0100 Subject: [PATCH 2/4] more state transitions --- Sources/BeaconChain/StateTransition.swift | 52 +------------------ .../StateTransitions/Blocks/Deposits.swift | 29 +++++++++++ .../Blocks/VoluntaryExits.swift | 27 ++++++++++ 3 files changed, 58 insertions(+), 50 deletions(-) create mode 100644 Sources/BeaconChain/StateTransitions/Blocks/Deposits.swift create mode 100644 Sources/BeaconChain/StateTransitions/Blocks/VoluntaryExits.swift diff --git a/Sources/BeaconChain/StateTransition.swift b/Sources/BeaconChain/StateTransition.swift index 8f5b8fa..9585154 100644 --- a/Sources/BeaconChain/StateTransition.swift +++ b/Sources/BeaconChain/StateTransition.swift @@ -25,8 +25,8 @@ extension StateTransition { proposerSlashings(state: &state, block: block) attesterSlashings(state: &state, block: block) attestations(state: &state, block: block) - deposits(state: &state, block: block) - voluntaryExits(state: &state, block: block) + Deposits.transition(state: &state, block: block) + VoluntaryExits.transition(state: &state, block: block) Transfers.transition(state: &state, block: block) } @@ -218,54 +218,6 @@ extension StateTransition { } } - static func deposits(state: inout BeaconState, block: BeaconBlock) { - assert(block.body.deposits.count <= MAX_DEPOSITS) - - for deposit in block.body.deposits { - let serializedDepositData = Data(count: 64) // @todo when we have SSZ - - assert( - verifyMerkleBranch( - leaf: BeaconChain.hash(serializedDepositData), - branch: deposit.branch, - depth: Int(DEPOSIT_CONTRACT_TREE_DEPTH), - index: Int(deposit.index), - root: state.latestEth1Data.depositRoot - ) - ) - - BeaconChain.processDeposit( - state: &state, - deposit: deposit - ) - - state.depositIndex += 1 - } - } - - static func voluntaryExits(state: inout BeaconState, block: BeaconBlock) { - assert(block.body.voluntaryExits.count <= MAX_VOLUNTARY_EXITS) - - for exit in block.body.voluntaryExits { - let validator = state.validatorRegistry[Int(exit.validatorIndex)] - - let epoch = BeaconChain.getCurrentEpoch(state: state) - assert(validator.exitEpoch > epoch.delayedActivationExitEpoch()) - assert(epoch >= exit.epoch) - - assert( - BLS.verify( - pubkey: validator.pubkey, - message: BeaconChain.signedRoot(exit, field: "signature"), - signature: exit.signature, - domain: state.fork.domain(epoch: exit.epoch, type: .exit) - ) - ) - - state.validatorRegistry[Int(exit.validatorIndex)].initiatedExit = true - } - } - static func verifyMerkleBranch(leaf: Bytes32, branch: [Bytes32], depth: Int, index: Int, root: Bytes32) -> Bool { var value = leaf for i in 0.. epoch.delayedActivationExitEpoch()) + assert(epoch >= exit.epoch) + + assert( + BLS.verify( + pubkey: validator.pubkey, + message: BeaconChain.signedRoot(exit, field: "signature"), + signature: exit.signature, + domain: state.fork.domain(epoch: exit.epoch, type: .exit) + ) + ) + + state.validatorRegistry[Int(exit.validatorIndex)].initiatedExit = true + } + } +} From 97f8e0e0c15a26e00a9e3cb1abccb12b8460df7a Mon Sep 17 00:00:00 2001 From: decanus Date: Sun, 10 Mar 2019 00:57:42 +0100 Subject: [PATCH 3/4] moved attestations --- Sources/BeaconChain/StateTransition.swift | 82 +----------------- .../Blocks/Attestations.swift | 84 +++++++++++++++++++ 2 files changed, 85 insertions(+), 81 deletions(-) create mode 100644 Sources/BeaconChain/StateTransitions/Blocks/Attestations.swift diff --git a/Sources/BeaconChain/StateTransition.swift b/Sources/BeaconChain/StateTransition.swift index 9585154..7c617bb 100644 --- a/Sources/BeaconChain/StateTransition.swift +++ b/Sources/BeaconChain/StateTransition.swift @@ -24,7 +24,7 @@ extension StateTransition { eth1data(state: &state, block: block) proposerSlashings(state: &state, block: block) attesterSlashings(state: &state, block: block) - attestations(state: &state, block: block) + Attestations.transition(state: &state, block: block) Deposits.transition(state: &state, block: block) VoluntaryExits.transition(state: &state, block: block) Transfers.transition(state: &state, block: block) @@ -138,86 +138,6 @@ extension StateTransition { } } - static func attestations(state: inout BeaconState, block: BeaconBlock) { - assert(block.body.attestations.count <= MAX_ATTESTATIONS) - - for attestation in block.body.attestations { - assert(attestation.data.slot >= GENESIS_SLOT) - assert(attestation.data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot) - assert(state.slot < attestation.data.slot + SLOTS_PER_EPOCH) - - let e = (attestation.data.slot + 1).toEpoch() >= BeaconChain.getCurrentEpoch(state: state) ? state.justifiedEpoch : state.previousJustifiedEpoch - assert(attestation.data.justifiedEpoch == e) - assert(attestation.data.justifiedBlockRoot == BeaconChain.getBlockRoot(state: state, slot: attestation.data.justifiedEpoch.startSlot())) - - assert( - state.latestCrosslinks[Int(attestation.data.shard)] == attestation.data.latestCrosslink || - state.latestCrosslinks[Int(attestation.data.shard)] == Crosslink( - epoch: attestation.data.slot.toEpoch(), - crosslinkDataRoot: attestation.data.crosslinkDataRoot - ) - ) - - assert(attestation.custodyBitfield == Data(repeating: 0, count: 32)) - assert(attestation.aggregationBitfield != Data(repeating: 0, count: 32)) - - let crosslinkCommittee = BeaconChain.crosslinkCommittees(state, at: attestation.data.slot).filter { - $0.1 == attestation.data.shard - }.first?.0 - - for i in 0.. Bool { var value = leaf for i in 0..= GENESIS_SLOT) + assert(attestation.data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot) + assert(state.slot < attestation.data.slot + SLOTS_PER_EPOCH) + + let e = (attestation.data.slot + 1).toEpoch() >= BeaconChain.getCurrentEpoch(state: state) ? state.justifiedEpoch : state.previousJustifiedEpoch + assert(attestation.data.justifiedEpoch == e) + assert(attestation.data.justifiedBlockRoot == BeaconChain.getBlockRoot(state: state, slot: attestation.data.justifiedEpoch.startSlot())) + + assert( + state.latestCrosslinks[Int(attestation.data.shard)] == attestation.data.latestCrosslink || + state.latestCrosslinks[Int(attestation.data.shard)] == Crosslink( + epoch: attestation.data.slot.toEpoch(), + crosslinkDataRoot: attestation.data.crosslinkDataRoot + ) + ) + + assert(attestation.custodyBitfield == Data(repeating: 0, count: 32)) + assert(attestation.aggregationBitfield != Data(repeating: 0, count: 32)) + + let crosslinkCommittee = BeaconChain.crosslinkCommittees(state, at: attestation.data.slot).filter { + $0.1 == attestation.data.shard + }.first?.0 + + for i in 0.. Date: Sun, 10 Mar 2019 00:58:51 +0100 Subject: [PATCH 4/4] moved correctly --- Sources/BeaconChain/StateTransitions/Blocks/Deposits.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/BeaconChain/StateTransitions/Blocks/Deposits.swift b/Sources/BeaconChain/StateTransitions/Blocks/Deposits.swift index 5cbd7d8..cc72e9a 100644 --- a/Sources/BeaconChain/StateTransitions/Blocks/Deposits.swift +++ b/Sources/BeaconChain/StateTransitions/Blocks/Deposits.swift @@ -9,7 +9,7 @@ class Deposits: BlockTransitions { let serializedDepositData = Data(count: 64) // @todo when we have SSZ assert( - verifyMerkleBranch( + StateTransition.verifyMerkleBranch( leaf: BeaconChain.hash(serializedDepositData), branch: deposit.branch, depth: Int(DEPOSIT_CONTRACT_TREE_DEPTH),