don't re-use ports after going offline

This commit is contained in:
woodser 2022-12-03 13:03:44 +00:00
parent cbd3f614c0
commit b7c20e0d27

View File

@ -92,20 +92,20 @@ const TestConfig = {
startupHavenods: [{ startupHavenods: [{
appName: "haveno-" + getBaseCurrencyNetwork() + "_arbitrator", // arbritrator appName: "haveno-" + getBaseCurrencyNetwork() + "_arbitrator", // arbritrator
logProcessOutput: true, logProcessOutput: true,
url: "http://localhost:8079", port: "8079",
accountPasswordRequired: false, accountPasswordRequired: false,
accountPassword: "abctesting123", accountPassword: "abctesting123",
}, { }, {
appName: "haveno-" + getBaseCurrencyNetwork() + "_user1", // user1 appName: "haveno-" + getBaseCurrencyNetwork() + "_user1", // user1
logProcessOutput: true, logProcessOutput: true,
url: "http://localhost:8080", port: "8080",
accountPasswordRequired: false, accountPasswordRequired: false,
accountPassword: "abctesting456", accountPassword: "abctesting456",
walletUrl: "http://127.0.0.1:38091", walletUrl: "http://127.0.0.1:38091",
}, { }, {
appName: "haveno-" + getBaseCurrencyNetwork() + "_user2", // user2 appName: "haveno-" + getBaseCurrencyNetwork() + "_user2", // user2
logProcessOutput: true, logProcessOutput: true,
url: "http://localhost:8081", port: "8081",
accountPasswordRequired: false, accountPasswordRequired: false,
accountPassword: "abctesting789", accountPassword: "abctesting789",
walletUrl: "http://127.0.0.1:38092", walletUrl: "http://127.0.0.1:38092",
@ -132,10 +132,10 @@ const TestConfig = {
address: "LXUTUN5mTPc2LsS7cEjkyjTRcfYyJGoUuQ" address: "LXUTUN5mTPc2LsS7cEjkyjTRcfYyJGoUuQ"
} }
], ],
proxyPorts: new Map<string, string[]>([ // map proxied ports to havenod api and p2p ports ports: new Map<string, string[]>([ // map http ports to havenod api and p2p ports
["8079", ["9998", "4444"]], // arbitrator ["8079", ["9998", "4444"]], // arbitrator
["8080", ["9999", "5555"]], // user1 ["8080", ["9999", "5555"]], // user1
["8081", ["10000", "6666"]], // user2 ["8081", ["10000", "6666"]], // user2
["8082", ["10001", "7777"]], ["8082", ["10001", "7777"]],
["8083", ["10002", "7778"]], ["8083", ["10002", "7778"]],
["8084", ["10003", "7779"]], ["8084", ["10003", "7779"]],
@ -174,10 +174,24 @@ const TestConfig = {
disputeWinner: DisputeResult.Winner.SELLER, disputeWinner: DisputeResult.Winner.SELLER,
disputeReason: DisputeResult.Reason.PEER_WAS_LATE, disputeReason: DisputeResult.Reason.PEER_WAS_LATE,
disputeSummary: "Seller is winner", disputeSummary: "Seller is winner",
maxConcurrency: 8 maxConcurrency: 10
} }
}; };
interface HavenodContext {
logProcessOutput?: boolean,
apiPassword?: string,
walletUsername?: string,
walletDefaultPassword?: string,
accountPasswordRequired?: boolean,
accountPassword?: string,
autoLogin?: boolean,
appName?: string,
port?: string,
excludePorts?: string[],
walletUrl?: string
}
interface TradeContext { interface TradeContext {
// trade flow // trade flow
@ -242,14 +256,14 @@ interface TradeContext {
} }
enum TradeRole { enum TradeRole {
MAKER = "MAKER", MAKER = "MAKER",
TAKER = "TAKER", TAKER = "TAKER",
} }
enum DisputeContext { enum DisputeContext {
NONE = "NONE", NONE = "NONE",
OPEN_AFTER_DEPOSITS_UNLOCK = "OPEN_AFTER_DEPOSITS_UNLOCK", OPEN_AFTER_DEPOSITS_UNLOCK = "OPEN_AFTER_DEPOSITS_UNLOCK",
OPEN_AFTER_PAYMENT_SENT = "OPEN_AFTER_PAYMENT_SENT" OPEN_AFTER_PAYMENT_SENT = "OPEN_AFTER_PAYMENT_SENT"
} }
interface TxContext { interface TxContext {
@ -1600,8 +1614,8 @@ test("Selects arbitrators which are online, registered, and least used", async (
await wait(TestConfig.walletSyncPeriodMs * 2); await wait(TestConfig.walletSyncPeriodMs * 2);
// get internal api addresses // get internal api addresses
const arbitratorApiUrl = "localhost:" + TestConfig.proxyPorts.get(getPort(arbitrator.getUrl()))![1]; // TODO: havenod.getApiUrl()? const arbitratorApiUrl = "localhost:" + TestConfig.ports.get(getPort(arbitrator.getUrl()))![1]; // TODO: havenod.getApiUrl()?
const arbitrator2ApiUrl = "localhost:" + TestConfig.proxyPorts.get(getPort(arbitrator2.getUrl()))![1]; const arbitrator2ApiUrl = "localhost:" + TestConfig.ports.get(getPort(arbitrator2.getUrl()))![1];
let err = undefined; let err = undefined;
try { try {
@ -1852,6 +1866,7 @@ async function executeTrade(ctx?: TradeContext): Promise<string> {
else expect(buyerPaymentAccountPayload).toBeUndefined(); else expect(buyerPaymentAccountPayload).toBeUndefined();
// shut down buyer and seller if configured // shut down buyer and seller if configured
const usedPorts = [getPort(ctx.buyer!.getUrl()), getPort(ctx.seller!.getUrl())];
const promises: Promise<void>[] = []; const promises: Promise<void>[] = [];
const buyerAppName = ctx.buyer!.getAppName(); const buyerAppName = ctx.buyer!.getAppName();
if (ctx.buyerOfflineAfterTake) { if (ctx.buyerOfflineAfterTake) {
@ -1874,9 +1889,10 @@ async function executeTrade(ctx?: TradeContext): Promise<string> {
// buyer comes online if offline // buyer comes online if offline
if (ctx.buyerOfflineAfterTake) { if (ctx.buyerOfflineAfterTake) {
ctx.buyer = await initHaveno({appName: buyerAppName}); ctx.buyer = await initHaveno({appName: buyerAppName, excludePorts: usedPorts});
if (isBuyerMaker) ctx.maker = ctx.buyer; if (isBuyerMaker) ctx.maker = ctx.buyer;
else ctx.taker = ctx.buyer; else ctx.taker = ctx.buyer;
usedPorts.push(getPort(ctx.buyer!.getUrl()));
} }
// test trade states // test trade states
@ -1945,9 +1961,10 @@ async function executeTrade(ctx?: TradeContext): Promise<string> {
// seller comes online if offline // seller comes online if offline
if (!ctx.seller) { if (!ctx.seller) {
ctx.seller = await initHaveno({appName: sellerAppName}); ctx.seller = await initHaveno({appName: sellerAppName, excludePorts: usedPorts});
if (isBuyerMaker) ctx.taker = ctx.seller; if (isBuyerMaker) ctx.taker = ctx.seller;
else ctx.maker = ctx.seller; else ctx.maker = ctx.seller;
usedPorts.push(getPort(ctx.seller!.getUrl()))
} }
// seller notified payment is sent // seller notified payment is sent
@ -2007,9 +2024,10 @@ async function executeTrade(ctx?: TradeContext): Promise<string> {
// buyer comes online if offline // buyer comes online if offline
if (ctx.buyerOfflineAfterPaymentSent) { if (ctx.buyerOfflineAfterPaymentSent) {
ctx.buyer = await initHaveno({appName: buyerAppName}); ctx.buyer = await initHaveno({appName: buyerAppName, excludePorts: usedPorts});
if (isBuyerMaker) ctx.maker = ctx.buyer; if (isBuyerMaker) ctx.maker = ctx.buyer;
else ctx.taker = ctx.buyer; else ctx.taker = ctx.buyer;
usedPorts.push(getPort(ctx.buyer!.getUrl()));
HavenoUtils.log(1, "Done starting buyer"); HavenoUtils.log(1, "Done starting buyer");
await wait(TestConfig.maxWalletStartupMs + TestConfig.walletSyncPeriodMs); await wait(TestConfig.maxWalletStartupMs + TestConfig.walletSyncPeriodMs);
} }
@ -2492,33 +2510,33 @@ async function initHavenos(numDaemons: number, config?: any) {
return Promise.all(havenodPromises); return Promise.all(havenodPromises);
} }
async function initHaveno(config?: any): Promise<HavenoClient> { async function initHaveno(ctx?: HavenodContext): Promise<HavenoClient> {
config = Object.assign({}, TestConfig.defaultHavenod, config); if (!ctx) ctx = {};
if (!config.appName) config.appName = "haveno-" + TestConfig.baseCurrencyNetwork + "_instance_" + GenUtils.getUUID(); Object.assign(ctx, TestConfig.defaultHavenod, Object.assign({}, ctx));
if (!ctx.appName) ctx.appName = "haveno-" + TestConfig.baseCurrencyNetwork + "_instance_" + GenUtils.getUUID();
// connect to existing server or start new process // connect to existing server or start new process
let havenod: HavenoClient; let havenod: HavenoClient;
try { try {
// try to connect to existing server // try to connect to existing server
havenod = new HavenoClient(config.url, config.apiPassword); if (!ctx.port) throw new Error("Cannot connect without port");
havenod = new HavenoClient("http://localhost:" + ctx.port, ctx.apiPassword!);
await havenod.getVersion(); await havenod.getVersion();
} catch (err: any) { } catch (err: any) {
// get port for haveno process // get port for haveno process
let proxyPort = ""; if (!ctx.port) {
if (config.url) proxyPort = getPort(config.url); for (const httpPort of Array.from(TestConfig.ports.keys())) {
else { if (httpPort === "8079" || httpPort === "8080" || httpPort === "8081") continue; // reserved for arbitrator, user1, and user2
for (const port of Array.from(TestConfig.proxyPorts.keys())) { if (!GenUtils.arrayContains(HAVENO_PROCESS_PORTS, httpPort) && (!ctx.excludePorts || !GenUtils.arrayContains(ctx.excludePorts, httpPort))) {
if (port === "8079" || port === "8080" || port === "8081") continue; // reserved for arbitrator, user1, and user2 HAVENO_PROCESS_PORTS.push(httpPort);
if (!GenUtils.arrayContains(HAVENO_PROCESS_PORTS, port)) { ctx.port = httpPort;
HAVENO_PROCESS_PORTS.push(port);
proxyPort = port;
break; break;
} }
} }
} }
if (!proxyPort) throw new Error("No unused test ports available"); if (!ctx.port) throw new Error("No unused test ports available");
// start haveno process using configured ports if available // start haveno process using configured ports if available
const cmd: string[] = [ const cmd: string[] = [
@ -2526,19 +2544,19 @@ async function initHaveno(config?: any): Promise<HavenoClient> {
"--baseCurrencyNetwork", TestConfig.baseCurrencyNetwork, "--baseCurrencyNetwork", TestConfig.baseCurrencyNetwork,
"--useLocalhostForP2P", TestConfig.baseCurrencyNetwork === BaseCurrencyNetwork.XMR_MAINNET ? "false" : "true", // TODO: disable for stagenet too "--useLocalhostForP2P", TestConfig.baseCurrencyNetwork === BaseCurrencyNetwork.XMR_MAINNET ? "false" : "true", // TODO: disable for stagenet too
"--useDevPrivilegeKeys", TestConfig.baseCurrencyNetwork === BaseCurrencyNetwork.XMR_LOCAL ? "true" : "false", "--useDevPrivilegeKeys", TestConfig.baseCurrencyNetwork === BaseCurrencyNetwork.XMR_LOCAL ? "true" : "false",
"--nodePort", TestConfig.proxyPorts.get(proxyPort)![1], "--nodePort", TestConfig.ports.get(ctx.port)![1],
"--appName", config.appName, "--appName", ctx.appName,
"--apiPassword", "apitest", "--apiPassword", "apitest",
"--apiPort", TestConfig.proxyPorts.get(proxyPort)![0], "--apiPort", TestConfig.ports.get(ctx.port)![0],
"--walletRpcBindPort", config.walletUrl ? getPort(config.walletUrl) : "" + await getAvailablePort(), // use configured port if given "--walletRpcBindPort", ctx.walletUrl ? getPort(ctx.walletUrl) : "" + await getAvailablePort(), // use configured port if given
"--passwordRequired", (config.accountPasswordRequired ? "true" : "false") "--passwordRequired", (ctx.accountPasswordRequired ? "true" : "false")
]; ];
havenod = await HavenoClient.startProcess(TestConfig.haveno.path, cmd, "http://localhost:" + proxyPort, config.logProcessOutput); havenod = await HavenoClient.startProcess(TestConfig.haveno.path, cmd, "http://localhost:" + ctx.port, ctx.logProcessOutput!);
HAVENO_PROCESSES.push(havenod); HAVENO_PROCESSES.push(havenod);
} }
// open account if configured // open account if configured
if (config.autoLogin) await initHavenoAccount(havenod, config.accountPassword); if (ctx.autoLogin) await initHavenoAccount(havenod, ctx.accountPassword!);
return havenod; return havenod;
async function getAvailablePort(): Promise<number> { async function getAvailablePort(): Promise<number> {