up
This commit is contained in:
parent
d3e4cf9bf5
commit
e5ad3af986
3 changed files with 121 additions and 59 deletions
|
|
@ -226,11 +226,9 @@ export default class {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async NewAddress(addressType: Types.AddressType, skipHealthCheck = false): Promise<NewAddressResponse> {
|
async NewAddress(addressType: Types.AddressType): Promise<NewAddressResponse> {
|
||||||
this.log("generating new address")
|
this.log("generating new address")
|
||||||
if (!skipHealthCheck) {
|
|
||||||
await this.Health()
|
await this.Health()
|
||||||
}
|
|
||||||
let lndAddressType: AddressType
|
let lndAddressType: AddressType
|
||||||
switch (addressType) {
|
switch (addressType) {
|
||||||
case Types.AddressType.NESTED_PUBKEY_HASH:
|
case Types.AddressType.NESTED_PUBKEY_HASH:
|
||||||
|
|
@ -363,9 +361,9 @@ export default class {
|
||||||
return res.response
|
return res.response
|
||||||
}
|
}
|
||||||
|
|
||||||
async ConnectPeer(pubkey: string, host: string) {
|
async ConnectPeer(addr: { pubkey: string, host: string }) {
|
||||||
const res = await this.lightning.connectPeer({
|
const res = await this.lightning.connectPeer({
|
||||||
addr: { pubkey, host },
|
addr,
|
||||||
perm: true,
|
perm: true,
|
||||||
timeout: 0n
|
timeout: 0n
|
||||||
}, DeadLineMetadata())
|
}, DeadLineMetadata())
|
||||||
|
|
|
||||||
37
src/tests/bitcoinCore.ts
Normal file
37
src/tests/bitcoinCore.ts
Normal file
|
|
@ -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 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,35 +1,89 @@
|
||||||
// @ts-ignore
|
|
||||||
import BitcoinCore from 'bitcoin-core';
|
|
||||||
import { LoadTestSettingsFromEnv, TestSettings } from "../services/main/settings.js"
|
import { LoadTestSettingsFromEnv, TestSettings } from "../services/main/settings.js"
|
||||||
import LND from '../services/lnd/lnd.js'
|
import LND from '../services/lnd/lnd.js'
|
||||||
import { AddressType } from '../../proto/autogenerated/ts/types.js';
|
import { AddressType } from '../../proto/autogenerated/ts/types.js';
|
||||||
|
import { BitcoinCoreWrapper } from "./bitcoinCore.js";
|
||||||
// dave <--> alice <--> carol <--> bob
|
// 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 Core(settings)
|
const core = new BitcoinCoreWrapper(settings)
|
||||||
await core.Init()
|
await core.Init()
|
||||||
const { alice, bob, carol, dave } = await initLndInstances(settings)
|
const instances = await initLndInstances(settings)
|
||||||
|
const { alice, bob, carol, dave } = instances
|
||||||
const aliceInfo = await alice.GetInfo()
|
const connectInfo = await getConnectInfo(instances)
|
||||||
const [alicePub, aliceHost] = aliceInfo.uris[0].split('@')
|
await bob.ConnectPeer(connectInfo.carol)
|
||||||
await carol.ConnectPeer(alicePub, aliceHost)
|
await carol.ConnectPeer(connectInfo.alice)
|
||||||
await dave.ConnectPeer(alicePub, aliceHost)
|
await alice.ConnectPeer(connectInfo.dave)
|
||||||
const carolInfo = await carol.GetInfo()
|
|
||||||
const [carolPub, carolHost] = carolInfo.uris[0].split('@')
|
|
||||||
await bob.ConnectPeer(carolPub, carolHost)
|
|
||||||
console.log("done connecting peer, sending coins")
|
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 addresses = await getAddresses(instances)
|
||||||
const carolAddr = await carol.NewAddress(AddressType.WITNESS_PUBKEY_HASH)
|
await sendCoinsToAddresses(core, addresses)
|
||||||
const daveAddr = await dave.NewAddress(AddressType.WITNESS_PUBKEY_HASH)
|
console.log("done sending coins, opening channels")
|
||||||
await core.SendToAddress(aliceAddr.address, 10)
|
await openChannels(instances, connectInfo, addresses)
|
||||||
await core.SendToAddress(bobAddr.address, 10)
|
console.log("done opening channels, sending balancing payment")
|
||||||
await core.SendToAddress(carolAddr.address, 10)
|
await sendBalancingPayment(instances)
|
||||||
await core.SendToAddress(daveAddr.address, 10)
|
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)
|
await core.Mine(6)
|
||||||
}
|
}
|
||||||
|
|
||||||
const initLndInstances = async (settings: TestSettings) => {
|
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 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, () => { }, () => { })
|
const alice = new LND(settings.lndSettings, console.log, console.log, () => { }, () => { })
|
||||||
await alice.Warmup()
|
await alice.Warmup()
|
||||||
|
|
||||||
|
|
@ -46,37 +100,10 @@ const initLndInstances = async (settings: TestSettings) => {
|
||||||
await dave.Warmup()
|
await dave.Warmup()
|
||||||
return { alice, bob, carol, dave }
|
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 stopAllInstances = (instances: LndInstances) => {
|
||||||
const wallet = await this.core.createWallet('');
|
instances.alice.Stop()
|
||||||
console.log({ wallet })
|
instances.bob.Stop()
|
||||||
this.addr = await this.core.getNewAddress()
|
instances.carol.Stop()
|
||||||
console.log({ addr: this.addr })
|
instances.dave.Stop()
|
||||||
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 })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue