Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

enhancement/merkle #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 0 additions & 11 deletions Sources/BeaconChain/BeaconChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,6 @@ extension BeaconChain {
return firstCommittee[Int(slot) % firstCommittee.count]
}

static func merkleRoot(values: [Bytes32]) -> Bytes32 {
var o = [Data](repeating: Data(repeating: 0, count: 1), count: values.count - 1)
o.append(contentsOf: values)

for i in stride(from: values.count - 1, through: 0, by: -1) {
o[i] = hash(o[i * 2] + o[i * 2 + 1])
}

return o[1]
}

static func getAttestationParticipants(
state: BeaconState,
attestationData: AttestationData,
Expand Down
28 changes: 28 additions & 0 deletions Sources/BeaconChain/Merkle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Foundation

class Merkle {

static func root(_ values: [Bytes32]) -> Bytes32 {
var o = [Data](repeating: Data(repeating: 0, count: 1), count: values.count - 1)
o.append(contentsOf: values)

for i in stride(from: values.count - 1, through: 0, by: -1) {
o[i] = BeaconChain.hash(o[i * 2] + o[i * 2 + 1])
}

return o[1]
}

static func verifyBranch(leaf: Bytes32, branch: [Bytes32], depth: Int, index: Int, root: Bytes32) -> Bool {
var value = leaf
for i in 0..<depth {
if index / (2 ** i) % 2 == 1 {
value = BeaconChain.hash(branch[i] + value)
} else {
value = BeaconChain.hash(value + branch[i])
}
}

return value == root
}
}
17 changes: 2 additions & 15 deletions Sources/BeaconChain/StateTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class StateTransition {
state.latestBlockRoots[Int((state.slot - 1) % LATEST_BLOCK_ROOTS_LENGTH)] = previousBlockRoot

if state.slot % LATEST_BLOCK_ROOTS_LENGTH == 0 {
state.batchedBlockRoots.append(BeaconChain.merkleRoot(values: state.latestBlockRoots))
state.batchedBlockRoots.append(Merkle.root(state.latestBlockRoots))
}
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ extension StateTransition {
let serializedDepositData = Data(count: 64) // @todo when we have SSZ

assert(
verifyMerkleBranch(
Merkle.verifyBranch(
leaf: BeaconChain.hash(serializedDepositData),
branch: deposit.branch,
depth: Int(DEPOSIT_CONTRACT_TREE_DEPTH),
Expand Down Expand Up @@ -297,19 +297,6 @@ extension StateTransition {
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..<depth {
if index / (2 ** i) % 2 == 1 {
value = BeaconChain.hash(branch[i] + value)
} else {
value = BeaconChain.hash(value + branch[i])
}
}

return value == root
}
}

extension StateTransition {
Expand Down