Skip to content

Commit

Permalink
util: make db interface generic
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrocheleau committed Apr 27, 2023
1 parent ec3a913 commit 510b0c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
9 changes: 4 additions & 5 deletions packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Block, BlockHeader } from '@ethereumjs/block'
import { Chain, Common, ConsensusAlgorithm, ConsensusType, Hardfork } from '@ethereumjs/common'
import { KECCAK256_RLP, Lock, concatBytesNoTypeCheck } from '@ethereumjs/util'
import { DB, KECCAK256_RLP, Lock, concatBytesNoTypeCheck } from '@ethereumjs/util'
import { bytesToHex, equalsBytes, hexToBytes } from 'ethereum-cryptography/utils'
import { MemoryLevel } from 'memory-level'

import { CasperConsensus, CliqueConsensus, EthashConsensus } from './consensus'
import { DBOp, DBSaveLookups, DBSetBlockOrHeader, DBSetHashToNumber, DBSetTD } from './db/helpers'
Expand All @@ -17,14 +16,14 @@ import type { BlockchainInterface, BlockchainOptions, OnBlock } from './types'
import type { BlockData } from '@ethereumjs/block'
import type { CliqueConfig } from '@ethereumjs/common'
import type { BigIntLike } from '@ethereumjs/util'
import type { AbstractLevel } from 'abstract-level'
import { MapDB } from './db/map'

/**
* This class stores and interacts with blocks.
*/
export class Blockchain implements BlockchainInterface {
consensus: Consensus
db: AbstractLevel<string | Uint8Array | Uint8Array, string | Uint8Array, string | Uint8Array>
db: DB<Uint8Array | string, Uint8Array | string>
dbManager: DBManager

private _genesisBlock?: Block /** The genesis block of this blockchain */
Expand Down Expand Up @@ -115,7 +114,7 @@ export class Blockchain implements BlockchainInterface {
this._validateBlocks = opts.validateBlocks ?? true
this._customGenesisState = opts.genesisState

this.db = opts.db ? opts.db : new MemoryLevel()
this.db = opts.db ? opts.db : new MapDB()
this.dbManager = new DBManager(this.db, this._common)

if (opts.consensus) {
Expand Down
35 changes: 22 additions & 13 deletions packages/util/src/db.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
export type BatchDBOp = PutBatch | DelBatch
export type BatchDBOp<
TKey extends Uint8Array | string = Uint8Array,
TValue extends Uint8Array | string = Uint8Array
> = PutBatch<TKey, TValue> | DelBatch<TKey>

export interface PutBatch {
export interface PutBatch<
TKey extends Uint8Array | string = Uint8Array,
TValue extends Uint8Array | string = Uint8Array
> {
type: 'put'
key: Uint8Array
value: Uint8Array
key: TKey
value: TValue
}

export interface DelBatch {
export interface DelBatch<TKey extends Uint8Array | string = Uint8Array> {
type: 'del'
key: Uint8Array
key: TKey
}

export interface DB {
export interface DB<
TKey extends Uint8Array | string = Uint8Array,
TValue extends Uint8Array | string = Uint8Array
> {
/**
* Retrieves a raw value from db.
* @param key
* @returns A Promise that resolves to `Uint8Array` if a value is found or `null` if no value is found.
*/
get(key: Uint8Array): Promise<Uint8Array | null>
get(key: TKey): Promise<TValue | null>

/**
* Writes a value directly to db.
* @param key The key as a `Uint8Array`
* @param key The key as a `TValue`
* @param value The value to be stored
*/
put(key: Uint8Array, val: Uint8Array): Promise<void>
put(key: TKey, val: TValue): Promise<void>

/**
* Removes a raw value in the underlying db.
* @param keys
*/
del(key: Uint8Array): Promise<void>
del(key: TKey): Promise<void>

/**
* Performs a batch operation on db.
* @param opStack A stack of levelup operations
*/
batch(opStack: BatchDBOp[]): Promise<void>
batch(opStack: BatchDBOp<TKey, TValue>[]): Promise<void>

/**
* Returns a copy of the DB instance, with a reference
* to the **same** underlying db instance.
*/
copy(): DB
copy(): DB<TKey, TValue>
}

0 comments on commit 510b0c6

Please sign in to comment.