Skip to content

Commit

Permalink
Potential bugs fix (#412)
Browse files Browse the repository at this point in the history
* Missing await in addTransaction

* Throw any errors of getBlock

* Do not return if getLatestBlocks fail, or shield might not get saved

* remove useless try catch block
  • Loading branch information
panleone authored Oct 3, 2024
1 parent 0e675c5 commit 7887e6a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
54 changes: 20 additions & 34 deletions scripts/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,42 +89,28 @@ export class ExplorerNetwork extends Network {
* @returns {Promise<Object>} the block fetched from explorer
*/
async getBlock(blockHeight, skipCoinstake = false) {
try {
const block = await this.safeFetchFromExplorer(
`/api/v2/block/${blockHeight}`
const block = await this.safeFetchFromExplorer(
`/api/v2/block/${blockHeight}`
);
const newTxs = [];
// This is bad. We're making so many requests
// This is a quick fix to try to be compliant with the blockbook
// API, and not the PIVX extension.
// In the Blockbook API /block doesn't have any chain specific information
// Like hex, shield info or what not.
// We could change /getshieldblocks to /getshieldtxs?
// In addition, always skip the coinbase transaction and in case the coinstake one
// TODO: once v6.0 and shield stake is activated we might need to change this optimization
for (const tx of block.txs.slice(skipCoinstake ? 2 : 1)) {
const r = await fetch(
`${this.strUrl}/api/v2/tx-specific/${tx.txid}`
);
const newTxs = [];
// This is bad. We're making so many requests
// This is a quick fix to try to be compliant with the blockbook
// API, and not the PIVX extension.
// In the Blockbook API /block doesn't have any chain specific information
// Like hex, shield info or what not.
// We could change /getshieldblocks to /getshieldtxs?
// In addition, always skip the coinbase transaction and in case the coinstake one
// TODO: once v6.0 and shield stake is activated we might need to change this optimization
for (const tx of block.txs.slice(skipCoinstake ? 2 : 1)) {
const r = await fetch(
`${this.strUrl}/api/v2/tx-specific/${tx.txid}`
);
if (!r.ok) throw new Error('failed');
const newTx = await r.json();
newTxs.push(newTx);
}
block.txs = newTxs;
return block;
} catch (e) {
// Don't display block not found errors to user
// This is a bug with blockbook, where it sends a bad
// request error or newly minted blocks
if (
e.message.match(/block not found/i) ||
e.message.match(/safe fetch/) ||
e.message.match(/bad request/i)
) {
return;
}
throw e;
if (!r.ok) throw new Error('failed');
const newTx = await r.json();
newTxs.push(newTx);
}
block.txs = newTxs;
return block;
}

async getBlockCount() {
Expand Down
3 changes: 1 addition & 2 deletions scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,6 @@ export class Wallet {
) {
try {
block = await cNet.getBlock(blockHeight);
if (!block) return;
if (block.txs) {
if (
this.hasShield() &&
Expand Down Expand Up @@ -1230,7 +1229,7 @@ export class Wallet {
}
const txs = await db.getTxs();
for (const tx of txs) {
this.addTransaction(tx, true);
await this.addTransaction(tx, true);
}
}
}
Expand Down

0 comments on commit 7887e6a

Please sign in to comment.