|
| 1 | +import { binding } from "./binding.js"; |
| 2 | + |
| 3 | +// this is to sync the constant from zig to Bun which is 0xffffffff |
| 4 | +const ERROR_INDEX = binding.getErrorIndex(); |
| 5 | + |
| 6 | +/** Pre-electra, byte count for random value is 1, post-electra, byte count for random value is 2 */ |
| 7 | +export declare enum ByteCount { |
| 8 | + One = 1, |
| 9 | + Two = 2, |
| 10 | +} |
| 11 | + |
| 12 | +export function computeProposerIndex( |
| 13 | + seed: Uint8Array, |
| 14 | + activeIndices: Uint32Array, |
| 15 | + effectiveBalanceIncrements: Uint16Array, |
| 16 | + randByteCount: ByteCount, |
| 17 | + maxEffectiveBalanceElectra: number, |
| 18 | + effectiveBalanceIncrement: number, |
| 19 | + rounds: number, |
| 20 | +): number { |
| 21 | + const result = binding.computeProposerIndex( |
| 22 | + seed, |
| 23 | + seed.length, |
| 24 | + activeIndices, |
| 25 | + activeIndices.length, |
| 26 | + effectiveBalanceIncrements, |
| 27 | + effectiveBalanceIncrements.length, |
| 28 | + randByteCount, |
| 29 | + maxEffectiveBalanceElectra, |
| 30 | + effectiveBalanceIncrement, |
| 31 | + rounds, |
| 32 | + ); |
| 33 | + if (result === ERROR_INDEX) { |
| 34 | + throw new Error("Failed to compute proposer index"); |
| 35 | + } |
| 36 | + return result; |
| 37 | +} |
| 38 | + |
| 39 | +export function computeProposerIndexElectra( |
| 40 | + seed: Uint8Array, |
| 41 | + activeIndices: Uint32Array, |
| 42 | + effectiveBalanceIncrements: Uint16Array, |
| 43 | + maxEffectiveBalanceElectra: number, |
| 44 | + effectiveBalanceIncrement: number, |
| 45 | + rounds: number, |
| 46 | +): number { |
| 47 | + const result = binding.computeProposerIndexElectra( |
| 48 | + seed, |
| 49 | + seed.length, |
| 50 | + activeIndices, |
| 51 | + activeIndices.length, |
| 52 | + effectiveBalanceIncrements, |
| 53 | + effectiveBalanceIncrements.length, |
| 54 | + maxEffectiveBalanceElectra, |
| 55 | + effectiveBalanceIncrement, |
| 56 | + rounds, |
| 57 | + ); |
| 58 | + if (result === ERROR_INDEX) { |
| 59 | + throw new Error("Failed to compute proposer index"); |
| 60 | + } |
| 61 | + return result; |
| 62 | +} |
| 63 | + |
| 64 | +export function computeSyncCommitteeIndices( |
| 65 | + seed: Uint8Array, |
| 66 | + activeIndices: Uint32Array, |
| 67 | + effectiveBalanceIncrements: Uint16Array, |
| 68 | + randByteCount: ByteCount, |
| 69 | + syncCommitteeSize: number, |
| 70 | + maxEffectiveBalanceElectra: number, |
| 71 | + effectiveBalanceIncrement: number, |
| 72 | + rounds: number, |
| 73 | +): Uint32Array { |
| 74 | + const out = new Uint32Array(syncCommitteeSize); |
| 75 | + const result = binding.computeSyncCommitteeIndices( |
| 76 | + seed, |
| 77 | + seed.length, |
| 78 | + activeIndices, |
| 79 | + activeIndices.length, |
| 80 | + effectiveBalanceIncrements, |
| 81 | + effectiveBalanceIncrements.length, |
| 82 | + randByteCount, |
| 83 | + maxEffectiveBalanceElectra, |
| 84 | + effectiveBalanceIncrement, |
| 85 | + rounds, |
| 86 | + out, |
| 87 | + out.length, |
| 88 | + ); |
| 89 | + if (result !== 0) { |
| 90 | + throw new Error( |
| 91 | + `Failed to compute sync committee indices, result = ${result}`, |
| 92 | + ); |
| 93 | + } |
| 94 | + return out; |
| 95 | +} |
| 96 | + |
| 97 | +export function computeSyncCommitteeIndicesElectra( |
| 98 | + seed: Uint8Array, |
| 99 | + activeIndices: Uint32Array, |
| 100 | + effectiveBalanceIncrements: Uint16Array, |
| 101 | + syncCommitteeSize: number, |
| 102 | + maxEffectiveBalanceElectra: number, |
| 103 | + effectiveBalanceIncrement: number, |
| 104 | + rounds: number, |
| 105 | +): Uint32Array { |
| 106 | + const out = new Uint32Array(syncCommitteeSize); |
| 107 | + const result = binding.computeSyncCommitteeIndicesElectra( |
| 108 | + seed, |
| 109 | + seed.length, |
| 110 | + activeIndices, |
| 111 | + activeIndices.length, |
| 112 | + effectiveBalanceIncrements, |
| 113 | + effectiveBalanceIncrements.length, |
| 114 | + maxEffectiveBalanceElectra, |
| 115 | + effectiveBalanceIncrement, |
| 116 | + rounds, |
| 117 | + out, |
| 118 | + out.length, |
| 119 | + ); |
| 120 | + if (result !== 0) { |
| 121 | + throw new Error( |
| 122 | + `Failed to compute sync committee indices electra, result = ${result}`, |
| 123 | + ); |
| 124 | + } |
| 125 | + return out; |
| 126 | +} |
0 commit comments