Skip to content

Commit

Permalink
fix: do not throw error if commit ViewDU without tracking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Aug 26, 2024
1 parent 0b3ba85 commit ed1b08a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 26 deletions.
6 changes: 1 addition & 5 deletions packages/ssz/src/viewDU/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ class ContainerTreeViewDU<Fields extends Record<string, Type<unknown>>> extends
* Same method to `type/container.ts` that call ViewDU.serializeToBytes() of internal fields.
*/
serializeToBytes(output: ByteViews, offset: number): number {
// it's the responsibility of consumer to call commit() before calling this method
// if we do the commit() here, it'll lose all HashComputations that we want to batch
if (this.nodesChanged.size !== 0 || this.viewsChanged.size !== 0) {
throw Error(`Must commit changes before serializeToBytes(Uint8Array(${output.uint8Array.length}, ${offset})`);
}
this.commit();

let fixedIndex = offset;
let variableIndex = offset + this.type.fixedEnd;
Expand Down
15 changes: 4 additions & 11 deletions packages/ssz/src/viewDU/listBasic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ export class ListBasicTreeViewDU<ElementType extends BasicType<unknown>> extends
*/
sliceTo(index: number): this {
if (index < 0) {
throw Error(`Does not support sliceTo() with negative index ${index}`);
throw new Error(`Does not support sliceTo() with negative index ${index}`);
}

// it's the responsibility of consumer to call commit() before calling this method
// if we do the commit() here, it'll lose all HashComputations that we want to batch
if (this.nodesChanged.size > 0) {
throw Error(`Must commit changes before sliceTo(${index})`);
}
// Commit before getting rootNode to ensure all pending data is in the rootNode
this.commit();

// All nodes beyond length are already zero
if (index >= this._length - 1) {
Expand Down Expand Up @@ -87,11 +84,7 @@ export class ListBasicTreeViewDU<ElementType extends BasicType<unknown>> extends
* Same method to `type/listBasic.ts` leveraging cached nodes.
*/
serializeToBytes(output: ByteViews, offset: number): number {
// it's the responsibility of consumer to call commit() before calling this method
// if we do the commit() here, it'll lose all HashComputations that we want to batch
if (this.nodesChanged.size > 0) {
throw Error(`Must commit changes before serializeToBytes(Uint8Array(${output.uint8Array.length}), ${offset})`);
}
this.commit();
const {nodes, nodesPopulated} = this.cache;
const chunksNode = this.type.tree_getChunksNode(this._rootNode);
return tree_serializeToBytesArrayBasic(
Expand Down
13 changes: 3 additions & 10 deletions packages/ssz/src/viewDU/listComposite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ export class ListCompositeTreeViewDU<
* Note: Using index = -1, returns an empty list of length 0.
*/
sliceTo(index: number): this {
// it's the responsibility of consumer to call commit() before calling this method
// if we do the commit() here, it'll lose all HashComputations that we want to batch
if (this.viewsChanged.size > 0) {
throw Error(`Must commit changes before sliceTo(${index})`);
}
// Commit before getting rootNode to ensure all pending data is in the rootNode
this.commit();
const rootNode = this._rootNode;
const length = this._length;

Expand Down Expand Up @@ -114,11 +111,7 @@ export class ListCompositeTreeViewDU<
* Same method to `type/listComposite.ts` leveraging cached nodes.
*/
serializeToBytes(output: ByteViews, offset: number): number {
// it's the responsibility of consumer to call commit() before calling this method
// if we do the commit() here, it'll lose all HashComputations that we want to batch
if (this.viewsChanged.size > 0) {
throw Error(`Must commit changes before serializeToBytes(Uint8Array(${output.uint8Array.length}, ${offset})`);
}
this.commit();
this.populateAllNodes();
const chunksNode = this.type.tree_getChunksNode(this._rootNode);
return tree_serializeToBytesArrayComposite(
Expand Down

0 comments on commit ed1b08a

Please sign in to comment.