Skip to content

Commit 79a07a9

Browse files
committed
feat: assume_valid_target
1 parent a73282e commit 79a07a9

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-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: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,62 @@ const lightRPCProperties: Record<string, Omit<Parameters<CKBRPC['addMethod']>[0]
118118
},
119119
}
120120

121+
class Method extends SdkRpcMethod {
122+
constructor(node: CKBComponents.Node, options: CKBComponents.Method) {
123+
super(node, options, rpcConfig)
124+
}
125+
}
126+
121127
export class FullCKBRPC extends CKBRPC {
128+
constructor(url: string, rpcConfig: Partial<RPCConfig>) {
129+
super(url, rpcConfig)
130+
this.setNode({ url })
131+
132+
this.getSyncState = new Method(this.node, {
133+
name: 'getSyncState',
134+
method: 'sync_state',
135+
paramsFormatters: [],
136+
resultFormatters: (state: {
137+
assume_valid_target: string
138+
assume_valid_target_reached: boolean
139+
best_known_block_number: any
140+
best_known_block_timestamp: any
141+
fast_time: any
142+
ibd: any
143+
inflight_blocks_count: any
144+
low_time: any
145+
normal_time: any
146+
orphan_blocks_count: any
147+
}) => {
148+
if (!state) {
149+
return state
150+
}
151+
return {
152+
assumeValidTarget: state.assume_valid_target,
153+
assumeValidTargetReached: state.assume_valid_target_reached,
154+
bestKnownBlockNumber: state.best_known_block_number,
155+
bestKnownBlockTimestamp: state.best_known_block_timestamp,
156+
fastTime: state.fast_time,
157+
ibd: state.ibd,
158+
inflightBlocksCount: state.inflight_blocks_count,
159+
lowTime: state.low_time,
160+
normalTime: state.normal_time,
161+
orphanBlocksCount: state.orphan_blocks_count,
162+
}
163+
},
164+
}).call
165+
}
166+
122167
getGenesisBlockHash = async () => {
123168
return this.getBlockHash('0x0')
124169
}
125170

126171
getGenesisBlock = async (): Promise<Block> => {
127172
return this.getBlockByNumber('0x0')
128173
}
129-
}
130174

131-
class Method extends SdkRpcMethod {
132-
constructor(node: CKBComponents.Node, options: CKBComponents.Method) {
133-
super(node, options, rpcConfig)
175+
getSyncState = async (): Promise<CKBComponents.SyncState> => {
176+
return this.getSyncState()
134177
}
135178
}
136179

0 commit comments

Comments
 (0)