@xyo-network/chain-services 1.17.0 → 1.17.1
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/dist/neutral/AccountBalance/accountBalanceServiceFromArchivist.d.ts +1 -1
- package/dist/neutral/AccountBalance/accountBalanceServiceFromArchivist.d.ts.map +1 -1
- package/dist/neutral/AccountBalance/index.d.ts +0 -1
- package/dist/neutral/AccountBalance/index.d.ts.map +1 -1
- package/dist/neutral/BaseService.d.ts +0 -3
- package/dist/neutral/BaseService.d.ts.map +1 -1
- package/dist/neutral/BlockReward/EvmBlockRewardViewer.d.ts +17 -0
- package/dist/neutral/BlockReward/EvmBlockRewardViewer.d.ts.map +1 -0
- package/dist/neutral/BlockReward/index.d.ts +1 -3
- package/dist/neutral/BlockReward/index.d.ts.map +1 -1
- package/dist/neutral/ChainBlockNumberIteration/ChainBlockNumberIterationService.d.ts +4 -2
- package/dist/neutral/ChainBlockNumberIteration/ChainBlockNumberIterationService.d.ts.map +1 -1
- package/dist/neutral/ChainService/Evm/Evm.d.ts +2 -1
- package/dist/neutral/ChainService/Evm/Evm.d.ts.map +1 -1
- package/dist/neutral/ChainService/Memory/Memory.d.ts +2 -0
- package/dist/neutral/ChainService/Memory/Memory.d.ts.map +1 -1
- package/dist/neutral/ChainValidator/XyoValidator.d.ts +2 -16
- package/dist/neutral/ChainValidator/XyoValidator.d.ts.map +1 -1
- package/dist/neutral/PendingTransactions/BasePendingTransactions.d.ts +7 -6
- package/dist/neutral/PendingTransactions/BasePendingTransactions.d.ts.map +1 -1
- package/dist/neutral/StakeIntent/XyoStakeIntentService.d.ts +5 -4
- package/dist/neutral/StakeIntent/XyoStakeIntentService.d.ts.map +1 -1
- package/dist/neutral/blockViewerFromChainIteratorAndArchivist.d.ts.map +1 -1
- package/dist/neutral/implementation/accountBalance.d.ts +7 -0
- package/dist/neutral/implementation/accountBalance.d.ts.map +1 -0
- package/dist/neutral/implementation/blockViewer.d.ts +7 -0
- package/dist/neutral/implementation/blockViewer.d.ts.map +1 -0
- package/dist/neutral/implementation/chain/evm.d.ts +7 -0
- package/dist/neutral/implementation/chain/evm.d.ts.map +1 -0
- package/dist/neutral/implementation/chain/index.d.ts +7 -0
- package/dist/neutral/implementation/chain/index.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/index.d.ts +2 -0
- package/dist/neutral/implementation/evm/index.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/initChainId.d.ts +4 -0
- package/dist/neutral/implementation/evm/initChainId.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/initEvmProvider.d.ts +11 -0
- package/dist/neutral/implementation/evm/initEvmProvider.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/initInfuraProvider.d.ts +6 -0
- package/dist/neutral/implementation/evm/initInfuraProvider.d.ts.map +1 -0
- package/dist/neutral/implementation/evm/initJsonRpcProvider.d.ts +6 -0
- package/dist/neutral/implementation/evm/initJsonRpcProvider.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createBootstrapHead.d.ts +5 -0
- package/dist/neutral/implementation/head/createBootstrapHead.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getBridgeDestinationDetails.d.ts +8 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getBridgeDestinationDetails.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getBridgeSourceDetails.d.ts +9 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getBridgeSourceDetails.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getForkDetails.d.ts +4 -0
- package/dist/neutral/implementation/head/createForkedHead/config/getForkDetails.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/config/index.d.ts +4 -0
- package/dist/neutral/implementation/head/createForkedHead/config/index.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/createForkedHead.d.ts +5 -0
- package/dist/neutral/implementation/head/createForkedHead/createForkedHead.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeDestinationObservation.d.ts +11 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeDestinationObservation.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeIntent.d.ts +11 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeIntent.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeSourceObservation.d.ts +11 -0
- package/dist/neutral/implementation/head/createForkedHead/getBridgeSourceObservation.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/getFirstBlockForNewChain.d.ts +13 -0
- package/dist/neutral/implementation/head/createForkedHead/getFirstBlockForNewChain.d.ts.map +1 -0
- package/dist/neutral/implementation/head/createForkedHead/index.d.ts +2 -0
- package/dist/neutral/implementation/head/createForkedHead/index.d.ts.map +1 -0
- package/dist/neutral/implementation/head/getForkFromBlock.d.ts +12 -0
- package/dist/neutral/implementation/head/getForkFromBlock.d.ts.map +1 -0
- package/dist/neutral/implementation/head/head.d.ts +11 -0
- package/dist/neutral/implementation/head/head.d.ts.map +1 -0
- package/dist/neutral/implementation/head/index.d.ts +2 -0
- package/dist/neutral/implementation/head/index.d.ts.map +1 -0
- package/dist/neutral/implementation/head/submitNewChain.d.ts +10 -0
- package/dist/neutral/implementation/head/submitNewChain.d.ts.map +1 -0
- package/dist/neutral/implementation/index.d.ts +13 -0
- package/dist/neutral/implementation/index.d.ts.map +1 -0
- package/dist/neutral/implementation/mempoolRunner.d.ts +7 -0
- package/dist/neutral/implementation/mempoolRunner.d.ts.map +1 -0
- package/dist/neutral/implementation/mempoolViewer.d.ts +7 -0
- package/dist/neutral/implementation/mempoolViewer.d.ts.map +1 -0
- package/dist/neutral/implementation/pendingTransactions.d.ts +9 -0
- package/dist/neutral/implementation/pendingTransactions.d.ts.map +1 -0
- package/dist/neutral/implementation/producer.d.ts +5 -0
- package/dist/neutral/implementation/producer.d.ts.map +1 -0
- package/dist/neutral/implementation/reward.d.ts +14 -0
- package/dist/neutral/implementation/reward.d.ts.map +1 -0
- package/dist/neutral/implementation/time.d.ts +6 -0
- package/dist/neutral/implementation/time.d.ts.map +1 -0
- package/dist/neutral/implementation/validator.d.ts +5 -0
- package/dist/neutral/implementation/validator.d.ts.map +1 -0
- package/dist/neutral/index.d.ts +2 -2
- package/dist/neutral/index.d.ts.map +1 -1
- package/dist/neutral/index.mjs +1565 -1140
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/neutral/simple/block/index.d.ts +2 -0
- package/dist/neutral/simple/block/index.d.ts.map +1 -0
- package/dist/neutral/simple/block/runner/SimpleBlockRunner.d.ts +117 -0
- package/dist/neutral/simple/block/runner/SimpleBlockRunner.d.ts.map +1 -0
- package/dist/neutral/simple/block/runner/generateTransactionFeeTransfers.d.ts.map +1 -0
- package/dist/neutral/simple/block/runner/index.d.ts +2 -0
- package/dist/neutral/simple/block/runner/index.d.ts.map +1 -0
- package/dist/neutral/simple/block/runner/spec/SimpleBlockRewardViewer.d.ts +2 -0
- package/dist/neutral/simple/block/runner/spec/SimpleBlockRewardViewer.d.ts.map +1 -0
- package/dist/neutral/simple/index.d.ts +2 -0
- package/dist/neutral/simple/index.d.ts.map +1 -0
- package/package.json +30 -27
- package/src/AccountBalance/accountBalanceServiceFromArchivist.ts +35 -8
- package/src/AccountBalance/index.ts +0 -1
- package/src/BaseService.ts +0 -13
- package/src/BlockReward/EvmBlockRewardViewer.ts +50 -0
- package/src/BlockReward/index.ts +1 -3
- package/src/ChainBlockNumberIteration/ChainBlockNumberIterationService.ts +14 -2
- package/src/ChainService/Evm/Evm.ts +5 -4
- package/src/ChainService/Memory/Memory.ts +5 -0
- package/src/ChainValidator/XyoValidator.ts +25 -30
- package/src/NetworkStakeStepReward/BaseNetworkStakeStepRewardService.ts +18 -18
- package/src/PendingTransactions/BasePendingTransactions.ts +10 -5
- package/src/Schemas/BaseSchemasService.ts +1 -1
- package/src/StakeIntent/XyoStakeIntentService.ts +18 -8
- package/src/StepStake/BaseStepStakeService.ts +2 -2
- package/src/blockViewerFromChainIteratorAndArchivist.ts +8 -1
- package/src/implementation/accountBalance.ts +29 -0
- package/src/implementation/blockViewer.ts +29 -0
- package/src/implementation/chain/evm.ts +39 -0
- package/src/implementation/chain/index.ts +46 -0
- package/src/implementation/evm/index.ts +1 -0
- package/src/implementation/evm/initChainId.ts +20 -0
- package/src/implementation/evm/initEvmProvider.ts +24 -0
- package/src/implementation/evm/initInfuraProvider.ts +26 -0
- package/src/implementation/evm/initJsonRpcProvider.ts +20 -0
- package/src/implementation/head/createBootstrapHead.ts +31 -0
- package/src/implementation/head/createForkedHead/config/getBridgeDestinationDetails.ts +27 -0
- package/src/implementation/head/createForkedHead/config/getBridgeSourceDetails.ts +19 -0
- package/src/implementation/head/createForkedHead/config/getForkDetails.ts +10 -0
- package/src/implementation/head/createForkedHead/config/index.ts +3 -0
- package/src/implementation/head/createForkedHead/createForkedHead.ts +29 -0
- package/src/implementation/head/createForkedHead/getBridgeDestinationObservation.ts +42 -0
- package/src/implementation/head/createForkedHead/getBridgeIntent.ts +51 -0
- package/src/implementation/head/createForkedHead/getBridgeSourceObservation.ts +46 -0
- package/src/implementation/head/createForkedHead/getFirstBlockForNewChain.ts +41 -0
- package/src/implementation/head/createForkedHead/index.ts +1 -0
- package/src/implementation/head/getForkFromBlock.ts +43 -0
- package/src/implementation/head/head.ts +49 -0
- package/src/implementation/head/index.ts +1 -0
- package/src/implementation/head/submitNewChain.ts +27 -0
- package/src/implementation/index.ts +12 -0
- package/src/implementation/mempoolRunner.ts +29 -0
- package/src/implementation/mempoolViewer.ts +29 -0
- package/src/implementation/pendingTransactions.ts +36 -0
- package/src/implementation/producer.ts +16 -0
- package/src/implementation/reward.ts +53 -0
- package/src/implementation/time.ts +26 -0
- package/src/implementation/validator.ts +15 -0
- package/src/index.ts +2 -2
- package/src/simple/block/index.ts +1 -0
- package/src/{BlockProducer/BaseBlockProducerService.ts → simple/block/runner/SimpleBlockRunner.ts} +111 -74
- package/src/simple/block/runner/index.ts +1 -0
- package/src/simple/block/runner/spec/SimpleBlockRewardViewer.ts +413 -0
- package/src/simple/index.ts +1 -0
- package/dist/neutral/AccountBalance/BaseAccountBalanceService.d.ts +0 -35
- package/dist/neutral/AccountBalance/BaseAccountBalanceService.d.ts.map +0 -1
- package/dist/neutral/AccountTransfers/BaseAccountTransfersService.d.ts +0 -13
- package/dist/neutral/AccountTransfers/BaseAccountTransfersService.d.ts.map +0 -1
- package/dist/neutral/AccountTransfers/index.d.ts +0 -2
- package/dist/neutral/AccountTransfers/index.d.ts.map +0 -1
- package/dist/neutral/BlockProducer/BaseBlockProducerService.d.ts +0 -85
- package/dist/neutral/BlockProducer/BaseBlockProducerService.d.ts.map +0 -1
- package/dist/neutral/BlockProducer/generateTransactionFeeTransfers.d.ts.map +0 -1
- package/dist/neutral/BlockProducer/index.d.ts +0 -2
- package/dist/neutral/BlockProducer/index.d.ts.map +0 -1
- package/dist/neutral/BlockReward/BaseBlockRewardService.d.ts +0 -10
- package/dist/neutral/BlockReward/BaseBlockRewardService.d.ts.map +0 -1
- package/dist/neutral/BlockReward/EvmBlockRewardService.d.ts +0 -18
- package/dist/neutral/BlockReward/EvmBlockRewardService.d.ts.map +0 -1
- package/dist/neutral/BlockReward/MemoryBlockRewardService.d.ts +0 -23
- package/dist/neutral/BlockReward/MemoryBlockRewardService.d.ts.map +0 -1
- package/src/AccountBalance/BaseAccountBalanceService.ts +0 -98
- package/src/AccountTransfers/BaseAccountTransfersService.ts +0 -36
- package/src/AccountTransfers/index.ts +0 -1
- package/src/BlockProducer/index.ts +0 -1
- package/src/BlockReward/BaseBlockRewardService.ts +0 -17
- package/src/BlockReward/EvmBlockRewardService.ts +0 -49
- package/src/BlockReward/MemoryBlockRewardService.ts +0 -72
- /package/dist/neutral/{BlockProducer → simple/block/runner}/generateTransactionFeeTransfers.d.ts +0 -0
- /package/src/{BlockProducer → simple/block/runner}/generateTransactionFeeTransfers.ts +0 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { buildNextBlock, createTransferPayload } from '@xyo-network/chain-protocol'
|
|
2
|
+
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
3
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
+
import type {
|
|
5
|
+
BridgeIntent, BridgeIntentFields,
|
|
6
|
+
SignedHydratedBlockWithHashMeta,
|
|
7
|
+
} from '@xyo-network/xl1-protocol'
|
|
8
|
+
import { BridgeIntentSchema, XYO_BRIDGE_ADDRESS } from '@xyo-network/xl1-protocol'
|
|
9
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
getBridgeDestinationDetails, getBridgeSourceDetails, getForkBlockReward,
|
|
13
|
+
} from './config/index.ts'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get the bridge intent for a given block.
|
|
17
|
+
* @param previousBlock The previous block to base the intent on.
|
|
18
|
+
* @param account The wallet account to use for the intent.
|
|
19
|
+
* @param chainService The chain service to use for the intent.
|
|
20
|
+
*/
|
|
21
|
+
export const getBridgeIntent = async (
|
|
22
|
+
previousBlock: SignedHydratedBlockWithHashMeta,
|
|
23
|
+
account: WalletInstance,
|
|
24
|
+
chainService: ChainService,
|
|
25
|
+
): Promise<SignedHydratedBlockWithHashMeta> => {
|
|
26
|
+
// Create transfer to bridge address
|
|
27
|
+
const transferPayload = createTransferPayload(
|
|
28
|
+
account.address,
|
|
29
|
+
{ [XYO_BRIDGE_ADDRESS]: getForkBlockReward() },
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
// Use timestamp as nonce
|
|
33
|
+
const nonce = `${Date.now()}`
|
|
34
|
+
|
|
35
|
+
// Create Bridge Intent
|
|
36
|
+
const bridgeIntentFields: BridgeIntentFields = {
|
|
37
|
+
...await getBridgeSourceDetails(account, chainService),
|
|
38
|
+
...getBridgeDestinationDetails(),
|
|
39
|
+
nonce,
|
|
40
|
+
}
|
|
41
|
+
const bridgeIntent = new PayloadBuilder<BridgeIntent>({ schema: BridgeIntentSchema })
|
|
42
|
+
.fields(bridgeIntentFields)
|
|
43
|
+
.build()
|
|
44
|
+
|
|
45
|
+
return await buildNextBlock(
|
|
46
|
+
previousBlock[0],
|
|
47
|
+
[],
|
|
48
|
+
[bridgeIntent, transferPayload],
|
|
49
|
+
[account],
|
|
50
|
+
)
|
|
51
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { buildNextBlock } from '@xyo-network/chain-protocol'
|
|
2
|
+
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
3
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
+
import type {
|
|
5
|
+
BridgeSourceObservation, BridgeSourceObservationFields,
|
|
6
|
+
SignedHydratedBlockWithHashMeta,
|
|
7
|
+
} from '@xyo-network/xl1-protocol'
|
|
8
|
+
import { BridgeSourceObservationSchema } from '@xyo-network/xl1-protocol'
|
|
9
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
10
|
+
|
|
11
|
+
import { getBridgeDestinationDetails, getBridgeSourceDetails } from './config/index.ts'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get the bridge source observation for a given block.
|
|
15
|
+
* @param previousBlock The previous block to base the observation on.
|
|
16
|
+
* @param account The wallet account to use for the observation.
|
|
17
|
+
* @param chainService The chain service to use for the observation.
|
|
18
|
+
*/
|
|
19
|
+
export const getBridgeSourceObservation = async (
|
|
20
|
+
previousBlock: SignedHydratedBlockWithHashMeta,
|
|
21
|
+
account: WalletInstance,
|
|
22
|
+
chainService: ChainService,
|
|
23
|
+
): Promise<SignedHydratedBlockWithHashMeta> => {
|
|
24
|
+
// TODO: Find actual transfer TX from previous block/source intent?
|
|
25
|
+
// This hash should contain both the source intent and the transfer to the bridge address
|
|
26
|
+
// As the block is itself both a block and a transaction, we can use its hash for now
|
|
27
|
+
const srcTxHash = previousBlock[0]._hash
|
|
28
|
+
|
|
29
|
+
const bridgeSourceObservationFields: BridgeSourceObservationFields = {
|
|
30
|
+
...await getBridgeSourceDetails(account, chainService),
|
|
31
|
+
...getBridgeDestinationDetails(),
|
|
32
|
+
// Observation
|
|
33
|
+
srcConfirmation: srcTxHash,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const bridgeSourceObservation = new PayloadBuilder<BridgeSourceObservation>({ schema: BridgeSourceObservationSchema })
|
|
37
|
+
.fields(bridgeSourceObservationFields)
|
|
38
|
+
.build()
|
|
39
|
+
|
|
40
|
+
return await buildNextBlock(
|
|
41
|
+
previousBlock[0],
|
|
42
|
+
[],
|
|
43
|
+
[bridgeSourceObservation],
|
|
44
|
+
[account],
|
|
45
|
+
)
|
|
46
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { buildBlock, type BuildNextBlockOptions } from '@xyo-network/chain-protocol'
|
|
2
|
+
import type { WithHashMeta } from '@xyo-network/payload-model'
|
|
3
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
4
|
+
import type {
|
|
5
|
+
BlockBoundWitness,
|
|
6
|
+
SignedHydratedBlockWithHashMeta,
|
|
7
|
+
} from '@xyo-network/xl1-protocol'
|
|
8
|
+
import { AttoXL1, XYO_STEP_REWARD_ADDRESS } from '@xyo-network/xl1-protocol'
|
|
9
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get the first block for the new forked chain.
|
|
13
|
+
* @param forkBlock The block to fork from
|
|
14
|
+
* @param account The wallet account to sign the block
|
|
15
|
+
* @param chainService The chain service instance
|
|
16
|
+
* @returns The first block for the new forked chain
|
|
17
|
+
*/
|
|
18
|
+
export const getFirstBlockForNewChain = async (
|
|
19
|
+
forkBlock: WithHashMeta<BlockBoundWitness>,
|
|
20
|
+
account: WalletInstance,
|
|
21
|
+
chainService: ChainService,
|
|
22
|
+
): Promise<SignedHydratedBlockWithHashMeta> => {
|
|
23
|
+
const {
|
|
24
|
+
_hash: previousBlockHash, block: previousBlockNumber, step_hashes: previousStepHashes = [], protocol,
|
|
25
|
+
} = forkBlock
|
|
26
|
+
const chainId = await chainService.chainId()
|
|
27
|
+
const options: BuildNextBlockOptions = {
|
|
28
|
+
blockPayloads: [],
|
|
29
|
+
chainId,
|
|
30
|
+
previousBlockHash,
|
|
31
|
+
previousBlockNumber,
|
|
32
|
+
previousStepHashes,
|
|
33
|
+
signers: [account],
|
|
34
|
+
txs: [],
|
|
35
|
+
protocol,
|
|
36
|
+
chainStepRewardAddress: XYO_STEP_REWARD_ADDRESS,
|
|
37
|
+
stepRewardPoolBalance: AttoXL1(0n),
|
|
38
|
+
}
|
|
39
|
+
// Can not use buildNextBlock because we need to control the chain id change here
|
|
40
|
+
return await buildBlock(options)
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './createForkedHead.ts'
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Address, Hash } from '@xylabs/sdk-js'
|
|
2
|
+
import {
|
|
3
|
+
asHash, hexFromBigInt, isDefined,
|
|
4
|
+
} from '@xylabs/sdk-js'
|
|
5
|
+
import type { ArchivistInstance } from '@xyo-network/archivist-model'
|
|
6
|
+
import type { SignedBlockBoundWitnessWithHashMeta } from '@xyo-network/xl1-protocol'
|
|
7
|
+
import { isSignedBlockBoundWitnessWithHashMeta } from '@xyo-network/xl1-protocol'
|
|
8
|
+
import type { ChainService } from '@xyo-network/xl1-protocol-sdk'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Determine if the chain should fork based on the current head and chain configuration.
|
|
12
|
+
* @param head The current head block
|
|
13
|
+
* @param chainService The chain service instance
|
|
14
|
+
* @param chainArchivist The chain archivist instance
|
|
15
|
+
* @returns The block to fork from if the chain should fork, undefined otherwise
|
|
16
|
+
*/
|
|
17
|
+
export const getForkFromBlock = async (
|
|
18
|
+
head: SignedBlockBoundWitnessWithHashMeta,
|
|
19
|
+
chainService: ChainService,
|
|
20
|
+
chainArchivist: ArchivistInstance,
|
|
21
|
+
): Promise<SignedBlockBoundWitnessWithHashMeta | undefined> => {
|
|
22
|
+
// If the head's chain doesn't match our chainId, we may need to fork
|
|
23
|
+
if (head.chain !== await chainService.chainId()) {
|
|
24
|
+
// Get the forked at hash from the chain service
|
|
25
|
+
const forkedAtBigInt = await chainService.forkedAtHash()
|
|
26
|
+
const forkedAtHex = hexFromBigInt(forkedAtBigInt) // Validate it's a proper hex string
|
|
27
|
+
const forkedAtHash: Hash | undefined = asHash(forkedAtHex)
|
|
28
|
+
if (isDefined(forkedAtHash)) {
|
|
29
|
+
// If we have a forkedAtHash, we need to check if the correct block exists
|
|
30
|
+
const [forkedAtBlock] = await chainArchivist.get([forkedAtHash])
|
|
31
|
+
const forkedChainId: Address = await chainService.forkedChainId()
|
|
32
|
+
const forkedAtBlockNumber: number = Number(await chainService.forkedAtBlockNumber())
|
|
33
|
+
// If we found the block hash we should fork at, check if it's a valid BlockBoundWitness
|
|
34
|
+
if (isSignedBlockBoundWitnessWithHashMeta(forkedAtBlock)
|
|
35
|
+
// And it is on the correct forked from chain
|
|
36
|
+
&& forkedAtBlock.chain === forkedChainId
|
|
37
|
+
// And it is the correct forked at block number
|
|
38
|
+
&& forkedAtBlock.block === forkedAtBlockNumber) {
|
|
39
|
+
return forkedAtBlock
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import { assertEx, isDefined } from '@xylabs/sdk-js'
|
|
3
|
+
import type { ArchivistInstance } from '@xyo-network/archivist-model'
|
|
4
|
+
import type { WalletInstance } from '@xyo-network/wallet-model'
|
|
5
|
+
import type { Initializable, SignedBlockBoundWitnessWithHashMeta } from '@xyo-network/xl1-protocol'
|
|
6
|
+
import { type ChainService, findMostRecentBlock } from '@xyo-network/xl1-protocol-sdk'
|
|
7
|
+
|
|
8
|
+
import { createBootstrapHead } from './createBootstrapHead.ts'
|
|
9
|
+
import { createForkedHead } from './createForkedHead/index.ts'
|
|
10
|
+
import { getForkFromBlock } from './getForkFromBlock.ts'
|
|
11
|
+
import { submitNewChain } from './submitNewChain.ts'
|
|
12
|
+
|
|
13
|
+
let headSingleton: Promisable<SignedBlockBoundWitnessWithHashMeta> | undefined
|
|
14
|
+
|
|
15
|
+
export const initHead: Initializable<{
|
|
16
|
+
account: WalletInstance
|
|
17
|
+
chainArchivist: ArchivistInstance
|
|
18
|
+
chainService: ChainService
|
|
19
|
+
chainSubmissionsArchivistWrite: ArchivistInstance
|
|
20
|
+
}, SignedBlockBoundWitnessWithHashMeta>
|
|
21
|
+
= async (params): Promise<SignedBlockBoundWitnessWithHashMeta> => {
|
|
22
|
+
const {
|
|
23
|
+
account, chainArchivist, chainSubmissionsArchivistWrite, chainService,
|
|
24
|
+
} = params
|
|
25
|
+
if (headSingleton) return headSingleton
|
|
26
|
+
let head = await findMostRecentBlock(chainArchivist)
|
|
27
|
+
|
|
28
|
+
// If there is a head
|
|
29
|
+
if (head) {
|
|
30
|
+
// If there is a head but check if it matches our chainId or we should fork from it
|
|
31
|
+
const forkFromBlock = await getForkFromBlock(head, chainService, chainArchivist)
|
|
32
|
+
// If we should fork form a block in the current chain
|
|
33
|
+
if (isDefined(forkFromBlock)) {
|
|
34
|
+
// Create a new chain from the fork from block
|
|
35
|
+
const chain = await createForkedHead(forkFromBlock, account, chainService)
|
|
36
|
+
await submitNewChain(chain, chainArchivist, chainSubmissionsArchivistWrite)
|
|
37
|
+
const newBlock = assertEx(chain.at(-1), () => new Error('Failed to get new head after forking'))
|
|
38
|
+
head = newBlock[0]
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
// If there is no head, create one
|
|
42
|
+
const chain = await createBootstrapHead(account, chainService)
|
|
43
|
+
await submitNewChain(chain, chainArchivist, chainSubmissionsArchivistWrite)
|
|
44
|
+
const newBlock = assertEx(chain.at(-1), () => new Error('Failed to get new head after genesis'))
|
|
45
|
+
head = newBlock[0]
|
|
46
|
+
}
|
|
47
|
+
headSingleton = head
|
|
48
|
+
return headSingleton
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './head.ts'
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { delay } from '@xylabs/sdk-js'
|
|
2
|
+
import type { ArchivistInstance, WriteArchivist } from '@xyo-network/archivist-model'
|
|
3
|
+
import type { SignedHydratedBlockWithHashMeta } from '@xyo-network/xl1-protocol'
|
|
4
|
+
import { flattenHydratedBlock } from '@xyo-network/xl1-protocol-sdk'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Submit a new chain to the archivist.
|
|
8
|
+
* @param chain The new chain to submit
|
|
9
|
+
* @param chainArchivist The chain archivist instance
|
|
10
|
+
* @param chainSubmissionsArchivistWrite The chain submissions archivist instance
|
|
11
|
+
*/
|
|
12
|
+
export const submitNewChain = async (
|
|
13
|
+
chain: SignedHydratedBlockWithHashMeta[],
|
|
14
|
+
chainArchivist: ArchivistInstance,
|
|
15
|
+
chainSubmissionsArchivistWrite: WriteArchivist,
|
|
16
|
+
): Promise<void> => {
|
|
17
|
+
for (const block of chain) {
|
|
18
|
+
const [bw] = block
|
|
19
|
+
await chainSubmissionsArchivistWrite.insert(flattenHydratedBlock(block))
|
|
20
|
+
// Wait for block to show up in finalized archivist
|
|
21
|
+
while (true) {
|
|
22
|
+
const result = await chainArchivist.get([bw._hash])
|
|
23
|
+
if (result.length > 0) break
|
|
24
|
+
await delay(1000) // Wait 1 second before retrying
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './accountBalance.ts'
|
|
2
|
+
export * from './blockViewer.ts'
|
|
3
|
+
export * from './chain/index.ts'
|
|
4
|
+
export * from './evm/index.ts'
|
|
5
|
+
export * from './head/index.ts'
|
|
6
|
+
export * from './mempoolRunner.ts'
|
|
7
|
+
export * from './mempoolViewer.ts'
|
|
8
|
+
export * from './pendingTransactions.ts'
|
|
9
|
+
export * from './producer.ts'
|
|
10
|
+
export * from './reward.ts'
|
|
11
|
+
export * from './time.ts'
|
|
12
|
+
export * from './validator.ts'
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isDefined, type Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Initializable, InitializableParams } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import type { Config, MempoolRunner } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import {
|
|
5
|
+
HttpRpcTransport,
|
|
6
|
+
JsonRpcMempoolRunner,
|
|
7
|
+
MempoolRunnerRpcSchemas,
|
|
8
|
+
} from '@xyo-network/xl1-rpc'
|
|
9
|
+
|
|
10
|
+
let mempoolRunnerSingleton: Promisable<MempoolRunner> | undefined
|
|
11
|
+
|
|
12
|
+
export type InitMempoolRunnerParams = InitializableParams<{
|
|
13
|
+
config: Config
|
|
14
|
+
}>
|
|
15
|
+
|
|
16
|
+
export const initMempoolRunner: Initializable<InitMempoolRunnerParams, MempoolRunner>
|
|
17
|
+
= (params): Promisable<MempoolRunner> => {
|
|
18
|
+
const { config, logger } = params
|
|
19
|
+
if (mempoolRunnerSingleton) return mempoolRunnerSingleton
|
|
20
|
+
const endpoint = config.services?.apiEndpoint
|
|
21
|
+
if (isDefined(endpoint)) {
|
|
22
|
+
const transport = new HttpRpcTransport(endpoint, { ...MempoolRunnerRpcSchemas })
|
|
23
|
+
const runner = new JsonRpcMempoolRunner(transport)
|
|
24
|
+
logger?.log('Using mempoolRunner RPC service at', endpoint)
|
|
25
|
+
return runner
|
|
26
|
+
} else {
|
|
27
|
+
throw new Error('No MempoolRunner endpoint configured')
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isDefined, type Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Initializable, InitializableParams } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import type { Config, MempoolViewer } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import {
|
|
5
|
+
HttpRpcTransport,
|
|
6
|
+
JsonRpcMempoolViewer,
|
|
7
|
+
MempoolViewerRpcSchemas,
|
|
8
|
+
} from '@xyo-network/xl1-rpc'
|
|
9
|
+
|
|
10
|
+
let mempoolViewerSingleton: Promisable<MempoolViewer> | undefined
|
|
11
|
+
|
|
12
|
+
export type InitMempoolViewerParams = InitializableParams<{
|
|
13
|
+
config: Config
|
|
14
|
+
}>
|
|
15
|
+
|
|
16
|
+
export const initMempoolViewer: Initializable<InitMempoolViewerParams, MempoolViewer>
|
|
17
|
+
= (params): Promisable<MempoolViewer> => {
|
|
18
|
+
const { config, logger } = params
|
|
19
|
+
if (mempoolViewerSingleton) return mempoolViewerSingleton
|
|
20
|
+
const endpoint = config.services?.apiEndpoint
|
|
21
|
+
if (isDefined(endpoint)) {
|
|
22
|
+
const transport = new HttpRpcTransport(endpoint, { ...MempoolViewerRpcSchemas })
|
|
23
|
+
const viewer = new JsonRpcMempoolViewer(transport)
|
|
24
|
+
logger?.log('Using MempoolViewer RPC service at', endpoint)
|
|
25
|
+
return viewer
|
|
26
|
+
} else {
|
|
27
|
+
throw new Error('No MempoolViewer endpoint configured')
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isDefined, type Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Initializable, InitializableParams } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import type { Config, MempoolViewer } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import { timeBudget } from '@xyo-network/xl1-protocol-sdk'
|
|
5
|
+
import {
|
|
6
|
+
HttpRpcTransport, JsonRpcMempoolViewer, MempoolViewerRpcSchemas,
|
|
7
|
+
} from '@xyo-network/xl1-rpc'
|
|
8
|
+
|
|
9
|
+
import { BasePendingTransactionsService, type BasePendingTransactionsServiceParams } from '../PendingTransactions/index.ts'
|
|
10
|
+
|
|
11
|
+
let serviceSingleton: Promisable<MempoolViewer> | undefined
|
|
12
|
+
|
|
13
|
+
export type InitPendingTransactionsServiceParams = InitializableParams<{
|
|
14
|
+
config: Config
|
|
15
|
+
params?: BasePendingTransactionsServiceParams
|
|
16
|
+
}>
|
|
17
|
+
|
|
18
|
+
export const initPendingTransactions: Initializable<InitPendingTransactionsServiceParams, MempoolViewer> = async (
|
|
19
|
+
{
|
|
20
|
+
config, params, logger,
|
|
21
|
+
}: InitPendingTransactionsServiceParams,
|
|
22
|
+
): Promise<MempoolViewer> => {
|
|
23
|
+
if (serviceSingleton) return serviceSingleton
|
|
24
|
+
return await timeBudget('initPendingTransactions', logger, async () => {
|
|
25
|
+
const endpoint = config.services?.apiEndpoint
|
|
26
|
+
if (isDefined(endpoint)) {
|
|
27
|
+
const transport = new HttpRpcTransport(endpoint, { ...MempoolViewerRpcSchemas })
|
|
28
|
+
const viewer = new JsonRpcMempoolViewer(transport)
|
|
29
|
+
logger?.log('Using MempoolViewer RPC service at', endpoint)
|
|
30
|
+
return viewer
|
|
31
|
+
} else {
|
|
32
|
+
serviceSingleton = await BasePendingTransactionsService.create(params)
|
|
33
|
+
}
|
|
34
|
+
return serviceSingleton
|
|
35
|
+
}, 2000, true)
|
|
36
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Initializable } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import { type BlockProducerService, timeBudget } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
|
|
5
|
+
import { SimpleBlockRunner, type SimpleBlockRunnerParams } from '../simple/index.ts'
|
|
6
|
+
|
|
7
|
+
let serviceSingleton: Promisable<BlockProducerService> | undefined
|
|
8
|
+
|
|
9
|
+
export const initBlockProducer: Initializable<SimpleBlockRunnerParams, BlockProducerService>
|
|
10
|
+
= async (params: SimpleBlockRunnerParams): Promise<BlockProducerService> => {
|
|
11
|
+
if (serviceSingleton) return serviceSingleton
|
|
12
|
+
return await timeBudget('initBlockProducer', params.logger, async () => {
|
|
13
|
+
serviceSingleton = await SimpleBlockRunner.create(params)
|
|
14
|
+
return serviceSingleton
|
|
15
|
+
}, 2000, true)
|
|
16
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Logger } from '@xylabs/sdk-js'
|
|
2
|
+
import { assertEx } from '@xylabs/sdk-js'
|
|
3
|
+
import type { Initializable } from '@xyo-network/xl1-protocol'
|
|
4
|
+
import type {
|
|
5
|
+
BlockRewardViewer, ChainContractViewer, Config, SimpleBlockRewardViewerParams,
|
|
6
|
+
} from '@xyo-network/xl1-protocol-sdk'
|
|
7
|
+
import { SimpleBlockRewardViewer, timeBudget } from '@xyo-network/xl1-protocol-sdk'
|
|
8
|
+
|
|
9
|
+
import { EvmBlockRewardViewer, type EvmBlockRewardViewerParams } from '../BlockReward/EvmBlockRewardViewer.ts'
|
|
10
|
+
import { canUseEvmProvider, initEvmProvider } from './evm/index.ts'
|
|
11
|
+
|
|
12
|
+
let rewardServiceSingleton: Promise<BlockRewardViewer> | undefined
|
|
13
|
+
|
|
14
|
+
export const initBlockRewardViewer: Initializable<
|
|
15
|
+
Partial<SimpleBlockRewardViewerParams> & {
|
|
16
|
+
chainContractViewer: ChainContractViewer
|
|
17
|
+
config: Pick<Config, 'evm'>
|
|
18
|
+
},
|
|
19
|
+
BlockRewardViewer
|
|
20
|
+
> = (params): Promise<BlockRewardViewer> => {
|
|
21
|
+
if (rewardServiceSingleton) return rewardServiceSingleton
|
|
22
|
+
return timeBudget('initBlockRewardViewer', params.logger, () => {
|
|
23
|
+
const { config } = params
|
|
24
|
+
rewardServiceSingleton = canUseEvmBlockRewardViewer({ config })
|
|
25
|
+
? initEvmBlockRewardViewer(params)
|
|
26
|
+
: initXyoBlockRewardViewer(params as SimpleBlockRewardViewerParams)
|
|
27
|
+
return rewardServiceSingleton
|
|
28
|
+
}, 2000, true)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const initXyoBlockRewardViewer = (params: SimpleBlockRewardViewerParams): Promise<BlockRewardViewer> => {
|
|
32
|
+
if (rewardServiceSingleton) return rewardServiceSingleton
|
|
33
|
+
return timeBudget('initXyoBlockRewardViewer', params.logger, () => {
|
|
34
|
+
rewardServiceSingleton = SimpleBlockRewardViewer.create(params)
|
|
35
|
+
return rewardServiceSingleton
|
|
36
|
+
}, 2000, true)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const canUseEvmBlockRewardViewer = (params: { config: Pick<Config, 'evm'> }) => canUseEvmProvider({ config: params.config })
|
|
40
|
+
|
|
41
|
+
export const initEvmBlockRewardViewer = async (params: ({
|
|
42
|
+
chainContractViewer: ChainContractViewer
|
|
43
|
+
config: Pick<Config, 'evm'>
|
|
44
|
+
logger?: Logger
|
|
45
|
+
})): Promise<BlockRewardViewer> => {
|
|
46
|
+
if (rewardServiceSingleton) return rewardServiceSingleton
|
|
47
|
+
return await timeBudget('initEvmBlockRewardViewer', params.logger, async () => {
|
|
48
|
+
const provider = assertEx(await initEvmProvider(params))
|
|
49
|
+
const evmBlockRewardViewerParams: EvmBlockRewardViewerParams = { ...params, provider }
|
|
50
|
+
rewardServiceSingleton = EvmBlockRewardViewer.create(evmBlockRewardViewerParams)
|
|
51
|
+
return rewardServiceSingleton
|
|
52
|
+
}, 2000, true)
|
|
53
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Initializable } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import { timeBudget, type TimeSyncViewer } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
import type { Config } from 'cosmiconfig'
|
|
5
|
+
|
|
6
|
+
import { type BaseTimeServiceParams, BaseTimeSyncService } from '../Time/index.ts'
|
|
7
|
+
import { canUseEvmProvider, initEvmProvider } from './evm/index.ts'
|
|
8
|
+
|
|
9
|
+
let timeSyncServiceSingleton: Promisable<TimeSyncViewer> | undefined
|
|
10
|
+
|
|
11
|
+
export const initTimeService: Initializable<BaseTimeServiceParams<Pick<Config, 'evm'>>, TimeSyncViewer> = async ({
|
|
12
|
+
blockViewer, config, logger, meterProvider, traceProvider,
|
|
13
|
+
}) => {
|
|
14
|
+
if (timeSyncServiceSingleton) return timeSyncServiceSingleton
|
|
15
|
+
|
|
16
|
+
return await timeBudget('initTimeService', logger, async () => {
|
|
17
|
+
// Configure provider for optional Ethereum time sync
|
|
18
|
+
const ethProvider = canUseEvmProvider({ config }) ? await initEvmProvider({ config }) : undefined
|
|
19
|
+
|
|
20
|
+
// Create service
|
|
21
|
+
timeSyncServiceSingleton = BaseTimeSyncService.create({
|
|
22
|
+
blockViewer, ethProvider, logger, meterProvider, traceProvider,
|
|
23
|
+
})
|
|
24
|
+
return await timeSyncServiceSingleton
|
|
25
|
+
}, 2000, true)
|
|
26
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Promisable } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Initializable } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import type { BlockProducerService } from '@xyo-network/xl1-protocol-sdk'
|
|
4
|
+
|
|
5
|
+
import type { SimpleBlockRunnerParams } from '../simple/index.ts'
|
|
6
|
+
import { initBlockProducer } from './producer.ts'
|
|
7
|
+
|
|
8
|
+
let serviceSingleton: Promisable<BlockProducerService> | undefined
|
|
9
|
+
|
|
10
|
+
export const initValidator: Initializable<SimpleBlockRunnerParams, BlockProducerService>
|
|
11
|
+
= (params: SimpleBlockRunnerParams): Promisable<BlockProducerService> => {
|
|
12
|
+
if (serviceSingleton) return serviceSingleton
|
|
13
|
+
serviceSingleton = initBlockProducer(params)
|
|
14
|
+
return serviceSingleton
|
|
15
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
export * from './AccountBalance/index.ts'
|
|
2
|
-
export * from './AccountTransfers/index.ts'
|
|
3
2
|
export * from './BaseService.ts'
|
|
4
|
-
export * from './BlockProducer/index.ts'
|
|
5
3
|
export * from './BlockReward/index.ts'
|
|
6
4
|
export * from './blockViewerFromChainIteratorAndArchivist.ts'
|
|
7
5
|
export * from './ChainBlockNumberIteration/index.ts'
|
|
8
6
|
export * from './ChainService/index.ts'
|
|
9
7
|
export * from './ChainValidator/index.ts'
|
|
10
8
|
export * from './Election/index.ts'
|
|
9
|
+
export * from './implementation/index.ts'
|
|
11
10
|
export * from './model/index.ts'
|
|
12
11
|
export * from './NetworkStakeStepReward/index.ts'
|
|
13
12
|
export * from './PendingTransactions/index.ts'
|
|
14
13
|
export * from './Schemas/index.ts'
|
|
14
|
+
export * from './simple/index.ts'
|
|
15
15
|
export * from './StakeIntent/index.ts'
|
|
16
16
|
export * from './StepStake/index.ts'
|
|
17
17
|
export * from './Time/index.ts'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './runner/index.ts'
|