-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new type for list of uint64 (#352)
* feat: implement ListUintNum64Type * feat: use ListUintNum64Type for lodestar Balances type
- Loading branch information
Showing
5 changed files
with
288 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {LeafNode, Node, packedUintNum64sToLeafNodes, subtreeFillToContents} from "@chainsafe/persistent-merkle-tree"; | ||
|
||
import {ListBasicTreeViewDU} from "../viewDU/listBasic"; | ||
import {ListBasicOpts, ListBasicType} from "./listBasic"; | ||
import {UintNumberType} from "./uint"; | ||
import {addLengthNode} from "./arrayBasic"; | ||
|
||
/** | ||
* Specific implementation of ListBasicType for UintNumberType with some optimizations. | ||
*/ | ||
export class ListUintNum64Type extends ListBasicType<UintNumberType> { | ||
constructor(limit: number, opts?: ListBasicOpts) { | ||
super(new UintNumberType(8), limit, opts); | ||
} | ||
|
||
/** | ||
* Return a ListBasicTreeViewDU with nodes populated | ||
*/ | ||
toViewDU(value: number[]): ListBasicTreeViewDU<UintNumberType> { | ||
// no need to serialize and deserialize like in the abstract class | ||
const {treeNode, leafNodes} = this.packedUintNum64sToNode(value); | ||
// cache leaf nodes in the ViewDU | ||
return this.getViewDU(treeNode, { | ||
nodes: leafNodes, | ||
length: value.length, | ||
nodesPopulated: true, | ||
}); | ||
} | ||
|
||
/** | ||
* No need to serialize and deserialize like in the abstract class | ||
*/ | ||
value_toTree(value: number[]): Node { | ||
const {treeNode} = this.packedUintNum64sToNode(value); | ||
return treeNode; | ||
} | ||
|
||
private packedUintNum64sToNode(value: number[]): {treeNode: Node; leafNodes: LeafNode[]} { | ||
if (value.length > this.limit) { | ||
throw new Error(`Exceeds limit: ${value.length} > ${this.limit}`); | ||
} | ||
|
||
const leafNodes = packedUintNum64sToLeafNodes(value); | ||
// subtreeFillToContents mutates the leafNodes array | ||
const rootNode = subtreeFillToContents([...leafNodes], this.chunkDepth); | ||
const treeNode = addLengthNode(rootNode, value.length); | ||
return {treeNode, leafNodes}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.