db: use kvstore from nim-eth

This commit is contained in:
Jacek Sieka 2020-04-24 08:46:55 +02:00 committed by zah
parent 1cf9fa1f0b
commit ed7dbab70f
8 changed files with 51 additions and 14 deletions

8
.gitmodules vendored
View File

@ -98,11 +98,6 @@
url = https://github.com/status-im/nim-libp2p.git
ignore = dirty
branch = master
[submodule "vendor/nim-result"]
path = vendor/nim-result
url = https://github.com/arnetheduck/nim-result.git
ignore = dirty
branch = master
[submodule "vendor/nim-nat-traversal"]
path = vendor/nim-nat-traversal
url = https://github.com/status-im/nim-nat-traversal.git
@ -168,3 +163,6 @@
url = https://github.com/status-im/nim-testutils.git
ignore = dirty
branch = master
[submodule "vendor/nim-sqlite3-abi"]
path = vendor/nim-sqlite3-abi
url = https://github.com/arnetheduck/nim-sqlite3-abi.git

View File

@ -1,4 +1,10 @@
import strutils
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.
type DbBackend = enum
sqlite,
@ -9,11 +15,44 @@ const
nimbus_db_backend* {.strdefine.} = "rocksdb"
dbBackend = parseEnum[DbBackend](nimbus_db_backend)
type
ChainDB* = ref object of RootObj
kv: KvStoreRef
# 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")
proc del*(db: ChainDB, key: openArray[byte]) =
db.kv.del(key).expect("working database")
when dbBackend == sqlite:
import eth/trie/backends/sqlite_backend as database_backend
import eth/db/kvstore_sqlite as database_backend
proc newChainDB*(path: string): ChainDB =
ChainDB(kv: kvStore SqKvStore.init(path, "nimbus").tryGet())
elif dbBackend == rocksdb:
import eth/trie/backends/rocksdb_backend as database_backend
import eth/db/kvstore_rocksdb as database_backend
proc newChainDB*(path: string): ChainDB =
ChainDB(kv: kvStore RocksStoreRef.init(path, "nimbus").tryGet())
elif dbBackend == lmdb:
import eth/trie/backends/lmdb_backend as database_backend
# 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".}
export database_backend

@ -1 +1 @@
Subproject commit 114cdccaa087c54abcc4dad9ed3366af247de79e
Subproject commit 3e7f422f11754732df673ed280389024e1ed3cb1

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 5bb6ee64512cd8db1c52d10145e548860aa938e7
Subproject commit fab4f9bb1e830cc742105d4c80fd7917ed10717f

1
vendor/nim-result vendored

@ -1 +0,0 @@
Subproject commit bdc585bf9f3ad0acaad18c7d12deab172373b5f4

2
vendor/nim-rocksdb vendored

@ -1 +1 @@
Subproject commit 8da2f119514d933f24efc009182cdea24a5fe107
Subproject commit e528ee949a753c7c6644da26a4a83197e55a9e35

1
vendor/nim-sqlite3-abi vendored Submodule

@ -0,0 +1 @@
Subproject commit fc2028746d367e259d55ba41fdf5587a0683ee59

2
vendor/nim-stew vendored

@ -1 +1 @@
Subproject commit 2a1df5d2dd4963369f2dbae583ab714a1601a8cd
Subproject commit 8065e36c5af31f2f3f3b0d9ea242ae4eef193a30