Skip to content
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
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
5 changes: 4 additions & 1 deletion packages/neuron-wallet/src/services/rpc-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Block from '../models/chain/block'
import BlockHeader from '../models/chain/block-header'
import TransactionWithStatus from '../models/chain/transaction-with-status'
import logger from '../utils/logger'
import { generateRPC } from '../utils/ckb-rpc'
import { FullCKBRPC, generateRPC } from '../utils/ckb-rpc'
import { NetworkType } from '../models/network'
import TxStatus, { TxStatusType } from '../models/chain/tx-status'

Expand Down Expand Up @@ -71,6 +71,9 @@ export default class RpcService {

public async getSyncState(): Promise<CKBComponents.SyncState> {
const syncState = await this.retry(async () => {
if (this.rpc instanceof FullCKBRPC) {
return await this.rpc.getSyncState()
}
return await this.rpc.syncState()
})
return syncState
Expand Down
5 changes: 4 additions & 1 deletion packages/neuron-wallet/src/types/ckbComponents.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ declare namespace CKBComponents {
type BannedAddress = api.BannedAddr
type WitnessArgs = api.WitnessArgs
type BlockEconomicState = api.BlockEconomicState
type SyncState = api.SyncState
type SyncState = api.SyncState & {
assumeValidTarget: string
assumeValidTargetReached: boolean
}
type TransactionProof = api.TransactionProof
type TxVerbosity = api.TxVerbosity
type TxPoolVerbosity = api.TxPoolVerbosity
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
46 changes: 42 additions & 4 deletions packages/neuron-wallet/src/utils/ckb-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import CommonUtils from './common'
import { NetworkType } from '../models/network'
import type { RPCConfig } from '@ckb-lumos/rpc/lib/types/common'
import { RPC } from '@ckb-lumos/rpc/lib/types/rpc'

export interface LightScriptFilter {
script: Script
Expand All @@ -22,6 +23,11 @@ export interface LightScriptFilter {

export type LightScriptSyncStatus = LightScriptFilter

interface ExtendedSyncState extends RPC.SyncState {
assume_valid_target: string
assume_valid_target_reached: boolean
}

const lightRPCProperties: Record<string, Omit<Parameters<CKBRPC['addMethod']>[0], 'name'>> = {
setScripts: {
method: 'set_scripts',
Expand Down Expand Up @@ -118,19 +124,51 @@ 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: ExtendedSyncState) => {
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