Skip to content

Commit 9be8e22

Browse files
authored
feat: assume_valid_target (nervosnetwork#3307)
* feat: assume_valid_target * fix: comments
1 parent bb4c6ce commit 9be8e22

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

packages/neuron-wallet/src/controllers/sync-api.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export default class SyncApiController {
133133
const currentTimestamp = Date.now()
134134
const network = NetworksService.getInstance().getCurrent()
135135
const tipHeader = await new RpcService(network.remote, network.type).getTipHeader()
136+
const syncState = await new RpcService(network.remote, network.type).getSyncState()
136137

137138
const { bestKnownBlockNumber, bestKnownBlockTimestamp } = await this.#fetchBestKnownBlockInfo()
138139
const foundBestKnownBlockNumber = this.#foundBestKnownBlockNumber(bestKnownBlockNumber)
@@ -146,9 +147,7 @@ export default class SyncApiController {
146147
? +process.env.CKB_NODE_ASSUME_VALID_TARGET_BLOCK_NUMBER
147148
: undefined
148149
const isLookingValidTarget =
149-
network.type === NetworkType.Default &&
150-
!!setAssumeValidTargetBlockNumber &&
151-
bestKnownBlockNumber < setAssumeValidTargetBlockNumber
150+
network.type === NetworkType.Default && !!setAssumeValidTargetBlockNumber && !syncState.assumeValidTargetReached
152151

153152
const newSyncState: SyncState = {
154153
nodeUrl: network.remote,

packages/neuron-wallet/src/services/rpc-service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Block from '../models/chain/block'
44
import BlockHeader from '../models/chain/block-header'
55
import TransactionWithStatus from '../models/chain/transaction-with-status'
66
import logger from '../utils/logger'
7-
import { generateRPC } from '../utils/ckb-rpc'
7+
import { FullCKBRPC, generateRPC } from '../utils/ckb-rpc'
88
import { NetworkType } from '../models/network'
99
import TxStatus, { TxStatusType } from '../models/chain/tx-status'
1010

@@ -71,6 +71,9 @@ export default class RpcService {
7171

7272
public async getSyncState(): Promise<CKBComponents.SyncState> {
7373
const syncState = await this.retry(async () => {
74+
if (this.rpc instanceof FullCKBRPC) {
75+
return await this.rpc.getSyncState()
76+
}
7477
return await this.rpc.syncState()
7578
})
7679
return syncState

packages/neuron-wallet/src/types/ckbComponents.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ declare namespace CKBComponents {
7676
type BannedAddress = api.BannedAddr
7777
type WitnessArgs = api.WitnessArgs
7878
type BlockEconomicState = api.BlockEconomicState
79-
type SyncState = api.SyncState
79+
type SyncState = api.SyncState & {
80+
assumeValidTarget: string
81+
assumeValidTargetReached: boolean
82+
}
8083
type TransactionProof = api.TransactionProof
8184
type TxVerbosity = api.TxVerbosity
8285
type TxPoolVerbosity = api.TxPoolVerbosity

packages/neuron-wallet/src/types/rpc.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ declare namespace RPC {
251251
txs_fee: string
252252
}
253253
export interface SyncState {
254+
assume_valid_target: string
255+
assume_valid_target_reached: boolean
254256
best_known_block_number: string
255257
best_known_block_timestamp: string
256258
fast_time: string

packages/neuron-wallet/src/utils/ckb-rpc.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import CommonUtils from './common'
1414
import { NetworkType } from '../models/network'
1515
import type { RPCConfig } from '@ckb-lumos/rpc/lib/types/common'
16+
import { RPC } from '@ckb-lumos/rpc/lib/types/rpc'
1617

1718
export interface LightScriptFilter {
1819
script: Script
@@ -22,6 +23,11 @@ export interface LightScriptFilter {
2223

2324
export type LightScriptSyncStatus = LightScriptFilter
2425

26+
interface ExtendedSyncState extends RPC.SyncState {
27+
assume_valid_target: string
28+
assume_valid_target_reached: boolean
29+
}
30+
2531
const lightRPCProperties: Record<string, Omit<Parameters<CKBRPC['addMethod']>[0], 'name'>> = {
2632
setScripts: {
2733
method: 'set_scripts',
@@ -118,19 +124,51 @@ const lightRPCProperties: Record<string, Omit<Parameters<CKBRPC['addMethod']>[0]
118124
},
119125
}
120126

127+
class Method extends SdkRpcMethod {
128+
constructor(node: CKBComponents.Node, options: CKBComponents.Method) {
129+
super(node, options, rpcConfig)
130+
}
131+
}
132+
121133
export class FullCKBRPC extends CKBRPC {
134+
constructor(url: string, rpcConfig: Partial<RPCConfig>) {
135+
super(url, rpcConfig)
136+
this.setNode({ url })
137+
138+
this.getSyncState = new Method(this.node, {
139+
name: 'getSyncState',
140+
method: 'sync_state',
141+
paramsFormatters: [],
142+
resultFormatters: (state: ExtendedSyncState) => {
143+
if (!state) {
144+
return state
145+
}
146+
return {
147+
assumeValidTarget: state.assume_valid_target,
148+
assumeValidTargetReached: state.assume_valid_target_reached,
149+
bestKnownBlockNumber: state.best_known_block_number,
150+
bestKnownBlockTimestamp: state.best_known_block_timestamp,
151+
fastTime: state.fast_time,
152+
ibd: state.ibd,
153+
inflightBlocksCount: state.inflight_blocks_count,
154+
lowTime: state.low_time,
155+
normalTime: state.normal_time,
156+
orphanBlocksCount: state.orphan_blocks_count,
157+
}
158+
},
159+
}).call
160+
}
161+
122162
getGenesisBlockHash = async () => {
123163
return this.getBlockHash('0x0')
124164
}
125165

126166
getGenesisBlock = async (): Promise<Block> => {
127167
return this.getBlockByNumber('0x0')
128168
}
129-
}
130169

131-
class Method extends SdkRpcMethod {
132-
constructor(node: CKBComponents.Node, options: CKBComponents.Method) {
133-
super(node, options, rpcConfig)
170+
getSyncState = async (): Promise<CKBComponents.SyncState> => {
171+
return this.getSyncState()
134172
}
135173
}
136174

0 commit comments

Comments
 (0)