Skip to content

Commit

Permalink
fix: do not care HashComp in List ViewDU.commit()
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jun 27, 2024
1 parent 05caa27 commit febffda
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 42 deletions.
18 changes: 2 additions & 16 deletions packages/ssz/src/viewDU/arrayBasic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ export class ArrayBasicTreeViewDU<ElementType extends BasicType<unknown>> extend
commit(hashComps: HashComputationGroup | null = null): void {
const isOldRootHashed = this._rootNode.h0 !== null;
if (this.nodesChanged.size === 0) {
if (!isOldRootHashed && hashComps !== null) {
getHashComputations(this._rootNode, hashComps.offset, hashComps.byLevel);
}
return;
}

Expand All @@ -173,26 +170,15 @@ export class ArrayBasicTreeViewDU<ElementType extends BasicType<unknown>> extend
}

const chunksNode = this.type.tree_getChunksNode(this._rootNode);
const hashCompsThis =
hashComps != null && isOldRootHashed
? {
byLevel: hashComps.byLevel,
offset: hashComps.offset + this.type.tree_chunksNodeOffset(),
}
: null;
const newChunksNode = setNodesAtDepth(chunksNode, this.type.chunkDepth, indexes, nodes, hashCompsThis);
const newChunksNode = setNodesAtDepth(chunksNode, this.type.chunkDepth, indexes, nodes);

this._rootNode = this.type.tree_setChunksNode(
this._rootNode,
newChunksNode,
this.dirtyLength ? this._length : null,
isOldRootHashed ? hashComps : null
null
);

if (!isOldRootHashed && hashComps !== null) {
getHashComputations(this._rootNode, hashComps.offset, hashComps.byLevel);
}

this.nodesChanged.clear();
this.dirtyLength = false;
}
Expand Down
29 changes: 3 additions & 26 deletions packages/ssz/src/viewDU/arrayComposite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,13 @@ export class ArrayCompositeTreeViewDU<
commit(hashComps: HashComputationGroup | null = null): void {
const isOldRootHashed = this._rootNode.h0 !== null;
if (this.viewsChanged.size === 0) {
if (!isOldRootHashed && hashComps !== null) {
getHashComputations(this._rootNode, hashComps.offset, hashComps.byLevel);
}
return;
}

const nodesChanged: {index: number; node: Node}[] = [];
// each view may mutate HashComputationGroup at offset + depth
const hashCompsView =
hashComps != null && isOldRootHashed
? {
byLevel: hashComps.byLevel,
// Depth includes the extra level for the length node
offset: hashComps.offset + this.type.depth,
}
: null;

for (const [index, view] of this.viewsChanged) {
const node = this.type.elementType.commitViewDU(view, hashCompsView);
const node = this.type.elementType.commitViewDU(view);
// Set new node in nodes array to ensure data represented in the tree and fast nodes access is equal
this.nodes[index] = node;
nodesChanged.push({index, node});
Expand All @@ -212,26 +200,15 @@ export class ArrayCompositeTreeViewDU<
const nodes = nodesChangedSorted.map((entry) => entry.node);

const chunksNode = this.type.tree_getChunksNode(this._rootNode);
const hashCompsThis =
hashComps != null && isOldRootHashed
? {
byLevel: hashComps.byLevel,
offset: hashComps.offset + this.type.tree_chunksNodeOffset(),
}
: null;
const newChunksNode = setNodesAtDepth(chunksNode, this.type.chunkDepth, indexes, nodes, hashCompsThis);
const newChunksNode = setNodesAtDepth(chunksNode, this.type.chunkDepth, indexes, nodes, null);

this._rootNode = this.type.tree_setChunksNode(
this._rootNode,
newChunksNode,
this.dirtyLength ? this._length : null,
hashComps
null
);

if (!isOldRootHashed && hashComps !== null) {
getHashComputations(this._rootNode, hashComps.offset, hashComps.byLevel);
}

this.viewsChanged.clear();
this.dirtyLength = false;
}
Expand Down

0 comments on commit febffda

Please sign in to comment.