-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track balances as Number64UintType (#2973)
* Track balances as Number64UintType * Use Number64ListType ssz type, refactor BalanceList * Use applyDeltaInBatch * Fix spec & unit tests * Fix perf test to go with numbered balance * New ssz, persistent-merkle-tree and fix tests * Chore: clean up commented code * Fix test/perf/phase0/epoch/processEffectiveBalanceUpdates.test.ts Co-authored-by: Lion - dapplion <[email protected]>
- Loading branch information
Showing
52 changed files
with
303 additions
and
297 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
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
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
92 changes: 92 additions & 0 deletions
92
packages/beacon-state-transition/src/allForks/util/balanceList.ts
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,92 @@ | ||
import {List, Number64ListType, TreeBacked} from "@chainsafe/ssz"; | ||
import {Tree} from "@chainsafe/persistent-merkle-tree"; | ||
|
||
/** | ||
* Manage balances of BeaconState. | ||
*/ | ||
export class BalanceList implements List<number> { | ||
[index: number]: number; | ||
tree: Tree; | ||
type: Number64ListType; | ||
|
||
constructor(type: Number64ListType, tree: Tree) { | ||
this.type = type; | ||
this.tree = tree; | ||
} | ||
|
||
get length(): number { | ||
return this.type.tree_getLength(this.tree); | ||
} | ||
|
||
get(index: number): number | undefined { | ||
return this.type.tree_getProperty(this.tree, index) as number | undefined; | ||
} | ||
|
||
set(index: number, value: number): void { | ||
this.type.tree_setProperty(this.tree, index, value); | ||
} | ||
|
||
applyDelta(index: number, delta: number): number { | ||
return this.type.tree_applyDeltaAtIndex(this.tree, index, delta); | ||
} | ||
|
||
applyDeltaInBatch(deltaByIndex: Map<number, number>): void { | ||
this.type.tree_applyDeltaInBatch(this.tree, deltaByIndex); | ||
} | ||
|
||
/** Return the new balances */ | ||
updateAll(deltas: number[]): number[] { | ||
const [newTree, newBalances] = this.type.tree_newTreeFromDeltas(this.tree, deltas); | ||
this.tree.rootNode = newTree.rootNode; | ||
this.type.tree_setLength(this.tree, newBalances.length); | ||
return newBalances; | ||
} | ||
|
||
push(value: number): number { | ||
return this.type.tree_push(this.tree, value); | ||
} | ||
|
||
pop(): number { | ||
return this.type.tree_pop(this.tree); | ||
} | ||
|
||
*[Symbol.iterator](): Iterator<number> { | ||
for (let i = 0; i < this.length; i++) { | ||
yield this.get(i) as number; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
find(fn: (value: number, index: number, list: this) => boolean): number | undefined { | ||
return; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
findIndex(fn: (value: number, index: number, list: this) => boolean): number { | ||
return -1; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export const CachedBalanceListProxyHandler: ProxyHandler<BalanceList> = { | ||
get(target: BalanceList, key: PropertyKey): unknown { | ||
if (!Number.isNaN(Number(String(key)))) { | ||
return target.get(key as number); | ||
} else if (target[key as keyof BalanceList]) { | ||
return target[key as keyof BalanceList]; | ||
} else { | ||
const treeBacked = target.type.createTreeBacked(target.tree); | ||
if (key in treeBacked) { | ||
return treeBacked[key as keyof TreeBacked<List<number>>]; | ||
} | ||
return undefined; | ||
} | ||
}, | ||
set(target: BalanceList, key: PropertyKey, value: number): boolean { | ||
if (!Number.isNaN(Number(key))) { | ||
target.set(key as number, value); | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}; |
100 changes: 0 additions & 100 deletions
100
packages/beacon-state-transition/src/allForks/util/cachedBalanceList.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.