Browse Source

feat(bitcoind): support custom rpc port for bitcoind nodes

feat/auto-update
jamaljsr 5 years ago
parent
commit
a8ec410fad
  1. 1
      TODO.md
  2. 5
      src/components/designer/bitcoind/MineBlocksInput.spec.tsx
  3. 16
      src/lib/bitcoin/bitcoindService.ts
  4. 8
      src/store/models/bitcoind.ts
  5. 6
      src/types/index.ts

1
TODO.md

@ -5,6 +5,7 @@ Small Stuff
- pull info from LND
- add new LND node
- open/close channel
- use dynamic ports for nodes to avoid conflicts
- switch renovatebot to dependabot and use automatic security fixes
- build images if they don't exist
- generate alice/bob/carol names for lnd nodes

5
src/components/designer/bitcoind/MineBlocksInput.spec.tsx

@ -45,11 +45,12 @@ describe('MineBlocksInput', () => {
it('should mine a block when the button is clicked', () => {
const mineMock = injections.bitcoindService.mine as jest.Mock;
mineMock.mockResolvedValue(true);
const { input, btn } = renderComponent();
const { input, btn, store } = renderComponent();
const numBlocks = 5;
fireEvent.change(input, { target: { value: numBlocks } });
fireEvent.click(btn);
expect(mineMock).toBeCalledWith(numBlocks);
const port = store.getState().network.networks[0].nodes.bitcoin[0].ports.rpc;
expect(mineMock).toBeCalledWith(numBlocks, port);
});
it('should display an error if mining fails', async () => {

16
src/lib/bitcoin/bitcoindService.ts

@ -2,24 +2,24 @@ import BitcoinCore from 'bitcoin-core';
import { BitcoindLibrary } from 'types';
class BitcoindService implements BitcoindLibrary {
creatClient() {
creatClient(port = 18433) {
return new BitcoinCore({
port: '18443',
port: `${port}`,
username: 'polaruser',
password: 'polarpass',
});
}
async getBlockchainInfo() {
return await this.creatClient().getBlockchainInfo();
async getBlockchainInfo(port?: number) {
return await this.creatClient(port).getBlockchainInfo();
}
async getWalletInfo() {
return await this.creatClient().getWalletInfo();
async getWalletInfo(port?: number) {
return await this.creatClient(port).getWalletInfo();
}
async mine(numBlocks: number) {
const client = this.creatClient();
async mine(numBlocks: number, port?: number) {
const client = this.creatClient(port);
const addr = await client.getNewAddress();
return await client.generateToAddress(numBlocks, addr);
}

8
src/store/models/bitcoind.ts

@ -21,14 +21,16 @@ const bitcoindModel: BitcoindModel = {
state.walletInfo = walletInfo;
}),
getInfo: thunk(async (actions, node, { injections }) => {
actions.setChainInfo(await injections.bitcoindService.getBlockchainInfo());
actions.setWalletinfo(await injections.bitcoindService.getWalletInfo());
actions.setChainInfo(
await injections.bitcoindService.getBlockchainInfo(node.ports.rpc),
);
actions.setWalletinfo(await injections.bitcoindService.getWalletInfo(node.ports.rpc));
}),
mine: thunk(async (actions, { blocks, node }, { injections }) => {
if (blocks < 0) {
throw new Error('The number of blocks to mine must be a positve number');
}
await injections.bitcoindService.mine(blocks);
await injections.bitcoindService.mine(blocks, node.ports.rpc);
await actions.getInfo(node);
}),
};

6
src/types/index.ts

@ -66,9 +66,9 @@ export interface DockerLibrary {
}
export interface BitcoindLibrary {
getBlockchainInfo: () => Promise<ChainInfo>;
getWalletInfo: () => Promise<WalletInfo>;
mine: (numBlocks: number) => Promise<string[]>;
getBlockchainInfo: (port?: number) => Promise<ChainInfo>;
getWalletInfo: (port?: number) => Promise<WalletInfo>;
mine: (numBlocks: number, port?: number) => Promise<string[]>;
}
export interface StoreInjections {

Loading…
Cancel
Save