Skip to content

Commit

Permalink
feat: Adapt ckb light node (#2590)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu authored Mar 24, 2023
1 parent 67bcd86 commit 2dca3fc
Show file tree
Hide file tree
Showing 56 changed files with 2,038 additions and 394 deletions.
1 change: 1 addition & 0 deletions .ckb-light-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.2.2
12 changes: 7 additions & 5 deletions packages/neuron-ui/src/components/NetworkSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { chainState } from 'states'
import { setCurrentNetowrk } from 'services/remote'

import { backToTop, RoutePath, useOnHandleNetwork, useOnWindowResize, useToggleChoiceGroupBorder } from 'utils'
import { LIGHT_CLIENT_TESTNET } from 'utils/const'
import styles from './networkSetting.module.scss'

const NetworkSetting = ({ chain = chainState, settings: { networks = [] } }: State.AppWithNeuronWallet) => {
Expand Down Expand Up @@ -71,11 +72,12 @@ const NetworkSetting = ({ chain = chainState, settings: { networks = [] } }: Sta
<span className={styles.url}>{`(${network.remote}`}</span>
<NetworkTypeLabel type={network.chain} />
</span>

<button type="button" data-action="edit" aria-label={t('common.edit')} title={t('common.edit')}>
<EditNetwork />
</button>
{isDefault ? null : (
{network.chain === LIGHT_CLIENT_TESTNET ? null : (
<button type="button" data-action="edit" aria-label={t('common.edit')} title={t('common.edit')}>
<EditNetwork />
</button>
)}
{isDefault || network.chain === LIGHT_CLIENT_TESTNET ? null : (
<button
type="button"
data-action="delete"
Expand Down
4 changes: 4 additions & 0 deletions packages/neuron-ui/src/components/NetworkTypeLabel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import { useTranslation } from 'react-i18next'
import { LIGHT_CLIENT_TESTNET } from 'utils/const'

const NetworkTypeLabel = ({ type }: { type: 'ckb' | 'ckb_testnet' | 'ckb_dev' | string }) => {
const [t] = useTranslation()
Expand All @@ -10,6 +11,9 @@ const NetworkTypeLabel = ({ type }: { type: 'ckb' | 'ckb_testnet' | 'ckb_dev' |
case 'ckb_testnet': {
return <span className="label secondary">{t('settings.network.testnet')}</span>
}
case LIGHT_CLIENT_TESTNET: {
return <span className="label secondary">{t('settings.network.lightTestnet')}</span>
}
default: {
return <span className="label third">{t('settings.network.devnet')}</span>
}
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@
},
"mainnet": "Mainnet",
"testnet": "Testnet",
"lightTestnet": "Light Testnet",
"devnet": "Devnet"
},
"locale": {
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@
},
"mainnet": "主网",
"testnet": "测试网",
"lightTestnet": "轻节点测试网",
"devnet": "开发网"
},
"locale": {
Expand Down
3 changes: 1 addition & 2 deletions packages/neuron-ui/src/services/chain.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import CKBCore from '@nervosnetwork/ckb-sdk-core'

export const ckbCore = new CKBCore('')
export const { getHeader, getBlock, getBlockchainInfo, getTipHeader, getHeaderByNumber } = ckbCore.rpc
export const { getHeader, getBlockchainInfo, getTipHeader, getHeaderByNumber } = ckbCore.rpc

export const { calculateDaoMaximumWithdraw } = ckbCore

export const { toUint64Le, parseEpoch } = ckbCore.utils
export default {
ckbCore,
getBlock,
getBlockchainInfo,
getHeader,
getTipHeader,
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ body {
justify-content: center;
align-items: center;
height: 18px;
width: 58px;
min-width: 58px;
padding: 0 4px;
font-size: 9px;
font-weight: 600;
Expand Down
2 changes: 2 additions & 0 deletions packages/neuron-ui/src/utils/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ export const DEPRECATED_CODE_HASH: Record<string, string> = {
AcpOnLina: '0x0fb343953ee78c9986b091defb6252154e0bb51044fd2879fde5b27314506111',
AcpOnAggron: '0x86a1c6987a4acbe1a887cca4c9dd2ac9fcb07405bbeda51b861b18bbf7492c4b',
}

export const LIGHT_CLIENT_TESTNET = 'light_client_testnet'
12 changes: 12 additions & 0 deletions packages/neuron-wallet/electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ win:
extraFiles:
- from: "bin/win/ckb.exe"
to: "bin/ckb.exe"
- from: "bin/win/ckb-light-client.exe"
to: "bin/ckb-light-client.exe"
- from: "light/ckb_light.toml"
to: "light/ckb_light.toml"
target:
- target: nsis
arch:
Expand All @@ -53,6 +57,10 @@ mac:
extraFiles:
- from: "bin/mac/ckb"
to: "bin/ckb"
- from: "bin/mac/ckb-light-client"
to: "bin/ckb-light-client"
- from: "light/ckb_light.toml"
to: "light/ckb_light.toml"
hardenedRuntime: true
gatekeeperAssess: false
entitlements: assets/entitlements.plist
Expand All @@ -71,5 +79,9 @@ linux:
extraFiles:
- from: "bin/linux/ckb"
to: "bin/ckb"
- from: "bin/linux/ckb-light-client"
to: "bin/ckb-light-client"
- from: "light/ckb_light.toml"
to: "light/ckb_light.toml"
target:
- AppImage
55 changes: 55 additions & 0 deletions packages/neuron-wallet/light/ckb_light.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# chain = "mainnet"
# chain = "testnet"
# chain = "your_path_to/dev.toml"
chain = "testnet"

[store]
path = "data/store"

[network]
path = "data/network"

listen_addresses = ["/ip4/0.0.0.0/tcp/8118"]
### Specify the public and routable network addresses
# public_addresses = []

# Node connects to nodes listed here to discovery other peers when there's no local stored peers.
# When chain.spec is changed, this usually should also be changed to the bootnodes in the new chain.
bootnodes = [
# Hangzhou, Asia
"/ip4/47.111.169.36/tcp/8111/p2p/QmNQ4jky6uVqLDrPU7snqxARuNGWNLgSrTnssbRuy3ij2W",
# Ohio, North America
"/ip4/18.217.146.65/tcp/8111/p2p/QmT6DFfm18wtbJz3y4aPNn3ac86N4d4p4xtfQRRPf73frC",
# Singapore, Asia
"/ip4/18.136.60.221/tcp/8111/p2p/QmTt6HeNakL8Fpmevrhdna7J4NzEMf9pLchf1CXtmtSrwb",
# London, Europe
"/ip4/35.176.207.239/tcp/8111/p2p/QmSJTsMsMGBjzv1oBNwQU36VhQRxc2WQpFoRu1ZifYKrjZ"
]

### Whitelist-only mode
# whitelist_only = false
### Whitelist peers connecting from the given IP addresses
# whitelist_peers = []

### Enable `SO_REUSEPORT` feature to reuse port on Linux, not supported on other OS yet
# reuse_port_on_linux = true

max_peers = 125
max_outbound_peers = 8
# 2 minutes
ping_interval_secs = 120
# 20 minutes
ping_timeout_secs = 1200
connect_outbound_interval_secs = 15
# If set to true, try to register upnp
upnp = false
# If set to true, network service will add discovered local address to peer store, it's helpful for private net development
discovery_local_address = false
# If set to true, random cleanup when there are too many inbound nodes
# Ensure that itself can continue to serve as a bootnode node
bootnode_mode = false

[rpc]
# Light client rpc is designed for self hosting, exposing to public network is not recommended and may cause security issues.
# By default RPC only binds to localhost, thus it only allows accessing from the same machine.
listen_address = "127.0.0.1:9000"
5 changes: 3 additions & 2 deletions packages/neuron-wallet/src/block-sync-renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import DataUpdateSubject from 'models/subjects/data-update'
import AddressCreatedSubject from 'models/subjects/address-created-subject'
import WalletDeletedSubject from 'models/subjects/wallet-deleted-subject'
import TxDbChangedSubject from 'models/subjects/tx-db-changed-subject'
import { LumosCellQuery, LumosCell } from './sync/indexer-connector'
import { LumosCellQuery, LumosCell } from './sync/connector'
import { WorkerMessage, StartParams, QueryIndexerParams } from './task'
import logger from 'utils/logger'
import CommonUtils from 'utils/common'
Expand Down Expand Up @@ -105,7 +105,8 @@ export const createBlockSyncTask = async () => {
// prevents the sync task from being started repeatedly if fork does not finish executing.
child = fork(path.join(__dirname, 'task-wrapper.js'), [], {
env: { fileBasePath: env.fileBasePath },
stdio: ['ipc', process.stdout, 'pipe']
stdio: ['ipc', process.stdout, 'pipe'],
execArgv: env.app.isPackaged ? [] : ['--inspect']
})

child.on('message', ({ id, message, channel }: WorkerMessage) => {
Expand Down
44 changes: 44 additions & 0 deletions packages/neuron-wallet/src/block-sync-renderer/sync/connector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Subject } from 'rxjs'

export interface BlockTips {
cacheTipNumber: number
indexerTipNumber: number | undefined
}

export interface LumosCellQuery {
lock: CKBComponents.Script | null
type: CKBComponents.Script | null
data: string | null
}

export interface LumosCell {
block_hash: string
out_point: {
tx_hash: string
index: string
}
cell_output: {
capacity: string
lock: {
code_hash: string
args: string
hash_type: string
}
type?: {
code_hash: string
args: string
hash_type: string
}
}
data?: string
}

export abstract class Connector<TransactionsSubjectParam = unknown> {
abstract blockTipsSubject: Subject<BlockTips>
abstract transactionsSubject: Subject<{ txHashes: CKBComponents.Hash[]; params: TransactionsSubjectParam }>

abstract connect(): Promise<void>
abstract notifyCurrentBlockNumberProcessed(param: TransactionsSubjectParam): void
abstract stop(): void
abstract getLiveCellsByScript(query: LumosCellQuery): Promise<unknown>
}
Loading

1 comment on commit 2dca3fc

@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 4507571326

Please sign in to comment.