-
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.
- Loading branch information
Showing
7 changed files
with
171 additions
and
140 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
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
27 changes: 19 additions & 8 deletions
27
Blockchain/Sources/Blockchain/State/StateBackendProtocol.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 |
---|---|---|
@@ -1,20 +1,31 @@ | ||
import Foundation | ||
import Utils | ||
|
||
public enum StateTrieBackendOperation: Sendable { | ||
public enum StateBackendOperation: Sendable { | ||
case write(key: Data, value: Data) | ||
case writeRawValue(key: Data32, value: Data) | ||
case refIncrement(key: Data) | ||
case refDecrement(key: Data) | ||
} | ||
|
||
// key: trie node hash (32 bytes) | ||
// value: trie node data (64 bytes) | ||
public protocol StateTrieBackend: Sendable { | ||
/// key: trie node hash (32 bytes) | ||
/// value: trie node data (64 bytes) | ||
/// ref counting requirements: | ||
/// - write do not increment ref count, only explicit ref increment do | ||
/// - lazy prune is used. e.g. when ref count is reduced to zero, the value will only be removed | ||
/// when gc is performed | ||
/// - raw value have its own ref counting | ||
/// - writeRawValue increment ref count, and write if necessary | ||
/// - raw value ref count is only decremented when connected trie node is removed during gc | ||
public protocol StateBackendProtocol: Sendable { | ||
func read(key: Data) async throws -> Data? | ||
func readAll(prefix: Data, startKey: Data?, limit: UInt32?) async throws -> [(key: Data, value: Data)] | ||
func batchRead(keys: [Data]) async throws -> [(key: Data, value: Data?)] | ||
func batchUpdate(_ ops: [StateTrieBackendOperation]) async throws | ||
func batchUpdate(_ ops: [StateBackendOperation]) async throws | ||
|
||
// remove entries with zero ref count | ||
func gc() async throws | ||
// hash is the blake2b256 hash of the value | ||
func readValue(hash: Data32) async throws -> Data? | ||
|
||
/// remove entries with zero ref count | ||
/// callback returns a dependent raw value key if the data is regular leaf node | ||
func gc(callback: @Sendable (Data) -> Data32?) async throws | ||
} |
Oops, something went wrong.