From 0d942529991de913f378907c768502df349c5e72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Fri, 20 Nov 2020 09:35:55 +0100 Subject: [PATCH 1/3] Add SatStackNotReady error to also throw in send flow --- src/families/bitcoin/bridge/libcore.js | 4 ++++ src/families/bitcoin/presync.js | 10 +++------- src/families/bitcoin/satstack.js | 10 ++++++++++ src/families/bitcoin/satstack.test.js | 24 ++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/families/bitcoin/bridge/libcore.js b/src/families/bitcoin/bridge/libcore.js index e12312f0a0..c2d6605be4 100644 --- a/src/families/bitcoin/bridge/libcore.js +++ b/src/families/bitcoin/bridge/libcore.js @@ -26,6 +26,7 @@ import { getMainAccount } from "../../../account"; import { getMinRelayFee } from "../fees"; import { isChangeOutput, perCoinLogic } from "../transaction"; import { makeAccountBridgeReceive } from "../../../bridge/jsHelpers"; +import { requiresSatStackReady } from "../satstack"; const receive = makeAccountBridgeReceive({ injectGetAddressParams: (account) => { @@ -190,6 +191,9 @@ const prepareTransaction = async ( a: Account, t: Transaction ): Promise => { + if (a.currency.id === "bitcoin") { + await requiresSatStackReady(); + } let networkInfo = t.networkInfo; if (!networkInfo) { networkInfo = await getAccountNetworkInfo(a); diff --git a/src/families/bitcoin/presync.js b/src/families/bitcoin/presync.js index daa3347bcc..1daea58498 100644 --- a/src/families/bitcoin/presync.js +++ b/src/families/bitcoin/presync.js @@ -1,13 +1,9 @@ // @flow -import { SatStackNotReady } from "../../errors"; import type { CryptoCurrency } from "../../types"; -import { isSatStackEnabled, fetchSatStackStatus } from "./satstack"; +import { requiresSatStackReady } from "./satstack"; export default async function presync(currency: CryptoCurrency) { - if (isSatStackEnabled() && currency.id === "bitcoin") { - const status = await fetchSatStackStatus(); - if (status.type !== "ready") { - throw new SatStackNotReady(); - } + if (currency.id === "bitcoin") { + await requiresSatStackReady(); } } diff --git a/src/families/bitcoin/satstack.js b/src/families/bitcoin/satstack.js index fa0b064a8d..e91e380fda 100644 --- a/src/families/bitcoin/satstack.js +++ b/src/families/bitcoin/satstack.js @@ -2,6 +2,7 @@ import { Observable, interval, from } from "rxjs"; import url from "url"; import { share, switchMap } from "rxjs/operators"; +import { SatStackNotReady } from "../../errors"; import { getCryptoCurrencyById } from "../../currencies"; import network from "../../network"; import { RPCFieldRequired } from "../../errors"; @@ -232,6 +233,15 @@ export async function fetchSatStackStatus(): Promise { return { type: "ready" }; } +export async function requiresSatStackReady() { + if (isSatStackEnabled()) { + const status = await fetchSatStackStatus(); + if (status.type !== "ready") { + throw new SatStackNotReady(); + } + } +} + export const statusObservable: Observable = interval(1000).pipe( switchMap(() => from(fetchSatStackStatus())), share() diff --git a/src/families/bitcoin/satstack.test.js b/src/families/bitcoin/satstack.test.js index 74aada4f97..786167015a 100644 --- a/src/families/bitcoin/satstack.test.js +++ b/src/families/bitcoin/satstack.test.js @@ -10,6 +10,7 @@ import { setMockStatus, fetchSatStackStatus, statusObservable, + requiresSatStackReady, } from "./satstack"; import dataset from "./datasets/bitcoin"; import { inferDescriptorFromAccount } from "./descriptor"; @@ -349,3 +350,26 @@ describe("statusObservable", () => { await p; }); }); + +describe("requiresSatStackReady", () => { + beforeEach(() => { + setEnv("MOCK", "1"); + }); + afterEach(() => { + setMockStatus({ type: "ready" }); + setEnv("SATSTACK", false); + setEnv("MOCK", ""); + }); + test("without satstack", async () => { + await expect(requiresSatStackReady()).resolves.toBe(); + }); + test("with satstack", async () => { + setEnv("SATSTACK", true); + await expect(requiresSatStackReady()).resolves.toBe(); + }); + test("with satstack not ready", async () => { + setEnv("SATSTACK", true); + setMockStatus({ type: "node-disconnected" }); + await expect(requiresSatStackReady()).rejects.toThrow(); + }); +}); From ab274950a8c79f74bd8c477bdb38ff0065e40ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Fri, 20 Nov 2020 11:07:23 +0100 Subject: [PATCH 2/3] Split SatStackNotReady in two cases --- src/errors.js | 5 ++++- src/families/bitcoin/satstack.js | 12 +++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/errors.js b/src/errors.js index e386b0fbd6..786e066830 100644 --- a/src/errors.js +++ b/src/errors.js @@ -126,7 +126,10 @@ export const CosmosBroadcastError = { "17": createCustomErrorClass(" CosmosBroadcastCodeNoSignatures"), }; -export const SatStackNotReady = createCustomErrorClass("SatStackNotReady"); +export const SatStackAccessDown = createCustomErrorClass("SatStackAccessDown"); +export const SatStackStillSyncing = createCustomErrorClass( + "SatStackStillSyncing" +); export const SwapNoAvailableProviders = createCustomErrorClass( "SwapNoAvailableProviders" diff --git a/src/families/bitcoin/satstack.js b/src/families/bitcoin/satstack.js index e91e380fda..e58614283c 100644 --- a/src/families/bitcoin/satstack.js +++ b/src/families/bitcoin/satstack.js @@ -2,7 +2,7 @@ import { Observable, interval, from } from "rxjs"; import url from "url"; import { share, switchMap } from "rxjs/operators"; -import { SatStackNotReady } from "../../errors"; +import { SatStackAccessDown, SatStackStillSyncing } from "../../errors"; import { getCryptoCurrencyById } from "../../currencies"; import network from "../../network"; import { RPCFieldRequired } from "../../errors"; @@ -236,8 +236,14 @@ export async function fetchSatStackStatus(): Promise { export async function requiresSatStackReady() { if (isSatStackEnabled()) { const status = await fetchSatStackStatus(); - if (status.type !== "ready") { - throw new SatStackNotReady(); + switch (status.type) { + case "ready": + return; + case "syncing": + case "scanning": + throw new SatStackStillSyncing(); + default: + throw new SatStackAccessDown(); } } } From 90b66f31add5aba1b74e6d7f3dc70bb7c8570206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Fri, 20 Nov 2020 11:09:25 +0100 Subject: [PATCH 3/3] Fixes typo for cosmos errors --- src/errors.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/errors.js b/src/errors.js index 786e066830..e3369c9f47 100644 --- a/src/errors.js +++ b/src/errors.js @@ -116,14 +116,14 @@ export const CosmosBroadcastError = { "7": createCustomErrorClass("CosmosBroadcastCodeInvalidAddress"), "8": createCustomErrorClass("CosmosBroadcastCodeInvalidPubKey"), "9": createCustomErrorClass("CosmosBroadcastCodeUnknownAddress"), - "10": createCustomErrorClass(" CosmosBroadcastCodeInsufficientCoins"), - "11": createCustomErrorClass(" CosmosBroadcastCodeInvalidCoins"), - "12": createCustomErrorClass(" CosmosBroadcastCodeOutOfGas"), - "13": createCustomErrorClass(" CosmosBroadcastCodeMemoTooLarge"), - "14": createCustomErrorClass(" CosmosBroadcastCodeInsufficientFee"), - "15": createCustomErrorClass(" CosmosBroadcastCodeTooManySignatures"), - "16": createCustomErrorClass(" CosmosBroadcastCodeGasOverflow"), - "17": createCustomErrorClass(" CosmosBroadcastCodeNoSignatures"), + "10": createCustomErrorClass("CosmosBroadcastCodeInsufficientCoins"), + "11": createCustomErrorClass("CosmosBroadcastCodeInvalidCoins"), + "12": createCustomErrorClass("CosmosBroadcastCodeOutOfGas"), + "13": createCustomErrorClass("CosmosBroadcastCodeMemoTooLarge"), + "14": createCustomErrorClass("CosmosBroadcastCodeInsufficientFee"), + "15": createCustomErrorClass("CosmosBroadcastCodeTooManySignatures"), + "16": createCustomErrorClass("CosmosBroadcastCodeGasOverflow"), + "17": createCustomErrorClass("CosmosBroadcastCodeNoSignatures"), }; export const SatStackAccessDown = createCustomErrorClass("SatStackAccessDown");