-
Notifications
You must be signed in to change notification settings - Fork 15
Feat/query module #327
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
Merged
Merged
Feat/query module #327
Changes from 38 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
90ce9b5
feat: add GraphqlExplorer to sdk
saitunc 75cf0a4
feat: update ClientAppChain to include explorer
saitunc 83843df
test: add test case for waitTxInclusion
saitunc 338ca4e
refactor: add BlockExplorer interface
saitunc bf334f0
refactor: export BlockExplorer type from sequencer package
saitunc 1dcffa8
test: add BlockExplorer to the client module
saitunc 7453c7c
feat: add InMemoryBlockExplorer for server appChain
saitunc 291b1d0
refactor: remove comment line
saitunc 06f20c0
refactor: make GrahpqlExplorer logic cleaner
saitunc 6025b4d
refactor: update appChains for correct explorer objects
saitunc 21ba1d3
refactor: export InMemoryBlockExplorer from sdk
saitunc 72ec837
refactor: change the test name for clarification
saitunc 692f694
fix: fix conditional causes infinite loop
saitunc 1e2e000
test: add assertion of pending state of transaction
saitunc 1bd28c6
refactor: add better status handling
saitunc 0ef1e61
test: fix transaction fetching test and add block getter test
saitunc 92fa991
fix: define graphqlfragment to make query work
saitunc 05f25bf
style: fix lint
saitunc da600e2
refactor: refactor query module and implement regarding transport module
saitunc ac91d26
refactor: refactor graphqlExplorer to a transport module
saitunc 3a14b00
refactor: update InMemoryBlockExplorer
saitunc 20d1980
refactor: update variable names
saitunc bbbd10a
test: add equality assertion for using getBlock with hash or height
saitunc c215af3
fix: add inject to BlockExplorerTrasnportModule
saitunc 8692533
fix: inject sequencer module for InMemoryBlockExplorer
saitunc 9c6ae6c
refactor(test): remove unnecessary preBlockQuery assertion
saitunc 970fa5c
feat: change check logic in waitTxUnclusion in query module
saitunc fbd4d38
refactor: change return value types of inclusion func and remove casting
saitunc 5c9638d
feat: add assertion to check that promise is not resolved
saitunc 262084e
refactor: remove unused files
saitunc 84a9572
refactor: remove expor of BlockExplorer
saitunc 77b6799
refactor: remove fragment and use query directly
saitunc 6f31a60
feat: add interval and maxAttempts to waitTxInclusion function
saitunc 9c7396c
docs: add tsdocs to the BlockExplorerQuery functions
saitunc e5bae79
refactor: add return type to getBlock in BlockExplorerTransportModule
saitunc 1aff8a0
style: fix lint
saitunc 26a43a5
build(deps): update o1js verison to 2.10 from 1.6 in sdk package.json
saitunc 728b9e4
refactor: resolve circular deps with sequencer type exports
saitunc faa5325
refactor: remove logs and unused type
saitunc 2c532a8
refactor: remove @proto-kit/api from sdk packages.json
saitunc afd6d80
refactor: update return type of block query
saitunc e893ce5
refactor: add full error message in GraphqlBlockExplorerTransportModule
saitunc 8d4e2c5
test: add hash equality assertion to queried block with produced block
saitunc b16e37f
refactor: replace getBlock param with discriminated union type
saitunc 42caa2e
style: change waitTxInclusion name to fetchTxInclusion
saitunc c3c29f7
style: run lint
saitunc da705e7
refactor: add ClientTransaction type for de/serialization
saitunc 6bf3607
refactor: update return types of client/server block explorers
saitunc 53a74c1
refactor(test): refactor block explorer test
saitunc 3df1cee
refactor: check undefined block in client explorer query
saitunc 646a224
refactor: update return type of block as BlockModel
saitunc b539707
refactor: remove unnecessary stringification
saitunc d31208b
refactor: ƒix lint and add query response interface for lint fix
saitunc fde6c1c
refactor: add block explorer module to Settlement.ts
saitunc 74de168
refactor: add graphqlClient module to ClientAppchain in Settlement.ts
saitunc eca06f6
style: fix lint in Settlement.ts
saitunc e44857f
refactor: add explorer modules to client and testing chains
saitunc 5c04b85
fix: add correct block explorer to client and testing chains
saitunc a74d61b
fix: add blockExplorer to createPrismaAppchain
saitunc 663d7ed
fix: add blockExplorer config to createPrismaAppchain
saitunc 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
83 changes: 83 additions & 0 deletions
83
packages/sdk/src/graphql/GraphqlBlockExplorerTransportModule.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { inject, injectable } from "tsyringe"; | ||
| import { gql } from "@urql/core"; | ||
| import { | ||
| AppChainModule, | ||
| Block, | ||
| BlockExplorerTransportModule, | ||
| InclusionStatus | ||
| } from "@proto-kit/sequencer"; | ||
|
|
||
| import { GraphqlClient } from "./GraphqlClient"; | ||
|
|
||
| @injectable() | ||
| export class GraphqlBlockExplorerTransportModule | ||
| extends AppChainModule | ||
| implements BlockExplorerTransportModule | ||
| { | ||
| public constructor( | ||
| @inject("GraphqlClient") private readonly graphqlClient: GraphqlClient | ||
| ) { | ||
| super(); | ||
| } | ||
|
|
||
| public async waitTxInclusion(txHash: string): Promise<InclusionStatus> { | ||
| const query = gql` | ||
| query transactionState($hash: String!) { | ||
| transactionState(hash: $hash) | ||
| } | ||
| `; | ||
|
|
||
| const queryResult = await this.graphqlClient.client | ||
| .query(query, { hash: txHash }) | ||
| .toPromise(); | ||
|
|
||
| if (queryResult.error) { | ||
| throw new Error(`Error in waitTxInclusion query: ${queryResult.error}`); | ||
| } | ||
|
|
||
| return queryResult.data?.transactionState; | ||
| } | ||
|
|
||
| async getBlock(param?: string | number): Promise<Block> { | ||
| let hash: string | undefined; | ||
| let height: number | undefined; | ||
|
|
||
| if (typeof param === "string") { | ||
| hash = param; | ||
| } else if (typeof param === "number") { | ||
| height = param; | ||
| } | ||
|
|
||
| const query = gql` | ||
| query block($hash: String, $height: Float) { | ||
| block(hash: $hash, height: $height) { | ||
| hash | ||
| previousBlockHash | ||
| height | ||
| txs { | ||
| tx { | ||
| methodId | ||
| nonce | ||
| sender | ||
| argsFields | ||
| auxiliaryData | ||
| } | ||
| status | ||
| statusMessage | ||
| } | ||
| transactionsHash | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| const queryResult = await this.graphqlClient.client | ||
| .query(query, { hash, height }) | ||
| .toPromise(); | ||
|
|
||
| if (queryResult.error) { | ||
| throw new Error("Error fetching block!"); | ||
| } | ||
|
|
||
| return queryResult.data; | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { inject, injectable } from "tsyringe"; | ||
| import { | ||
| AppChainModule, | ||
| Block, | ||
| BlockExplorerTransportModule, | ||
| BlockStorage, | ||
| ClientBlock, | ||
| TransactionStorage, | ||
| InclusionStatus | ||
| } from "@proto-kit/sequencer"; | ||
| import { ModuleContainerLike } from "@proto-kit/common"; | ||
|
|
||
| @injectable() | ||
| export class InMemoryBlockExplorer | ||
| extends AppChainModule | ||
| implements BlockExplorerTransportModule | ||
| { | ||
| private readonly blockStorage: BlockStorage; | ||
|
|
||
| private readonly transactionStorage: TransactionStorage; | ||
|
|
||
| public constructor( | ||
| @inject("Sequencer") public sequencer: ModuleContainerLike | ||
| ) { | ||
| super(); | ||
| this.blockStorage = | ||
| sequencer.dependencyContainer.resolve<BlockStorage>("BlockStorage"); | ||
| this.transactionStorage = | ||
| sequencer.dependencyContainer.resolve<TransactionStorage>( | ||
| "TransactionStorage" | ||
| ); | ||
| } | ||
|
|
||
| public async waitTxInclusion(txHash: string): Promise<InclusionStatus> { | ||
| const dbTx = await this.transactionStorage.findTransaction(txHash); | ||
|
|
||
| if (dbTx?.block !== undefined) { | ||
| return InclusionStatus.INCLUDED; | ||
| } | ||
|
|
||
| return InclusionStatus.UNKNOWN; | ||
| } | ||
|
|
||
| async getBlock(param?: string | number): Promise<ClientBlock | undefined> { | ||
| let hash: string | undefined; | ||
| let height: number | undefined; | ||
|
|
||
| if (typeof param === "string") { | ||
| hash = param; | ||
| } else if (typeof param === "number") { | ||
| height = param; | ||
| } | ||
|
|
||
| let block: Block | undefined; | ||
|
|
||
| if (hash !== undefined) { | ||
| block = await this.blockStorage.getBlock(hash); | ||
| } else { | ||
| const blockHeight = | ||
| height ?? (await this.blockStorage.getCurrentBlockHeight()) - 1; | ||
| block = await this.blockStorage.getBlockAt(blockHeight); | ||
| } | ||
|
|
||
| if (block !== undefined) { | ||
| return block; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
packages/sequencer/src/helpers/query/BlockExplorerQuery.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { inject, injectable } from "tsyringe"; | ||
| import { sleep } from "@proto-kit/common"; | ||
| import { BlockExplorerTransportModule, ClientBlock } from "./BlockExplorerTransportModule"; | ||
|
|
||
| export enum InclusionStatus { | ||
| UNKNOWN = "unknown", | ||
| PENDING = "pending", | ||
| INCLUDED = "included", | ||
| SETTLED = "settled", | ||
| } | ||
|
|
||
| export type TransactionFetcher = (txHash: string) => Promise<InclusionStatus>; | ||
saitunc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @injectable() | ||
| export class BlockExplorerQuery { | ||
| public constructor( | ||
| @inject("BlockExplorerTransportModule") | ||
| private readonly blockExplorer: BlockExplorerTransportModule | ||
| ) {} | ||
|
|
||
| /** | ||
| * Waits for a transaction to be included in a block by polling at regular intervals. | ||
| * | ||
| * @param txHash - Hash string of the transaction to search for | ||
| * @param interval - Polling interval in milliseconds (default: 1000) | ||
| * @param maxAttempts - Maximum number of polling attempts (default: 10) | ||
| * @returns Promise resolving to an object containing the transaction inclusion state | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // Wait with default settings (1s interval, 10 attempts) | ||
| * const result = await blockExplorer.waitTxInclusion(txHash); | ||
| * | ||
| * // Wait with custom interval (500ms) and more attempts (20) | ||
| * const result = await blockExplorer.waitTxInclusion(txHash, 500, 20); | ||
| * ``` | ||
| */ | ||
| public async waitTxInclusion( | ||
| txHash: string, | ||
| interval = 1000, | ||
| maxAttempts = 10 | ||
| ): Promise<{ transactionState: InclusionStatus }> { | ||
| let remainingAttempts = maxAttempts; | ||
|
|
||
| while (true) { | ||
| const status = await this.blockExplorer.waitTxInclusion(txHash); | ||
| console.log("from transport: ", status); | ||
saitunc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (status === "INCLUDED") { | ||
| return { transactionState: InclusionStatus.INCLUDED }; | ||
| } | ||
|
|
||
| remainingAttempts -= 1; | ||
|
|
||
| if (remainingAttempts <= 0) { | ||
| return { transactionState: InclusionStatus.UNKNOWN }; | ||
| } | ||
| console.log("baybay"); | ||
rpanic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await sleep(interval); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves a block by its hash or height. | ||
| * | ||
| * @param {string|number} [param] - Block hash (string) or block height (number). If omitted, returns the latest block. | ||
| * @returns {Promise<BlockModel|undefined>} Promise resolving to a BlockModel object, or undefined if block is not found | ||
| * | ||
| * @example | ||
| * // Get block by hash | ||
| * const block = await blockExplorer.getBlock("216543..."); | ||
| * | ||
| * @example | ||
| * // Get block by height | ||
| * const block = await blockExplorer.getBlock(42); | ||
| */ | ||
| async getBlock(param?: string | number): Promise<ClientBlock | undefined> { | ||
| return await this.blockExplorer.getBlock(param); | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
packages/sequencer/src/helpers/query/BlockExplorerTransportModule.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { Block } from "../../storage/model/Block"; | ||
|
|
||
| export type ClientBlock = Pick<Block, "hash" | "previousBlockHash" | "height" | "transactions" | "transactionsHash">; | ||
|
|
||
| export interface BlockExplorerTransportModule { | ||
| waitTxInclusion(txHash: string): Promise<string>; | ||
saitunc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| getBlock(param?: string | number): Promise<ClientBlock | undefined>; | ||
saitunc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.