diff --git a/src/services/lnd/lnd.ts b/src/services/lnd/lnd.ts index 996ffd36..0b27d0fb 100644 --- a/src/services/lnd/lnd.ts +++ b/src/services/lnd/lnd.ts @@ -226,11 +226,9 @@ export default class { }) } - async NewAddress(addressType: Types.AddressType, skipHealthCheck = false): Promise { + async NewAddress(addressType: Types.AddressType): Promise { this.log("generating new address") - if (!skipHealthCheck) { - await this.Health() - } + await this.Health() let lndAddressType: AddressType switch (addressType) { case Types.AddressType.NESTED_PUBKEY_HASH: @@ -363,9 +361,9 @@ export default class { return res.response } - async ConnectPeer(pubkey: string, host: string) { + async ConnectPeer(addr: { pubkey: string, host: string }) { const res = await this.lightning.connectPeer({ - addr: { pubkey, host }, + addr, perm: true, timeout: 0n }, DeadLineMetadata()) diff --git a/src/tests/bitcoinCore.ts b/src/tests/bitcoinCore.ts new file mode 100644 index 00000000..bdac0b2a --- /dev/null +++ b/src/tests/bitcoinCore.ts @@ -0,0 +1,37 @@ +// @ts-ignore +import BitcoinCore from 'bitcoin-core'; +import { TestSettings } from '../services/main/settings'; +export class BitcoinCoreWrapper { + core: BitcoinCore + addr: { address: string } + constructor(settings: TestSettings) { + this.core = new BitcoinCore({ + //network: 'regtest', + host: '127.0.0.1', + port: `${settings.bitcoinCoreSettings.port}`, + username: settings.bitcoinCoreSettings.user, + password: settings.bitcoinCoreSettings.pass, + // use a long timeout due to the time it takes to mine a lot of blocks + timeout: 5 * 60 * 1000, + }) + } + + Init = async () => { + const wallet = await this.core.createWallet(''); + console.log({ wallet }) + this.addr = await this.core.getNewAddress() + console.log({ addr: this.addr }) + await this.Mine(101) + const info = await this.core.getWalletInfo(); + console.log({ info }) + } + + Mine = async (blocks: number) => { + await this.core.generateToAddress(blocks, this.addr) + } + + SendToAddress = async (address: string, amount: number) => { + const tx = await this.core.sendToAddress(address, amount) + console.log({ tx }) + } +} \ No newline at end of file diff --git a/src/tests/networkSetup.ts b/src/tests/networkSetup.ts index a9456324..d1a2dbe4 100644 --- a/src/tests/networkSetup.ts +++ b/src/tests/networkSetup.ts @@ -1,35 +1,89 @@ -// @ts-ignore -import BitcoinCore from 'bitcoin-core'; + import { LoadTestSettingsFromEnv, TestSettings } from "../services/main/settings.js" import LND from '../services/lnd/lnd.js' import { AddressType } from '../../proto/autogenerated/ts/types.js'; +import { BitcoinCoreWrapper } from "./bitcoinCore.js"; // dave <--> alice <--> carol <--> bob +type LndInstances = { alice: LND; bob: LND; carol: LND; dave: LND; } +type Addresses = { alice: string, bob: string, carol: string, dave: string } +type InstanceInfo = { pubkey: string; host: string; } +type InstancesInfo = { alice: InstanceInfo; bob: InstanceInfo; carol: InstanceInfo; dave: InstanceInfo; } export const setupNetwork = async () => { const settings = LoadTestSettingsFromEnv() - const core = new Core(settings) + const core = new BitcoinCoreWrapper(settings) await core.Init() - const { alice, bob, carol, dave } = await initLndInstances(settings) - - const aliceInfo = await alice.GetInfo() - const [alicePub, aliceHost] = aliceInfo.uris[0].split('@') - await carol.ConnectPeer(alicePub, aliceHost) - await dave.ConnectPeer(alicePub, aliceHost) - const carolInfo = await carol.GetInfo() - const [carolPub, carolHost] = carolInfo.uris[0].split('@') - await bob.ConnectPeer(carolPub, carolHost) + const instances = await initLndInstances(settings) + const { alice, bob, carol, dave } = instances + const connectInfo = await getConnectInfo(instances) + await bob.ConnectPeer(connectInfo.carol) + await carol.ConnectPeer(connectInfo.alice) + await alice.ConnectPeer(connectInfo.dave) console.log("done connecting peer, sending coins") - const aliceAddr = await alice.NewAddress(AddressType.WITNESS_PUBKEY_HASH, true) - const bobAddr = await bob.NewAddress(AddressType.WITNESS_PUBKEY_HASH) - const carolAddr = await carol.NewAddress(AddressType.WITNESS_PUBKEY_HASH) - const daveAddr = await dave.NewAddress(AddressType.WITNESS_PUBKEY_HASH) - await core.SendToAddress(aliceAddr.address, 10) - await core.SendToAddress(bobAddr.address, 10) - await core.SendToAddress(carolAddr.address, 10) - await core.SendToAddress(daveAddr.address, 10) + + const addresses = await getAddresses(instances) + await sendCoinsToAddresses(core, addresses) + console.log("done sending coins, opening channels") + await openChannels(instances, connectInfo, addresses) + console.log("done opening channels, sending balancing payment") + await sendBalancingPayment(instances) + stopAllInstances(instances) + console.log("network setup complete, starting tests") +} + +const sendBalancingPayment = async (instances: LndInstances) => { + const invoice = await instances.dave.NewInvoice(2_000_000, "balancing payment", 3600) + await instances.bob.PayInvoice(invoice.payRequest, 0, 1000) +} + +const openChannels = async (instances: LndInstances, info: InstancesInfo, addresses: Addresses) => { + await instances.bob.OpenChannel(info.carol.pubkey, addresses.bob, 5_000_000, 0) + await instances.carol.OpenChannel(info.alice.pubkey, addresses.carol, 5_000_000, 0) + await instances.alice.OpenChannel(info.dave.pubkey, addresses.alice, 5_000_000, 0) +} + +const sendCoinsToAddresses = async (core: BitcoinCoreWrapper, addresses: Addresses) => { + await core.SendToAddress(addresses.alice, 10) + await core.SendToAddress(addresses.bob, 10) + await core.SendToAddress(addresses.carol, 10) + await core.SendToAddress(addresses.dave, 10) await core.Mine(6) } -const initLndInstances = async (settings: TestSettings) => { +const getAddresses = async (instances: LndInstances): Promise => { + const alice = (await instances.alice.NewAddress(AddressType.WITNESS_PUBKEY_HASH)).address + const bob = (await instances.bob.NewAddress(AddressType.WITNESS_PUBKEY_HASH)).address + const carol = (await instances.carol.NewAddress(AddressType.WITNESS_PUBKEY_HASH)).address + const dave = (await instances.dave.NewAddress(AddressType.WITNESS_PUBKEY_HASH)).address + return { alice, bob, carol, dave } +} + +const getAllInfo = async (instances: LndInstances) => { + const aliceInfo = await instances.alice.GetInfo() + const bobInfo = await instances.bob.GetInfo() + const carolInfo = await instances.carol.GetInfo() + const daveInfo = await instances.dave.GetInfo() + return { aliceInfo, bobInfo, carolInfo, daveInfo } + +} + +const getConnectInfo = async (instances: LndInstances) => { + const { aliceInfo, bobInfo, carolInfo, daveInfo } = await getAllInfo(instances) + const [alicePub, aliceHost] = aliceInfo.uris[0].split('@') + const alice = { pubkey: alicePub, host: aliceHost } + + const [bobPub, bobHost] = bobInfo.uris[0].split('@') + const bob = { pubkey: bobPub, host: bobHost } + + const [carolPub, carolHost] = carolInfo.uris[0].split('@') + const carol = { pubkey: carolPub, host: carolHost } + + const [davePub, daveHost] = daveInfo.uris[0].split('@') + const dave = { pubkey: davePub, host: daveHost } + + return { alice, bob, carol, dave } +} + +const initLndInstances = async (settings: TestSettings): Promise => { const alice = new LND(settings.lndSettings, console.log, console.log, () => { }, () => { }) await alice.Warmup() @@ -46,37 +100,10 @@ const initLndInstances = async (settings: TestSettings) => { await dave.Warmup() return { alice, bob, carol, dave } } -class Core { - core: BitcoinCore - addr: { address: string } - constructor(settings: TestSettings) { - this.core = new BitcoinCore({ - //network: 'regtest', - host: '127.0.0.1', - port: `${settings.bitcoinCoreSettings.port}`, - username: settings.bitcoinCoreSettings.user, - password: settings.bitcoinCoreSettings.pass, - // use a long timeout due to the time it takes to mine a lot of blocks - timeout: 5 * 60 * 1000, - }) - } - Init = async () => { - const wallet = await this.core.createWallet(''); - console.log({ wallet }) - this.addr = await this.core.getNewAddress() - console.log({ addr: this.addr }) - await this.Mine(101) - const info = await this.core.getWalletInfo(); - console.log({ info }) - } - - Mine = async (blocks: number) => { - await this.core.generateToAddress(blocks, this.addr) - } - - SendToAddress = async (address: string, amount: number) => { - const tx = await this.core.sendToAddress(address, amount) - console.log({ tx }) - } +const stopAllInstances = (instances: LndInstances) => { + instances.alice.Stop() + instances.bob.Stop() + instances.carol.Stop() + instances.dave.Stop() }