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

fix: exclude gpu devices while mining #1560

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 3 additions & 7 deletions src/containers/floating/Settings/sections/mining/GpuDevices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ import { useMiningMetricsStore } from '@app/store/useMiningMetricsStore.ts';
const GpuDevices = () => {
const { t } = useTranslation(['common', 'settings'], { useSuspense: false });
const miningAllowed = useAppStateStore((s) => s.setupComplete);
const isCPUMining = useMiningMetricsStore((s) => s.cpu_mining_status.is_mining);
const isGPUMining = useMiningMetricsStore((s) => s.gpu_mining_status.is_mining);
const gpuDevices = useMiningMetricsStore((s) => s.gpu_devices);

const miningInitiated = useMiningStore((s) => s.miningInitiated);
const isGpuMiningEnabled = useAppConfigStore((s) => s.gpu_mining_enabled);
const isMiningInProgress = isCPUMining || isGPUMining;
const isDisabled = isMiningInProgress || miningInitiated || !miningAllowed || !isGpuMiningEnabled;
const excludedDevices = useMiningStore((s) => s.excludedGpuDevices);
const setExcludedDevice = useMiningStore((s) => s.setExcludedGpuDevice);
const isExcludingGpuDevices = useMiningStore((s) => s.isExcludingGpuDevices);

const handleSetExcludedDevice = useCallback(
async (device: number) => {
Expand Down Expand Up @@ -69,8 +65,8 @@ const GpuDevices = () => {
</Typography>
<ToggleSwitch
key={device.name}
checked={!excludedDevices.includes(i) && isGpuMiningEnabled}
disabled={isDisabled}
checked={!excludedDevices.includes(i)}
disabled={isExcludingGpuDevices || !miningAllowed || !isGpuMiningEnabled}
onChange={() => handleSetExcludedDevice(i)}
/>
</Stack>
Expand Down
22 changes: 18 additions & 4 deletions src/store/useMiningStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface State {
miningInitiated: boolean;
miningControlsEnabled: boolean;
isChangingMode: boolean;
isExcludingGpuDevices: boolean;
excludedGpuDevices: number[];
counter: number;
customLevelsDialogOpen: boolean;
Expand All @@ -35,6 +36,7 @@ const initialState: State = {
hashrateReady: false,
miningInitiated: false,
isChangingMode: false,
isExcludingGpuDevices: false,
miningControlsEnabled: true,
network: 'unknown',
excludedGpuDevices: [],
Expand Down Expand Up @@ -84,12 +86,18 @@ export const useMiningStore = create<MiningStoreState>()((set) => ({
};
}),
setExcludedGpuDevice: async (excludedGpuDevices) => {
const hardware = useMiningMetricsStore.getState().gpu_devices;
const totalGpuDevices = hardware.length;
console.error('Excluded GPU devices: ', excludedGpuDevices);
console.error('Hardware: ', hardware);
set({ isExcludingGpuDevices: true });
const metricsState = useMiningMetricsStore.getState();

if (metricsState.cpu_mining_status.is_mining || metricsState.gpu_mining_status.is_mining) {
console.info('Pausing mining...');
await pauseMining();
}

try {
await invoke('set_excluded_gpu_devices', { excludedGpuDevices });

const totalGpuDevices = useMiningMetricsStore.getState().gpu_devices?.length || 0;
if (excludedGpuDevices.length === totalGpuDevices) {
const appConfigStore = useAppConfigStore.getState();
appConfigStore.setGpuMiningEnabled(false);
Expand All @@ -101,5 +109,11 @@ export const useMiningStore = create<MiningStoreState>()((set) => ({
appStateStore.setError(e as string);
set({ excludedGpuDevices: undefined });
}

if (useMiningStore.getState().miningInitiated) {
console.info('Restarting mining...');
await startMining();
}
set({ isExcludingGpuDevices: false });
},
}));