Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 47 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
Loading