diff --git a/eth/trie/backends/lmdb_backend.nim b/eth/trie/backends/lmdb_backend.nim index 7c8ba16..bbf5a15 100644 --- a/eth/trie/backends/lmdb_backend.nim +++ b/eth/trie/backends/lmdb_backend.nim @@ -10,6 +10,7 @@ else: const MDB_NOSUBDIR = 0x4000 + MDB_RDONLY = 0x20000 MDB_NOTFOUND = -30798 when defined(cpu64): @@ -147,7 +148,7 @@ proc del*(db: ChainDB, key: openarray[byte]) = proc close*(db: ChainDB) = mdb_env_close(db.env) -proc newChainDB*(basePath: string): ChainDB = +proc newChainDB*(basePath: string, readOnly = false): ChainDB = result.new() let dataDir = basePath / "nimbus.db" @@ -156,9 +157,12 @@ proc newChainDB*(basePath: string): ChainDB = ok = mdb_env_set_mapsize(result.env, LMDB_MAP_SIZE) == 0 if not ok: raiseStorageInitError() - + + var openFlags = MDB_NOSUBDIR + if readOnly: openFlags = openFlags or MDB_RDONLY # file mode ignored on windows - ok = mdb_env_open(result.env, dataDir, MDB_NOSUBDIR.cuint, 0o664.cint) == 0 + ok = mdb_env_open(result.env, dataDir, openFlags.cuint, 0o664.cint) == 0 if not ok: raiseStorageInitError() - result.put(emptyRlpHash.data, emptyRlp) + if not readOnly: + result.put(emptyRlpHash.data, emptyRlp) diff --git a/eth/trie/backends/rocksdb_backend.nim b/eth/trie/backends/rocksdb_backend.nim index 2353e32..b0f97ba 100644 --- a/eth/trie/backends/rocksdb_backend.nim +++ b/eth/trie/backends/rocksdb_backend.nim @@ -35,7 +35,7 @@ proc del*(db: ChainDB, key: openarray[byte]) = proc close*(db: ChainDB) = db.store.close -proc newChainDB*(basePath: string): ChainDB = +proc newChainDB*(basePath: string, readOnly = false): ChainDB = result.new() let dataDir = basePath / "data" @@ -44,8 +44,8 @@ proc newChainDB*(basePath: string): ChainDB = createDir(dataDir) createDir(backupsDir) - let s = result.store.init(dataDir, backupsDir) + let s = result.store.init(dataDir, backupsDir, readOnly) if not s.ok: raiseStorageInitError() - put(result, emptyRlpHash.data, emptyRlp) - + if not readOnly: + put(result, emptyRlpHash.data, emptyRlp)