Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stability Fixes #180

Merged
merged 17 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/dapp/src/components/TokenDapp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,7 @@ export const TokenDapp: FC<{

<div className="columns">
<label htmlFor="short-text">Short Text</label>
<input
type="text"
<textarea
id="short-text"
name="short-text"
value={shortText}
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/background/accounDeployAction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtQueueItem } from "../shared/actionQueue/types"
import { BaseWalletAccount } from "../shared/wallet.model"
import { BackgroundService } from "./background"
import { addTransaction } from "./transactions/store"
import { addTransaction } from "../shared/transactions/store"
21 changes: 20 additions & 1 deletion packages/extension/src/background/accountMessaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { encryptForUi } from "./crypto"

export const handleAccountMessage: HandleMessage<AccountMessage> = async ({
msg,
background: { wallet, actionQueue },
background: { wallet },
messagingKeys: { privateKey },
respond,
}) => {
Expand All @@ -29,6 +29,25 @@ export const handleAccountMessage: HandleMessage<AccountMessage> = async ({
})
}

case "DISCOVER_ACCOUNTS": {
const { networkId } = msg.data
try {
if (networkId) {
await wallet.deriveActiveAccountsForNetworkIfNonExistence(networkId)
} else {
await wallet.deriveActiveAccountsIfNonExistence()
}
return respond({ type: "DISCOVER_ACCOUNTS_RES" })
} catch (exception) {
console.error("Failed to discover accounts", exception)
return respond({
type: "DISCOVER_ACCOUNTS_REJ",
data: { error: `${exception}` },
})
}

}

case "NEW_ACCOUNT": {
if (!(await wallet.isSessionOpen())) {
throw Error("you need an open session")
Expand Down
21 changes: 10 additions & 11 deletions packages/extension/src/background/networkStatus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ExplorerProvider, NodeProvider } from "@alephium/web3"
import urljoin from "url-join"

import { Network, NetworkStatus } from "../shared/network"
import { KeyValueStorage } from "../shared/storage"
import { createStaleWhileRevalidateCache } from "./swr"
import { fetchWithTimeout } from "./utils/fetchWithTimeout"

type SwrCacheKey = string

Expand Down Expand Up @@ -45,13 +42,15 @@ export const isNetworkHealthy = async (network: Network): Promise<boolean> => {
const nodeProvider = new NodeProvider(network.nodeUrl)
const explorerProvider = new ExplorerProvider(network.explorerApiUrl)

try {
const nodeReleaseVersion = (await nodeProvider.infos.getInfosVersion()).version
const explorerReleaseVersion = (await explorerProvider.infos.getInfos()).releaseVersion
return swr(`${network.id}-network-status`, async () => {
try {
const nodeReleaseVersion = (await nodeProvider.infos.getInfosVersion()).version
const explorerReleaseVersion = (await explorerProvider.infos.getInfos()).releaseVersion

return !!nodeReleaseVersion && !!explorerReleaseVersion
} catch (exception) {
console.debug('Exception when checking network healthy', exception)
return false
}
return !!nodeReleaseVersion && !!explorerReleaseVersion
} catch (exception) {
console.debug('Exception when checking network healthy', exception)
return false
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Transaction } from "../../shared/transactions"
import { BaseWalletAccount } from "../../shared/wallet.model"
import { accountsEqual } from "../../shared/wallet.service"
import { walletStore } from "../../shared/wallet/walletStore"
import { transactionsStore } from "./store"
import { transactionsStore } from "../../shared/transactions/store"

// selects transactions that are pending and match the provided account

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { ExplorerProvider } from "@alephium/web3"
import { getNetwork } from "../../../shared/network"
import {
Transaction,
getInFlightTransactions,
} from "../../../shared/transactions"
import { Transaction } from "../../../shared/transactions"
import { getTransactionsStatusUpdate } from "../determineUpdates"

export async function getTransactionsUpdate(transactions: Transaction[]) {
const transactionsToCheck = getInFlightTransactions(transactions)
export async function getTransactionsUpdate(transactionsToCheck: Transaction[]) {

// as this function tends to run into 429 errors, we'll simply keep the old status when it fails
// TODO: we should add a cooldown when user run into 429 errors
Expand Down Expand Up @@ -43,5 +39,5 @@ export async function getTransactionsUpdate(transactions: Transaction[]) {
[],
)

return getTransactionsStatusUpdate(transactions, updatedTransactions)
return getTransactionsStatusUpdate(transactionsToCheck, updatedTransactions)
}
32 changes: 4 additions & 28 deletions packages/extension/src/background/transactions/sources/voyager.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { ExplorerProvider } from "@alephium/web3"
import join from "url-join"

import { Network, getNetwork } from "../../../shared/network"
import { Transaction, compareTransactions } from "../../../shared/transactions"
import { Network } from "../../../shared/network"
import { Transaction, getTransactionsPerAccount } from "../../../shared/transactions"
import { WalletAccount } from "../../../shared/wallet.model"
import { fetchWithTimeout } from "../../utils/fetchWithTimeout"
import { mapAlephiumTransactionToTransaction } from "../transformers"
import { Transaction as AlephiumTransaction } from '@alephium/web3/dist/src/api/api-explorer'

export interface VoyagerTransaction {
blockId: string
Expand Down Expand Up @@ -39,27 +36,6 @@ export async function getTransactionHistory(
accountsToPopulate: WalletAccount[],
metadataTransactions: Transaction[],
) {
const transactionsPerAccount = await Promise.all(
accountsToPopulate.map(async (account) => {
const network = await getNetwork(account.networkId)
const explorerProvider = new ExplorerProvider(network.explorerApiUrl)

// confirmed txs
const transactions: AlephiumTransaction[] = await explorerProvider.addresses.getAddressesAddressTransactions(account.address)

return transactions.map((transaction) =>
mapAlephiumTransactionToTransaction(
transaction,
account,
metadataTransactions.find((tx) =>
compareTransactions(tx, {
hash: transaction.hash,
account: { networkId: account.networkId },
}),
)?.meta,
),
)
}),
)
return transactionsPerAccount.flat()
const transactionsPerAccount = await getTransactionsPerAccount(accountsToPopulate, metadataTransactions)
return Array.from(transactionsPerAccount.values()).flat()
}
49 changes: 1 addition & 48 deletions packages/extension/src/background/transactions/store.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,6 @@
import { differenceWith } from "lodash-es"

import { ArrayStorage } from "../../shared/storage"
import { StorageChange } from "../../shared/storage/types"
import {
Transaction,
TransactionRequest,
compareTransactions,
} from "../../shared/transactions"
import { transactionsStore } from "../../shared/transactions/store"
import { runAddedHandlers, runChangedStatusHandlers } from "./onupdate"

export const transactionsStore = new ArrayStorage<Transaction>([], {
namespace: "core:transactions",
areaName: "local",
compare: compareTransactions,
})

export const addTransaction = async (transaction: TransactionRequest) => {
// sanity checks
if (!transaction.hash) {
return // dont throw
}

const newTransaction = {
status: "RECEIVED" as const,
timestamp: Date.now(),
...transaction,
}

return transactionsStore.push(newTransaction)
}

export const getUpdatedTransactionsForChangeSet = (
changeSet: StorageChange<Transaction[]>,
) => {
const updatedTransactions = differenceWith(
changeSet.oldValue ?? [],
changeSet.newValue ?? [],
equalTransactionWithStatus,
)
return updatedTransactions
}

const equalTransactionWithStatus = (
a: Transaction,
b: Transaction,
): boolean => {
return compareTransactions(a, b) && a.status === b.status
}

transactionsStore.subscribe((_, changeSet) => {
const findOldTransaction = (hash: string) => changeSet.oldValue?.find(
(oldTransaction) => oldTransaction.hash === hash,
Expand Down
11 changes: 5 additions & 6 deletions packages/extension/src/background/transactions/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WalletAccount } from "../../shared/wallet.model"
import { accountsEqual } from "../../shared/wallet.service"
import { getTransactionsUpdate } from "./sources/onchain"
import { getTransactionHistory } from "./sources/voyager"
import { transactionsStore } from "./store"
import { transactionsStore } from "../../shared/transactions/store"
import { partition } from "lodash"

export interface TransactionTracker {
Expand All @@ -21,15 +21,14 @@ export const transactionTracker: TransactionTracker = {
uniqAccounts,
allTransactions,
)
return transactionsStore.push(historyTransactions)

// Set the tx history directly here, which potentially trims historical transactions
return transactionsStore.set(historyTransactions)
},
async update() {
const allTransactions = await transactionsStore.get()
const pendingTransactions = getInFlightTransactions(allTransactions)
const updatedTransactions = await getTransactionsUpdate(
// is smart enough to filter for just the pending transactions, as the rest needs no update
allTransactions,
)
const updatedTransactions = await getTransactionsUpdate(pendingTransactions)
const [toBeRemoved, toBeKept] = partition(updatedTransactions, (tx) => tx.status === "REMOVED_FROM_MEMPOOL")
if (toBeRemoved.length > 0) {
await transactionsStore.remove((tx) => toBeRemoved.some((toBeRemovedTx) => tx.hash === toBeRemovedTx.hash))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ReviewTransactionResult,
} from "../../shared/actionQueue/types"
import { BackgroundService } from "../background"
import { addTransaction } from "./store"
import { addTransaction } from "../../shared/transactions/store"

export const executeTransactionAction = async (
transaction: ReviewTransactionResult,
Expand Down
Loading
Loading