Skip to content

Commit

Permalink
feat: assume_valid_target
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan committed Feb 12, 2025
1 parent a73282e commit 8065543
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
5 changes: 2 additions & 3 deletions packages/neuron-wallet/src/controllers/sync-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export default class SyncApiController {
const currentTimestamp = Date.now()
const network = NetworksService.getInstance().getCurrent()
const tipHeader = await new RpcService(network.remote, network.type).getTipHeader()
const syncState = await new RpcService(network.remote, network.type).getSyncState()

const { bestKnownBlockNumber, bestKnownBlockTimestamp } = await this.#fetchBestKnownBlockInfo()
const foundBestKnownBlockNumber = this.#foundBestKnownBlockNumber(bestKnownBlockNumber)
Expand All @@ -146,9 +147,7 @@ export default class SyncApiController {
? +process.env.CKB_NODE_ASSUME_VALID_TARGET_BLOCK_NUMBER
: undefined
const isLookingValidTarget =
network.type === NetworkType.Default &&
!!setAssumeValidTargetBlockNumber &&
bestKnownBlockNumber < setAssumeValidTargetBlockNumber
network.type === NetworkType.Default && !!setAssumeValidTargetBlockNumber && !syncState.assumeValidTargetReached

const newSyncState: SyncState = {
nodeUrl: network.remote,
Expand Down
9 changes: 7 additions & 2 deletions packages/neuron-wallet/src/services/rpc-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ export default class RpcService {
})
}

public async getSyncState(): Promise<CKBComponents.SyncState> {
public async getSyncState(): Promise<
CKBComponents.SyncState & {
assumeValidTarget: string
assumeValidTargetReached: boolean
}
> {
const syncState = await this.retry(async () => {
return await this.rpc.syncState()
return await this.rpc.getSyncState()
})
return syncState
}
Expand Down
2 changes: 2 additions & 0 deletions packages/neuron-wallet/src/types/rpc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ declare namespace RPC {
txs_fee: string
}
export interface SyncState {
assume_valid_target: string
assume_valid_target_reached: boolean
best_known_block_number: string
best_known_block_timestamp: string
fast_time: string
Expand Down
59 changes: 55 additions & 4 deletions packages/neuron-wallet/src/utils/ckb-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,62 @@ const lightRPCProperties: Record<string, Omit<Parameters<CKBRPC['addMethod']>[0]
},
}

class Method extends SdkRpcMethod {
constructor(node: CKBComponents.Node, options: CKBComponents.Method) {
super(node, options, rpcConfig)
}
}

export class FullCKBRPC extends CKBRPC {
constructor(url: string, rpcConfig: Partial<RPCConfig>) {
super(url, rpcConfig)
this.setNode({ url })

this.getSyncState = new Method(this.node, {
name: 'getSyncState',
method: 'sync_state',
paramsFormatters: [],
resultFormatters: (state: {
assume_valid_target: string
assume_valid_target_reached: boolean
best_known_block_number: any
best_known_block_timestamp: any
fast_time: any
ibd: any
inflight_blocks_count: any
low_time: any
normal_time: any
orphan_blocks_count: any
}) => {
if (!state) {
return state
}
return {
assumeValidTarget: state.assume_valid_target,
assumeValidTargetReached: state.assume_valid_target_reached,
bestKnownBlockNumber: state.best_known_block_number,
bestKnownBlockTimestamp: state.best_known_block_timestamp,
fastTime: state.fast_time,
ibd: state.ibd,
inflightBlocksCount: state.inflight_blocks_count,
lowTime: state.low_time,
normalTime: state.normal_time,
orphanBlocksCount: state.orphan_blocks_count,
}
},
}).call
}

getGenesisBlockHash = async () => {
return this.getBlockHash('0x0')
}

getGenesisBlock = async (): Promise<Block> => {
return this.getBlockByNumber('0x0')
}
}

class Method extends SdkRpcMethod {
constructor(node: CKBComponents.Node, options: CKBComponents.Method) {
super(node, options, rpcConfig)
getSyncState = async (): Promise<CKBComponents.SyncState> => {
return this.getSyncState()
}
}

Expand Down Expand Up @@ -233,6 +276,14 @@ export class LightRPC extends Base {
return genesisBlock.header.hash
}

getSyncState = async () => {
const headerTip = await this.getTipHeader()
return {
bestKnownBlockNumber: headerTip.number,
bestKnownBlockTimestamp: headerTip.timestamp,
} as CKBComponents.SyncState
}

syncState = async () => {
const headerTip = await this.getTipHeader()
return {
Expand Down

1 comment on commit 8065543

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 13282429741

Please sign in to comment.