@xyo-network/xl1-cli-lib 1.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +165 -0
- package/README.md +46 -0
- package/dist/node/commands/api/index.d.ts +2 -0
- package/dist/node/commands/api/index.d.ts.map +1 -0
- package/dist/node/commands/api/runApi.d.ts +9 -0
- package/dist/node/commands/api/runApi.d.ts.map +1 -0
- package/dist/node/commands/index.d.ts +4 -0
- package/dist/node/commands/index.d.ts.map +1 -0
- package/dist/node/commands/producer/index.d.ts +2 -0
- package/dist/node/commands/producer/index.d.ts.map +1 -0
- package/dist/node/commands/producer/runProducer.d.ts +11 -0
- package/dist/node/commands/producer/runProducer.d.ts.map +1 -0
- package/dist/node/commands/validator/index.d.ts +2 -0
- package/dist/node/commands/validator/index.d.ts.map +1 -0
- package/dist/node/commands/validator/runValidator.d.ts +11 -0
- package/dist/node/commands/validator/runValidator.d.ts.map +1 -0
- package/dist/node/images.d.ts +2 -0
- package/dist/node/images.d.ts.map +1 -0
- package/dist/node/index.d.ts +4 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.mjs +1712 -0
- package/dist/node/index.mjs.map +1 -0
- package/dist/node/initConfig.d.ts +3 -0
- package/dist/node/initConfig.d.ts.map +1 -0
- package/dist/node/initLogger.d.ts +4 -0
- package/dist/node/initLogger.d.ts.map +1 -0
- package/dist/node/optionsFromZodSchema.d.ts +4 -0
- package/dist/node/optionsFromZodSchema.d.ts.map +1 -0
- package/dist/node/orchestration/ChainInitializableParams.d.ts +11 -0
- package/dist/node/orchestration/ChainInitializableParams.d.ts.map +1 -0
- package/dist/node/orchestration/actor/implementation/BalanceActor.d.ts +18 -0
- package/dist/node/orchestration/actor/implementation/BalanceActor.d.ts.map +1 -0
- package/dist/node/orchestration/actor/implementation/ChainHeadUpdateActor.d.ts +19 -0
- package/dist/node/orchestration/actor/implementation/ChainHeadUpdateActor.d.ts.map +1 -0
- package/dist/node/orchestration/actor/implementation/ProducerActor.d.ts +28 -0
- package/dist/node/orchestration/actor/implementation/ProducerActor.d.ts.map +1 -0
- package/dist/node/orchestration/actor/implementation/ValidatorActor.d.ts +26 -0
- package/dist/node/orchestration/actor/implementation/ValidatorActor.d.ts.map +1 -0
- package/dist/node/orchestration/actor/implementation/index.d.ts +5 -0
- package/dist/node/orchestration/actor/implementation/index.d.ts.map +1 -0
- package/dist/node/orchestration/actor/index.d.ts +3 -0
- package/dist/node/orchestration/actor/index.d.ts.map +1 -0
- package/dist/node/orchestration/actor/model/Actor.d.ts +39 -0
- package/dist/node/orchestration/actor/model/Actor.d.ts.map +1 -0
- package/dist/node/orchestration/actor/model/Orchestrator.d.ts +27 -0
- package/dist/node/orchestration/actor/model/Orchestrator.d.ts.map +1 -0
- package/dist/node/orchestration/actor/model/index.d.ts +3 -0
- package/dist/node/orchestration/actor/model/index.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/ChainFinalized/archivist.d.ts +4 -0
- package/dist/node/orchestration/archivists/ChainFinalized/archivist.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/ChainFinalized/index.d.ts +2 -0
- package/dist/node/orchestration/archivists/ChainFinalized/index.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/ChainFinalized/local.d.ts +7 -0
- package/dist/node/orchestration/archivists/ChainFinalized/local.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/ChainFinalized/remote.d.ts +7 -0
- package/dist/node/orchestration/archivists/ChainFinalized/remote.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/ChainSubmissions/archivist.d.ts +7 -0
- package/dist/node/orchestration/archivists/ChainSubmissions/archivist.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/ChainSubmissions/index.d.ts +2 -0
- package/dist/node/orchestration/archivists/ChainSubmissions/index.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/ChainSubmissions/remote.d.ts +7 -0
- package/dist/node/orchestration/archivists/ChainSubmissions/remote.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/PendingTransactions/archivist.d.ts +6 -0
- package/dist/node/orchestration/archivists/PendingTransactions/archivist.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/PendingTransactions/index.d.ts +2 -0
- package/dist/node/orchestration/archivists/PendingTransactions/index.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/PendingTransactions/local.d.ts +7 -0
- package/dist/node/orchestration/archivists/PendingTransactions/local.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/PendingTransactions/remote.d.ts +7 -0
- package/dist/node/orchestration/archivists/PendingTransactions/remote.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/RejectedTransactions/archivist.d.ts +4 -0
- package/dist/node/orchestration/archivists/RejectedTransactions/archivist.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/RejectedTransactions/index.d.ts +2 -0
- package/dist/node/orchestration/archivists/RejectedTransactions/index.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/RejectedTransactions/local.d.ts +4 -0
- package/dist/node/orchestration/archivists/RejectedTransactions/local.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/StakeIntentState/archivist.d.ts +5 -0
- package/dist/node/orchestration/archivists/StakeIntentState/archivist.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/StakeIntentState/index.d.ts +2 -0
- package/dist/node/orchestration/archivists/StakeIntentState/index.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/StakeIntentState/local.d.ts +7 -0
- package/dist/node/orchestration/archivists/StakeIntentState/local.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/index.d.ts +6 -0
- package/dist/node/orchestration/archivists/index.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/lib/index.d.ts +2 -0
- package/dist/node/orchestration/archivists/lib/index.d.ts.map +1 -0
- package/dist/node/orchestration/archivists/lib/localPersistentArchivist.d.ts +20 -0
- package/dist/node/orchestration/archivists/lib/localPersistentArchivist.d.ts.map +1 -0
- package/dist/node/orchestration/health/index.d.ts +2 -0
- package/dist/node/orchestration/health/index.d.ts.map +1 -0
- package/dist/node/orchestration/health/initHealthEndpoints.d.ts +11 -0
- package/dist/node/orchestration/health/initHealthEndpoints.d.ts.map +1 -0
- package/dist/node/orchestration/host/implementation/DefaultHost.d.ts +12 -0
- package/dist/node/orchestration/host/implementation/DefaultHost.d.ts.map +1 -0
- package/dist/node/orchestration/host/implementation/DefaultServiceCollection.d.ts +24 -0
- package/dist/node/orchestration/host/implementation/DefaultServiceCollection.d.ts.map +1 -0
- package/dist/node/orchestration/host/implementation/DefaultServiceProvider.d.ts +7 -0
- package/dist/node/orchestration/host/implementation/DefaultServiceProvider.d.ts.map +1 -0
- package/dist/node/orchestration/host/implementation/index.d.ts +3 -0
- package/dist/node/orchestration/host/implementation/index.d.ts.map +1 -0
- package/dist/node/orchestration/host/index.d.ts +3 -0
- package/dist/node/orchestration/host/index.d.ts.map +1 -0
- package/dist/node/orchestration/host/model/Host.d.ts +19 -0
- package/dist/node/orchestration/host/model/Host.d.ts.map +1 -0
- package/dist/node/orchestration/host/model/ServiceCollection.d.ts +20 -0
- package/dist/node/orchestration/host/model/ServiceCollection.d.ts.map +1 -0
- package/dist/node/orchestration/host/model/ServiceProvider.d.ts +4 -0
- package/dist/node/orchestration/host/model/ServiceProvider.d.ts.map +1 -0
- package/dist/node/orchestration/host/model/index.d.ts +4 -0
- package/dist/node/orchestration/host/model/index.d.ts.map +1 -0
- package/dist/node/orchestration/index.d.ts +6 -0
- package/dist/node/orchestration/index.d.ts.map +1 -0
- package/dist/node/orchestration/initServices.d.ts +9 -0
- package/dist/node/orchestration/initServices.d.ts.map +1 -0
- package/dist/node/orchestration/map/BalanceSummary/index.d.ts +2 -0
- package/dist/node/orchestration/map/BalanceSummary/index.d.ts.map +1 -0
- package/dist/node/orchestration/map/BalanceSummary/initBalanceSummaryMap.d.ts +7 -0
- package/dist/node/orchestration/map/BalanceSummary/initBalanceSummaryMap.d.ts.map +1 -0
- package/dist/node/orchestration/map/BalanceSummary/local.d.ts +7 -0
- package/dist/node/orchestration/map/BalanceSummary/local.d.ts.map +1 -0
- package/dist/node/orchestration/map/driver/index.d.ts +2 -0
- package/dist/node/orchestration/map/driver/index.d.ts.map +1 -0
- package/dist/node/orchestration/map/driver/lmdb/Params.d.ts +7 -0
- package/dist/node/orchestration/map/driver/lmdb/Params.d.ts.map +1 -0
- package/dist/node/orchestration/map/driver/lmdb/SynchronousLmdbMap.d.ts +32 -0
- package/dist/node/orchestration/map/driver/lmdb/SynchronousLmdbMap.d.ts.map +1 -0
- package/dist/node/orchestration/map/driver/lmdb/index.d.ts +2 -0
- package/dist/node/orchestration/map/driver/lmdb/index.d.ts.map +1 -0
- package/dist/node/orchestration/map/driver/lmdb/spec/SynchronousLmdbMap.spec.d.ts +2 -0
- package/dist/node/orchestration/map/driver/lmdb/spec/SynchronousLmdbMap.spec.d.ts.map +1 -0
- package/dist/node/orchestration/map/index.d.ts +3 -0
- package/dist/node/orchestration/map/index.d.ts.map +1 -0
- package/dist/node/orchestration/map/localPersistentMap.d.ts +13 -0
- package/dist/node/orchestration/map/localPersistentMap.d.ts.map +1 -0
- package/dist/node/orchestration/repository/index.d.ts +2 -0
- package/dist/node/orchestration/repository/index.d.ts.map +1 -0
- package/dist/node/orchestration/repository/lib/index.d.ts +2 -0
- package/dist/node/orchestration/repository/lib/index.d.ts.map +1 -0
- package/dist/node/orchestration/repository/lib/repositoryFromArchivist.d.ts +6 -0
- package/dist/node/orchestration/repository/lib/repositoryFromArchivist.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/account.d.ts +7 -0
- package/dist/node/orchestration/services/implementation/account.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/balance.d.ts +4 -0
- package/dist/node/orchestration/services/implementation/balance.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/chain/evm.d.ts +7 -0
- package/dist/node/orchestration/services/implementation/chain/evm.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/chain/index.d.ts +4 -0
- package/dist/node/orchestration/services/implementation/chain/index.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/evm/index.d.ts +2 -0
- package/dist/node/orchestration/services/implementation/evm/index.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/evm/initChainId.d.ts +4 -0
- package/dist/node/orchestration/services/implementation/evm/initChainId.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/evm/initEvmProvider.d.ts +11 -0
- package/dist/node/orchestration/services/implementation/evm/initEvmProvider.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/evm/initInfuraProvider.d.ts +6 -0
- package/dist/node/orchestration/services/implementation/evm/initInfuraProvider.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/evm/initJsonRpcProvider.d.ts +6 -0
- package/dist/node/orchestration/services/implementation/evm/initJsonRpcProvider.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/head.d.ts +10 -0
- package/dist/node/orchestration/services/implementation/head.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/index.d.ts +11 -0
- package/dist/node/orchestration/services/implementation/index.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/iterator.d.ts +6 -0
- package/dist/node/orchestration/services/implementation/iterator.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/pendingTransactions.d.ts +4 -0
- package/dist/node/orchestration/services/implementation/pendingTransactions.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/producer.d.ts +4 -0
- package/dist/node/orchestration/services/implementation/producer.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/reward.d.ts +4 -0
- package/dist/node/orchestration/services/implementation/reward.d.ts.map +1 -0
- package/dist/node/orchestration/services/implementation/validator.d.ts +4 -0
- package/dist/node/orchestration/services/implementation/validator.d.ts.map +1 -0
- package/dist/node/orchestration/services/index.d.ts +2 -0
- package/dist/node/orchestration/services/index.d.ts.map +1 -0
- package/dist/node/orchestration/status/RuntimeStatusMonitor.d.ts +31 -0
- package/dist/node/orchestration/status/RuntimeStatusMonitor.d.ts.map +1 -0
- package/dist/node/orchestration/status/ServiceStatus.d.ts +9 -0
- package/dist/node/orchestration/status/ServiceStatus.d.ts.map +1 -0
- package/dist/node/orchestration/status/index.d.ts +3 -0
- package/dist/node/orchestration/status/index.d.ts.map +1 -0
- package/dist/node/orchestration/store/StoreKind.d.ts +2 -0
- package/dist/node/orchestration/store/StoreKind.d.ts.map +1 -0
- package/dist/node/orchestration/store/getStoreDirectory.d.ts +10 -0
- package/dist/node/orchestration/store/getStoreDirectory.d.ts.map +1 -0
- package/dist/node/orchestration/store/index.d.ts +3 -0
- package/dist/node/orchestration/store/index.d.ts.map +1 -0
- package/dist/node/runCLI.d.ts +2 -0
- package/dist/node/runCLI.d.ts.map +1 -0
- package/dist/node/spec/BootstrapChain.spec.d.ts +2 -0
- package/dist/node/spec/BootstrapChain.spec.d.ts.map +1 -0
- package/dist/node/start.d.ts +2 -0
- package/dist/node/start.d.ts.map +1 -0
- package/dist/node/waitForHostPort.d.ts +2 -0
- package/dist/node/waitForHostPort.d.ts.map +1 -0
- package/dist/node/xl1.d.ts +2 -0
- package/dist/node/xl1.d.ts.map +1 -0
- package/dist/node/xl1.mjs +1610 -0
- package/dist/node/xl1.mjs.map +1 -0
- package/package.json +96 -0
- package/src/commands/api/index.ts +1 -0
- package/src/commands/api/runApi.ts +12 -0
- package/src/commands/index.ts +3 -0
- package/src/commands/producer/index.ts +1 -0
- package/src/commands/producer/runProducer.ts +38 -0
- package/src/commands/validator/index.ts +1 -0
- package/src/commands/validator/runValidator.ts +38 -0
- package/src/images.ts +19 -0
- package/src/index.ts +3 -0
- package/src/initConfig.ts +6 -0
- package/src/initLogger.ts +23 -0
- package/src/optionsFromZodSchema.ts +69 -0
- package/src/orchestration/ChainInitializableParams.ts +12 -0
- package/src/orchestration/actor/implementation/BalanceActor.ts +50 -0
- package/src/orchestration/actor/implementation/ChainHeadUpdateActor.ts +65 -0
- package/src/orchestration/actor/implementation/ProducerActor.ts +244 -0
- package/src/orchestration/actor/implementation/ValidatorActor.ts +113 -0
- package/src/orchestration/actor/implementation/index.ts +4 -0
- package/src/orchestration/actor/index.ts +2 -0
- package/src/orchestration/actor/model/Actor.ts +114 -0
- package/src/orchestration/actor/model/Orchestrator.ts +71 -0
- package/src/orchestration/actor/model/index.ts +2 -0
- package/src/orchestration/archivists/ChainFinalized/archivist.ts +29 -0
- package/src/orchestration/archivists/ChainFinalized/index.ts +1 -0
- package/src/orchestration/archivists/ChainFinalized/local.ts +18 -0
- package/src/orchestration/archivists/ChainFinalized/remote.ts +20 -0
- package/src/orchestration/archivists/ChainSubmissions/archivist.ts +20 -0
- package/src/orchestration/archivists/ChainSubmissions/index.ts +1 -0
- package/src/orchestration/archivists/ChainSubmissions/remote.ts +20 -0
- package/src/orchestration/archivists/PendingTransactions/archivist.ts +29 -0
- package/src/orchestration/archivists/PendingTransactions/index.ts +1 -0
- package/src/orchestration/archivists/PendingTransactions/local.ts +21 -0
- package/src/orchestration/archivists/PendingTransactions/remote.ts +20 -0
- package/src/orchestration/archivists/RejectedTransactions/archivist.ts +18 -0
- package/src/orchestration/archivists/RejectedTransactions/index.ts +1 -0
- package/src/orchestration/archivists/RejectedTransactions/local.ts +20 -0
- package/src/orchestration/archivists/StakeIntentState/archivist.ts +21 -0
- package/src/orchestration/archivists/StakeIntentState/index.ts +1 -0
- package/src/orchestration/archivists/StakeIntentState/local.ts +19 -0
- package/src/orchestration/archivists/index.ts +5 -0
- package/src/orchestration/archivists/lib/index.ts +1 -0
- package/src/orchestration/archivists/lib/localPersistentArchivist.ts +57 -0
- package/src/orchestration/health/index.ts +1 -0
- package/src/orchestration/health/initHealthEndpoints.ts +72 -0
- package/src/orchestration/host/implementation/DefaultHost.ts +25 -0
- package/src/orchestration/host/implementation/DefaultServiceCollection.ts +60 -0
- package/src/orchestration/host/implementation/DefaultServiceProvider.ts +12 -0
- package/src/orchestration/host/implementation/index.ts +2 -0
- package/src/orchestration/host/index.ts +2 -0
- package/src/orchestration/host/model/Host.ts +21 -0
- package/src/orchestration/host/model/ServiceCollection.ts +22 -0
- package/src/orchestration/host/model/ServiceProvider.ts +3 -0
- package/src/orchestration/host/model/index.ts +3 -0
- package/src/orchestration/index.ts +5 -0
- package/src/orchestration/initServices.ts +240 -0
- package/src/orchestration/map/BalanceSummary/index.ts +1 -0
- package/src/orchestration/map/BalanceSummary/initBalanceSummaryMap.ts +22 -0
- package/src/orchestration/map/BalanceSummary/local.ts +21 -0
- package/src/orchestration/map/driver/index.ts +1 -0
- package/src/orchestration/map/driver/lmdb/Params.ts +7 -0
- package/src/orchestration/map/driver/lmdb/SynchronousLmdbMap.ts +66 -0
- package/src/orchestration/map/driver/lmdb/index.ts +1 -0
- package/src/orchestration/map/driver/lmdb/spec/SynchronousLmdbMap.spec.ts +97 -0
- package/src/orchestration/map/index.ts +2 -0
- package/src/orchestration/map/localPersistentMap.ts +52 -0
- package/src/orchestration/repository/index.ts +1 -0
- package/src/orchestration/repository/lib/index.ts +1 -0
- package/src/orchestration/repository/lib/repositoryFromArchivist.ts +27 -0
- package/src/orchestration/services/implementation/account.ts +24 -0
- package/src/orchestration/services/implementation/balance.ts +13 -0
- package/src/orchestration/services/implementation/chain/evm.ts +38 -0
- package/src/orchestration/services/implementation/chain/index.ts +22 -0
- package/src/orchestration/services/implementation/evm/index.ts +1 -0
- package/src/orchestration/services/implementation/evm/initChainId.ts +20 -0
- package/src/orchestration/services/implementation/evm/initEvmProvider.ts +23 -0
- package/src/orchestration/services/implementation/evm/initInfuraProvider.ts +27 -0
- package/src/orchestration/services/implementation/evm/initJsonRpcProvider.ts +21 -0
- package/src/orchestration/services/implementation/head.ts +44 -0
- package/src/orchestration/services/implementation/index.ts +10 -0
- package/src/orchestration/services/implementation/iterator.ts +36 -0
- package/src/orchestration/services/implementation/pendingTransactions.ts +14 -0
- package/src/orchestration/services/implementation/producer.ts +13 -0
- package/src/orchestration/services/implementation/reward.ts +41 -0
- package/src/orchestration/services/implementation/validator.ts +14 -0
- package/src/orchestration/services/index.ts +1 -0
- package/src/orchestration/status/RuntimeStatusMonitor.ts +117 -0
- package/src/orchestration/status/ServiceStatus.ts +21 -0
- package/src/orchestration/status/index.ts +2 -0
- package/src/orchestration/store/StoreKind.ts +1 -0
- package/src/orchestration/store/getStoreDirectory.ts +14 -0
- package/src/orchestration/store/index.ts +2 -0
- package/src/runCLI.ts +111 -0
- package/src/spec/BootstrapChain.spec.ts +133 -0
- package/src/spec/MultiProducer.ChainOutput.json +864 -0
- package/src/start.ts +8 -0
- package/src/waitForHostPort.ts +26 -0
- package/src/xl1.ts +8 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { filterAs } from '@xylabs/array'
|
|
2
|
+
import type { Hash } from '@xylabs/hex'
|
|
3
|
+
import type { AnyNonPromise } from '@xylabs/promise'
|
|
4
|
+
import type { ArchivistInstance } from '@xyo-network/archivist-model'
|
|
5
|
+
import type { Payload, WithStorageMeta } from '@xyo-network/payload-model'
|
|
6
|
+
import type { PayloadRepository } from '@xyo-network/xl1-protocol'
|
|
7
|
+
|
|
8
|
+
export const repositoryFromArchivist = <T extends Payload = Payload>(
|
|
9
|
+
archivist: ArchivistInstance,
|
|
10
|
+
predicate: (value: AnyNonPromise) => WithStorageMeta<T>) => {
|
|
11
|
+
const repository: PayloadRepository<Hash, WithStorageMeta<T>> = {
|
|
12
|
+
get: async (hashes: Hash[]) => {
|
|
13
|
+
const payloads = await archivist.get(hashes)
|
|
14
|
+
const filtered = filterAs(payloads, predicate)
|
|
15
|
+
return filtered
|
|
16
|
+
},
|
|
17
|
+
insert: async (payloads: T[]) => {
|
|
18
|
+
const filtered = filterAs(payloads, predicate)
|
|
19
|
+
await archivist.insert(filtered)
|
|
20
|
+
return filtered
|
|
21
|
+
},
|
|
22
|
+
next: () => {
|
|
23
|
+
throw new Error('Not implemented')
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
return repository
|
|
27
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/promise'
|
|
2
|
+
import { isUndefined } from '@xylabs/typeof'
|
|
3
|
+
import { ADDRESS_INDEX, generateXyoBaseWalletFromPhrase } from '@xyo-network/chain-protocol'
|
|
4
|
+
import { HDWallet } from '@xyo-network/wallet'
|
|
5
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
6
|
+
import type { Initializable, InitializableParams } from '@xyo-network/xl1-protocol'
|
|
7
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
8
|
+
|
|
9
|
+
let accountServiceSingleton: Promisable<WalletInstance> | undefined
|
|
10
|
+
|
|
11
|
+
export const initAccount: Initializable<InitializableParams<{ config: Config }>, WalletInstance> = async ({ config, logger }): Promise<WalletInstance> => {
|
|
12
|
+
if (accountServiceSingleton) return accountServiceSingleton
|
|
13
|
+
let walletPhrase = config.producer.mnemonic
|
|
14
|
+
if (isUndefined(walletPhrase)) {
|
|
15
|
+
logger?.log('No wallet mnemonic specified!')
|
|
16
|
+
const randomMnemonic = HDWallet.generateMnemonic()
|
|
17
|
+
logger?.log(`Using randomly generated mnemonic: ${randomMnemonic}`)
|
|
18
|
+
walletPhrase = randomMnemonic
|
|
19
|
+
}
|
|
20
|
+
const wallet = await generateXyoBaseWalletFromPhrase(walletPhrase)
|
|
21
|
+
const account = await wallet.derivePath(ADDRESS_INDEX.XYO)
|
|
22
|
+
accountServiceSingleton = account
|
|
23
|
+
return accountServiceSingleton
|
|
24
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/promise'
|
|
2
|
+
import type { BaseAccountBalanceServiceParams } from '@xyo-network/chain-services'
|
|
3
|
+
import { BaseAccountBalanceService } from '@xyo-network/chain-services'
|
|
4
|
+
import type { AccountBalanceServiceV2, Initializable } from '@xyo-network/xl1-protocol'
|
|
5
|
+
|
|
6
|
+
let balanceServiceSingleton: Promisable<AccountBalanceServiceV2> | undefined
|
|
7
|
+
|
|
8
|
+
export const initBalanceService: Initializable<BaseAccountBalanceServiceParams, AccountBalanceServiceV2>
|
|
9
|
+
= async (params): Promise<AccountBalanceServiceV2> => {
|
|
10
|
+
if (balanceServiceSingleton) return balanceServiceSingleton
|
|
11
|
+
balanceServiceSingleton = await BaseAccountBalanceService.create(params)
|
|
12
|
+
return balanceServiceSingleton
|
|
13
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
2
|
+
import type { Address } from '@xylabs/hex'
|
|
3
|
+
import { asAddress, ZERO_ADDRESS } from '@xylabs/hex'
|
|
4
|
+
import type { Promisable } from '@xylabs/promise'
|
|
5
|
+
import { isDefined } from '@xylabs/typeof'
|
|
6
|
+
import type { BaseAccountableServiceParams } from '@xyo-network/chain-services'
|
|
7
|
+
import { EvmChainService } from '@xyo-network/chain-services'
|
|
8
|
+
import type { Initializable } from '@xyo-network/xl1-protocol'
|
|
9
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
10
|
+
import type { ContractRunner } from 'ethers'
|
|
11
|
+
import { Wallet } from 'ethers/wallet'
|
|
12
|
+
|
|
13
|
+
import { canUseEvmProvider, initEvmProvider } from '../evm/index.ts'
|
|
14
|
+
|
|
15
|
+
let chainStakeServiceSingleton: Promisable<EvmChainService> | undefined
|
|
16
|
+
|
|
17
|
+
export const canUseEvmContractChainService = (config: Config) => {
|
|
18
|
+
const { id } = config.chain
|
|
19
|
+
return isDefined(id) && id !== ZERO_ADDRESS && canUseEvmProvider({ config })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const initEvmContractChainService: Initializable<BaseAccountableServiceParams, EvmChainService> = async ({
|
|
23
|
+
account, config, traceProvider, meterProvider, logger,
|
|
24
|
+
}) => {
|
|
25
|
+
if (chainStakeServiceSingleton) return chainStakeServiceSingleton
|
|
26
|
+
// Parse config
|
|
27
|
+
const emvStakingContractAddress = assertEx(config.chain.id, () => 'Error: config.chain.id is required')
|
|
28
|
+
// Configure ContractRunner
|
|
29
|
+
const id: Address = assertEx(asAddress(emvStakingContractAddress), () => 'Error: config.chain.id is not a valid address')
|
|
30
|
+
const provider = assertEx(await initEvmProvider({ config }))
|
|
31
|
+
const privateKey = assertEx(account.private?.hex, () => 'Error: Account does not have a private key')
|
|
32
|
+
const runner: ContractRunner = new Wallet(privateKey, provider)
|
|
33
|
+
// Create service
|
|
34
|
+
chainStakeServiceSingleton = EvmChainService.create({
|
|
35
|
+
id, runner, traceProvider, meterProvider, logger,
|
|
36
|
+
})
|
|
37
|
+
return await chainStakeServiceSingleton
|
|
38
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/promise'
|
|
2
|
+
import type { BaseAccountableServiceParams } from '@xyo-network/chain-services'
|
|
3
|
+
import { MemoryChainService } from '@xyo-network/chain-services'
|
|
4
|
+
import type { ChainService, Initializable } from '@xyo-network/xl1-protocol'
|
|
5
|
+
|
|
6
|
+
import { canUseEvmContractChainService, initEvmContractChainService } from './evm.ts'
|
|
7
|
+
|
|
8
|
+
let chainStakeServiceSingleton: Promisable<ChainService> | undefined
|
|
9
|
+
|
|
10
|
+
export const initChainService: Initializable<BaseAccountableServiceParams, ChainService> = ({ account, config }) => init({
|
|
11
|
+
config, name: 'ChainService', account,
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const init: Initializable<BaseAccountableServiceParams, ChainService> = (params): Promisable<ChainService> => {
|
|
15
|
+
if (chainStakeServiceSingleton) return chainStakeServiceSingleton
|
|
16
|
+
const { config } = params
|
|
17
|
+
chainStakeServiceSingleton = canUseEvmContractChainService(config)
|
|
18
|
+
? initEvmContractChainService({ ...params, name: 'ChainStakeService' })
|
|
19
|
+
: MemoryChainService.create({ ...params, name: 'ChainStakeService' })
|
|
20
|
+
// Create service
|
|
21
|
+
return chainStakeServiceSingleton
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './initEvmProvider.ts'
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
2
|
+
import { hexFrom, isHex } from '@xylabs/hex'
|
|
3
|
+
import { isDefined } from '@xylabs/typeof'
|
|
4
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
5
|
+
|
|
6
|
+
export const canUseChainId = (config: Config): boolean => {
|
|
7
|
+
return isDefined(config.evm.chainId)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const getChainId = (config: Config) => {
|
|
11
|
+
const chainId = assertEx(config.evm.chainId, () => 'Missing config.evm.chainId')
|
|
12
|
+
if (isHex(chainId, { prefix: true })) {
|
|
13
|
+
const hex = hexFrom(chainId)
|
|
14
|
+
const parsed = Number.parseInt(hex, 16)
|
|
15
|
+
return parsed
|
|
16
|
+
} else {
|
|
17
|
+
const parsed = Number.parseInt(chainId, 10)
|
|
18
|
+
return parsed
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
2
|
+
import type { Logger } from '@xylabs/logger'
|
|
3
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import type { JsonRpcProvider } from 'ethers/providers'
|
|
5
|
+
|
|
6
|
+
import { canUseInfuraProvider, initInfuraProvider } from './initInfuraProvider.ts'
|
|
7
|
+
import { canUseJsonRpcProvider, initJsonRpcProvider } from './initJsonRpcProvider.ts'
|
|
8
|
+
|
|
9
|
+
let provider: Promise<JsonRpcProvider> | undefined
|
|
10
|
+
|
|
11
|
+
export const initEvmProvider = async ({ config }: { config: Config; logger?: Logger }) => {
|
|
12
|
+
if (provider) return provider
|
|
13
|
+
if (canUseInfuraProvider(config)) {
|
|
14
|
+
provider = initInfuraProvider(config)
|
|
15
|
+
} else if (canUseJsonRpcProvider(config)) {
|
|
16
|
+
provider = initJsonRpcProvider(config)
|
|
17
|
+
}
|
|
18
|
+
return assertEx(await provider, () => 'Error: No provider available')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const canUseEvmProvider = ({ config }: { config: Config }) => {
|
|
22
|
+
return canUseInfuraProvider(config) || canUseJsonRpcProvider(config)
|
|
23
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
2
|
+
import { isDefined } from '@xylabs/typeof'
|
|
3
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import { InfuraProvider } from 'ethers/providers'
|
|
5
|
+
|
|
6
|
+
import { canUseChainId, getChainId } from './initChainId.ts'
|
|
7
|
+
|
|
8
|
+
let instance: Promise<InfuraProvider> | undefined
|
|
9
|
+
|
|
10
|
+
export const initInfuraProvider = (config: Config) => {
|
|
11
|
+
if (instance) return instance
|
|
12
|
+
const providerConfig = getInfuraProviderConfig(config)
|
|
13
|
+
instance = Promise.resolve(new InfuraProvider(...providerConfig))
|
|
14
|
+
return instance
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const canUseInfuraProvider = (config: Config): boolean => {
|
|
18
|
+
return canUseChainId(config)
|
|
19
|
+
&& isDefined(config.evm?.infura?.projectId)
|
|
20
|
+
&& isDefined(config.evm?.infura?.projectSecret)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const getInfuraProviderConfig = (config: Config) => {
|
|
24
|
+
const projectId = assertEx(config.evm?.infura?.projectId, () => 'Missing config.evm.infura.projectId')
|
|
25
|
+
const projectSecret = assertEx(config.evm?.infura?.projectSecret, () => 'Missing config.evm.infura.projectSecret')
|
|
26
|
+
return [getChainId(config), projectId, projectSecret] as const
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
2
|
+
import { isDefined } from '@xylabs/typeof'
|
|
3
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import { JsonRpcProvider } from 'ethers/providers'
|
|
5
|
+
|
|
6
|
+
import { canUseChainId, getChainId } from './initChainId.ts'
|
|
7
|
+
|
|
8
|
+
export const initJsonRpcProvider = (config: Config) => {
|
|
9
|
+
const providerConfig = getJsonRpcProviderConfig(config)
|
|
10
|
+
return Promise.resolve(new JsonRpcProvider(...providerConfig))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const canUseJsonRpcProvider = (config: Config) => {
|
|
14
|
+
return canUseChainId(config)
|
|
15
|
+
&& isDefined(config.evm.jsonRpc?.url)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const getJsonRpcProviderConfig = (config: Config) => {
|
|
19
|
+
const jsonRpcUrl = assertEx(config.evm.jsonRpc?.url, () => 'Missing config.evm.jsonRpc.url')
|
|
20
|
+
return [jsonRpcUrl, getChainId(config)] as const
|
|
21
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { delay } from '@xylabs/delay'
|
|
2
|
+
import { ZERO_ADDRESS } from '@xylabs/hex'
|
|
3
|
+
import type { Promisable } from '@xylabs/promise'
|
|
4
|
+
import type { ArchivistInstance } from '@xyo-network/archivist-model'
|
|
5
|
+
import { createGenesisBlock, findMostRecentBlock } from '@xyo-network/chain-protocol'
|
|
6
|
+
import type { WithStorageMeta } from '@xyo-network/payload-model'
|
|
7
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
8
|
+
import type { BlockBoundWitness, Initializable } from '@xyo-network/xl1-protocol'
|
|
9
|
+
import { flattenHydratedBlock } from '@xyo-network/xl1-protocol-sdk'
|
|
10
|
+
|
|
11
|
+
let headSingleton: Promisable<WithStorageMeta<BlockBoundWitness>> | undefined
|
|
12
|
+
|
|
13
|
+
const createBootstrapHead = async (
|
|
14
|
+
chainSubmissionsArchivistWrite: ArchivistInstance,
|
|
15
|
+
chainArchivist: ArchivistInstance,
|
|
16
|
+
account: WalletInstance,
|
|
17
|
+
): Promise<WithStorageMeta<BlockBoundWitness>> => {
|
|
18
|
+
const block = await createGenesisBlock(account, ZERO_ADDRESS, 10_000_000n, account.address)
|
|
19
|
+
const [bw] = block
|
|
20
|
+
await chainSubmissionsArchivistWrite.insert(flattenHydratedBlock(block))
|
|
21
|
+
// Wait for block to show up in finalized archivist
|
|
22
|
+
while (true) {
|
|
23
|
+
const result = await chainArchivist.get([bw._hash])
|
|
24
|
+
if (result.length > 0) break
|
|
25
|
+
await delay(1000) // Wait 1 second before retrying
|
|
26
|
+
}
|
|
27
|
+
return bw
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const initHead: Initializable<{
|
|
31
|
+
account: WalletInstance
|
|
32
|
+
chainArchivist: ArchivistInstance
|
|
33
|
+
chainSubmissionsArchivistWrite: ArchivistInstance
|
|
34
|
+
}, WithStorageMeta<BlockBoundWitness>>
|
|
35
|
+
= async (params): Promise<WithStorageMeta<BlockBoundWitness>> => {
|
|
36
|
+
const {
|
|
37
|
+
account, chainArchivist, chainSubmissionsArchivistWrite,
|
|
38
|
+
} = params
|
|
39
|
+
if (headSingleton) return headSingleton
|
|
40
|
+
let head = await findMostRecentBlock(chainArchivist)
|
|
41
|
+
if (!head) head = await createBootstrapHead(chainSubmissionsArchivistWrite, chainArchivist, account)
|
|
42
|
+
headSingleton = head
|
|
43
|
+
return headSingleton
|
|
44
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './account.ts'
|
|
2
|
+
export * from './balance.ts'
|
|
3
|
+
export * from './chain/index.ts'
|
|
4
|
+
export * from './evm/index.ts'
|
|
5
|
+
export * from './head.ts'
|
|
6
|
+
export * from './iterator.ts'
|
|
7
|
+
export * from './pendingTransactions.ts'
|
|
8
|
+
export * from './producer.ts'
|
|
9
|
+
export * from './reward.ts'
|
|
10
|
+
export * from './validator.ts'
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BaseElectionServicesParams,
|
|
3
|
+
XyoChainIteratorParams,
|
|
4
|
+
XyoStakeIntentServiceParams,
|
|
5
|
+
} from '@xyo-network/chain-services'
|
|
6
|
+
import {
|
|
7
|
+
BaseElectionService, ChainBlockNumberIterationService,
|
|
8
|
+
XyoStakeIntentService,
|
|
9
|
+
} from '@xyo-network/chain-services'
|
|
10
|
+
import type {
|
|
11
|
+
ElectionService, EventingChainBlockNumberIterator, Initializable, StakeIntentService,
|
|
12
|
+
} from '@xyo-network/xl1-protocol'
|
|
13
|
+
|
|
14
|
+
let chainIteratorServiceSingleton: Promise<EventingChainBlockNumberIterator> | undefined
|
|
15
|
+
let stakeIntentServiceSingleton: Promise<StakeIntentService> | undefined
|
|
16
|
+
let electionServiceSingleton: Promise<ElectionService> | undefined
|
|
17
|
+
|
|
18
|
+
export const initChainIterator: Initializable<XyoChainIteratorParams, EventingChainBlockNumberIterator> = (params) => {
|
|
19
|
+
if (chainIteratorServiceSingleton) return chainIteratorServiceSingleton
|
|
20
|
+
chainIteratorServiceSingleton = ChainBlockNumberIterationService.create(params)
|
|
21
|
+
return chainIteratorServiceSingleton
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const initStakeIntentService: Initializable<XyoStakeIntentServiceParams, StakeIntentService>
|
|
25
|
+
= (params) => {
|
|
26
|
+
if (stakeIntentServiceSingleton) return stakeIntentServiceSingleton
|
|
27
|
+
stakeIntentServiceSingleton = XyoStakeIntentService.create(params)
|
|
28
|
+
return stakeIntentServiceSingleton
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const initElectionService: Initializable<BaseElectionServicesParams, ElectionService>
|
|
32
|
+
= (params) => {
|
|
33
|
+
if (electionServiceSingleton) return electionServiceSingleton
|
|
34
|
+
electionServiceSingleton = BaseElectionService.create(params)
|
|
35
|
+
return electionServiceSingleton
|
|
36
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/promise'
|
|
2
|
+
import type { BasePendingTransactionsServiceParams } from '@xyo-network/chain-services'
|
|
3
|
+
import { BasePendingTransactionsService } from '@xyo-network/chain-services'
|
|
4
|
+
import type { Initializable, PendingTransactionsService } from '@xyo-network/xl1-protocol'
|
|
5
|
+
|
|
6
|
+
let serviceSingleton: Promisable<PendingTransactionsService> | undefined
|
|
7
|
+
|
|
8
|
+
export const initPendingTransactions: Initializable<BasePendingTransactionsServiceParams, PendingTransactionsService> = async (
|
|
9
|
+
params: BasePendingTransactionsServiceParams,
|
|
10
|
+
): Promise<PendingTransactionsService> => {
|
|
11
|
+
if (serviceSingleton) return serviceSingleton
|
|
12
|
+
serviceSingleton = await BasePendingTransactionsService.create(params)
|
|
13
|
+
return serviceSingleton
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/promise'
|
|
2
|
+
import type { BaseBlockProducerServiceParams } from '@xyo-network/chain-services'
|
|
3
|
+
import { BaseBlockProducerService } from '@xyo-network/chain-services'
|
|
4
|
+
import type { BlockProducerService, Initializable } from '@xyo-network/xl1-protocol'
|
|
5
|
+
|
|
6
|
+
let serviceSingleton: Promisable<BlockProducerService> | undefined
|
|
7
|
+
|
|
8
|
+
export const initBlockProducer: Initializable<BaseBlockProducerServiceParams, BlockProducerService>
|
|
9
|
+
= async (params: BaseBlockProducerServiceParams): Promise<BlockProducerService> => {
|
|
10
|
+
if (serviceSingleton) return serviceSingleton
|
|
11
|
+
serviceSingleton = await BaseBlockProducerService.create(params)
|
|
12
|
+
return serviceSingleton
|
|
13
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
2
|
+
import type { BaseBlockRewardServiceParams, EvmBlockRewardServiceParams } from '@xyo-network/chain-services'
|
|
3
|
+
import { EvmBlockRewardService, MemoryBlockRewardService } from '@xyo-network/chain-services'
|
|
4
|
+
import type { BlockRewardService, Initializable } from '@xyo-network/xl1-protocol'
|
|
5
|
+
import type { Config } from '@xyo-network/xl1-protocol-sdk'
|
|
6
|
+
|
|
7
|
+
import { canUseEvmProvider, initEvmProvider } from './evm/index.ts'
|
|
8
|
+
|
|
9
|
+
let rewardServiceSingleton: Promise<BlockRewardService> | undefined
|
|
10
|
+
|
|
11
|
+
export const initBlockRewardService: Initializable<
|
|
12
|
+
BaseBlockRewardServiceParams | EvmBlockRewardServiceParams,
|
|
13
|
+
BlockRewardService
|
|
14
|
+
> = (params): Promise<BlockRewardService> => {
|
|
15
|
+
if (rewardServiceSingleton) return rewardServiceSingleton
|
|
16
|
+
const { config } = params
|
|
17
|
+
rewardServiceSingleton = canUseEvmBlockRewardService({ config })
|
|
18
|
+
? initEvmBlockRewardService(params as EvmBlockRewardServiceParams)
|
|
19
|
+
: initXyoBlockRewardService(params)
|
|
20
|
+
return rewardServiceSingleton
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const initXyoBlockRewardService = (params: BaseBlockRewardServiceParams): Promise<BlockRewardService> => {
|
|
24
|
+
if (rewardServiceSingleton) return rewardServiceSingleton
|
|
25
|
+
rewardServiceSingleton = MemoryBlockRewardService.create(params)
|
|
26
|
+
return rewardServiceSingleton
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const canUseEvmBlockRewardService = (params: { config: Config }) => canUseEvmProvider({ config: params.config })
|
|
30
|
+
|
|
31
|
+
const initEvmBlockRewardService = async (params: EvmBlockRewardServiceParams): Promise<BlockRewardService> => {
|
|
32
|
+
if (rewardServiceSingleton) return rewardServiceSingleton
|
|
33
|
+
const { account: paramsAccount } = params
|
|
34
|
+
const account = assertEx(paramsAccount, () => 'Error: Account is required')
|
|
35
|
+
const provider = assertEx(await initEvmProvider(params))
|
|
36
|
+
const evmBlockRewardServiceParams: EvmBlockRewardServiceParams = {
|
|
37
|
+
...params, provider, account,
|
|
38
|
+
}
|
|
39
|
+
rewardServiceSingleton = EvmBlockRewardService.create(evmBlockRewardServiceParams)
|
|
40
|
+
return rewardServiceSingleton
|
|
41
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/promise'
|
|
2
|
+
import type { BaseBlockProducerServiceParams } from '@xyo-network/chain-services'
|
|
3
|
+
import type { BlockProducerService, Initializable } from '@xyo-network/xl1-protocol'
|
|
4
|
+
|
|
5
|
+
import { initBlockProducer } from './producer.ts'
|
|
6
|
+
|
|
7
|
+
let serviceSingleton: Promisable<BlockProducerService> | undefined
|
|
8
|
+
|
|
9
|
+
export const initValidator: Initializable<BaseBlockProducerServiceParams, BlockProducerService>
|
|
10
|
+
= (params: BaseBlockProducerServiceParams): Promisable<BlockProducerService> => {
|
|
11
|
+
if (serviceSingleton) return serviceSingleton
|
|
12
|
+
serviceSingleton = initBlockProducer(params)
|
|
13
|
+
return serviceSingleton
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './implementation/index.ts'
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { CreatableStatus } from '@xylabs/creatable'
|
|
2
|
+
|
|
3
|
+
import { LoggerStatusReporter } from './ServiceStatus.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Enum-like record for statuses with priority allowing
|
|
7
|
+
* for determination of the aggregated status.
|
|
8
|
+
*/
|
|
9
|
+
const statusPriority: Record<CreatableStatus, number> = {
|
|
10
|
+
error: 0,
|
|
11
|
+
stopped: 1,
|
|
12
|
+
stopping: 2,
|
|
13
|
+
creating: 3,
|
|
14
|
+
created: 4,
|
|
15
|
+
starting: 5,
|
|
16
|
+
started: 6,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A sentinel status that represents the initial state
|
|
21
|
+
* before any statuses are reported.
|
|
22
|
+
* It is used to ensure that the monitor has a valid status
|
|
23
|
+
* even when no statuses have been reported yet and to
|
|
24
|
+
* prevent the system from showing an "error" state
|
|
25
|
+
* when no statuses are available.
|
|
26
|
+
*/
|
|
27
|
+
const SENTINEL_STATUS: CreatableStatus = 'starting'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Given an array of statuses, this function reduces them
|
|
31
|
+
* to the minimum status based on the defined priority.
|
|
32
|
+
* @param statuses An array of statuses to reduce.
|
|
33
|
+
* @returns The minimum status based on priority.
|
|
34
|
+
*/
|
|
35
|
+
const reduceToMinimumStatus = (statuses: CreatableStatus[]): CreatableStatus => {
|
|
36
|
+
let minStatus: CreatableStatus = SENTINEL_STATUS
|
|
37
|
+
let minPriority = Infinity
|
|
38
|
+
|
|
39
|
+
for (const status of statuses) {
|
|
40
|
+
const priority = statusPriority[status]
|
|
41
|
+
if (priority < minPriority) {
|
|
42
|
+
minPriority = priority
|
|
43
|
+
minStatus = status
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return minStatus
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type GlobalTransitionHandler = {
|
|
50
|
+
from?: CreatableStatus
|
|
51
|
+
handler: (from: CreatableStatus, to: CreatableStatus) => void
|
|
52
|
+
to?: CreatableStatus
|
|
53
|
+
}
|
|
54
|
+
export type TransitionHandler = GlobalTransitionHandler & {
|
|
55
|
+
name?: string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export class RuntimeStatusMonitor extends LoggerStatusReporter {
|
|
59
|
+
private globalTransitions: GlobalTransitionHandler[] = []
|
|
60
|
+
private transitions: TransitionHandler[] = []
|
|
61
|
+
|
|
62
|
+
getGlobalStatus(): CreatableStatus {
|
|
63
|
+
// If no statuses have been reported, return the sentinel status
|
|
64
|
+
if (Object.keys(this.statusMap).length === 0) return SENTINEL_STATUS
|
|
65
|
+
return reduceToMinimumStatus(Object.values(this.statusMap))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getStatus(name: string): CreatableStatus | undefined {
|
|
69
|
+
return this.statusMap[name]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
onGlobalTransition(match: { from?: CreatableStatus; to?: CreatableStatus }, handler: (from: CreatableStatus, to: CreatableStatus) => void) {
|
|
73
|
+
this.globalTransitions.push({ ...match, handler })
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Register a callback to be called on a specific transition.
|
|
78
|
+
*/
|
|
79
|
+
onTransition(match: { from?: CreatableStatus; name: string; to?: CreatableStatus }, handler: (from: CreatableStatus, to: CreatableStatus) => void) {
|
|
80
|
+
this.transitions.push({ ...match, handler })
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
override report(name: string, status: CreatableStatus, progress?: number | Error): void {
|
|
84
|
+
// Capture the current status before updating
|
|
85
|
+
const previous = this.statusMap[name]
|
|
86
|
+
const previousGlobal = this.getGlobalStatus()
|
|
87
|
+
|
|
88
|
+
// Update the status map with the new status
|
|
89
|
+
super.report(name, status, progress)
|
|
90
|
+
|
|
91
|
+
// If the status hasn't changed, do not run handlers
|
|
92
|
+
if (previous === status) return
|
|
93
|
+
this.runTransitions(this.transitions, previous, status, name)
|
|
94
|
+
|
|
95
|
+
// If the global status hasn't changed, do not run handlers
|
|
96
|
+
const globalStatus = this.getGlobalStatus()
|
|
97
|
+
if (previousGlobal === globalStatus) return
|
|
98
|
+
this.runTransitions(this.globalTransitions, previousGlobal, globalStatus)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private runTransitions(
|
|
102
|
+
transitions: (TransitionHandler | GlobalTransitionHandler)[],
|
|
103
|
+
prev: CreatableStatus,
|
|
104
|
+
next: CreatableStatus,
|
|
105
|
+
name?: string,
|
|
106
|
+
) {
|
|
107
|
+
for (const {
|
|
108
|
+
from, to, name: matchName, handler,
|
|
109
|
+
} of transitions as TransitionHandler[]) {
|
|
110
|
+
if ((matchName === undefined || matchName === name)
|
|
111
|
+
&& (from === undefined || from === prev)
|
|
112
|
+
&& (to === undefined || to === next)) {
|
|
113
|
+
handler(prev, next)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CreatableName, CreatableStatus, CreatableStatusReporter,
|
|
3
|
+
} from '@xylabs/creatable'
|
|
4
|
+
import type { Logger } from '@xylabs/logger'
|
|
5
|
+
|
|
6
|
+
export class LoggerStatusReporter implements CreatableStatusReporter {
|
|
7
|
+
protected logger: Logger
|
|
8
|
+
|
|
9
|
+
protected statusMap: Record<CreatableName, CreatableStatus> = {}
|
|
10
|
+
|
|
11
|
+
constructor(logger: Logger) {
|
|
12
|
+
this.logger = logger
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
report(name: string, status: CreatableStatus, progress?: number | Error): void {
|
|
16
|
+
this.statusMap[name] = status
|
|
17
|
+
const starting = (Object.entries(this.statusMap).map(([, value]): number => value === 'starting' ? 1 : 0)).reduce((a, b) => a + b, 0)
|
|
18
|
+
const started = (Object.entries(this.statusMap).map(([, value]): number => value === 'started' ? 1 : 0)).reduce((a, b) => a + b, 0)
|
|
19
|
+
this.logger.info(`${started}/${starting + started} ${name} status: ${status}`, { progress })
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type StoreKind = 'lmdb'
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import type { StoreKind } from './StoreKind.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a convention-based directory path for an archivist store
|
|
7
|
+
* @param name The name of the archivist
|
|
8
|
+
* @param storageRoot The root directory for storage
|
|
9
|
+
* @param kind The kind of the archivist (optional)
|
|
10
|
+
* @returns The directory path for the archivist store
|
|
11
|
+
*/
|
|
12
|
+
export const getStoreDirectory = (name: string, storageRoot: string, kind?: StoreKind): string => {
|
|
13
|
+
return (kind === undefined) ? Path.join(storageRoot, name) : Path.join(storageRoot, kind, name)
|
|
14
|
+
}
|