Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Dec 11, 2024
1 parent 3e8ff13 commit 218ce1d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 28 deletions.
17 changes: 0 additions & 17 deletions Database/Sources/Database/JamCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,3 @@ struct JamCoder<Key: Encodable, Value: Codable>: StoreCoder {
try JamDecoder.decode(Value.self, from: data, withConfig: config)
}
}

struct NoopCoder: StoreCoder {
typealias Key = Data
typealias Value = Data

func encode(key: Key) throws -> Data {
key
}

func encode(value: Value) throws -> Data {
value
}

func decode(data: Data) throws -> Value {
data
}
}
19 changes: 19 additions & 0 deletions Database/Sources/Database/NoopCoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation
import RocksDBSwift

struct NoopCoder: StoreCoder {
typealias Key = Data
typealias Value = Data

func encode(key: Key) throws -> Data {
key
}

func encode(value: Value) throws -> Data {
value
}

func decode(data: Data) throws -> Value {
data
}
}
Empty file.
102 changes: 102 additions & 0 deletions Database/Tests/DatabaseTests/RocksDBBackendTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import Blockchain
import Database
import Foundation
import Testing
import Utils

final class RocksDBBackendTests {
let path = {
let tmpDir = FileManager.default.temporaryDirectory
return tmpDir.appendingPathComponent("\(UUID().uuidString)")
}()

let config: ProtocolConfigRef = .dev
let genesisBlock: BlockRef
var backend: RocksDBBackend!

init() async throws {
genesisBlock = BlockRef.dummy(config: config)

// Initialize backend with genesis block
backend = try await RocksDBBackend(
path: path,
config: config,
genesisBlock: genesisBlock,
genesisStateData: [:]
)
}

deinit {
backend = nil
try? FileManager.default.removeItem(at: path)
}

@Test
func testGenesisBlockInitialization() async throws {
// Verify genesis block was properly stored
let exists = try await backend.hasBlock(hash: genesisBlock.hash)
#expect(exists == true)

// Verify it's both a head and finalized head
let isHead = try await backend.isHead(hash: genesisBlock.hash)
#expect(isHead == true)

let finalizedHead = try await backend.getFinalizedHead()
#expect(finalizedHead == genesisBlock.hash)

// Verify block number
let blockNumber = try await backend.getBlockNumber(hash: genesisBlock.hash)
#expect(blockNumber == 0)
}

@Test
func testBlockOperations() async throws {
// Create and add a new block
let block1 = BlockRef.dummy(config: config, parent: genesisBlock)

try await backend.add(block: block1)
try await backend.updateHead(hash: block1.hash, parent: genesisBlock.hash)

// Verify block was stored
let storedBlock = try await backend.getBlock(hash: block1.hash)
#expect(storedBlock == block1)

// Verify block indexes
let blocksByTimeslot = try await backend.getBlockHash(byTimeslot: 1)
#expect(blocksByTimeslot.contains(block1.hash))

let blocksByNumber = try await backend.getBlockHash(byNumber: 1)
#expect(blocksByNumber.contains(block1.hash))

// Test block removal
try await backend.remove(hash: block1.hash)
let exists = try await backend.hasBlock(hash: block1.hash)
#expect(exists == false)
}

@Test
func testChainReorganization() async throws {
// Create two competing chains
let block1 = BlockRef.dummy(config: config, parent: genesisBlock)

let block2 = BlockRef.dummy(config: config, parent: genesisBlock).mutate { block in
block.header.unsigned.timeslot = 123
}

// Add both blocks and update heads
try await backend.add(block: block1)
try await backend.add(block: block2)
try await backend.updateHead(hash: block1.hash, parent: genesisBlock.hash)
try await backend.updateHead(hash: block2.hash, parent: genesisBlock.hash)

// Verify both are heads
let heads = try await backend.getHeads()
#expect(heads.contains(block1.hash))
#expect(heads.contains(block2.hash))

// Test finalization of one chain
try await backend.setFinalizedHead(hash: block1.hash)
let finalizedHead = try await backend.getFinalizedHead()
#expect(finalizedHead == block1.hash)
}
}
11 changes: 0 additions & 11 deletions Database/Tests/RocksDBSwiftTests/RocksDBTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,6 @@ final class RocksDBTests {
#expect(retrieved?.isEmpty == true)
}

@Test func testErrorConditions() throws {
// Test invalid operations
let invalidDB = try? RocksDB<Columns>(path: URL(fileURLWithPath: "/nonexistent/path"))
#expect(invalidDB == nil)

// Test deleting non-existent key
try rocksDB.delete(column: .col1, key: "nonexistent".data)
let value = try rocksDB.get(column: .col1, key: "nonexistent".data)
#expect(value == nil)
}

@Test func testIterator() throws {
// Setup test data
let testData = [
Expand Down

0 comments on commit 218ce1d

Please sign in to comment.