Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitcoin node config + types refactor #174

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 10 additions & 1 deletion apps/guardian-ui/src/GuardianApi.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { JsonRpcError, JsonRpcWebsocket } from 'jsonrpc-client-websocket';
import { ConfigGenParams, ConsensusState, PeerHashMap } from './setup/types';
import {
AuditSummary,
ConfigResponse,
FederationStatus,
ServerStatus,
StatusResponse,
Versions,
ConfigGenParams,
ConsensusState,
PeerHashMap,
ModulesConfigResponse,
} from './types';

export interface SocketAndAuthInterface {
Expand Down Expand Up @@ -208,6 +211,7 @@ enum AdminRpc {
federationStatus = 'consensus_status',
inviteCode = 'invite_code',
config = 'config',
modulesConfig = 'modules_config_json',
module = 'module',
audit = 'audit',
}
Expand All @@ -224,6 +228,7 @@ export interface AdminApiInterface extends SharedApiInterface {
inviteCode: () => Promise<string>;
config: (connection: string) => Promise<ConfigResponse>;
audit: () => Promise<AuditSummary>;
modulesConfig: () => Promise<ModulesConfigResponse>;
moduleApiCall: <T>(moduleId: number, rpc: ModuleRpc) => Promise<T>;
}

Expand Down Expand Up @@ -373,6 +378,10 @@ export class GuardianApi
return this.base.call<AuditSummary>(AdminRpc.audit);
};

modulesConfig = () => {
return this.base.call<ModulesConfigResponse>(AdminRpc.modulesConfig);
};

moduleApiCall = <T>(moduleId: number, rpc: ModuleRpc): Promise<T> => {
const method = `${AdminRpc.module}_${moduleId}_${rpc}`;
return this.base.call_any_method<T>(method);
Expand Down
10 changes: 8 additions & 2 deletions apps/guardian-ui/src/admin/FederationAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Flex, Box, Icon, Text, useTheme, Heading } from '@chakra-ui/react';
import { CopyInput } from '@fedimint/ui';
import { useTranslation } from '@fedimint/utils';
import { useAdminContext } from '../hooks';
import { ConfigResponse, StatusResponse } from '../types';
import {
ConfigResponse,
ModulesConfigResponse,
StatusResponse,
} from '../types';
import { GatewaysCard } from '../components/GatewaysCard';
import { ReactComponent as CopyIcon } from '../assets/svgs/copy.svg';
import { GuardiansCard } from '../components/GuardiansCard';
Expand All @@ -17,12 +21,14 @@ export const FederationAdmin: React.FC = () => {
const [status, setStatus] = useState<StatusResponse>();
const [inviteCode, setInviteCode] = useState<string>('');
const [config, setConfig] = useState<ConfigResponse>();
const [modulesConfigs, setModulesConfigs] = useState<ModulesConfigResponse>();
const { t } = useTranslation();

useEffect(() => {
// TODO: poll server status
api.status().then(setStatus).catch(console.error);
api.inviteCode().then(setInviteCode).catch(console.error);
api.modulesConfig().then(setModulesConfigs).catch(console.error);
}, [api]);

useEffect(() => {
Expand Down Expand Up @@ -69,7 +75,7 @@ export const FederationAdmin: React.FC = () => {
<FederationInfoCard status={status} />
<Flex w='100%' direction='column' gap={5}>
<BalanceCard />
<BitcoinNodeCard config={config} />
<BitcoinNodeCard modulesConfigs={modulesConfigs} />
</Flex>
</Flex>
<GuardiansCard status={status} config={config} />
Expand Down
31 changes: 23 additions & 8 deletions apps/guardian-ui/src/components/BitcoinNodeCard.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
import { Card, CardBody, CardHeader, Text } from '@chakra-ui/react';
import { Card, CardBody, CardHeader, Skeleton, Text } from '@chakra-ui/react';
import { useTranslation } from '@fedimint/utils';
import React, { useMemo } from 'react';
import { ConfigResponse } from '../types';
import { ModuleConfig, ModuleKind, ModulesConfigResponse } from '../types';
import { KeyValues } from '@fedimint/ui';

interface Props {
config: ConfigResponse | undefined;
modulesConfigs: ModulesConfigResponse | undefined;
}

export const BitcoinNodeCard: React.FC<Props> = () => {
export const BitcoinNodeCard: React.FC<Props> = ({ modulesConfigs }) => {
const { t } = useTranslation();

const walletConfig = modulesConfigs
? Object.values(modulesConfigs).find(
(config): config is ModuleConfig<ModuleKind.Wallet> =>
config.kind === ModuleKind.Wallet
)
: undefined;

// TODO: Populate values from config.client_config.modules.config
// It's currently mysteriously hex encoded
const keyValues = useMemo(
() => [
{
key: 'url',
label: t('federation-dashboard.bitcoin-node.url-label'),
value: t('common.unknown'),
value: walletConfig ? (
walletConfig.client_default_bitcoin_rpc?.url
) : (
<Skeleton height='24px' width='160px' />
),
},
{
key: 'blockHeight',
EthnTuttle marked this conversation as resolved.
Show resolved Hide resolved
label: t('federation-dashboard.bitcoin-node.block-height-label'),
value: t('common.unknown'),
label: t('federation-dashboard.bitcoin-node.network-label'),
value: walletConfig ? (
walletConfig.network
) : (
<Skeleton height='24px' width='100px' />
),
},
],
[t]
[walletConfig, t]
);

return (
Expand Down
2 changes: 1 addition & 1 deletion apps/guardian-ui/src/components/ConnectGuardians.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { CopyInput, Table, TableRow } from '@fedimint/ui';
import { useTranslation } from '@fedimint/utils';
import { useConsensusPolling, useSetupContext } from '../hooks';
import { ModuleKind, ServerStatus } from '../types';
import { GuardianRole } from '../setup/types';
import { GuardianRole } from '../types';
import { getModuleParamsFromConfig } from '../utils/api';
import { ReactComponent as CopyIcon } from '../assets/svgs/copy.svg';

Expand Down
2 changes: 1 addition & 1 deletion apps/guardian-ui/src/components/RoleSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useMemo, useState } from 'react';
import { Button, VStack, Icon } from '@chakra-ui/react';
import { RadioButtonGroup, RadioButtonOption } from '@fedimint/ui';
import { GuardianRole, SETUP_ACTION_TYPE } from '../setup/types';
import { GuardianRole, SETUP_ACTION_TYPE } from '../types';
import { ReactComponent as ArrowRightIcon } from '../assets/svgs/arrow-right.svg';
import { ReactComponent as CheckIcon } from '../assets/svgs/check.svg';
import { ReactComponent as StarsIcon } from '../assets/svgs/stars.svg';
Expand Down
7 changes: 1 addition & 6 deletions apps/guardian-ui/src/components/SetConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ import {
import { useTranslation } from '@fedimint/utils';
import { FormGroup, FormGroupHeading } from '@fedimint/ui';
import { useSetupContext } from '../hooks';
import {
BitcoinRpc,
ConfigGenParams,
GuardianRole,
Network,
} from '../setup/types';
import { BitcoinRpc, ConfigGenParams, GuardianRole, Network } from '../types';
import { ReactComponent as FedimintLogo } from '../assets/svgs/fedimint.svg';
import { ReactComponent as BitcoinLogo } from '../assets/svgs/bitcoin.svg';
import { ReactComponent as ArrowRightIcon } from '../assets/svgs/arrow-right.svg';
Expand Down
2 changes: 1 addition & 1 deletion apps/guardian-ui/src/components/SetupProgress.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Box, Center, Flex, Text, useTheme } from '@chakra-ui/react';
import { ReactComponent as CheckIcon } from '../assets/svgs/white-check.svg';
import { StepState } from '../setup/types';
import { StepState } from '../types';

interface StepProps {
text: string;
Expand Down
2 changes: 1 addition & 1 deletion apps/guardian-ui/src/components/VerifyGuardians.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '@chakra-ui/react';
import { CopyInput, FormGroup, Table } from '@fedimint/ui';
import { useConsensusPolling, useSetupContext } from '../hooks';
import { GuardianRole, Peer } from '../setup/types';
import { GuardianRole, Peer } from '../types';
import { ReactComponent as ArrowRightIcon } from '../assets/svgs/arrow-right.svg';
import { ReactComponent as CopyIcon } from '../assets/svgs/copy.svg';
import { formatApiErrorMessage } from '../utils/api';
Expand Down
2 changes: 1 addition & 1 deletion apps/guardian-ui/src/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"bitcoin-node": {
"label": "Bitcoin Node",
"url-label": "URL",
"block-height-label": "Current block height"
"network-label": "Network"
},
"guardians": {
"label": "Guardians",
Expand Down
2 changes: 1 addition & 1 deletion apps/guardian-ui/src/setup/FederationSetup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Box, Button, Text, Heading, Icon, VStack } from '@chakra-ui/react';
import { ReactComponent as ArrowLeftIcon } from '../assets/svgs/arrow-left.svg';
import { useTranslation } from '@fedimint/utils';
import { useSetupContext } from '../hooks';
import { GuardianRole, SetupProgress, SETUP_ACTION_TYPE } from './types';
import { GuardianRole, SetupProgress, SETUP_ACTION_TYPE } from '../types';
import { RoleSelector } from '../components/RoleSelector';
import { SetConfiguration } from '../components/SetConfiguration';
import { ConnectGuardians } from '../components/ConnectGuardians';
Expand Down
4 changes: 2 additions & 2 deletions apps/guardian-ui/src/setup/SetupContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
SETUP_ACTION_TYPE,
SetupProgress,
ConfigGenParams,
} from './types';
import { ServerStatus } from '../types';
ServerStatus,
} from '../types';
import { GuardianApi } from '../GuardianApi';

const LOCAL_STORAGE_SETUP_KEY = 'setup-guardian-ui-state';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export interface PeerStatus {
flagged: boolean;
}

export interface Peer {
name: string;
cert: string;
api_url: string;
p2p_url: string;
status: ServerStatus;
}

export interface FederationStatus {
session_count: number;
peers_online: number;
Expand Down Expand Up @@ -113,47 +121,16 @@ interface RouteHint {
src_node_id: string;
}

export enum Status {
Loading,
Setup,
Admin,
}

export interface AppState {
status: Status;
needsAuth: boolean;
initServerStatus?: ServerStatus;
appError?: string;
}

export enum APP_ACTION_TYPE {
SET_STATUS = 'SET_STATUS',
SET_NEEDS_AUTH = 'SET_NEEDS_AUTH',
SET_INIT_SERVER_STATUS = 'SET_INIT_SERVER_STATUS',
SET_ERROR = 'SET_ERROR',
export enum Network {
Testnet = 'testnet',
Mainnet = 'bitcoin',
Regtest = 'regtest',
Signet = 'signet',
}

export type AppAction =
| {
type: APP_ACTION_TYPE.SET_STATUS;
payload: Status;
}
| {
type: APP_ACTION_TYPE.SET_NEEDS_AUTH;
payload: boolean;
}
| {
type: APP_ACTION_TYPE.SET_INIT_SERVER_STATUS;
payload: ServerStatus | undefined;
}
| {
type: APP_ACTION_TYPE.SET_ERROR;
payload: string | undefined;
};

export interface InitializationState {
needsAuth: boolean;
serverStatus: ServerStatus;
export interface BitcoinRpc {
kind: string;
url: string;
}

export interface ModuleSummary {
Expand All @@ -164,3 +141,41 @@ export interface AuditSummary {
net_assets: MSats;
module_summaries: Record<string, ModuleSummary | undefined>;
}

// Consider sharing these types with types/setup.ts *ModuleParams? Need to
// confirm that setup API returns identical types to the admin API.
interface ModuleConfigs {
[ModuleKind.Ln]: {
network: Network;
fee_consensus: {
contract_input: number;
contract_output: number;
};
};
[ModuleKind.Mint]: {
fee_consensus: {
note_issuance_abs: number;
note_spend_abs: number;
};
max_notes_per_denomination: number;
peer_tbs_pks: Record<number, Record<number, string>>;
};
[ModuleKind.Wallet]: {
client_default_bitcoin_rpc: BitcoinRpc;
default_fee: { sats_per_kvb: number };
fee_consensus: {
peg_in_abs: number;
peg_out_abs: number;
};
finality_delay: number;
network: Network;
peer_peg_in_keys: Record<number, { key: string }>;
peg_in_descriptor: number;
};
}

export type ModuleConfig<T extends ModuleKind = ModuleKind> = {
kind: T;
} & ModuleConfigs[T];

export type ModulesConfigResponse = Record<string, ModuleConfig>;
39 changes: 39 additions & 0 deletions apps/guardian-ui/src/types/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ServerStatus } from './api';

export enum Status {
Loading,
Setup,
Admin,
}

export interface AppState {
status: Status;
needsAuth: boolean;
initServerStatus?: ServerStatus;
appError?: string;
}

export enum APP_ACTION_TYPE {
SET_STATUS = 'SET_STATUS',
SET_NEEDS_AUTH = 'SET_NEEDS_AUTH',
SET_INIT_SERVER_STATUS = 'SET_INIT_SERVER_STATUS',
SET_ERROR = 'SET_ERROR',
}

export type AppAction =
| {
type: APP_ACTION_TYPE.SET_STATUS;
payload: Status;
}
| {
type: APP_ACTION_TYPE.SET_NEEDS_AUTH;
payload: boolean;
}
| {
type: APP_ACTION_TYPE.SET_INIT_SERVER_STATUS;
payload: ServerStatus | undefined;
}
| {
type: APP_ACTION_TYPE.SET_ERROR;
payload: string | undefined;
};
3 changes: 3 additions & 0 deletions apps/guardian-ui/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './app';
export * from './api';
export * from './setup';
Loading