-
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.
Merge branch 'master' into dev-issues-278
- Loading branch information
Showing
38 changed files
with
332 additions
and
246 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
--maxwidth 140 | ||
--asynccapturing debugCheck | ||
--closurevoid preserve | ||
--disable typeSugar |
47 changes: 47 additions & 0 deletions
47
Blockchain/Sources/Blockchain/AuxDataStore/DataStore.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,47 @@ | ||
import Utils | ||
|
||
enum DataStoreError: Error { | ||
case invalidPackageHash(Data32) | ||
case invalidSegmentRoot(Data32) | ||
} | ||
|
||
public final class DataStore: Sendable { | ||
private let impl: DataStoreProtocol | ||
private let network: DataStoreNetworkProtocol | ||
|
||
public init(_ impl: DataStoreProtocol, _ network: DataStoreNetworkProtocol) { | ||
self.impl = impl | ||
self.network = network | ||
} | ||
|
||
public func fetchSegment(segments: [WorkItem.ImportedDataSegment]) async throws -> [Data4104] { | ||
var result: [Data4104] = [] | ||
|
||
for segment in segments { | ||
let segmentRoot = switch segment.root { | ||
case let .segmentRoot(root): | ||
root | ||
case let .workPackageHash(hash): | ||
try await impl.getSegmentRoot(forWorkPackageHash: hash).unwrap(orError: DataStoreError.invalidPackageHash(hash)) | ||
} | ||
let erasureRoot = try await impl.getEasureRoot(forSegmentRoot: segmentRoot) | ||
.unwrap(orError: DataStoreError.invalidSegmentRoot(segmentRoot)) | ||
|
||
if let localData = try await impl.get(erasureRoot: erasureRoot, index: segment.index) { | ||
result.append(localData) | ||
} else { | ||
// TODO: use network for fetch shards and reconstruct the segment | ||
fatalError("not implemented") | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
public func set(data: Data4104, erasureRoot: Data32, index: UInt16) async throws { | ||
try await impl.set(data: data, erasureRoot: erasureRoot, index: index) | ||
|
||
// TODO; erasure code the data and store each chunk | ||
// so assurer can query them later with CE137 | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Blockchain/Sources/Blockchain/AuxDataStore/DataStoreProtocol.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,22 @@ | ||
import Utils | ||
|
||
public protocol DataStoreProtocol: Sendable { | ||
// segment root => erasure root | ||
func getEasureRoot(forSegmentRoot: Data32) async throws -> Data32? | ||
func set(erasureRoot: Data32, forSegmentRoot: Data32) async throws | ||
func delete(erasureRoot: Data32) async throws | ||
|
||
// work package hash => segment root | ||
func getSegmentRoot(forWorkPackageHash: Data32) async throws -> Data32? | ||
func set(segmentRoot: Data32, forWorkPackageHash: Data32) async throws | ||
func delete(segmentRoot: Data32) async throws | ||
|
||
// erasure root + index => segment data | ||
func get(erasureRoot: Data32, index: UInt16) async throws -> Data4104? | ||
func set(data: Data4104, erasureRoot: Data32, index: UInt16) async throws | ||
} | ||
|
||
public protocol DataStoreNetworkProtocol: Sendable { | ||
// Use CE139/CE140 to fetch remote chunk | ||
func fetchRemoteChunk(erasureRoot: Data32, shardIndex: UInt16, segmentIndices: [UInt16]) async throws -> Data12? | ||
} |
54 changes: 54 additions & 0 deletions
54
Blockchain/Sources/Blockchain/AuxDataStore/InMemoryDataStoreBackend.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,54 @@ | ||
import Utils | ||
|
||
public actor InMemoryDataStoreBackend { | ||
// segment root => erasure root | ||
private var erasureRootBySegmentRoot: [Data32: Data32] = [:] | ||
|
||
// work package hash => segment root | ||
private var segmentRootByWorkPackageHash: [Data32: Data32] = [:] | ||
|
||
// erasure root + index => segment data | ||
private var chunks: [Data32: [UInt16: Data4104]] = [:] | ||
|
||
public init() {} | ||
} | ||
|
||
extension InMemoryDataStoreBackend: DataStoreProtocol { | ||
public func getEasureRoot(forSegmentRoot: Data32) async throws -> Data32? { | ||
erasureRootBySegmentRoot[forSegmentRoot] | ||
} | ||
|
||
public func set(erasureRoot: Data32, forSegmentRoot: Data32) async throws { | ||
erasureRootBySegmentRoot[forSegmentRoot] = erasureRoot | ||
} | ||
|
||
public func delete(erasureRoot: Data32) async throws { | ||
erasureRootBySegmentRoot.removeValue(forKey: erasureRoot) | ||
} | ||
|
||
public func getSegmentRoot(forWorkPackageHash: Data32) async throws -> Data32? { | ||
segmentRootByWorkPackageHash[forWorkPackageHash] | ||
} | ||
|
||
public func set(segmentRoot: Data32, forWorkPackageHash: Data32) async throws { | ||
segmentRootByWorkPackageHash[forWorkPackageHash] = segmentRoot | ||
} | ||
|
||
public func delete(segmentRoot: Data32) async throws { | ||
segmentRootByWorkPackageHash.removeValue(forKey: segmentRoot) | ||
} | ||
|
||
public func get(erasureRoot: Data32, index: UInt16) async throws -> Data4104? { | ||
chunks[erasureRoot]?[index] | ||
} | ||
|
||
public func set(data: Data4104, erasureRoot: Data32, index: UInt16) async throws { | ||
chunks[erasureRoot, default: [:]][index] = data | ||
} | ||
} | ||
|
||
extension InMemoryDataStoreBackend: DataStoreNetworkProtocol { | ||
public func fetchRemoteChunk(erasureRoot _: Data32, shardIndex _: UInt16, segmentIndices _: [UInt16]) async throws -> Data12? { | ||
nil | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
Blockchain/Sources/Blockchain/DataStore/FilesystemDataStore.swift
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
Blockchain/Sources/Blockchain/DataStore/InMemoryDataStore.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
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
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.