Skip to content
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

Improve auto consensus query fn #245

Merged
merged 6 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions packages/auto-consensus/src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { AnyTuple, Api, ApiPromise, Codec, StorageKey } from '@autonomys/auto-utils'
import type { RawBlock, RawBlockHeader } from './types/block'
import { parseBlockExtrinsics, parseBlockTransfers } from './utils/parse'
import { queryMethodPath } from './utils/query'

const PIECE_SIZE = BigInt(1048576)
Expand All @@ -12,22 +13,30 @@ export const rpc = async <T>(api: Api, methodPath: string, params: any[] = []):
export const query = async <T>(api: Api, methodPath: string, params: any[] = []): Promise<T> =>
await queryMethodPath<T>(api, `query.${methodPath}`, params)

export const block = async (api: Api) => await rpc<RawBlock>(api, 'chain.getBlock', [])

export const header = async (api: Api) => await rpc<RawBlockHeader>(api, 'chain.getHeader', [])

export const block = async (api: Api, blockHash?: string) =>
await rpc<RawBlock>(api, 'chain.getBlock', [blockHash])

export const blockExtrinsics = async (api: Api, blockHash?: string) =>
await block(api, blockHash).then((block) => parseBlockExtrinsics(block))

export const blockTransfers = async (api: Api, blockHash?: string) =>
await block(api, blockHash).then((block) => parseBlockTransfers(block))

export const blockNumber = async (api: Api): Promise<number> => {
// Get the block
const _block = await block(api)

return _block.block.header.number.toNumber()
}

export const blockHash = async (api: Api) => {
const _blockHash = await rpc<Codec>(api, 'chain.getBlockHash', [])
export const blockHash = async (api: Api, blockNumber?: number) => {
const _blockHash = await rpc<Codec>(api, 'chain.getBlockHash', [blockNumber])
return _blockHash.toString()
}

export const finalizedHead = async (api: Api) =>
await rpc<RawBlockHeader>(api, 'chain.getFinalizedHead', [])

export const networkTimestamp = async (api: Api) => await query<Codec>(api, 'timestamp.now', [])

export const solutionRanges = async (api: Api) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/auto-consensus/src/types/block.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BN } from '@autonomys/auto-utils'
import type { BN, Codec } from '@autonomys/auto-utils'

export type RawBlock = {
block: {
Expand All @@ -11,6 +11,7 @@ export type RawBlock = {
logs: string[]
}
}
extrinsics: Codec[]
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/auto-consensus/src/types/extrinsic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type Extrinsic = {
hash: string
isSigned: boolean
section: string
method: string
signer: string
signature: any
callIndex: number
args: any
}
3 changes: 3 additions & 0 deletions packages/auto-consensus/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// file: src/types/index.ts

export * from './account'
export * from './balance'
export * from './block'
export * from './domain'
export * from './events'
export * from './extrinsic'
export * from './staking'
37 changes: 37 additions & 0 deletions packages/auto-consensus/src/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { AnyTuple, BN, Codec, StorageKey } from '@autonomys/auto-utils'
import type { BalanceData, RawBalanceData } from '../types/balance'
import { RawBlock } from '../types/block'
import { DomainRegistry } from '../types/domain'
import type { Extrinsic } from '../types/extrinsic'
import {
Deposit,
Operator,
Expand Down Expand Up @@ -32,6 +34,41 @@ export const parseBalance = (data: RawBalanceData): BalanceData => {
}
}

export const parseExtrinsic = (extrinsic: Codec): Extrinsic => {
const hash = extrinsic.hash.toHex()
const {
isSigned,
method: { section, method },
} = extrinsic.toHuman() as any
const {
signature: { signer, signature },
method: { callIndex, args },
} = extrinsic.toPrimitive() as any
return {
hash,
isSigned,
section,
method,
signer,
signature,
callIndex,
args,
}
}

export const parseBlockExtrinsics = (block: RawBlock): Extrinsic[] => {
if (!block.block.extrinsics || block.block.extrinsics.length === 0) return []
return block.block.extrinsics.map(parseExtrinsic)
}

export const parseBlockTransfers = (block: RawBlock): Extrinsic[] => {
return parseBlockExtrinsics(block).filter(
(e) =>
(e.section === 'balances' && e.method === 'transfer') ||
(e.section === 'transporter' && e.method === 'transfer'),
)
}

export const parseDomain = (domain: [StorageKey<AnyTuple>, Codec]): DomainRegistry => {
const header = domain[0].toHuman() as [string]
return {
Expand Down