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

feature/slashable-attestation-verification #66

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
56 changes: 0 additions & 56 deletions Sources/BeaconChain/BeaconChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,62 +273,6 @@ extension BeaconChain {

extension BeaconChain {

static func verifySlashableAttestation(state: BeaconState, slashableAttestation: SlashableAttestation) -> Bool {
if slashableAttestation.custodyBitfield != Data(repeating: 0, count: slashableAttestation.custodyBitfield.count) {
return false
}

if slashableAttestation.validatorIndices.count == 0 {
return false
}

for i in 0..<(slashableAttestation.validatorIndices.count - 1) {
if slashableAttestation.validatorIndices[i] >= slashableAttestation.validatorIndices[i + 1] {
return false
}
}

if !verifyBitfield(bitfield: slashableAttestation.custodyBitfield, committeeSize: slashableAttestation.validatorIndices.count) {
return false
}

if slashableAttestation.validatorIndices.count > MAX_INDICES_PER_SLASHABLE_VOTE {
return false
}

var custodyBit0Indices = [UInt64]()
var custodyBit1Indices = [UInt64]()

for (i, validatorIndex) in slashableAttestation.validatorIndices.enumerated() {
if getBitfieldBit(bitfield: slashableAttestation.custodyBitfield, i: i) == 0b0 {
custodyBit0Indices.append(validatorIndex)
} else {
custodyBit1Indices.append(validatorIndex)
}
}

return BLS.verify(
pubkeys: [
BLS.aggregate(
pubkeys: custodyBit0Indices.map { (i) in
return state.validatorRegistry[Int(i)].pubkey
}
),
BLS.aggregate(
pubkeys: custodyBit1Indices.map { (i) in
return state.validatorRegistry[Int(i)].pubkey
}
)
],
messages: [
hashTreeRoot(AttestationDataAndCustodyBit(data: slashableAttestation.data, custodyBit: false)),
hashTreeRoot(AttestationDataAndCustodyBit(data: slashableAttestation.data, custodyBit: true)),
],
signature: slashableAttestation.aggregateSignature,
domain: getDomain(fork: state.fork, epoch: slashableAttestation.data.slot.toEpoch(), domainType: Domain.ATTESTATION)
)
}

static func isDoubleVote(_ left: AttestationData, _ right: AttestationData) -> Bool {
return left.slot.toEpoch() == right.slot.toEpoch()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,60 @@ struct SlashableAttestation {
let data: AttestationData
let custodyBitfield: Data
let aggregateSignature: Data

func verify(state: BeaconState) -> Bool {
if custodyBitfield != Data(repeating: 0, count: custodyBitfield.count) {
return false
}

if validatorIndices.count == 0 {
return false
}

for i in 0..<(validatorIndices.count - 1) {
if validatorIndices[i] >= validatorIndices[i + 1] {
return false
}
}

if !BeaconChain.verifyBitfield(bitfield: custodyBitfield, committeeSize: validatorIndices.count) {
return false
}

if validatorIndices.count > MAX_INDICES_PER_SLASHABLE_VOTE {
return false
}

var custodyBit0Indices = [UInt64]()
var custodyBit1Indices = [UInt64]()

for (i, validatorIndex) in validatorIndices.enumerated() {
if BeaconChain.getBitfieldBit(bitfield: custodyBitfield, i: i) == 0b0 {
custodyBit0Indices.append(validatorIndex)
} else {
custodyBit1Indices.append(validatorIndex)
}
}

return BLS.verify(
pubkeys: [
BLS.aggregate(
pubkeys: custodyBit0Indices.map { (i) in
return state.validatorRegistry[Int(i)].pubkey
}
),
BLS.aggregate(
pubkeys: custodyBit1Indices.map { (i) in
return state.validatorRegistry[Int(i)].pubkey
}
)
],
messages: [
BeaconChain.hashTreeRoot(AttestationDataAndCustodyBit(data: data, custodyBit: false)),
BeaconChain.hashTreeRoot(AttestationDataAndCustodyBit(data: data, custodyBit: true)),
],
signature: aggregateSignature,
domain: BeaconChain.getDomain(fork: state.fork, epoch: data.slot.toEpoch(), domainType: Domain.ATTESTATION)
)
}
}
4 changes: 2 additions & 2 deletions Sources/BeaconChain/StateTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ extension StateTransition {
|| BeaconChain.isSurroundVote(slashableAttestation1.data, slashableAttestation2.data)
)

assert(BeaconChain.verifySlashableAttestation(state: state, slashableAttestation: slashableAttestation1))
assert(BeaconChain.verifySlashableAttestation(state: state, slashableAttestation: slashableAttestation2))
assert(slashableAttestation1.verify(state: state))
assert(slashableAttestation2.verify(state: state))

let slashableIndices = slashableAttestation1.validatorIndices.filter {
slashableAttestation2.validatorIndices.contains($0)
Expand Down