Skip to content

Commit

Permalink
fixup! feat(wallet): the wallet now only fetches UTXOs on tx history …
Browse files Browse the repository at this point in the history
…change
  • Loading branch information
AngelCastilloB committed Nov 15, 2024
1 parent 204ee8b commit 08301b5
Showing 1 changed file with 66 additions and 11 deletions.
77 changes: 66 additions & 11 deletions packages/wallet/src/services/TransactionsTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ const allTransactionsByAddresses = async (
return response;
};

const getLastTransactionsAtBlock = (
transactions: Cardano.HydratedTx[],
blockNo: Cardano.BlockNo
): Cardano.HydratedTx[] => {
const txsFromSameBlock = [];

for (let i = transactions.length - 1; i >= 0; --i) {
const tx = transactions[i];
if (tx.blockHeader.blockNo === blockNo) {
txsFromSameBlock.push(tx);
} else {
break;
}
}

return txsFromSameBlock;
};

const findIntersectionAndUpdateTxStore = ({
chainHistoryProvider,
logger,
Expand All @@ -130,10 +148,12 @@ const findIntersectionAndUpdateTxStore = ({
combinator: exhaustMap,
equals: transactionsEquals,
onFatalError,
// eslint-disable-next-line complexity
provider: async () => {

Check failure on line 152 in packages/wallet/src/services/TransactionsTracker.ts

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-20.04)

Async arrow function 'provider' has too many statements (38). Maximum allowed is 30

Check failure on line 152 in packages/wallet/src/services/TransactionsTracker.ts

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-20.04)

Refactor this function to reduce its Cognitive Complexity from 30 to the 15 allowed
let rollbackOccured = false;
// eslint-disable-next-line no-constant-condition
while (true) {
let rollbackOccured = false;

const lastStoredTransaction: Cardano.HydratedTx | undefined = localTransactions[localTransactions.length - 1];

lastStoredTransaction &&
Expand All @@ -152,25 +172,60 @@ const findIntersectionAndUpdateTxStore = ({
lowerBound !== undefined && `since block ${lowerBound}`
);

const txRolledBack =
lastStoredTransaction && newTransactions[newTransactions.length - 1]?.id !== lastStoredTransaction.id;
if (lowerBound === undefined) {
localTransactions = newTransactions;
return localTransactions;
}

if (txRolledBack) {
rollbackOccured = true;
logger.debug(`Transaction ${lastStoredTransaction.id} was rolled back`);
rollback$.next(lastStoredTransaction);
if (newTransactions.length === 0) {
// If no transactions found from that block range, it means the last known block has been rolled back.
while (localTransactions.length > 0) {
const lastKnownTx = localTransactions[localTransactions.length - 1];

if (lastKnownTx.blockHeader.blockNo === lowerBound) {
rollbackOccured = true;
logger.debug(`Transaction ${lastKnownTx.id} was rolled back`);
rollback$.next(lastKnownTx);
localTransactions.pop();
} else {
break;
}
}

// Rollback by 1 transaction, try again in next loop iteration
localTransactions.pop();
} else {
continue;
}

const localTxsFromSameBlock = getLastTransactionsAtBlock(localTransactions, lowerBound);

// Roll back transactions from this block not found in the result
for (const localTx of localTxsFromSameBlock) {
const txFound = newTransactions.find((tx) => tx.id === localTx.id);

if (!txFound) {
rollbackOccured = true;
logger.debug(`Transaction ${localTx.id} was rolled back`);
rollback$.next(localTx);

const index = localTransactions.findLastIndex((tx) => tx.id === localTx.id);

if (index !== -1) {
localTransactions.splice(index, 1);
}
}
}

if (!rollbackOccured) {
const lastLocalTxs = localTransactions.slice(-newTransactions.length);

const areTransactionsSame =
lastLocalTxs.length === newTransactions.length &&
lastLocalTxs.every((tx, index) => tx.id === newTransactions[index].id);

if (!areTransactionsSame || rollbackOccured) {
if (!areTransactionsSame) {
// Remove overlapping transactions
localTransactions = localTransactions.slice(0, -localTxsFromSameBlock.length);
localTransactions = [...localTransactions, ...newTransactions];

store.setAll(localTransactions);
}

Expand Down

0 comments on commit 08301b5

Please sign in to comment.