Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
feat: Fetch timelocks from RPC when new Bitcoin block is found
Browse files Browse the repository at this point in the history
This also changes the rpcSlice to store parsed logs
  • Loading branch information
binarybaron committed Jun 12, 2024
1 parent ecec68f commit d7b56b3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 47 deletions.
2 changes: 0 additions & 2 deletions src/main/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export async function stopCli() {
export async function spawnSubcommand(
subCommand: string,
options: { [option: string]: string },
onLog: (log: CliLog[]) => void,
onExit: (code: number | null, signal: NodeJS.Signals | null) => void,
onStdOut: (data: string) => void
): Promise<ChildProcessWithoutNullStreams> {
Expand Down Expand Up @@ -258,7 +257,6 @@ export async function startRPC() {
'server-address': `${RPC_BIND_HOST}:${RPC_BIND_PORT}`,
},
async (logs) => {
store.dispatch(rpcAddLogs(logs));
RPC_LOG_EVENT_EMITTER.emit(logs);

const processType = store.getState().rpc.process.type;
Expand Down
13 changes: 5 additions & 8 deletions src/main/cli/dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,11 @@ export default async function getSavedLogsOfSwapId(

const rpcProcess = store.getState().rpc.process;
const currentProcessLogs =
rpcProcess.type === RpcProcessStateType.NOT_STARTED
? ''
: rpcProcess.stdOut;
const rpcProcessLogs = getLogsFromRawFileString(currentProcessLogs).filter(
(log) => {
return getCliLogSpanSwapId(log) === swapId;
}
);
rpcProcess.type === RpcProcessStateType.NOT_STARTED ? [] : rpcProcess.logs;

const rpcProcessLogs = currentProcessLogs.filter((log): log is CliLog => {
return typeof log !== 'string' && getCliLogSpanSwapId(log) === swapId;
});

const allLogs = [...legacyLogs, ...logs, ...rpcProcessLogs];
const allLogsWithoutDuplicates = uniqBy(
Expand Down
20 changes: 19 additions & 1 deletion src/main/store/mainStoreListeners.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createListenerMiddleware } from '@reduxjs/toolkit';
import { getRawSwapInfo } from '../cli/rpc';
import { getRawSwapInfo, getRawSwapInfos } from '../cli/rpc';
import {
CliLog,
getCliLogSpanSwapId,
isCliLogAdvancingState,
isCliLogGotNotificationForNewBlock,
isCliLogReleasingSwapLockLog,
} from '../../models/cliModel';
import logger from '../../utils/logger';
Expand Down Expand Up @@ -40,5 +41,22 @@ export function createMainListeners() {
},
});

// Listener for "Got notification for new Block" log
// If we get a new block, we fetch all swap infos because the state of the timelocks might have changed
listener.startListening({
predicate: (action) => {
return action.type === 'rpc/rpcAddLog';
},
effect: async (action) => {
const logs = action.payload.logs as CliLog[];
const newBlockLog = logs.find(isCliLogGotNotificationForNewBlock);

if (newBlockLog) {
logger.debug('Fetching all swap infos because a new block was found');
await getRawSwapInfos();
}
},
});

return listener;
}
13 changes: 13 additions & 0 deletions src/models/cliModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,16 @@ export interface CliLogDownloadingMoneroWalletRpc extends CliLog {
download_url: string;
};
}

export interface CliLogGotNotificationForNewBlock extends CliLog {
fields: {
message: 'Got notification for new block';
block_height: string;
};
}

export function isCliLogGotNotificationForNewBlock(
log: CliLog
): log is CliLogGotNotificationForNewBlock {
return log.fields.message === 'Got notification for new block';
}
2 changes: 1 addition & 1 deletion src/renderer/components/pages/help/RpcControlBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function RpcControlBox() {
isRunning || rpcProcess.type === RpcProcessStateType.EXITED ? (
<CliLogsBox
label="Swap Daemon Logs (current session only)"
logs={getLogsAndStringsFromRawFileString(rpcProcess.stdOut)}
logs={rpcProcess.logs}
/>
) : null
}
Expand Down
71 changes: 36 additions & 35 deletions src/store/features/rpcSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ import {
} from '../../models/rpcModel';
import {
CliLog,
isCliLog,
isCliLogDownloadingMoneroWalletRpc,
isCliLogFailedToSyncMoneroWallet,
isCliLogFinishedSyncingMoneroWallet,
isCliLogStartedRpcServer,
isCliLogStartedSyncingMoneroWallet,
} from '../../models/cliModel';
import { getLogsAndStringsFromRawFileString } from 'utils/parseUtils';

type Process =
| {
type: RpcProcessStateType.STARTED;
stdOut: string;
logs: (CliLog | string)[];
}
| {
type: RpcProcessStateType.LISTENING_FOR_CONNECTIONS;
stdOut: string;
logs: (CliLog | string)[];
address: string;
}
| {
type: RpcProcessStateType.EXITED;
stdOut: string;
logs: (CliLog | string)[];
exitCode: number | null;
}
| {
Expand Down Expand Up @@ -88,42 +90,42 @@ export const rpcSlice = createSlice({
slice.process.type === RpcProcessStateType.STARTED ||
slice.process.type === RpcProcessStateType.LISTENING_FOR_CONNECTIONS
) {
slice.process.stdOut += action.payload;
}
},
rpcAddLogs(slice, action: PayloadAction<CliLog[]>) {
action.payload.forEach((log) => {
if (
isCliLogStartedRpcServer(log) &&
slice.process.type === RpcProcessStateType.STARTED
) {
slice.process = {
type: RpcProcessStateType.LISTENING_FOR_CONNECTIONS,
stdOut: slice.process.stdOut,
address: log.fields.addr,
};
} else if (isCliLogDownloadingMoneroWalletRpc(log)) {
slice.state.moneroWalletRpc.updateState = {
progress: log.fields.progress,
downloadUrl: log.fields.download_url,
};
const logs = getLogsAndStringsFromRawFileString(action.payload);
slice.process.logs.push(...logs);

if (log.fields.progress === '100%') {
slice.state.moneroWalletRpc.updateState = false;
logs.filter(isCliLog).forEach((log) => {
if (
isCliLogStartedRpcServer(log) &&
slice.process.type === RpcProcessStateType.STARTED
) {
slice.process = {
type: RpcProcessStateType.LISTENING_FOR_CONNECTIONS,
logs: slice.process.logs,
address: log.fields.addr,
};
} else if (isCliLogDownloadingMoneroWalletRpc(log)) {
slice.state.moneroWalletRpc.updateState = {
progress: log.fields.progress,
downloadUrl: log.fields.download_url,
};

if (log.fields.progress === '100%') {
slice.state.moneroWalletRpc.updateState = false;
}
} else if (isCliLogStartedSyncingMoneroWallet(log)) {
slice.state.moneroWallet.isSyncing = true;
} else if (isCliLogFinishedSyncingMoneroWallet(log)) {
slice.state.moneroWallet.isSyncing = false;
} else if (isCliLogFailedToSyncMoneroWallet(log)) {
slice.state.moneroWallet.isSyncing = false;
}
} else if (isCliLogStartedSyncingMoneroWallet(log)) {
slice.state.moneroWallet.isSyncing = true;
} else if (isCliLogFinishedSyncingMoneroWallet(log)) {
slice.state.moneroWallet.isSyncing = false;
} else if (isCliLogFailedToSyncMoneroWallet(log)) {
slice.state.moneroWallet.isSyncing = false;
}
});
});
}
},
rpcInitiate(slice) {
slice.process = {
type: RpcProcessStateType.STARTED,
stdOut: '',
logs: [],
};
},
rpcProcessExited(
Expand All @@ -139,7 +141,7 @@ export const rpcSlice = createSlice({
) {
slice.process = {
type: RpcProcessStateType.EXITED,
stdOut: slice.process.stdOut,
logs: slice.process.logs,
exitCode: action.payload.exitCode,
};
slice.state.moneroWalletRpc = {
Expand Down Expand Up @@ -200,7 +202,6 @@ export const rpcSlice = createSlice({
export const {
rpcProcessExited,
rpcAppendStdOut,
rpcAddLogs,
rpcInitiate,
rpcSetBalance,
rpcSetWithdrawTxId,
Expand Down

0 comments on commit d7b56b3

Please sign in to comment.