-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* import pipeline wip * lazy hash * blockRef * update import pipeline * validator activity stats * fix format
- Loading branch information
Showing
31 changed files
with
523 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
--extensionacl on-declarations | ||
--maxwidth 150 | ||
--asynccapturing debugCheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
Blockchain/Sources/Blockchain/BlockImporter/BlockImporter.swift
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
Blockchain/Sources/Blockchain/BlockImporter/BlockValidator.swift
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
Blockchain/Sources/Blockchain/BlockImporter/PendingBlock.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
Blockchain/Sources/Blockchain/BlockchainDataProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Utils | ||
|
||
public enum BlockchainDataProviderError: Error { | ||
case unknownHash | ||
} | ||
|
||
public protocol BlockchainDataProvider { | ||
func hasHeader(hash: Data32) async throws -> Bool | ||
func isHead(hash: Data32) async throws -> Bool | ||
|
||
func getHeader(hash: Data32) async throws -> HeaderRef | ||
func getBlock(hash: Data32) async throws -> BlockRef | ||
func getState(hash: Data32) async throws -> StateRef | ||
func getFinalizedHead() async throws -> Data32 | ||
func getHeads() async throws -> [Data32] | ||
func getBlockHash(index: TimeslotIndex) async throws -> [Data32] | ||
|
||
func add(state: StateRef, isHead: Bool) async throws | ||
func setFinalizedHead(hash: Data32) async throws | ||
// remove header, block and state | ||
func remove(hash: Data32) async throws | ||
|
||
// protected method | ||
func _updateHeadNoCheck(hash: Data32, parent: Data32) async throws | ||
} | ||
|
||
extension BlockchainDataProvider { | ||
public func updateHead(hash: Data32, parent: Data32) async throws { | ||
try await debugCheck(await hasHeader(hash: hash)) | ||
try await debugCheck(await hasHeader(hash: parent)) | ||
|
||
try await _updateHeadNoCheck(hash: hash, parent: parent) | ||
} | ||
|
||
public func add(state: StateRef) async throws { | ||
try await add(state: state, isHead: false) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import Utils | ||
|
||
// TODO: add tests | ||
public actor InMemoryDataProvider: Sendable { | ||
public private(set) var heads: [StateRef] | ||
public private(set) var finalizedHead: StateRef | ||
|
||
private var stateByBlockHash: [Data32: StateRef] = [:] | ||
private var hashByTimeslot: [TimeslotIndex: [Data32]] = [:] | ||
|
||
public init(genesis: StateRef) async { | ||
heads = [genesis] | ||
finalizedHead = genesis | ||
|
||
addState(genesis) | ||
} | ||
|
||
private func addState(_ state: StateRef) { | ||
stateByBlockHash[state.value.lastBlock.hash] = state | ||
hashByTimeslot[state.value.lastBlock.header.timeslotIndex, default: []].append(state.value.lastBlock.hash) | ||
} | ||
} | ||
|
||
extension InMemoryDataProvider: BlockchainDataProvider { | ||
public func hasHeader(hash: Data32) async throws -> Bool { | ||
stateByBlockHash[hash] != nil | ||
} | ||
|
||
public func isHead(hash: Data32) async throws -> Bool { | ||
heads.contains(where: { $0.value.lastBlock.hash == hash }) | ||
} | ||
|
||
public func getHeader(hash: Data32) async throws -> HeaderRef { | ||
guard let header = stateByBlockHash[hash]?.value.lastBlock.header.asRef() else { | ||
throw BlockchainDataProviderError.unknownHash | ||
} | ||
return header | ||
} | ||
|
||
public func getBlock(hash: Data32) async throws -> BlockRef { | ||
guard let block = stateByBlockHash[hash]?.value.lastBlock else { | ||
throw BlockchainDataProviderError.unknownHash | ||
} | ||
return block | ||
} | ||
|
||
public func getState(hash: Data32) async throws -> StateRef { | ||
guard let state = stateByBlockHash[hash] else { | ||
throw BlockchainDataProviderError.unknownHash | ||
} | ||
return state | ||
} | ||
|
||
public func getFinalizedHead() async throws -> Data32 { | ||
finalizedHead.value.lastBlock.hash | ||
} | ||
|
||
public func getHeads() async throws -> [Data32] { | ||
heads.map(\.value.lastBlock.hash) | ||
} | ||
|
||
public func getBlockHash(index: TimeslotIndex) async throws -> [Data32] { | ||
hashByTimeslot[index] ?? [] | ||
} | ||
|
||
public func add(state: StateRef, isHead: Bool) async throws { | ||
addState(state) | ||
if isHead { | ||
try await updateHead(hash: state.value.lastBlock.hash, parent: state.value.lastBlock.header.parentHash) | ||
} | ||
} | ||
|
||
public func setFinalizedHead(hash: Data32) async throws { | ||
guard let state = stateByBlockHash[hash] else { | ||
throw BlockchainDataProviderError.unknownHash | ||
} | ||
finalizedHead = state | ||
} | ||
|
||
public func _updateHeadNoCheck(hash: Data32, parent: Data32) async throws { | ||
for i in 0 ..< heads.count where heads[i].value.lastBlock.hash == parent { | ||
assert(stateByBlockHash[hash] != nil) | ||
heads[i] = stateByBlockHash[hash]! | ||
return | ||
} | ||
} | ||
|
||
public func remove(hash: Data32) async throws { | ||
guard let state = stateByBlockHash[hash] else { | ||
return | ||
} | ||
stateByBlockHash.removeValue(forKey: hash) | ||
hashByTimeslot.removeValue(forKey: state.value.lastBlock.header.timeslotIndex) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// the STF | ||
public final class Runtime { | ||
public enum Error: Swift.Error { | ||
case safroleError(SafroleError) | ||
} | ||
|
||
public let config: ProtocolConfigRef | ||
|
||
public init(config: ProtocolConfigRef) { | ||
self.config = config | ||
} | ||
|
||
public func apply(block: BlockRef, state prevState: StateRef) throws(Error) -> StateRef { | ||
var newState = prevState.value | ||
newState.lastBlock = block | ||
let res = newState.updateSafrole( | ||
slot: block.header.timeslotIndex, entropy: newState.entropyPool.0, extrinsics: block.extrinsic.tickets | ||
) | ||
switch res { | ||
case let .success((state: postState, epochMark: _, ticketsMark: _)): | ||
newState.mergeWith(postState: postState) | ||
case let .failure(err): | ||
throw .safroleError(err) | ||
} | ||
|
||
return StateRef(newState) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.