From fdac9ef929f13bd99afb2a4389631113bfebc88c Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Thu, 4 May 2023 13:22:30 -0300 Subject: [PATCH 1/4] fix: sync task --- nets/DevNetSpec.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/nets/DevNetSpec.ts b/nets/DevNetSpec.ts index 0d43b03df..152b8064f 100644 --- a/nets/DevNetSpec.ts +++ b/nets/DevNetSpec.ts @@ -19,6 +19,7 @@ export abstract class DevNetSpec extends NetSpec { readonly binary readonly chain readonly nodeCount + #rawChainSpecPath?: Promise constructor(props: DevNetProps) { super() this.binary = props.bin @@ -41,11 +42,15 @@ export abstract class DevNetSpec extends NetSpec { } async rawChainSpecPath(signal: AbortSignal, devnetTempDir: string) { - return createRawChainSpec( - this.tempDir(devnetTempDir), - await this.binary(signal), - this.chain, - ) + const binary = await this.binary(signal) + if (!this.#rawChainSpecPath) { + this.#rawChainSpecPath = createRawChainSpec( + this.tempDir(devnetTempDir), + binary, + this.chain, + ) + } + return this.#rawChainSpecPath } async preflightNetwork(signal: AbortSignal, devnetTempDir: string) { From 6f153267389a3366e3807eb167b695f652430966 Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Thu, 4 May 2023 15:28:57 -0300 Subject: [PATCH 2/4] fix: use map for rawChainSpecPath --- nets/DevNetSpec.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/nets/DevNetSpec.ts b/nets/DevNetSpec.ts index 152b8064f..e8e1d2d05 100644 --- a/nets/DevNetSpec.ts +++ b/nets/DevNetSpec.ts @@ -4,6 +4,7 @@ import * as base58 from "../deps/std/encoding/base58.ts" import * as path from "../deps/std/path.ts" import { writableStreamFromWriter } from "../deps/std/streams.ts" import { getFreePort, portReady } from "../util/port.ts" +import { getOrInit } from "../util/state.ts" import { BinaryResolver } from "./bins.ts" import { createRawChainSpec } from "./chain_spec/mod.ts" import type { DevRelaySpec } from "./DevRelaySpec.ts" @@ -19,12 +20,13 @@ export abstract class DevNetSpec extends NetSpec { readonly binary readonly chain readonly nodeCount - #rawChainSpecPath?: Promise + readonly #rawChainSpecPaths constructor(props: DevNetProps) { super() this.binary = props.bin this.chain = props.chain this.nodeCount = props.nodeCount + this.#rawChainSpecPaths = new Map>() } abstract relay: DevRelaySpec @@ -43,14 +45,16 @@ export abstract class DevNetSpec extends NetSpec { async rawChainSpecPath(signal: AbortSignal, devnetTempDir: string) { const binary = await this.binary(signal) - if (!this.#rawChainSpecPath) { - this.#rawChainSpecPath = createRawChainSpec( - this.tempDir(devnetTempDir), + const tempDir = this.tempDir(devnetTempDir) + return getOrInit(this.#rawChainSpecPaths, tempDir, () => + createRawChainSpec( + tempDir, binary, this.chain, - ) - } - return this.#rawChainSpecPath + ).catch((error) => { + this.#rawChainSpecPaths.delete(tempDir) + throw error + })) } async preflightNetwork(signal: AbortSignal, devnetTempDir: string) { From 601c7a0c66722d3542887c79f7ea68fff5dc572a Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Thu, 4 May 2023 15:36:33 -0300 Subject: [PATCH 3/4] fix: remove extra variable --- nets/DevNetSpec.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nets/DevNetSpec.ts b/nets/DevNetSpec.ts index e8e1d2d05..a18dfe67e 100644 --- a/nets/DevNetSpec.ts +++ b/nets/DevNetSpec.ts @@ -44,12 +44,11 @@ export abstract class DevNetSpec extends NetSpec { } async rawChainSpecPath(signal: AbortSignal, devnetTempDir: string) { - const binary = await this.binary(signal) const tempDir = this.tempDir(devnetTempDir) - return getOrInit(this.#rawChainSpecPaths, tempDir, () => + return getOrInit(this.#rawChainSpecPaths, tempDir, async () => createRawChainSpec( tempDir, - binary, + await this.binary(signal), this.chain, ).catch((error) => { this.#rawChainSpecPaths.delete(tempDir) From bb6837e75a790e7c16d925bbe172c5057414b8aa Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Thu, 4 May 2023 16:05:09 -0300 Subject: [PATCH 4/4] fix: use PermanentMemo --- nets/DevNetSpec.ts | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/nets/DevNetSpec.ts b/nets/DevNetSpec.ts index a18dfe67e..53628f141 100644 --- a/nets/DevNetSpec.ts +++ b/nets/DevNetSpec.ts @@ -3,8 +3,8 @@ import * as ed25519 from "../deps/ed25519.ts" import * as base58 from "../deps/std/encoding/base58.ts" import * as path from "../deps/std/path.ts" import { writableStreamFromWriter } from "../deps/std/streams.ts" +import { PermanentMemo } from "../util/memo.ts" import { getFreePort, portReady } from "../util/port.ts" -import { getOrInit } from "../util/state.ts" import { BinaryResolver } from "./bins.ts" import { createRawChainSpec } from "./chain_spec/mod.ts" import type { DevRelaySpec } from "./DevRelaySpec.ts" @@ -20,13 +20,11 @@ export abstract class DevNetSpec extends NetSpec { readonly binary readonly chain readonly nodeCount - readonly #rawChainSpecPaths constructor(props: DevNetProps) { super() this.binary = props.bin this.chain = props.chain this.nodeCount = props.nodeCount - this.#rawChainSpecPaths = new Map>() } abstract relay: DevRelaySpec @@ -43,17 +41,13 @@ export abstract class DevNetSpec extends NetSpec { return path.join(parentDir, this.name) } + readonly #rawChainSpecPaths = new PermanentMemo() async rawChainSpecPath(signal: AbortSignal, devnetTempDir: string) { const tempDir = this.tempDir(devnetTempDir) - return getOrInit(this.#rawChainSpecPaths, tempDir, async () => - createRawChainSpec( - tempDir, - await this.binary(signal), - this.chain, - ).catch((error) => { - this.#rawChainSpecPaths.delete(tempDir) - throw error - })) + return this.#rawChainSpecPaths.run( + tempDir, + async () => createRawChainSpec(tempDir, await this.binary(signal), this.chain), + ) } async preflightNetwork(signal: AbortSignal, devnetTempDir: string) {