Skip to content

Commit

Permalink
Bug Fix: Web Store InsertChainMmrNodes Duplicate Ids Causes Error (0x…
Browse files Browse the repository at this point in the history
…PolygonMiden#627)

* Bug Fix: Web Store InsertChainMmrNodes Duplicate Ids Causes Error
---------

Co-authored-by: Dennis Garcia <[email protected]>
Co-authored-by: Julian <[email protected]>
  • Loading branch information
3 people authored Dec 12, 2024
1 parent dba555e commit 7757a81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

### Fixes

* Web Store InsertChainMmrNodes Duplicate Ids Causes Error (#627).
* Fixed client bugs where some note metadata was not being updated (#625).
* Added Sync Loop to Integration Tests for Small Speedup (#590).

Expand Down
25 changes: 17 additions & 8 deletions crates/rust-client/src/store/web_store/js/chainData.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,23 @@ export async function insertBlockHeader(

export async function insertChainMmrNodes(ids, nodes) {
try {
const data = nodes.map((node, index) => {
return {
id: ids[index],
node: node,
};
});

await chainMmrNodes.bulkAdd(data);
// Check if the arrays are not of the same length
if (ids.length !== nodes.length) {
throw new Error("ids and nodes arrays must be of the same length");
}

if (ids.length === 0) {
return;
}

// Create array of objects with id and node
const data = nodes.map((node, index) => ({
id: ids[index],
node: node,
}));

// Use bulkPut to add/overwrite the entries
await chainMmrNodes.bulkPut(data);
} catch (err) {
console.error("Failed to insert chain mmr nodes: ", err);
throw err;
Expand Down
12 changes: 6 additions & 6 deletions crates/rust-client/src/store/web_store/js/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ async function updateChainMmrNodes(tx, nodeIndexes, nodes) {
return;
}

// Create the updates array with objects matching the structure expected by your IndexedDB schema
const updates = nodeIndexes.map((index, i) => ({
id: index, // Assuming 'index' is the primary key or part of it
node: nodes[i], // Other attributes of the object
// Create array of objects with id and node
const data = nodes.map((node, index) => ({
id: nodeIndexes[index],
node: node,
}));

// Perform bulk update or insertion; assumes tx.chainMmrNodes is a valid table reference in a transaction
await tx.chainMmrNodes.bulkAdd(updates);
// Use bulkPut to add/overwrite the entries
await tx.chainMmrNodes.bulkPut(data);
} catch (err) {
console.error("Failed to update chain mmr nodes: ", err);
throw err;
Expand Down

0 comments on commit 7757a81

Please sign in to comment.