Skip to content
Closed
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
3 changes: 2 additions & 1 deletion src/components/common/RenameNodeModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { initChartFromNetwork } from 'utils/chart';
import { defaultRepoState } from 'utils/constants';
import { createNetwork } from 'utils/network';
import {
bitcoinServiceMock,
injections,
lightningServiceMock,
bitcoinServiceMock,
litdServiceMock,
renderWithProviders,
tapServiceMock,
Expand Down Expand Up @@ -41,6 +41,7 @@ describe('RenameNodeModal', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const initialState = {
network: {
Expand Down
15 changes: 10 additions & 5 deletions src/components/designer/bitcoin/actions/MineBlocksInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import styled from '@emotion/styled';
import { Button, Form, Input, InputNumber } from 'antd';
import { usePrefixedTranslation } from 'hooks';
import { BitcoinNode } from 'shared/types';
import { useStoreActions } from 'store';
import { useStoreActions, useStoreState } from 'store';

const InputGroup = Input.Group;

Expand All @@ -20,12 +20,17 @@ const Styled = {

const MineBlocksInput: React.FC<{ node: BitcoinNode }> = ({ node }) => {
const { l } = usePrefixedTranslation('cmps.designer.bitcoind.MineBlocksInput');
const [value, setValue] = useState(6);
const network = useStoreState(s => s.network.networkById(node.networkId));
const { notify } = useStoreActions(s => s.app);
const { mine } = useStoreActions(s => s.bitcoin);
const { updateManualMineCount } = useStoreActions(actions => actions.network);

const [count, setCount] = useState(network.manualMineCount);

const mineAsync = useAsyncCallback(async () => {
try {
await mine({ blocks: value, node });
await mine({ blocks: count, node });
await updateManualMineCount({ id: node.networkId, count });
} catch (error: any) {
notify({ message: l('error'), error });
}
Expand All @@ -35,9 +40,9 @@ const MineBlocksInput: React.FC<{ node: BitcoinNode }> = ({ node }) => {
<Form.Item label={l('label')}>
<InputGroup compact>
<Styled.InputNumber
value={value}
value={count}
max={1000}
onChange={v => setValue(parseInt(v as any))}
onChange={v => setCount(parseInt(v as any))}
disabled={mineAsync.loading}
/>
<Styled.Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { initChartFromNetwork } from 'utils/chart';
import { defaultRepoState } from 'utils/constants';
import { createNetwork } from 'utils/network';
import {
bitcoinServiceMock,
renderWithProviders,
suppressConsoleErrors,
testManagedImages,
bitcoinServiceMock,
} from 'utils/tests';
import SendOnChainModal from './SendOnChainModal';

Expand All @@ -31,6 +31,7 @@ describe('SendOnChainModal', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});

const initialState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ describe('CreateInvoiceModal', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const asset: LightningNodeChannelAsset = {
id: 'abcd',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ describe('OpenChannelModal', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const asset: LightningNodeChannelAsset = {
id: 'abcd',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ describe('PayInvoiceModal', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const asset: LightningNodeChannelAsset = {
id: 'abcd',
Expand Down
1 change: 1 addition & 0 deletions src/components/network/ImportNetwork.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('ImportNetwork component', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
chart = initChartFromNetwork(network);
filesMock.read.mockResolvedValue(JSON.stringify({ network, chart }));
Expand Down
1 change: 1 addition & 0 deletions src/lib/docker/composeFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('ComposeFile', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const btcNode = network.nodes.bitcoin[0];
const lndNode = network.nodes.lightning[0] as LndNode;
Expand Down
9 changes: 9 additions & 0 deletions src/lib/docker/dockerService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe('DockerService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
net.nodes.lightning[0].backendName = 'invalid';
dockerService.saveComposeFile(net);
Expand All @@ -217,6 +218,7 @@ describe('DockerService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
net.nodes.lightning[0].backendName = 'invalid';
dockerService.saveComposeFile(net);
Expand All @@ -242,6 +244,7 @@ describe('DockerService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
net.nodes.lightning[0].backendName = 'invalid';
dockerService.saveComposeFile(net);
Expand Down Expand Up @@ -308,6 +311,7 @@ describe('DockerService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
dockerService.saveComposeFile(net);
const { backendName } = net.nodes.lightning[0] as LitdNode;
Expand All @@ -331,6 +335,7 @@ describe('DockerService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const litdNode = net.nodes.lightning[0] as LitdNode;
litdNode.backendName = 'invalid';
Expand All @@ -357,6 +362,7 @@ describe('DockerService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
net.nodes.lightning[0].implementation = 'unknown' as any;
dockerService.saveComposeFile(net);
Expand Down Expand Up @@ -392,6 +398,7 @@ describe('DockerService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const chart = initChartFromNetwork(net);
// return 'any' to suppress "The operand of a 'delete' operator must be optional.ts(2790)" error
Expand Down Expand Up @@ -522,6 +529,7 @@ describe('DockerService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const chart = initChartFromNetwork(net);
const fileData: NetworksFile = {
Expand Down Expand Up @@ -716,6 +724,7 @@ describe('DockerService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
composeMock.upAll.mockResolvedValue(mockResult);
await dockerService.start(net);
Expand Down
1 change: 1 addition & 0 deletions src/lib/litd/litdProxyClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('LitdProxyClient', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const node = network.nodes.lightning[0] as LitdNode;
let actualIpc: IpcSender;
Expand Down
1 change: 1 addition & 0 deletions src/lib/litd/litdService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('LitdService', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const node = network.nodes.lightning[0] as LitdNode;

Expand Down
4 changes: 4 additions & 0 deletions src/store/models/designer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe('Designer model', () => {
tapdNodes: 0,
litdNodes: 0,
customNodes: {},
manualMineCount: 6,
});
store.getActions().designer.setActiveId(firstNetwork().id);
const { removeChart } = store.getActions().designer;
Expand Down Expand Up @@ -581,6 +582,7 @@ describe('Designer model', () => {
tapdNodes: 0,
litdNodes: 0,
customNodes: {},
manualMineCount: 6,
});
const newId = store.getState().network.networks[1].id;
setActiveId(newId);
Expand Down Expand Up @@ -610,6 +612,7 @@ describe('Designer model', () => {
tapdNodes: 0,
litdNodes: 0,
customNodes: {},
manualMineCount: 6,
});
const newId = store.getState().network.networks[1].id;
setActiveId(newId);
Expand Down Expand Up @@ -647,6 +650,7 @@ describe('Designer model', () => {
tapdNodes: 0,
litdNodes: 0,
customNodes: {},
manualMineCount: 0,
});
const newId = store.getState().network.networks[1].id;
setActiveId(newId);
Expand Down
1 change: 1 addition & 0 deletions src/store/models/lit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('LIT Model', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
const initialState = {
network: {
Expand Down
37 changes: 37 additions & 0 deletions src/store/models/network.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('Network model', () => {
tapdNodes: 0,
litdNodes: 1,
customNodes: {},
manualMineCount: 6,
};

beforeEach(() => {
Expand Down Expand Up @@ -1023,6 +1024,42 @@ describe('Network model', () => {
});
});

describe('ManualMineCount', () => {
beforeEach(async () => {
await store.getActions().network.addNetwork(addNetworkArgs);
});

it('should set manual mine count for a network', () => {
const { setManualMineCount } = store.getActions().network;
const networkId = firstNetwork().id;

expect(firstNetwork().manualMineCount).toBe(6);
setManualMineCount({ id: networkId, count: 10 });
expect(firstNetwork().manualMineCount).toBe(10);
});

it('should fail to set manual mine count with invalid network id', () => {
const { setManualMineCount } = store.getActions().network;
expect(() => setManualMineCount({ id: 999, count: 10 })).toThrow(
"Network with the id '999' was not found.",
);
});

it('should update manual mine count and persist changes', async () => {
const { updateManualMineCount } = store.getActions().network;
const networkId = firstNetwork().id;
await updateManualMineCount({ id: networkId, count: 15 });
expect(firstNetwork().manualMineCount).toBe(15);
});

it('should fail to update manual mine count with invalid network id', () => {
const { updateManualMineCount } = store.getActions().network;
expect(() => updateManualMineCount({ id: 999, count: 10 })).rejects.toThrow(
"Network with the id '999' was not found.",
);
});
});

describe('Other actions', () => {
it('should remove a network', async () => {
expect(store.getState().network.networks).toHaveLength(0);
Expand Down
23 changes: 23 additions & 0 deletions src/store/models/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface AddNetworkArgs {
tapdNodes: number;
litdNodes: number;
customNodes: Record<string, number>;
manualMineCount: number;
}

export interface AutoMinerModel {
Expand Down Expand Up @@ -192,6 +193,14 @@ export interface NetworkModel {
setAutoMineMode: Action<NetworkModel, { id: number; mode: AutoMineMode }>;
setMiningState: Action<NetworkModel, { id: number; mining: boolean }>;
mineBlock: Thunk<NetworkModel, { id: number }, StoreInjections, RootModel>;
setManualMineCount: Action<NetworkModel, { id: number; count: number }>;
updateManualMineCount: Thunk<
NetworkModel,
{ id: number; count: number },
StoreInjections,
RootModel,
Promise<void>
>;
}

const networkModel: NetworkModel = {
Expand Down Expand Up @@ -286,6 +295,7 @@ const networkModel: NetworkModel = {
managedImages: computedManagedImages,
customImages,
basePorts: settings.basePorts,
manualMineCount: 6,
});
actions.add(network);
const { networks } = getState();
Expand Down Expand Up @@ -1074,6 +1084,19 @@ const networkModel: NetworkModel = {
}
},
),
setManualMineCount: action((state, { id, count }) => {
const network = state.networks.find(n => n.id === id);
if (!network) throw new Error(l('networkByIdErr', { networkId: id }));
network.manualMineCount = count;
}),
updateManualMineCount: thunk(async (actions, { id, count }, { getState }) => {
const networks = getState().networks;
const network = networks.find(n => n.id === id);
if (!network) throw new Error(l('networkByIdErr', { networkId: id }));

actions.setManualMineCount({ id, count });
await actions.save();
}),
};

export default networkModel;
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface Network {
lightning: LightningNode[];
tap: TapNode[];
};
manualMineCount: number;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/utils/network.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ describe('Network Utils', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
});

Expand Down Expand Up @@ -507,6 +508,7 @@ describe('Network Utils', () => {
repoState: defaultRepoState,
managedImages: testManagedImages,
customImages: [],
manualMineCount: 6,
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ export const createNetwork = (config: {
customImages: { image: CustomImage; count: number }[];
status?: Status;
basePorts?: NodeBasePorts;
manualMineCount: number;
}): Network => {
const {
id,
Expand All @@ -471,6 +472,7 @@ export const createNetwork = (config: {
managedImages,
customImages,
basePorts,
manualMineCount,
} = config;
// need explicit undefined check because Status.Starting is 0
const status = config.status !== undefined ? config.status : Status.Stopped;
Expand All @@ -487,6 +489,7 @@ export const createNetwork = (config: {
tap: [],
},
autoMineMode: AutoMineMode.AutoOff,
manualMineCount,
};

const { bitcoin, lightning } = network.nodes;
Expand Down
Loading
Loading