@lodestar/beacon-node
Version:
A Typescript implementation of the beacon chain
47 lines • 2.33 kB
JavaScript
import { Tree, toGindex } from "@chainsafe/persistent-merkle-tree";
import { getEth1DepositCount } from "@lodestar/state-transition";
import { ssz } from "@lodestar/types";
import { toRootHex } from "@lodestar/utils";
import { Eth1Error, Eth1ErrorCode } from "../errors.js";
export async function getDeposits(
// eth1_deposit_index represents the next deposit index to be added
state, eth1Data, depositsGetter) {
const depositIndex = state.eth1DepositIndex;
const depositCount = eth1Data.depositCount;
if (depositIndex > depositCount) {
throw new Eth1Error({ code: Eth1ErrorCode.DEPOSIT_INDEX_TOO_HIGH, depositIndex, depositCount });
}
const depositsLen = getEth1DepositCount(state, eth1Data);
if (depositsLen === 0) {
return []; // If depositsLen === 0, we can return early since no deposit with be returned from depositsGetter
}
const indexRange = { gte: depositIndex, lt: depositIndex + depositsLen };
const deposits = await depositsGetter(indexRange, eth1Data);
if (deposits.length < depositsLen) {
throw new Eth1Error({ code: Eth1ErrorCode.NOT_ENOUGH_DEPOSITS, len: deposits.length, expectedLen: depositsLen });
}
if (deposits.length > depositsLen) {
throw new Eth1Error({ code: Eth1ErrorCode.TOO_MANY_DEPOSITS, len: deposits.length, expectedLen: depositsLen });
}
return deposits;
}
export function getDepositsWithProofs(depositEvents, depositRootTree, eth1Data) {
// Get tree at this particular depositCount to compute correct proofs
const viewAtDepositCount = depositRootTree.sliceTo(eth1Data.depositCount - 1);
const depositRoot = viewAtDepositCount.hashTreeRoot();
if (!ssz.Root.equals(depositRoot, eth1Data.depositRoot)) {
throw new Eth1Error({
code: Eth1ErrorCode.WRONG_DEPOSIT_ROOT,
root: toRootHex(depositRoot),
expectedRoot: toRootHex(eth1Data.depositRoot),
});
}
// Already commited for .hashTreeRoot()
const treeAtDepositCount = new Tree(viewAtDepositCount.node);
const depositTreeDepth = viewAtDepositCount.type.depth;
return depositEvents.map((log) => ({
proof: treeAtDepositCount.getSingleProof(toGindex(depositTreeDepth, BigInt(log.index))),
data: log.depositData,
}));
}
//# sourceMappingURL=deposits.js.map