@xyo-network/chain-services 1.15.1 → 1.15.3
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/README.md +5302 -2235
- package/dist/neutral/AccountBalance/BaseAccountBalanceService.d.ts +3 -5
- package/dist/neutral/AccountBalance/BaseAccountBalanceService.d.ts.map +1 -1
- package/dist/neutral/AccountBalance/accountBalanceServiceFromArchivist.d.ts +3 -2
- package/dist/neutral/AccountBalance/accountBalanceServiceFromArchivist.d.ts.map +1 -1
- package/dist/neutral/AccountTransfers/BaseAccountTransfersService.d.ts +3 -6
- package/dist/neutral/AccountTransfers/BaseAccountTransfersService.d.ts.map +1 -1
- package/dist/neutral/AccountTransfers/accountTransfersServiceFromArchivist.d.ts +3 -3
- package/dist/neutral/AccountTransfers/accountTransfersServiceFromArchivist.d.ts.map +1 -1
- package/dist/neutral/BlockProducer/spec/BaseBlockProducerService.spec.d.ts.map +1 -1
- package/dist/neutral/ChainBlockIteration/ChainBlockNumberIterationService.d.ts +20 -0
- package/dist/neutral/ChainBlockIteration/ChainBlockNumberIterationService.d.ts.map +1 -0
- package/dist/neutral/ChainBlockIteration/index.d.ts +3 -0
- package/dist/neutral/ChainBlockIteration/index.d.ts.map +1 -0
- package/dist/neutral/ChainBlockIteration/model/BlockNumberIteration.d.ts +7 -0
- package/dist/neutral/ChainBlockIteration/model/BlockNumberIteration.d.ts.map +1 -0
- package/dist/neutral/ChainBlockIteration/model/Params.d.ts +8 -0
- package/dist/neutral/ChainBlockIteration/model/Params.d.ts.map +1 -0
- package/dist/neutral/ChainBlockIteration/model/index.d.ts +3 -0
- package/dist/neutral/ChainBlockIteration/model/index.d.ts.map +1 -0
- package/dist/neutral/ChainBlockNumberIteration/ChainBlockNumberIterationService.d.ts +10 -7
- package/dist/neutral/ChainBlockNumberIteration/ChainBlockNumberIterationService.d.ts.map +1 -1
- package/dist/neutral/ChainBlockNumberIteration/model/Params.d.ts +3 -2
- package/dist/neutral/ChainBlockNumberIteration/model/Params.d.ts.map +1 -1
- package/dist/neutral/ChainValidator/XyoValidator.d.ts +2 -2
- package/dist/neutral/ChainValidator/XyoValidator.d.ts.map +1 -1
- package/dist/neutral/NetworkStakeStepReward/BaseNetworkStakeStepRewardService.d.ts +15 -14
- package/dist/neutral/NetworkStakeStepReward/BaseNetworkStakeStepRewardService.d.ts.map +1 -1
- package/dist/neutral/PendingTransactions/BasePendingTransactions.d.ts +2 -2
- package/dist/neutral/PendingTransactions/BasePendingTransactions.d.ts.map +1 -1
- package/dist/neutral/StakeIntent/XyoStakeIntentService.d.ts.map +1 -1
- package/dist/neutral/index.mjs +131 -157
- package/dist/neutral/index.mjs.map +1 -1
- package/package.json +41 -41
- package/src/AccountBalance/BaseAccountBalanceService.ts +6 -8
- package/src/AccountBalance/accountBalanceServiceFromArchivist.ts +18 -33
- package/src/AccountTransfers/BaseAccountTransfersService.ts +4 -13
- package/src/AccountTransfers/accountTransfersServiceFromArchivist.ts +18 -35
- package/src/BlockProducer/spec/BaseBlockProducerService.spec.ts +62 -8
- package/src/ChainBlockIteration/ChainBlockNumberIterationService.ts +108 -0
- package/src/ChainBlockIteration/index.ts +2 -0
- package/src/ChainBlockIteration/model/BlockNumberIteration.ts +7 -0
- package/src/ChainBlockIteration/model/Params.ts +9 -0
- package/src/ChainBlockIteration/model/index.ts +2 -0
- package/src/ChainBlockNumberIteration/ChainBlockNumberIterationService.ts +44 -30
- package/src/ChainBlockNumberIteration/model/Params.ts +3 -2
- package/src/ChainValidator/XyoValidator.ts +2 -2
- package/src/NetworkStakeStepReward/BaseNetworkStakeStepRewardService.ts +20 -14
- package/src/PendingTransactions/BasePendingTransactions.ts +2 -2
- package/src/StakeIntent/XyoStakeIntentService.ts +3 -1
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { assertEx } from '@xylabs/assert'
|
|
2
2
|
import type { Hex } from '@xylabs/hex'
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
isDefined, isNull, isUndefined,
|
|
5
|
+
} from '@xylabs/typeof'
|
|
5
6
|
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
7
|
+
import type { Payload, WithStorageMeta } from '@xyo-network/payload-model'
|
|
6
8
|
import type {
|
|
7
|
-
BlockBoundWitness,
|
|
8
|
-
ChainIteratorServiceEventData,
|
|
9
|
-
EventingChainBlockNumberIteratorService,
|
|
9
|
+
BlockBoundWitness, ChainIteratorServiceEventData, EventingChainBlockNumberIteratorService,
|
|
10
10
|
} from '@xyo-network/xl1-protocol'
|
|
11
11
|
import {
|
|
12
|
-
asBlockBoundWitness,
|
|
13
|
-
isBlockBoundWitness,
|
|
12
|
+
asBlockBoundWitness, asBlockBoundWitnessWithStorageMeta, isBlockBoundWitnessWithHashStorageMeta,
|
|
14
13
|
} from '@xyo-network/xl1-protocol'
|
|
14
|
+
import type { PayloadMapRead } from '@xyo-network/xl1-protocol-sdk'
|
|
15
15
|
import { LRUCache } from 'lru-cache'
|
|
16
16
|
|
|
17
17
|
import { BaseService } from '../BaseService.ts'
|
|
@@ -19,13 +19,14 @@ import type { XyoChainIteratorParams } from './model/index.ts'
|
|
|
19
19
|
|
|
20
20
|
export class ChainBlockNumberIterationService extends BaseService<XyoChainIteratorParams, ChainIteratorServiceEventData>
|
|
21
21
|
implements EventingChainBlockNumberIteratorService {
|
|
22
|
-
protected _blocksByBlockNumber = new LRUCache<number, BlockBoundWitness
|
|
22
|
+
protected _blocksByBlockNumber = new LRUCache<number, WithStorageMeta<BlockBoundWitness>>({ max: 10_000 })
|
|
23
|
+
protected _currentHead: WithStorageMeta<BlockBoundWitness> | undefined
|
|
23
24
|
|
|
24
|
-
get
|
|
25
|
+
get chainId(): Hex { return assertEx(this._currentHead?.chain ?? this.params?.head?.chain, () => 'Current head is not set') }
|
|
25
26
|
|
|
26
|
-
get
|
|
27
|
+
get chainMap(): PayloadMapRead<WithStorageMeta<Payload>> { return assertEx(this.params.chainMap) }
|
|
27
28
|
|
|
28
|
-
async get(block: number): Promise<BlockBoundWitness
|
|
29
|
+
async get(block: number): Promise<WithStorageMeta<BlockBoundWitness>> {
|
|
29
30
|
const head = await this.head()
|
|
30
31
|
// if(isUndefined(head)) return undefined
|
|
31
32
|
// Bail early if the block requested is newer than the current head
|
|
@@ -35,27 +36,33 @@ export class ChainBlockNumberIterationService extends BaseService<XyoChainIterat
|
|
|
35
36
|
// Start at the current head and traverse backwards until the requested block is found
|
|
36
37
|
const startingBlock = head
|
|
37
38
|
const currentBlockHash = await PayloadBuilder.hash(startingBlock)
|
|
38
|
-
let currentBlock = (await this.
|
|
39
|
+
let currentBlock = (await this.chainMap.get(currentBlockHash))
|
|
39
40
|
while (isDefined(currentBlock)) {
|
|
40
41
|
assertEx(asBlockBoundWitness(currentBlock), () => `Expected hash to be a block bound witness [${currentBlock?._hash}]`)
|
|
41
|
-
if (
|
|
42
|
+
if (isBlockBoundWitnessWithHashStorageMeta(currentBlock)) {
|
|
42
43
|
this._blocksByBlockNumber.set(currentBlock.block, currentBlock)
|
|
43
44
|
if (currentBlock.block === block) {
|
|
44
45
|
return currentBlock
|
|
45
46
|
}
|
|
46
47
|
const { previous } = currentBlock
|
|
47
48
|
if (isNull(previous)) break
|
|
48
|
-
currentBlock = (await this.
|
|
49
|
+
currentBlock = (await this.chainMap.get(previous))
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
throw new Error(`Block not found: ${block}`)
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
async head(): Promise<BlockBoundWitness
|
|
55
|
-
|
|
55
|
+
async head(): Promise<WithStorageMeta<BlockBoundWitness>> {
|
|
56
|
+
if (isDefined(this._currentHead)) return this._currentHead
|
|
57
|
+
if (isDefined(this.params.head)) {
|
|
58
|
+
const newHead = await this.getBoundWitnessAsBlockBoundWitnessWithStorageMeta(this.params.head)
|
|
59
|
+
this._currentHead = newHead
|
|
60
|
+
return newHead
|
|
61
|
+
}
|
|
62
|
+
throw new Error('Head is not set')
|
|
56
63
|
}
|
|
57
64
|
|
|
58
|
-
async next(block: number): Promise<BlockBoundWitness | undefined> {
|
|
65
|
+
async next(block: number): Promise<WithStorageMeta<BlockBoundWitness> | undefined> {
|
|
59
66
|
const currentBlock = block
|
|
60
67
|
const nextBlockNumber = currentBlock + 1
|
|
61
68
|
return await this.get(nextBlockNumber)
|
|
@@ -63,28 +70,35 @@ export class ChainBlockNumberIterationService extends BaseService<XyoChainIterat
|
|
|
63
70
|
|
|
64
71
|
// TODO: Decide on inclusive/exclusive (probably need inclusive to account for chain head)
|
|
65
72
|
// and then communicate via method name and documentation
|
|
66
|
-
async previous(block: number | undefined = undefined, count: number = 1): Promise<BlockBoundWitness[]> {
|
|
67
|
-
const results: BlockBoundWitness[] = []
|
|
68
|
-
let currentBlock: BlockBoundWitness | undefined = isDefined(block) ? (await this.get(block)) : await this.head()
|
|
69
|
-
while (currentBlock && results.length < count) {
|
|
70
|
-
if (
|
|
73
|
+
async previous(block: number | undefined = undefined, count: number = 1): Promise<WithStorageMeta<BlockBoundWitness>[]> {
|
|
74
|
+
const results: WithStorageMeta<BlockBoundWitness>[] = []
|
|
75
|
+
let currentBlock: WithStorageMeta<BlockBoundWitness> | undefined = isDefined(block) ? (await this.get(block)) : await this.head()
|
|
76
|
+
while (isDefined(currentBlock) && (results.length < count)) {
|
|
77
|
+
if (isBlockBoundWitnessWithHashStorageMeta(currentBlock)) {
|
|
71
78
|
results.push(currentBlock)
|
|
72
|
-
const { previous } = currentBlock
|
|
79
|
+
const { previous } = currentBlock as BlockBoundWitness
|
|
73
80
|
if (isNull(previous)) break
|
|
74
|
-
const nextBlock = await this.
|
|
75
|
-
currentBlock =
|
|
81
|
+
const nextBlock = await this.chainMap.get(previous)
|
|
82
|
+
currentBlock = asBlockBoundWitnessWithStorageMeta(nextBlock)
|
|
76
83
|
} else {
|
|
77
84
|
const hash = PayloadBuilder.hash(currentBlock)
|
|
78
|
-
assertEx(
|
|
85
|
+
assertEx(asBlockBoundWitnessWithStorageMeta(currentBlock), () => `Expected hash to be a block bound witness [${hash}]`)
|
|
79
86
|
}
|
|
80
87
|
}
|
|
81
88
|
return results
|
|
82
89
|
}
|
|
83
90
|
|
|
84
91
|
async updateHead(head: BlockBoundWitness): Promise<void> {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
this.
|
|
88
|
-
|
|
92
|
+
const newHead = await this.getBoundWitnessAsBlockBoundWitnessWithStorageMeta(head)
|
|
93
|
+
this._currentHead = newHead
|
|
94
|
+
void this.emit('headUpdated', { blocks: [newHead] })
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private async getBoundWitnessAsBlockBoundWitnessWithStorageMeta(head: BlockBoundWitness): Promise<WithStorageMeta<BlockBoundWitness>> {
|
|
98
|
+
const hash = await PayloadBuilder.hash(head)
|
|
99
|
+
const stored = (await this.chainMap.get(hash))
|
|
100
|
+
const newHead = asBlockBoundWitnessWithStorageMeta(stored)
|
|
101
|
+
if (isUndefined(newHead)) throw new Error(`Head block not found in archivist [${hash}]`)
|
|
102
|
+
return newHead
|
|
89
103
|
}
|
|
90
104
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Payload, WithStorageMeta } from '@xyo-network/payload-model'
|
|
2
2
|
import type { BlockBoundWitness } from '@xyo-network/xl1-protocol'
|
|
3
|
+
import type { PayloadMapRead } from '@xyo-network/xl1-protocol-sdk'
|
|
3
4
|
|
|
4
5
|
import type { BaseServiceParams } from '../../model/index.ts'
|
|
5
6
|
|
|
6
7
|
export interface XyoChainIteratorParams extends BaseServiceParams {
|
|
7
|
-
|
|
8
|
+
chainMap: PayloadMapRead<WithStorageMeta<Payload>>
|
|
8
9
|
head: BlockBoundWitness
|
|
9
10
|
}
|
|
@@ -6,7 +6,7 @@ import { ArchivistInstance, ReadArchivist } from '@xyo-network/archivist-model'
|
|
|
6
6
|
import {
|
|
7
7
|
BlockBoundWitness,
|
|
8
8
|
BlockRewardService,
|
|
9
|
-
|
|
9
|
+
ChainId,
|
|
10
10
|
ElectionService,
|
|
11
11
|
HydratedBlockStateValidationFunctionV2,
|
|
12
12
|
SignedHydratedTransactionWithStorageMeta,
|
|
@@ -20,7 +20,7 @@ import { Validator } from './model/index.ts'
|
|
|
20
20
|
export interface XyoValidatorParams extends BaseServiceParams {
|
|
21
21
|
account: AccountInstance
|
|
22
22
|
chainArchivist: ReadArchivist
|
|
23
|
-
chainId:
|
|
23
|
+
chainId: ChainId
|
|
24
24
|
electionService: ElectionService
|
|
25
25
|
pendingBundledTransactionsArchivist: ArchivistInstance
|
|
26
26
|
rewardService: BlockRewardService
|
|
@@ -2,7 +2,9 @@ import { creatable } from '@xylabs/creatable'
|
|
|
2
2
|
import { Address } from '@xylabs/hex'
|
|
3
3
|
import { Promisable } from '@xylabs/promise'
|
|
4
4
|
import { ReadArchivist } from '@xyo-network/archivist-model'
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
EventingChainBlockNumberIteratorService, NetworkStakeStepRewardService, StepContext,
|
|
7
|
+
} from '@xyo-network/xl1-protocol'
|
|
6
8
|
import { Provider } from 'ethers'
|
|
7
9
|
|
|
8
10
|
import { BaseService } from '../BaseService.ts'
|
|
@@ -16,55 +18,59 @@ export interface BaseNetworkStakeStepRewardServiceParams extends BaseServicePara
|
|
|
16
18
|
|
|
17
19
|
@creatable()
|
|
18
20
|
export class BaseNetworkStakeStepRewardService extends BaseService<BaseNetworkStakeStepRewardServiceParams> implements NetworkStakeStepRewardService {
|
|
19
|
-
|
|
21
|
+
networkStakeStepRewardAddressHistory(_address: Address): Promisable<Record<Address, bigint>> {
|
|
20
22
|
throw new Error('Method not implemented.')
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
networkStakeStepRewardAddressReward(_context: StepContext, _address: Address): Promisable<Record<Address, bigint>> {
|
|
24
26
|
throw new Error('Method not implemented.')
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
networkStakeStepRewardAddressShare(_context: StepContext, _address: Address): Promisable<[bigint, bigint]> {
|
|
28
30
|
throw new Error('Method not implemented.')
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
networkStakeStepRewardClaimedByAddress(_address: Address): Promisable<bigint> {
|
|
32
34
|
throw new Error('Method not implemented.')
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
|
|
37
|
+
networkStakeStepRewardForStep(_context: StepContext): Promisable<bigint> {
|
|
36
38
|
throw new Error('Method not implemented.')
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
networkStakeStepRewardForStepForPosition(_context: StepContext, _position: number): Promisable<[bigint, bigint]> {
|
|
40
42
|
throw new Error('Method not implemented.')
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
networkStakeStepRewardPoolRewards(_context: StepContext): Promisable<Record<Address, bigint>> {
|
|
44
46
|
throw new Error('Method not implemented.')
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
|
|
49
|
+
networkStakeStepRewardPoolShares(_context: StepContext): Promisable<Record<Address, bigint>> {
|
|
48
50
|
throw new Error('Method not implemented.')
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
|
|
53
|
+
networkStakeStepRewardPositionWeight(_context: StepContext, _position: number): Promisable<bigint> {
|
|
52
54
|
throw new Error('Method not implemented.')
|
|
53
55
|
}
|
|
54
56
|
|
|
55
|
-
|
|
57
|
+
networkStakeStepRewardPotentialPositionLoss(_context: StepContext, _position: number): Promisable<bigint> {
|
|
56
58
|
throw new Error('Method not implemented.')
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
|
|
61
|
+
networkStakeStepRewardRandomizer(_context: StepContext): Promisable<bigint> {
|
|
60
62
|
throw new Error('Method not implemented.')
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
networkStakeStepRewardStakerCount(_context: StepContext): Promisable<number> {
|
|
64
66
|
throw new Error('Method not implemented.')
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
networkStakeStepRewardUnclaimedByAddress(_address: Address): Promisable<bigint> {
|
|
70
|
+
throw new Error('Method not implemented.')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
networkStakeStepRewardWeightForAddress(_context: StepContext, _address: Address): Promisable<bigint> {
|
|
68
74
|
throw new Error('Method not implemented.')
|
|
69
75
|
}
|
|
70
76
|
}
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
Payload, PayloadBundle, Sequence, WithStorageMeta,
|
|
14
14
|
} from '@xyo-network/payload-model'
|
|
15
15
|
import {
|
|
16
|
-
asBlockBoundWitnessWithHashStorageMeta,
|
|
16
|
+
asBlockBoundWitnessWithHashStorageMeta, ChainId, isTransactionBoundWitnessWithStorageMeta, PendingTransactionsService,
|
|
17
17
|
SignedHydratedTransactionWithStorageMeta,
|
|
18
18
|
} from '@xyo-network/xl1-protocol'
|
|
19
19
|
import { TransactionJsonSchemaValidator, validateTransaction } from '@xyo-network/xl1-validation'
|
|
@@ -26,7 +26,7 @@ import { hydratedTransactionToPayloadBundle } from './hydratedTransactionToPaylo
|
|
|
26
26
|
|
|
27
27
|
export interface BasePendingTransactionsServiceParams extends BaseServiceParams {
|
|
28
28
|
chainArchivist?: ArchivistInstance
|
|
29
|
-
chainId?:
|
|
29
|
+
chainId?: ChainId
|
|
30
30
|
pendingBundledTransactionsArchivist?: ArchivistInstance
|
|
31
31
|
rejectedTransactionsArchivist?: ArchivistInstance
|
|
32
32
|
}
|
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
isChainIndexingServiceState,
|
|
26
26
|
StakeIntentService,
|
|
27
27
|
} from '@xyo-network/xl1-protocol'
|
|
28
|
+
import { readPayloadMapFromStore } from '@xyo-network/xl1-protocol-sdk'
|
|
28
29
|
import { Mutex } from 'async-mutex'
|
|
29
30
|
import { LRUCache } from 'lru-cache'
|
|
30
31
|
|
|
@@ -206,7 +207,8 @@ export class XyoStakeIntentService extends BaseService<XyoStakeIntentServicePara
|
|
|
206
207
|
const currentHead = await this.chainIterator.head()
|
|
207
208
|
if (isUndefined(currentHead)) return
|
|
208
209
|
const currentHeadHash = await PayloadBuilder.hash(currentHead)
|
|
209
|
-
const
|
|
210
|
+
const chainMap = readPayloadMapFromStore<WithStorageMeta<Payload>>(this.chainArchivist)
|
|
211
|
+
const result = await analyzeChain({ chainMap }, [new ChainStakeIntentAnalyzer('producer')], currentHeadHash, this._lastIndexedBlockHash)
|
|
210
212
|
const signedDeclarations = filterAs(result.find(isChainSummaryStakeIntent)?.intents ?? [], asChainStakeIntent)
|
|
211
213
|
if (currentHead.block === undefined) return
|
|
212
214
|
const currentHeadBlockNum = currentHead.block
|