Skip to content

Commit

Permalink
Merge branch 'master' of github.com:AcalaNetwork/boka
Browse files Browse the repository at this point in the history
* 'master' of github.com:AcalaNetwork/boka:
  refactor bandersnatch (#113)
  • Loading branch information
MacOMNI committed Sep 13, 2024
2 parents df0a630 + e5a1bc3 commit a59766e
Show file tree
Hide file tree
Showing 25 changed files with 869 additions and 630 deletions.
8 changes: 7 additions & 1 deletion .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

if which swiftlint >/dev/null; then
swiftlint lint --fix
swiftlint lint --fix --quiet
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
Expand All @@ -10,3 +10,9 @@ git diff --diff-filter=d --staged --name-only | grep -e '\(.*\).swift$' | while
swiftformat "${line}";
git add "$line";
done

files=$((git diff --cached --name-only --diff-filter=ACMR | grep -Ei "\.rs$") || true)
if [ ! -z "${files}" ]; then
make format-cargo
git add $(echo "$files" | paste -s -d " " -)
fi
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ jobs:
- name: Build
run: make build
- name: Test
run: make test
run: make test-all
2 changes: 1 addition & 1 deletion Blockchain/Sources/Blockchain/Blockchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class Blockchain: Sendable {

public func importBlock(_ block: BlockRef) async throws {
try await withSpan("importBlock") { span in
span.attributes["hash"] = block.hash.description
span.attributes.blockHash = block.hash.description

let runtime = Runtime(config: config)
let parent = try await dataProvider.getState(hash: block.header.parentHash)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum SafroleError: Error {
case extrinsicsNotSorted
case extrinsicsTooLow
case extrinsicsNotUnique
case bandersnatchError(BandersnatchError)
case bandersnatchError(Bandersnatch.Error)
case other(any Swift.Error)
}

Expand Down Expand Up @@ -252,15 +252,23 @@ extension Safrole {
}

do {
let verifier = try Verifier(ring: nextValidators.map(\.bandersnatch))
let newVerifier = try Verifier(ring: validatorQueue.map(\.bandersnatch))
let ctx = try Bandersnatch.RingContext(size: UInt(config.value.totalNumberOfValidators))
let commitment = try Bandersnatch.RingCommitment(
ring: nextValidators.map { try Bandersnatch.PublicKey(data: $0.bandersnatch) },
ctx: ctx
)
let newCommitment = try Bandersnatch.RingCommitment(
ring: validatorQueue.map { try Bandersnatch.PublicKey(data: $0.bandersnatch) },
ctx: ctx
)
let verifier = Bandersnatch.Verifier(ctx: ctx, commitment: commitment)

let (newNextValidators, newCurrentValidators, newPreviousValidators, newTicketsVerifier) = isEpochChange
? (
withoutOffenders(offenders: offenders, validators: validatorQueue),
nextValidators,
currentValidators,
newVerifier.ringRoot
newCommitment.data
)
: (nextValidators, currentValidators, previousValidators, ticketsVerifier)

Expand Down Expand Up @@ -364,7 +372,7 @@ extension Safrole {
return (state: postState, epochMark: epochMark, ticketsMark: ticketsMark)
} catch let e as SafroleError {
throw e
} catch let e as BandersnatchError {
} catch let e as Bandersnatch.Error {
throw .bandersnatchError(e)
} catch {
throw .other(error)
Expand Down
6 changes: 3 additions & 3 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicTickets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ extension ExtrinsicTickets: Dummy {
}

extension ExtrinsicTickets {
public func getTickets(verifier: Verifier, entropy: Data32) throws -> [Ticket] {
public func getTickets(verifier: Bandersnatch.Verifier, entropy: Data32) throws -> [Ticket] {
try tickets.array.map {
var vrfInputData = SigningContext.ticketSeal
vrfInputData.append(entropy.data)
vrfInputData.append($0.attempt)
let ticketId = verifier.ringVRFVerify(vrfInputData: vrfInputData, auxData: Data(), signature: $0.signature.data)
return try Ticket(id: ticketId.get(), attempt: $0.attempt)
let ticketId = try verifier.ringVRFVerify(vrfInputData: vrfInputData, auxData: Data(), signature: $0.signature.data)
return Ticket(id: ticketId, attempt: $0.attempt)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Blockchain/Sources/Blockchain/Validator/KeyStore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public protocol KeyStore {}
9 changes: 9 additions & 0 deletions Blockchain/Sources/Blockchain/Validator/Validator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Validator {
private let blockchain: Blockchain
private var keystore: KeyStore

public init(blockchain: Blockchain, keystore: KeyStore) {
self.blockchain = blockchain
self.keystore = keystore
}
}
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ deps: .lib/libblst.a .lib/libbandersnatch_vrfs.a .lib/librocksdb.a .lib/libec.a
test: githooks deps
./scripts/runTests.sh test

.PHONY: test-cargo
test-cargo:
cargo test --manifest-path Utils/Sources/bandersnatch/Cargo.toml

.PHONY: test-all
test-all: test test-cargo

.PHONY: build
build: githooks deps
./scripts/run.sh build
Expand Down Expand Up @@ -57,6 +64,13 @@ lint: githooks
format: githooks
swiftformat .

.PHONY: format-cargo
format-cargo:
cargo fmt --manifest-path Utils/Sources/bandersnatch/Cargo.toml

.PHONY: format-all
format-all: format format-cargo

.PHONY: run
run: githooks
swift run --package-path Boka
10 changes: 10 additions & 0 deletions TracingUtils/Sources/TracingUtils/SpanAttributes+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extension SpanAttributes {
public var blockHash: SpanAttributeConvertible? {
get {
self["blockHash"]
}
set {
self["blockHash"] = newValue
}
}
}
Loading

0 comments on commit a59766e

Please sign in to comment.