Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
LL-5363 Fix endless loop when out of memory inline install (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-cortes authored Apr 23, 2021
1 parent 7b5f27a commit 893f4bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
22 changes: 16 additions & 6 deletions src/apps/hw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Transport from "@ledgerhq/hw-transport";
import { getDeviceModel } from "@ledgerhq/devices";
import { UnexpectedBootloader } from "@ledgerhq/errors";
import { concat, of, empty, from, Observable, throwError } from "rxjs";
import { concat, of, empty, from, Observable, throwError, defer } from "rxjs";
import { mergeMap, map } from "rxjs/operators";
import type { Exec, AppOp, ListAppsEvent, ListAppsResult } from "./types";
import type { App, DeviceInfo } from "../types/manager";
Expand All @@ -21,8 +21,15 @@ import ManagerAPI from "../api/Manager";
import { getEnv } from "../env";
import hwListApps from "../hw/listApps";
import { polyfillApp, polyfillApplication } from "./polyfill";
import { reducer, isOutOfMemoryState, initState } from "../apps/logic";
import {
reducer,
isOutOfMemoryState,
initState,
predictOptimisticState,
} from "../apps/logic";
import { runAllWithProgress } from "../apps/runner";
import { openAppFromDashboard } from "../hw/connectApp";
import type { ConnectAppEvent } from "../hw/connectApp";

export const execWithTransport = (transport: Transport<*>): Exec => (
appOp: AppOp,
Expand All @@ -48,7 +55,7 @@ export type StreamAppInstallEvent =
export const streamAppInstall = (
transport: Transport<*>,
appName: string
): Observable<StreamAppInstallEvent> =>
): Observable<StreamAppInstallEvent | ConnectAppEvent> =>
concat(
of({ type: "listing-apps" }),
from(getDeviceInfo(transport)).pipe(
Expand All @@ -67,13 +74,16 @@ export const streamAppInstall = (
type: "install",
name: appName,
});
if (isOutOfMemoryState(state)) {
if (isOutOfMemoryState(predictOptimisticState(state))) {
// it will not be possible to install so we will fallback to app-not-installed.
return of({ type: "app-not-installed", appName });
}
const exec = execWithTransport(transport);
return runAllWithProgress(state, exec).pipe(
map((progress) => ({ type: "stream-install", progress }))
return concat(
runAllWithProgress(state, exec).pipe(
map((progress) => ({ type: "stream-install", progress }))
),
defer(() => from(openAppFromDashboard(transport, appName)))
);
}
return empty();
Expand Down
12 changes: 5 additions & 7 deletions src/hw/connectApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DisconnectedDeviceDuringOperation,
DisconnectedDevice,
} from "@ledgerhq/errors";
import type Transport from "@ledgerhq/hw-transport";
import type { DeviceModelId } from "@ledgerhq/devices";
import type { DerivationMode } from "../types";
import { getCryptoCurrencyById } from "../currencies";
Expand Down Expand Up @@ -60,9 +61,9 @@ export type ConnectAppEvent =
| { type: "opened", app?: AppAndVersion, derivation?: { address: string } }
| { type: "display-upgrade-warning", displayUpgradeWarning: boolean };

const openAppFromDashboard = (
transport,
appName
export const openAppFromDashboard = (
transport: Transport<*>,
appName: string
): Observable<ConnectAppEvent> =>
concat(
of({ type: "ask-open-app", appName }),
Expand All @@ -74,10 +75,7 @@ const openAppFromDashboard = (
case 0x6984:
case 0x6807:
return getEnv("EXPERIMENTAL_INLINE_INSTALL")
? concat(
streamAppInstall(transport, appName),
from(openAppFromDashboard(transport, appName))
)
? streamAppInstall(transport, appName)
: of({ type: "app-not-installed", appName });
case 0x6985:
case 0x5501:
Expand Down

1 comment on commit 893f4bf

@vercel
Copy link

@vercel vercel bot commented on 893f4bf Apr 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.