-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: upgradeToNewType as utility function (#405)
Co-authored-by: Cayman <caymannava@gmail.com>
- Loading branch information
1 parent
b4beed2
commit a7da328
Showing
3 changed files
with
79 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {BranchNode, Node, zeroNode} from "@chainsafe/persistent-merkle-tree"; | ||
import {ContainerType} from "../type/container"; | ||
|
||
/** Upgrade the current View/ViewDU to a root node of new type */ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function upgradeToNewType(rootNode: Node, oldType: ContainerType<any>, newType: ContainerType<any>): Node { | ||
const newFieldsCount = newType.fieldsEntries.length; | ||
const currentFieldsCount = oldType.fieldsEntries.length; | ||
if (newFieldsCount < currentFieldsCount) { | ||
throw Error(`Cannot convert to a type with fewer fields: ${newFieldsCount} < ${currentFieldsCount}`); | ||
} | ||
|
||
if (newType.depth === oldType.depth) { | ||
// no need to grow the tree | ||
return rootNode; | ||
} | ||
|
||
// grow the tree | ||
let node = rootNode; | ||
for (let depth = oldType.depth; depth < newType.depth; depth++) { | ||
node = new BranchNode(node, zeroNode(depth)); | ||
} | ||
|
||
return node; | ||
} |
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