-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(chainHead_v1): operationWaitingForContinue and chainHead_v1_continue #838
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,17 @@ import { defaultLogger } from '../../logger.js' | |
|
||
const logger = defaultLogger.child({ name: 'rpc-chainHead_v1' }) | ||
|
||
const callbacks = new Map<string, (data: any) => void>() | ||
type DescendantValuesParams = { | ||
prefix: string | ||
startKey: string | ||
} | ||
const following = new Map< | ||
string, | ||
{ | ||
callback: (data: any) => void | ||
pendingDescendantValues: Map<string, { hash: HexString; params: DescendantValuesParams[] }> | ||
} | ||
>() | ||
|
||
async function afterResponse(fn: () => void) { | ||
await new Promise((resolve) => setTimeout(resolve, 0)) | ||
|
@@ -61,11 +71,11 @@ export const chainHead_v1_follow: Handler<[boolean], string> = async ( | |
|
||
const cleanup = () => { | ||
context.chain.headState.unsubscribeHead(id) | ||
callbacks.delete(id) | ||
following.delete(id) | ||
} | ||
|
||
const callback = subscribe('chainHead_v1_followEvent', id, cleanup) | ||
callbacks.set(id, callback) | ||
following.set(id, { callback, pendingDescendantValues: new Map() }) | ||
|
||
afterResponse(async () => { | ||
callback({ | ||
|
@@ -103,7 +113,7 @@ export const chainHead_v1_header: Handler<[string, HexString], HexString | null> | |
context, | ||
[followSubscription, hash], | ||
) => { | ||
if (!callbacks.has(followSubscription)) return null | ||
if (!following.has(followSubscription)) return null | ||
const block = await context.chain.getBlock(hash) | ||
|
||
return block ? (await block.header).toHex() : null | ||
|
@@ -134,21 +144,21 @@ export const chainHead_v1_call: Handler<[string, HexString, string, HexString], | |
const block = await context.chain.getBlock(hash) | ||
|
||
if (!block) { | ||
callbacks.get(followSubscription)?.({ | ||
following.get(followSubscription)?.callback({ | ||
event: 'operationError', | ||
operationId, | ||
error: `Block ${hash} not found`, | ||
}) | ||
} else { | ||
try { | ||
const resp = await block.call(method, [callParameters]) | ||
callbacks.get(followSubscription)?.({ | ||
following.get(followSubscription)?.callback({ | ||
event: 'operationCallDone', | ||
operationId, | ||
output: resp.result, | ||
}) | ||
} catch (ex: any) { | ||
callbacks.get(followSubscription)?.({ | ||
following.get(followSubscription)?.callback({ | ||
event: 'operationError', | ||
operationId, | ||
error: ex.message, | ||
|
@@ -166,6 +176,48 @@ export interface StorageItemRequest { | |
type: 'value' | 'hash' | 'closestDescendantMerkleValue' | 'descendantsValues' | 'descendantsHashes' | ||
} | ||
|
||
const PAGE_SIZE = 1000 | ||
async function getDescendantValues( | ||
block: Block, | ||
params: DescendantValuesParams, | ||
): Promise<{ | ||
items: Array<{ | ||
key: string | ||
value?: HexString | ||
}> | ||
next: DescendantValuesParams | null | ||
}> { | ||
const pageSize = 100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you have 2 page_size and one is 1000 and one is 100? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my bad - leftover from previous code. Thanks for pointing it out! |
||
const keys = await block.getKeysPaged({ | ||
...params, | ||
pageSize: PAGE_SIZE, | ||
}) | ||
|
||
const items = await Promise.all( | ||
keys.map((key) => | ||
block.get(key).then((value) => ({ | ||
key, | ||
value, | ||
})), | ||
), | ||
) | ||
|
||
if (keys.length < pageSize) { | ||
return { | ||
items, | ||
next: null, | ||
} | ||
} | ||
|
||
return { | ||
items, | ||
next: { | ||
...params, | ||
startKey: keys[pageSize - 1], | ||
}, | ||
} | ||
} | ||
|
||
/** | ||
* Query the storage for a given block | ||
* | ||
|
@@ -183,7 +235,7 @@ export const chainHead_v1_storage: Handler< | |
afterResponse(async () => { | ||
const block = await context.chain.getBlock(hash) | ||
if (!block) { | ||
callbacks.get(followSubscription)?.({ | ||
following.get(followSubscription)?.callback({ | ||
event: 'operationError', | ||
operationId, | ||
error: 'Block not found', | ||
|
@@ -196,55 +248,50 @@ export const chainHead_v1_storage: Handler< | |
case 'value': { | ||
const value = await block.get(sir.key) | ||
if (value) { | ||
callbacks.get(followSubscription)?.({ | ||
following.get(followSubscription)?.callback({ | ||
event: 'operationStorageItems', | ||
operationId, | ||
items: [{ key: sir.key, value }], | ||
}) | ||
} | ||
break | ||
return null | ||
} | ||
case 'descendantsValues': { | ||
// TODO expose pagination | ||
const pageSize = 100 | ||
let startKey: string | null = '0x' | ||
while (startKey) { | ||
const keys = await block.getKeysPaged({ | ||
prefix: sir.key, | ||
pageSize, | ||
startKey, | ||
}) | ||
startKey = keys[pageSize - 1] ?? null | ||
|
||
const items = await Promise.all( | ||
keys.map((key) => | ||
block.get(key).then((value) => ({ | ||
key, | ||
value, | ||
})), | ||
), | ||
) | ||
callbacks.get(followSubscription)?.({ | ||
event: 'operationStorageItems', | ||
operationId, | ||
items, | ||
}) | ||
break | ||
} | ||
break | ||
const { items, next } = await getDescendantValues(block, { prefix: sir.key, startKey: '0x' }) | ||
|
||
following.get(followSubscription)?.callback({ | ||
event: 'operationStorageItems', | ||
operationId, | ||
items, | ||
}) | ||
|
||
return next | ||
} | ||
default: | ||
// TODO | ||
console.warn(`Storage type not implemented ${sir.type}`) | ||
return null | ||
} | ||
} | ||
|
||
await Promise.all(items.map(handleStorageItemRequest)) | ||
const listResult = await Promise.all(items.map(handleStorageItemRequest)) | ||
const pending = listResult.filter((v) => v !== null) | ||
|
||
callbacks.get(followSubscription)?.({ | ||
event: 'operationStorageDone', | ||
operationId, | ||
}) | ||
if (!pending.length) { | ||
following.get(followSubscription)?.callback({ | ||
event: 'operationStorageDone', | ||
operationId, | ||
}) | ||
} else { | ||
const follower = following.get(followSubscription) | ||
if (follower) { | ||
follower.pendingDescendantValues.set(operationId, { hash, params: pending }) | ||
follower.callback({ | ||
event: 'operationWaitingForContinue', | ||
operationId, | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
return { | ||
|
@@ -268,7 +315,7 @@ export const chainHead_v1_body: Handler<[string, HexString], OperationStarted | | |
context, | ||
[followSubscription, hash], | ||
) => { | ||
if (!callbacks.has(followSubscription)) return limitReached | ||
if (!following.has(followSubscription)) return limitReached | ||
const block = await context.chain.getBlock(hash) | ||
if (!block) { | ||
throw new ResponseError(-32801, 'Block not found') | ||
|
@@ -278,7 +325,7 @@ export const chainHead_v1_body: Handler<[string, HexString], OperationStarted | | |
afterResponse(async () => { | ||
const body = await block.extrinsics | ||
|
||
callbacks.get(followSubscription)?.({ | ||
following.get(followSubscription)?.callback({ | ||
event: 'operationBodyDone', | ||
operationId, | ||
value: body, | ||
|
@@ -288,18 +335,66 @@ export const chainHead_v1_body: Handler<[string, HexString], OperationStarted | | |
return operationStarted(operationId) | ||
} | ||
|
||
// Currently no-ops, will come into play when pagination is implemented | ||
/** | ||
* Resume an operation paused through `operationWaitingForContinue` | ||
* | ||
* @param context | ||
* @param params - [`followSubscription`, `operationId`] | ||
*/ | ||
export const chainHead_v1_continue: Handler<[string, HexString], null> = async ( | ||
_context, | ||
[_followSubscription, _operationId], | ||
context, | ||
[followSubscription, operationId], | ||
) => { | ||
const follower = following.get(followSubscription) | ||
const pendingOp = follower?.pendingDescendantValues.get(operationId) | ||
if (!pendingOp || !follower) { | ||
throw new ResponseError(-32803, "Operation ID doesn't have anything pending") | ||
} | ||
const block = await context.chain.getBlock(pendingOp.hash) | ||
if (!block) { | ||
throw new ResponseError(-32801, 'Block not found') | ||
} | ||
|
||
afterResponse(async () => { | ||
const handlePendingOperation = async (params: DescendantValuesParams) => { | ||
const { items, next } = await getDescendantValues(block, params) | ||
|
||
follower.callback({ | ||
event: 'operationStorageItems', | ||
operationId, | ||
items, | ||
}) | ||
|
||
return next | ||
} | ||
|
||
const listResult = await Promise.all(pendingOp.params.map(handlePendingOperation)) | ||
const pending = listResult.filter((v) => v !== null) | ||
|
||
if (!pending.length) { | ||
follower.pendingDescendantValues.delete(operationId) | ||
follower.callback({ | ||
event: 'operationStorageDone', | ||
operationId, | ||
}) | ||
} else { | ||
follower.pendingDescendantValues.set(operationId, { hash: pendingOp.hash, params: pending }) | ||
follower.callback({ | ||
event: 'operationWaitingForContinue', | ||
operationId, | ||
}) | ||
} | ||
}) | ||
|
||
return null | ||
} | ||
|
||
export const chainHead_v1_stopOperation: Handler<[string, HexString], null> = async ( | ||
_context, | ||
[_followSubscription, _operationId], | ||
[followSubscription, operationId], | ||
) => { | ||
following.get(followSubscription)?.pendingDescendantValues.delete(operationId) | ||
|
||
return null | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One downside about global variables is that for some use cases, we need to handle chopsticks instances running at a same time (e.g. unit tests) so need to ensure they don't interfere with each other.
In this particular case, it is fine as everything is indexed by a unique random ID. The only drawback I can see is potential memory leak (i.e. destroy a chopsticks instance will not clear entry here). But that's not a very big deal here.
So this is ok for now. Maybe later refactor this into context somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#839