diff --git a/codex/codex.nim b/codex/codex.nim index 4cf8d623..cad4be74 100644 --- a/codex/codex.nim +++ b/codex/codex.nim @@ -45,7 +45,8 @@ type config: CodexConf restServer: RestServerRef codexNode: CodexNodeRef - blockStoreManager: BlockStoreManager + repoStore: RepoStore + blockMaintainer: BlockMaintainer CodexPrivateKey* = libp2p.PrivateKey # alias @@ -93,7 +94,8 @@ proc bootstrapInteractions(config: CodexConf, repo: RepoStore): Future[Contracts proc start*(s: CodexServer) {.async.} = notice "Starting codex node" - await s.blockStoreManager.start() + await s.repoStore.start() + s.blockMaintainer.start() s.restServer.start() s.codexNode.contracts = await bootstrapInteractions(s.config, s.repoStore) @@ -133,10 +135,39 @@ proc stop*(s: CodexServer) {.async.} = await allFuturesThrowing( s.restServer.stop(), s.codexNode.stop(), - s.blockStoreManager.stop()) + s.repoStore.stop(), + s.blockMaintainer.stop()) s.runHandle.complete() +proc new(_: type ContractInteractions, config: CodexConf): ?ContractInteractions = + if not config.persistence: + if config.ethAccount.isSome: + warn "Ethereum account was set, but persistence is not enabled" + return + + without account =? config.ethAccount: + error "Persistence enabled, but no Ethereum account was set" + quit QuitFailure + + if deployment =? config.ethDeployment: + ContractInteractions.new(config.ethProvider, account, deployment) + else: + ContractInteractions.new(config.ethProvider, account) + +proc createDataStore(config: CodexConf): Datastore = + case config.repoKind + of repoFS: + return Datastore(FSDatastore.new($config.dataDir, depth = 5).expect("Should create repo file data store!")) + of repoSQLite: + return Datastore(SQLiteDatastore.new($config.dataDir).expect("Should create repo SQLite data store!")) + raise newException(Defect, "Unknown repoKind: " & $config.repoKind) + +proc getLocalBlockStore(repoStore: RepoStore, configCacheSize: Natural): BlockStore = + if configCacheSize > 0: + return CacheStore.new(backingStore = repoStore, cacheSize = configCacheSize * MiB) + return repoStore + proc new*(T: type CodexServer, config: CodexConf, privateKey: CodexPrivateKey): T = let @@ -177,21 +208,27 @@ proc new*(T: type CodexServer, config: CodexConf, privateKey: CodexPrivateKey): wallet = WalletRef.new(EthPrivateKey.random()) network = BlockExcNetwork.new(switch) - blockStoreManager = BlockStoreManager.new(BlockStoreManagerConfig( - dataDir: config.dataDir, - storageQuota: config.storageQuota, - blockTtlSeconds: config.blockTtlSeconds, - blockMaintenanceIntervalSeconds: config.blockMaintenanceIntervalSeconds, - blockMaintenanceNumberOfBlocks: config.blockMaintenanceNumberOfBlocks, - cacheSize: config.cacheSize, - repoKind: config.repoKind)) - blockStore = blockStoreManager.getBlockStore() + dataStore = createDataStore(config) + + repoStore = + RepoStore.new( + repoDs = dataStore, + metaDs = SQLiteDatastore.new(config.dataDir / CodexMetaNamespace).expect("Should create meta data store!"), + quotaMaxBytes = config.storageQuota.uint, + blockTtl = config.blockTtlSeconds.seconds) + + blockMaintainer = BlockMaintainer.new( + repoStore, + interval = config.blockMaintenanceIntervalSeconds.seconds, + numberOfBlocksPerInterval = config.blockMaintenanceNumberOfBlocks) + + localBlockStore = getLocalBlockStore(repoStore, config.cacheSize) peerStore = PeerCtxStore.new() pendingBlocks = PendingBlocksManager.new() - blockDiscovery = DiscoveryEngine.new(blockStore, peerStore, network, discovery, pendingBlocks) - engine = BlockExcEngine.new(blockStore, wallet, network, blockDiscovery, peerStore, pendingBlocks) - store = NetworkStore.new(engine, blockStore) + blockDiscovery = DiscoveryEngine.new(localBlockStore, peerStore, network, discovery, pendingBlocks) + engine = BlockExcEngine.new(localBlockStore, wallet, network, blockDiscovery, peerStore, pendingBlocks) + store = NetworkStore.new(engine, localBlockStore) erasure = Erasure.new(store, leoEncoderProvider, leoDecoderProvider) codexNode = CodexNodeRef.new(switch, store, engine, erasure, discovery) restServer = RestServerRef.new( @@ -206,4 +243,5 @@ proc new*(T: type CodexServer, config: CodexConf, privateKey: CodexPrivateKey): config: config, codexNode: codexNode, restServer: restServer, - blockStoreManager: blockStoreManager) + repoStore: repoStore, + blockMaintainer: blockMaintainer) diff --git a/codex/stores.nim b/codex/stores.nim index 5d78f1fd..64231481 100644 --- a/codex/stores.nim +++ b/codex/stores.nim @@ -1,4 +1,3 @@ -import ./stores/blockstoremanager import ./stores/cachestore import ./stores/blockstore import ./stores/networkstore @@ -6,5 +5,6 @@ import ./stores/repostore import ./stores/memorystore import ./stores/maintenance import ./stores/keyutils +import ./stores/consts -export blockstoremanager, cachestore, blockstore, networkstore, repostore, memorystore, maintenance, keyutils +export cachestore, blockstore, networkstore, repostore, memorystore, maintenance, keyutils, consts diff --git a/codex/stores/blockstoremanager.nim b/codex/stores/blockstoremanager.nim deleted file mode 100644 index 936656e4..00000000 --- a/codex/stores/blockstoremanager.nim +++ /dev/null @@ -1,84 +0,0 @@ -## Nim-Codex -## Copyright (c) 2023 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. - -import std/os - -import pkg/chronos -import pkg/datastore -import pkg/confutils -import pkg/confutils/defs - -import ../namespaces -import ./consts -import ./blockstore -import ./repostore -import ./cachestore -import ./maintenance - -type - BlockStoreManager* = ref object of RootObj - repoStore: RepoStore - maintenance: BlockMaintainer - blockStore: BlockStore - BlockStoreManagerConfig* = ref object - dataDir*: OutDir - storageQuota*: Natural - blockTtlSeconds*: int - blockMaintenanceIntervalSeconds*: int - blockMaintenanceNumberOfBlocks*: int - cacheSize*: Natural - repoKind*: RepoKind - -proc start*(self: BlockStoreManager): Future[void] {.async.} = - await self.repoStore.start() - self.maintenance.start() - -proc stop*(self: BlockStoreManager): Future[void] {.async.} = - await self.repoStore.stop() - await self.maintenance.stop() - -proc getBlockStore*(self: BlockStoreManager): BlockStore = - self.blockStore - -proc getRepoDatastore(config: BlockStoreManagerConfig): Datastore = - case config.repoKind - of repoFS: - return Datastore(FSDatastore.new($config.dataDir, depth = 5).expect("Should create repo file data store!")) - of repoSQLite: - return Datastore(SQLiteDatastore.new($config.dataDir).expect("Should create repo SQLite data store!")) - raise newException(Defect, "Unknown repoKind: " & $config.repoKind) - -proc createRepoStore(config: BlockStoreManagerConfig): RepoStore = - RepoStore.new( - repoDs = getRepoDatastore(config), - metaDs = SQLiteDatastore.new(config.dataDir / CodexMetaNamespace).expect("Should create meta data store!"), - quotaMaxBytes = config.storageQuota.uint, - blockTtl = config.blockTtlSeconds.seconds) - -proc createMaintenance(repoStore: RepoStore, config: BlockStoreManagerConfig): BlockMaintainer = - BlockMaintainer.new( - repoStore, - interval = config.blockMaintenanceIntervalSeconds.seconds, - numberOfBlocksPerInterval = config.blockMaintenanceNumberOfBlocks) - -proc getBlockStore(repoStore: RepoStore, config: BlockStoreManagerConfig): BlockStore = - if config.cacheSize > 0: - return CacheStore.new(backingStore = repoStore, cacheSize = config.cacheSize * MiB) - return repoStore - -proc new*(T: type BlockStoreManager, config: BlockStoreManagerConfig): T = - let - repoStore = createRepoStore(config) - maintenance = createMaintenance(repoStore, config) - blockStore = getBlockStore(repoStore, config) - - T( - repoStore: repoStore, - maintenance: maintenance, - blockStore: blockStore)