Skip to content

Commit

Permalink
feat: Supports adding light client custom nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan committed Jul 12, 2024
1 parent 79df3c0 commit dbf0027
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const useOnSubmit = ({
id = '',
name = '',
remote = '',
type,
networks = [],
callback,
dispatch,
Expand All @@ -18,14 +19,15 @@ export const useOnSubmit = ({
id: string
name: string
remote: string
type: Controller.NetworkType
networks: Readonly<State.Network[]>
callback: () => void
dispatch: StateDispatch
disabled: boolean
setIsUpdating: React.Dispatch<boolean>
}) =>
useCallback((): void => {
if (disabled) {
if (disabled || !type) {
return
}
let errorMessage: State.Message<ErrorCode, { fieldName: string; fieldValue?: string; length?: string }> | undefined
Expand Down Expand Up @@ -98,6 +100,7 @@ export const useOnSubmit = ({
createNetwork({
name,
remote,
type,
})(dispatch, callback)
return
}
Expand Down
31 changes: 30 additions & 1 deletion packages/neuron-ui/src/components/NetworkEditorDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const NetworkEditorDialog = ({
)
const [t] = useTranslation()
const [editor, setEditor] = useState({
type: 0,
name: '',
nameError: '',
url: url ?? '',
Expand All @@ -39,6 +40,7 @@ const NetworkEditorDialog = ({
const [isUpdating, setIsUpdating] = useState(false)

const disabled = !!(
!editor.type ||
!editor.name ||
!editor.url ||
editor.nameError ||
Expand All @@ -50,6 +52,7 @@ const NetworkEditorDialog = ({
useEffect(() => {
if (cachedNetwork) {
setEditor({
type: cachedNetwork.type,
name: cachedNetwork.name,
nameError: '',
url: cachedNetwork.remote,
Expand All @@ -65,11 +68,14 @@ const NetworkEditorDialog = ({
dataset: { field = '' },
} = e.target as HTMLInputElement
let error = ''
let fieldValue = value as string | number
try {
if (field === 'name') {
validateNetworkName(value, usedNetworkNames)
} else if (field === 'url') {
validateURL(value)
} else if (field === 'type') {
fieldValue = Number(value)
}
} catch (err) {
if (isErrorWithI18n(err)) {
Expand All @@ -79,7 +85,7 @@ const NetworkEditorDialog = ({

setEditor(state => ({
...state,
[field]: value,
[field]: fieldValue,
[`${field}Error`]: error,
}))
},
Expand All @@ -90,6 +96,7 @@ const NetworkEditorDialog = ({
id: id!,
name: editor.name,
remote: editor.url,
type: editor.type,
networks,
callback: onSuccess,
dispatch,
Expand All @@ -109,6 +116,27 @@ const NetworkEditorDialog = ({
isLoading={isUpdating}
>
<div className={styles.container}>
<div className={styles.radioGroup}>
<p className={styles.label}>{t('settings.network.type')}</p>
{[
{ value: '1', label: t('settings.network.full-node') },
{ value: '2', label: t('settings.network.light-client-node') },
].map(item => (
<div className={styles.radioItem} key={item.value}>
<label htmlFor={item.value}>
<input
id={item.value}
type="radio"
value={item.value}
data-field="type"
checked={Number(item.value) === editor.type}
onChange={onChange}
/>
<span>{item.label}</span>
</label>
</div>
))}
</div>
<TextField
value={editor.url}
field="url"
Expand All @@ -118,6 +146,7 @@ const NetworkEditorDialog = ({
placeholder={t('settings.network.edit-network.input-rpc')}
autoFocus
disabled={!!url}
className={styles.rpcItem}
/>
<TextField
value={editor.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,48 @@

.container {
width: 648px;

.rpcItem {
margin-bottom: 10px;
}

.radioGroup {
display: flex;
align-items: center;
margin-bottom: 10px;
gap: 70px;

.label {
color: var(--secondary-text-color);
}

.radioItem {
display: flex;
justify-content: space-between;
align-items: center;
padding: 14px 16px;
label {
padding: 0;
cursor: pointer;
color: var(--main-text-color);
}
input[type='radio'] {
display: none;
margin-right: 8px;
}
input[type='radio'] + span {
display: inline-block;
padding-left: 26px;
font-size: 14px;
line-height: 20px;
background: url('../../widgets/Icons/Radio.svg') no-repeat left top;
user-select: none;
}
input[type='radio']:checked + span {
background: url('../../widgets/Icons/RadioSelected.svg') no-repeat left top;
}
}
}
}

.actions {
Expand Down
10 changes: 8 additions & 2 deletions packages/neuron-ui/src/components/NetworkSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,15 @@ const NetworkSetting = ({ chain = chainState, settings: { networks = [] } }: Sta
}
}, [currentId, networks])

const isInternalLightNode = (id: string) => id.includes('light_client')

const showNetworks = useMemo(() => {
const internalLightNodeId = lastShowInternalNodeIds.get(NetworkType.Light)
return networks.filter(v => v.type !== NetworkType.Light || v.id === internalLightNodeId)
if (isInternalLightNode(internalLightNodeId)) {
return networks.filter(network => !isInternalLightNode(network.id) || network.id === internalLightNodeId)
}
const index = networks.findIndex(network => isInternalLightNode(network.id))
return networks.filter((network, i) => !isInternalLightNode(network.id) || index === i)
}, [currentId, networks])

return (
Expand All @@ -111,7 +117,7 @@ const NetworkSetting = ({ chain = chainState, settings: { networks = [] } }: Sta
),
suffix: (
<div className={styles.suffix}>
{currentId === network.id && network.type === NetworkType.Light ? (
{currentId === network.id && isInternalLightNode(network.id) && network.type === NetworkType.Light ? (
<Tooltip
tip={
<div>
Expand Down
10 changes: 2 additions & 8 deletions packages/neuron-ui/src/containers/Main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ const MainContent = () => {
[network, networks]
)

const isLightClientNetwork = network?.type === 2

useSyncChainData({
chainURL: network?.remote ?? '',
dispatch,
Expand Down Expand Up @@ -100,12 +98,6 @@ const MainContent = () => {
}, [])

const dialogProps = (function getDialogProps() {
if (isLightClientNetwork) {
return {
onConfirm: onCloseSwitchNetwork,
children: t('main.external-node-detected-dialog.external-node-is-light'),
}
}
if (sameUrlNetworks.length) {
return {
onConfirm: onSwitchNetwork,
Expand Down Expand Up @@ -172,6 +164,8 @@ const MainContent = () => {
cancelText={t('main.external-node-detected-dialog.ignore-external-node')}
title={t('main.external-node-detected-dialog.title')}
className={styles.networkDialog}
confirmProps={{ type: 'dashed' }}
cancelProps={{ type: 'dashed' }}
>
{dialogProps.children}
</Dialog>
Expand Down
14 changes: 8 additions & 6 deletions packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,10 @@
"lightTestnet": "Light Testnet",
"lightMainnet": "Light Mainnet",
"devnet": "Devnet",
"switch-network-type": "Switch to {{type}}"
"switch-network-type": "Switch to {{type}}",
"type": "Type",
"full-node": "Full Node",
"light-client-node": "Light Client Node"
},
"locale": {
"en": "English",
Expand Down Expand Up @@ -1216,12 +1219,11 @@
},
"main": {
"external-node-detected-dialog": {
"title": "External node detected",
"body-tips-without-network": "\"Internal Node\" is reserved for the built-in CKB node, please add a new network option for the external node.",
"body-tips-with-network": "\"Internal Node\" is reserved for the built-in CKB node, please select from the following network options or add a new one for the external node.",
"title": "Detected external node",
"body-tips-without-network": "You have selected the internal node but started the external node, if you want to continue to use the external node, please add a new network.",
"body-tips-with-network": "You have currently selected an internal node but started an external node, if you need to continu using the external node please switch to another external network or add a new network.",
"add-network": "Add Network",
"ignore-external-node": "Ignore external node",
"external-node-is-light": "Neuron does not support external light client nodes due to different security assumptions for light clients. Would you like to connect to the \"Light Client\" ?"
"ignore-external-node": "Ignore external node"
},
"no-disk-space-dialog": {
"tip": "Due to insufficient disk space, synchronization has been stopped. <br /> Please allocate more disk space or migrate the data to another disk.",
Expand Down
12 changes: 7 additions & 5 deletions packages/neuron-ui/src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,10 @@
"lightTestnet": "Light Testnet",
"lightMainnet": "Light Mainnet",
"devnet": "Devnet",
"switch-network-type": "Cambiar a {{type}}"
"switch-network-type": "Cambiar a {{type}}",
"type": "Tipo",
"full-node": "Nodo Completo",
"light-client-node": "Nodo Cliente Ligero"
},
"locale": {
"en": "English",
Expand Down Expand Up @@ -1197,11 +1200,10 @@
"main": {
"external-node-detected-dialog": {
"title": "Nodo externo detectado",
"body-tips-without-network": "\"Internal Node\" está reservado para el nodo CKB incorporado, agregue una nueva opción de red para el nodo externo.",
"body-tips-with-network": "\"Internal Node\" está reservado para el nodo CKB incorporado, seleccione una de las siguientes opciones de red o agregue una nueva para el nodo externo.",
"body-tips-without-network": "Has seleccionado el nodo interno pero has iniciado el nodo externo. Si deseas continuar usando el nodo externo, por favor añade una nueva red.",
"body-tips-with-network": "Actualmente has seleccionado un nodo interno pero has iniciado un nodo externo. Si necesitas continuar usando el nodo externo, por favor cambia a otra red externa o añade una nueva red.",
"add-network": "Agregar red",
"ignore-external-node": "Ignorar nodo externo",
"external-node-is-light": "Debido a las diferentes suposiciones de seguridad del cliente ligero, Neuron no admite nodos de cliente ligero externos. ¿Desea conectarse a un \"Light Client\" ?"
"ignore-external-node": "Ignorar nodo externo"
},
"no-disk-space-dialog": {
"tip": "La sincronización se ha detenido debido a falta de espacio en disco. <br /> Asigne más espacio en disco o migre los datos a otro disco.",
Expand Down
12 changes: 7 additions & 5 deletions packages/neuron-ui/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,10 @@
"lightTestnet": "Testnet léger",
"lightMainnet": "Mainnet léger",
"devnet": "Devnet",
"switch-network-type": "Basculer vers {{type}}"
"switch-network-type": "Basculer vers {{type}}",
"type": "Type",
"full-node": "Nœud Complet",
"light-client-node": "Nœud Client Léger"
},
"locale": {
"en": "English",
Expand Down Expand Up @@ -1207,11 +1210,10 @@
"main": {
"external-node-detected-dialog": {
"title": "Détection d'un noeud externe",
"body-tips-without-network": "\"Noeud interne\" est réservé au noeud CKB intégré, veuillez ajouter une nouvelle option réseau pour le noeud externe.",
"body-tips-with-network": "\"Noeud interne\" est réservé au noeud CKB intégré, veuillez sélectionner parmi les options réseau suivantes ou en ajouter une nouvelle pour le noeud externe.",
"body-tips-without-network": "Vous avez sélectionné le nœud interne mais démarré le nœud externe. Si vous souhaitez continuer à utiliser le nœud externe, veuillez ajouter un nouveau réseau.",
"body-tips-with-network": "Vous avez actuellement sélectionné un nœud interne mais démarré un nœud externe. Si vous avez besoin de continuer à utiliser le nœud externe, veuillez passer à un autre réseau externe ou ajouter un nouveau réseau.",
"add-network": "Ajouter un réseau",
"ignore-external-node": "Ignorer le noeud externe",
"external-node-is-light": "En raison de différentes hypothèses de sécurité du client léger, Neuron ne prend pas en charge les nœuds clients légers externes. Voulez-vous vous connecter à un \"Light Client\" ?"
"ignore-external-node": "Ignorer le noeud externe"
},
"no-disk-space-dialog": {
"tip": "En raison d'un espace disque insuffisant, la synchronisation a été interrompue. <br /> Veuillez allouer plus d'espace disque ou migrer les données vers un autre disque.",
Expand Down
12 changes: 7 additions & 5 deletions packages/neuron-ui/src/locales/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@
"testnet": "測試網",
"lightMainnet": "輕節點主網",
"devnet": "開發網",
"switch-network-type": "切換到 {{type}}"
"switch-network-type": "切換到 {{type}}",
"type": "類型",
"full-node": "全節點",
"light-client-node": "輕客戶端節點"
},
"locale": {
"en": "English",
Expand Down Expand Up @@ -1210,11 +1213,10 @@
"main": {
"external-node-detected-dialog": {
"title": "檢測到外部節點",
"body-tips-without-network": "\"Internal Node\" 僅用於連接內置節點,請添加新的網絡選項以連接外部節點。",
"body-tips-with-network": "\"Internal Node\" 僅用於連接內置節點,請選擇以下網絡選項或添加新的網絡選項以連接外部節點。",
"body-tips-without-network": "您選擇了內部節點,但啟動了外部節點。如果您想繼續使用外部節點,請添加一個新網絡",
"body-tips-with-network": "您當前選擇了內部節點,但啟動了外部節點。如果需要繼續使用外部節點,請切換到另一個外部網絡或添加一個新網絡",
"add-network": "添加網絡",
"ignore-external-node": "忽略外部節點",
"external-node-is-light": "由於輕客戶端的安全假設不同,Neuron 不支持外部輕客戶端節點。您想連接到 \"Light Client\" 嗎?"
"ignore-external-node": "忽略外部節點"
},
"no-disk-space-dialog": {
"tip": "由於磁盤空間不足,同步已停止。 <br /> 請分配更多磁盤空間或將數據遷移到其他磁盤。",
Expand Down
12 changes: 7 additions & 5 deletions packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,10 @@
"lightTestnet": "轻节点测试网",
"lightMainnet": "轻节点主网",
"devnet": "开发网",
"switch-network-type": "切换到 {{type}}"
"switch-network-type": "切换到 {{type}}",
"type": "类型",
"full-node": "全节点",
"light-client-node": "轻客户端节点"
},
"locale": {
"en": "English",
Expand Down Expand Up @@ -1209,11 +1212,10 @@
"main": {
"external-node-detected-dialog": {
"title": "检测到外部节点",
"body-tips-without-network": "\"Internal Node\" 仅用于连接内置节点,请添加新的网络选项以连接外部节点。",
"body-tips-with-network": "\"Internal Node\" 仅用于连接内置节点,请选择以下网络选项或添加新的网络选项以连接外部节点。",
"body-tips-without-network": "您选择了内部节点,但启动了外部节点。如果您想继续使用外部节点,请添加一个新网络",
"body-tips-with-network": "您当前选择了内部节点,但启动了外部节点。如果需要继续使用外部节点,请切换到另一个外部网络或添加一个新网络",
"add-network": "添加网络",
"ignore-external-node": "忽略外部节点",
"external-node-is-light": "由于轻客户端的安全假设不同,Neuron 不支持外部轻客户端节点。您想连接到 \"Light Client\" 吗?"
"ignore-external-node": "忽略外部节点"
},
"no-disk-space-dialog": {
"tip": "由于磁盘空间不足,同步已停止。 <br /> 请分配更多磁盘空间或将数据迁移到其他磁盘。",
Expand Down
9 changes: 8 additions & 1 deletion packages/neuron-ui/src/types/Controller/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,21 @@ declare namespace Controller {
description: string
}

enum NetworkType {
Default, // internal full node
Normal,
Light, // internal Light node
}

interface CreateNetworkParams {
name: string
remote: string
type: NetworkType
}

interface UpdateNetworkParams {
networkID: string
options: Partial<{ name: string; remote: string }>
options: Partial<{ name: string; remote: string; type: NetworkType }>
}

interface UpdateTransactionDescriptionParams {
Expand Down
Loading

1 comment on commit dbf0027

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

Please sign in to comment.