This commit is contained in:
hatim boufnichel 2024-04-25 19:37:36 +02:00
parent 4ac236d56e
commit 4cf983342d
4 changed files with 19 additions and 168 deletions

View file

@ -1,144 +1,27 @@
import { LoadTestSettingsFromEnv } from "../services/main/settings.js"
import { LoadTestSettingsFromEnv, TestSettings } from "../services/main/settings.js" import { BitcoinCoreWrapper } from "./bitcoinCore.js"
import LND from '../services/lnd/lnd.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 () => { export const setupNetwork = async () => {
const settings = LoadTestSettingsFromEnv() const settings = LoadTestSettingsFromEnv()
const core = new BitcoinCoreWrapper(settings) const core = new BitcoinCoreWrapper(settings)
await core.Init() await core.InitAddress()
const instances = await initLndInstances(settings) await core.Mine(1)
const { alice, bob, carol, dave } = instances const lnd = new LND(settings.lndSettings, () => { }, () => { }, () => { }, () => { })
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 addresses = await getAddresses(instances)
await sendCoinsToAddresses(core, addresses)
console.log("done sending coins, opening channels")
await openChannels(core, instances, connectInfo, addresses)
console.log("done opening channels, sending balancing payment")
console.log(await getAllBalances(instances))
await sendBalancingPayment(instances)
stopAllInstances(instances)
console.log("network setup complete, starting tests")
}
const sendBalancingPayment = async (instances: LndInstances) => {
const invoice = await instances.dave.NewInvoice(50_000, "balancing_payment", 3600)
const payment = await instances.carol.PayInvoice(invoice.payRequest, 0, 50_000)
console.log({ payment })
}
const openChannels = async (core: BitcoinCoreWrapper, instances: LndInstances, info: InstancesInfo, addresses: Addresses) => {
await openChannel(instances.bob, info.carol.pubkey, addresses.bob)
await openChannel(instances.carol, info.alice.pubkey, addresses.carol)
await openChannel(instances.alice, info.dave.pubkey, addresses.alice)
await slowMine(core, 6)
}
const openChannel = async (instance: LND, to: string, closeAddr: string) => {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
try { try {
await instance.OpenChannel(to, closeAddr, 10_000_000, 0) const info = await lnd.GetInfo()
console.log("success opening channel") if (!info.syncedToChain) {
throw new Error("not synced to chain")
}
if (!info.syncedToGraph) {
throw new Error("not synced to graph")
}
return return
} catch (e) { } catch (e) {
await new Promise((resolve) => setTimeout(resolve, 1000)) console.log("waiting for lnd to be ready")
console.log("error opening channel", e) await new Promise(resolve => setTimeout(resolve, 1000))
} }
} }
throw new Error("could not open channel after 10 tries") throw new Error("lnd is not ready after 10 seconds")
}
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 slowMine = async (core: BitcoinCoreWrapper, blocks: number) => {
for (let i = 0; i < blocks; i++) {
await core.Mine(1)
await new Promise((resolve) => setTimeout(resolve, 500))
}
}
const getAddresses = async (instances: LndInstances): Promise<Addresses> => {
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 getAllBalances = async (instances: LndInstances) => {
const aliceBal = await instances.alice.GetBalance()
const bobBal = await instances.bob.GetBalance()
const carolBal = await instances.carol.GetBalance()
const daveBal = await instances.dave.GetBalance()
return { aliceBal, bobBal, carolBal, daveBal }
}
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<LndInstances> => {
const alice = new LND(settings.lndSettings, console.log, console.log, () => { }, () => { })
await alice.Warmup()
const bobSettings = { ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }
const bob = new LND(bobSettings, console.log, console.log, () => { }, () => { })
await bob.Warmup()
const carolSettings = { ...settings.lndSettings, mainNode: settings.lndSettings.thirdNode }
const carol = new LND(carolSettings, console.log, console.log, () => { }, () => { })
await carol.Warmup()
const daveSettings = { ...settings.lndSettings, mainNode: settings.lndSettings.fourthNode }
const dave = new LND(daveSettings, console.log, console.log, () => { }, () => { })
await dave.Warmup()
return { alice, bob, carol, dave }
}
const stopAllInstances = (instances: LndInstances) => {
instances.alice.Stop()
instances.bob.Stop()
instances.carol.Stop()
instances.dave.Stop()
} }

View file

@ -1,27 +0,0 @@
import { LoadTestSettingsFromEnv } from "../services/main/settings.js"
import { BitcoinCoreWrapper } from "./bitcoinCore.js"
import LND from '../services/lnd/lnd.js'
export const prepareNetwork = async () => {
const settings = LoadTestSettingsFromEnv()
const core = new BitcoinCoreWrapper(settings)
await core.InitAddress()
await core.Mine(1)
const lnd = new LND(settings.lndSettings, () => { }, () => { }, () => { }, () => { })
for (let i = 0; i < 10; i++) {
try {
const info = await lnd.GetInfo()
if (!info.syncedToChain) {
throw new Error("not synced to chain")
}
if (!info.syncedToGraph) {
throw new Error("not synced to graph")
}
return
} catch (e) {
console.log("waiting for lnd to be ready")
await new Promise(resolve => setTimeout(resolve, 1000))
}
}
throw new Error("lnd is not ready after 10 seconds")
}

View file

@ -1,6 +1,5 @@
import { globby } from 'globby' import { globby } from 'globby'
import { setupNetwork } from './networkSetup.js' import { setupNetwork } from './networkSetup.js'
import { prepareNetwork } from './prepareNetwork.js'
import { Describe, SetupTest, teardown, TestBase } from './testBase.js' import { Describe, SetupTest, teardown, TestBase } from './testBase.js'
@ -21,11 +20,7 @@ const getDescribe = (fileName: string): Describe => {
} }
} }
const start = async () => { const start = async () => {
if (process.argv[2] === 'setup_network') {
await setupNetwork() await setupNetwork()
} else {
await prepareNetwork()
}
const files = await globby(["**/*.spec.js", "!**/node_modules/**"]) const files = await globby(["**/*.spec.js", "!**/node_modules/**"])
const modules: { file: string, module: TestModule }[] = [] const modules: { file: string, module: TestModule }[] = []
let devModule = -1 let devModule = -1

View file

@ -1,7 +1,7 @@
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js' import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, SetupTest, TestBase } from './testBase.js' import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, SetupTest, TestBase } from './testBase.js'
export const ignore = false export const ignore = false
export const dev = true export const dev = false
export default async (T: TestBase) => { export default async (T: TestBase) => {
await safelySetUserBalance(T, T.user1, 2000) await safelySetUserBalance(T, T.user1, 2000)
await testSuccessfulU2UPayment(T) await testSuccessfulU2UPayment(T)