2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2019-2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2020-04-24 06:46:55 +00:00
|
|
|
import strutils, eth/db/kvstore
|
|
|
|
|
|
|
|
export kvstore
|
|
|
|
|
|
|
|
# Database access layer that turns errors in the database into Defects as the
|
|
|
|
# code that uses it isn't equipped to handle errors of that kind - this should
|
|
|
|
# be reconsidered when making more changes here.
|
2018-12-27 02:59:06 +00:00
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
type DbBackend* = enum
|
2022-04-20 07:57:50 +00:00
|
|
|
none,
|
2019-01-06 22:06:26 +00:00
|
|
|
sqlite,
|
|
|
|
rocksdb,
|
|
|
|
lmdb
|
|
|
|
|
|
|
|
const
|
|
|
|
nimbus_db_backend* {.strdefine.} = "rocksdb"
|
2022-08-12 15:42:07 +00:00
|
|
|
dbBackend* = parseEnum[DbBackend](nimbus_db_backend)
|
|
|
|
|
|
|
|
when dbBackend == sqlite:
|
|
|
|
import eth/db/kvstore_sqlite3 as database_backend
|
|
|
|
elif dbBackend == rocksdb:
|
|
|
|
import ./kvstore_rocksdb as database_backend
|
2019-01-06 22:06:26 +00:00
|
|
|
|
2020-04-24 06:46:55 +00:00
|
|
|
type
|
|
|
|
ChainDB* = ref object of RootObj
|
2022-08-12 15:42:07 +00:00
|
|
|
kv*: KvStoreRef
|
|
|
|
when dbBackend == rocksdb:
|
|
|
|
rdb*: RocksStoreRef
|
2020-04-24 06:46:55 +00:00
|
|
|
|
|
|
|
# TODO KvStore is a virtual interface and TrieDB is a virtual interface - one
|
|
|
|
# will be enough eventually - unless the TrieDB interface gains operations
|
|
|
|
# that are not typical to KvStores
|
|
|
|
proc get*(db: ChainDB, key: openArray[byte]): seq[byte] =
|
|
|
|
var res: seq[byte]
|
|
|
|
proc onData(data: openArray[byte]) = res = @data
|
|
|
|
if db.kv.get(key, onData).expect("working database"):
|
|
|
|
return res
|
|
|
|
|
|
|
|
proc put*(db: ChainDB, key, value: openArray[byte]) =
|
|
|
|
db.kv.put(key, value).expect("working database")
|
|
|
|
|
|
|
|
proc contains*(db: ChainDB, key: openArray[byte]): bool =
|
|
|
|
db.kv.contains(key).expect("working database")
|
|
|
|
|
2023-01-27 14:57:48 +00:00
|
|
|
proc del*(db: ChainDB, key: openArray[byte]): bool =
|
2020-04-24 06:46:55 +00:00
|
|
|
db.kv.del(key).expect("working database")
|
|
|
|
|
2019-01-06 22:06:26 +00:00
|
|
|
when dbBackend == sqlite:
|
2020-04-24 06:46:55 +00:00
|
|
|
proc newChainDB*(path: string): ChainDB =
|
2021-07-15 13:12:33 +00:00
|
|
|
let db = SqStoreRef.init(path, "nimbus").expect("working database")
|
|
|
|
ChainDB(kv: kvStore db.openKvStore().expect("working database"))
|
2019-01-06 22:06:26 +00:00
|
|
|
elif dbBackend == rocksdb:
|
2023-08-04 11:10:09 +00:00
|
|
|
proc newChainDB*(path: string): ChainDB
|
|
|
|
{.gcsafe, deprecated: "use newCoreDbRef(LegacyDbPersistent,<path>)".} =
|
2022-08-12 15:42:07 +00:00
|
|
|
let rdb = RocksStoreRef.init(path, "nimbus").tryGet()
|
|
|
|
ChainDB(kv: kvStore rdb, rdb: rdb)
|
2019-01-06 22:06:26 +00:00
|
|
|
elif dbBackend == lmdb:
|
2020-04-24 06:46:55 +00:00
|
|
|
# TODO This implementation has several issues on restricted platforms, possibly
|
|
|
|
# due to mmap restrictions - see:
|
|
|
|
# https://github.com/status-im/nim-beacon-chain/issues/732
|
|
|
|
# https://github.com/status-im/nim-beacon-chain/issues/688
|
|
|
|
# It also has other issues, including exception safety:
|
|
|
|
# https://github.com/status-im/nim-beacon-chain/pull/809
|
|
|
|
|
|
|
|
{.error: "lmdb deprecated, needs reimplementing".}
|
2022-04-20 07:57:50 +00:00
|
|
|
elif dbBackend == none:
|
|
|
|
discard
|
2018-12-27 02:59:06 +00:00
|
|
|
|
2022-04-20 07:57:50 +00:00
|
|
|
when dbBackend != none:
|
|
|
|
export database_backend
|