From ed7dbab70fa5f7f225281501e0009e8ff4d1bd42 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Fri, 24 Apr 2020 08:46:55 +0200 Subject: [PATCH] db: use kvstore from nim-eth --- .gitmodules | 8 +++--- nimbus/db/select_backend.nim | 47 +++++++++++++++++++++++++++++++++--- vendor/nim-chronicles | 2 +- vendor/nim-eth | 2 +- vendor/nim-result | 1 - vendor/nim-rocksdb | 2 +- vendor/nim-sqlite3-abi | 1 + vendor/nim-stew | 2 +- 8 files changed, 51 insertions(+), 14 deletions(-) delete mode 160000 vendor/nim-result create mode 160000 vendor/nim-sqlite3-abi diff --git a/.gitmodules b/.gitmodules index f1615bc4d..fec1472f8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/nimbus/db/select_backend.nim b/nimbus/db/select_backend.nim index f873c718e..af017f6df 100644 --- a/nimbus/db/select_backend.nim +++ b/nimbus/db/select_backend.nim @@ -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 diff --git a/vendor/nim-chronicles b/vendor/nim-chronicles index 114cdccaa..3e7f422f1 160000 --- a/vendor/nim-chronicles +++ b/vendor/nim-chronicles @@ -1 +1 @@ -Subproject commit 114cdccaa087c54abcc4dad9ed3366af247de79e +Subproject commit 3e7f422f11754732df673ed280389024e1ed3cb1 diff --git a/vendor/nim-eth b/vendor/nim-eth index 5bb6ee645..fab4f9bb1 160000 --- a/vendor/nim-eth +++ b/vendor/nim-eth @@ -1 +1 @@ -Subproject commit 5bb6ee64512cd8db1c52d10145e548860aa938e7 +Subproject commit fab4f9bb1e830cc742105d4c80fd7917ed10717f diff --git a/vendor/nim-result b/vendor/nim-result deleted file mode 160000 index bdc585bf9..000000000 --- a/vendor/nim-result +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bdc585bf9f3ad0acaad18c7d12deab172373b5f4 diff --git a/vendor/nim-rocksdb b/vendor/nim-rocksdb index 8da2f1195..e528ee949 160000 --- a/vendor/nim-rocksdb +++ b/vendor/nim-rocksdb @@ -1 +1 @@ -Subproject commit 8da2f119514d933f24efc009182cdea24a5fe107 +Subproject commit e528ee949a753c7c6644da26a4a83197e55a9e35 diff --git a/vendor/nim-sqlite3-abi b/vendor/nim-sqlite3-abi new file mode 160000 index 000000000..fc2028746 --- /dev/null +++ b/vendor/nim-sqlite3-abi @@ -0,0 +1 @@ +Subproject commit fc2028746d367e259d55ba41fdf5587a0683ee59 diff --git a/vendor/nim-stew b/vendor/nim-stew index 2a1df5d2d..8065e36c5 160000 --- a/vendor/nim-stew +++ b/vendor/nim-stew @@ -1 +1 @@ -Subproject commit 2a1df5d2dd4963369f2dbae583ab714a1601a8cd +Subproject commit 8065e36c5af31f2f3f3b0d9ea242ae4eef193a30