Skip to content

Commit

Permalink
feat: simplify provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
tamtamchik committed Feb 10, 2025
1 parent d4bcbf3 commit eef9523
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Run slither
run: >
poetry run slither . --no-fail-pedantic --sarif results.sarif
poetry run slither . --no-fail-pedantic --sarif results.sarif
- name: Check results.sarif presence
id: results
Expand Down
10 changes: 7 additions & 3 deletions lib/protocol/helpers/nor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { certainAddress, log, trace } from "lib";

import { ProtocolContext, StakingModuleName } from "../types";

import { depositAndReportValidators } from "./staking";

const NOR_MODULE_ID = 1n;
const MIN_OPS_COUNT = 3n;
const MIN_OP_KEYS_COUNT = 10n;

Expand All @@ -16,10 +19,9 @@ export const norEnsureOperators = async (
minOperatorsCount = MIN_OPS_COUNT,
minOperatorKeysCount = MIN_OP_KEYS_COUNT,
) => {
const newOperatorsCount = await norEnsureOperatorsHaveMinKeys(ctx, minOperatorsCount, minOperatorKeysCount);

const { nor } = ctx.contracts;

const newOperatorsCount = await norEnsureOperatorsHaveMinKeys(ctx, minOperatorsCount, minOperatorKeysCount);
for (let operatorId = 0n; operatorId < minOperatorsCount; operatorId++) {
const nodeOperatorBefore = await nor.getNodeOperator(operatorId, false);

Expand All @@ -40,7 +42,9 @@ export const norEnsureOperators = async (
"Min keys count": minOperatorKeysCount,
});

return newOperatorsCount;
if (newOperatorsCount > 0) {
await depositAndReportValidators(ctx, NOR_MODULE_ID, newOperatorsCount);
}
};

/**
Expand Down
9 changes: 6 additions & 3 deletions lib/protocol/helpers/sdvt.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { expect } from "chai";
import { randomBytes } from "ethers";

import { impersonate, log, streccak, trace } from "lib";
import { ether, impersonate, log, streccak, trace } from "lib";

import { ether } from "../../units";
import { ProtocolContext } from "../types";

import { getOperatorManagerAddress, getOperatorName, getOperatorRewardAddress } from "./nor";
import { depositAndReportValidators } from "./staking";

const SDVT_MODULE_ID = 2n;
const MIN_OPS_COUNT = 3n;
const MIN_OP_KEYS_COUNT = 10n;

Expand Down Expand Up @@ -40,7 +41,9 @@ export const sdvtEnsureOperators = async (
expect(nodeOperatorAfter.totalVettedValidators).to.equal(nodeOperatorBefore.totalAddedValidators);
}

return newOperatorsCount;
if (newOperatorsCount > 0) {
await depositAndReportValidators(ctx, SDVT_MODULE_ID, newOperatorsCount);
}
};

/**
Expand Down
36 changes: 16 additions & 20 deletions lib/protocol/helpers/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import { ProtocolContext } from "../types";

import { report } from "./accounting";

const AMOUNT = ether("100");
const MAX_DEPOSIT = 150n;
const CURATED_MODULE_ID = 1n;
const SIMPLE_DVT_MODULE_ID = 2n;

/**
* Unpauses the staking contract.
*/
Expand Down Expand Up @@ -47,36 +42,37 @@ export const ensureStakeLimit = async (ctx: ProtocolContext) => {
}
};

export const depositAndReportValidators = async (ctx: ProtocolContext) => {
export const depositAndReportValidators = async (ctx: ProtocolContext, moduleId: bigint, depositsCount: bigint) => {
const { lido, depositSecurityModule } = ctx.contracts;
const ethHolder = await impersonate(certainAddress("provision:eht:whale"), ether("100000"));
const ethHolder = await impersonate(certainAddress("provision:eth:whale"), ether("100000"));

await lido.connect(ethHolder).submit(ZeroAddress, { value: ether("10000") });

// Deposit node operators
const dsmSigner = await impersonate(depositSecurityModule.address, AMOUNT);
await lido.connect(dsmSigner).deposit(MAX_DEPOSIT, CURATED_MODULE_ID, ZERO_HASH);
await lido.connect(dsmSigner).deposit(MAX_DEPOSIT, SIMPLE_DVT_MODULE_ID, ZERO_HASH);
// Deposit validators
const dsmSigner = await impersonate(depositSecurityModule.address, ether("100000"));
await lido.connect(dsmSigner).deposit(depositsCount, moduleId, ZERO_HASH);

const before = await lido.getBeaconStat();

log.debug("Validators on beacon chain before provisioning", {
depositedValidators: before.depositedValidators,
beaconValidators: before.beaconValidators,
beaconBalance: before.beaconBalance,
"Module ID to deposit": moduleId,
"Deposited": before.depositedValidators,
"Total": before.beaconValidators,
"Balance": before.beaconBalance,
});

// Add new validators to beacon chain
await report(ctx, {
clDiff: ether("32") * before.depositedValidators,
clAppearedValidators: before.depositedValidators,
clDiff: ether("32") * depositsCount,
clAppearedValidators: depositsCount,
});

const after = await lido.getBeaconStat();

log.debug("Validators on beacon chain after provisioning", {
depositedValidators: after.depositedValidators,
beaconValidators: after.beaconValidators,
beaconBalance: after.beaconBalance,
log.debug("Validators on beacon chain after depositing", {
"Module ID deposited": moduleId,
"Deposited": after.depositedValidators,
"Total": after.beaconValidators,
"Balance": after.beaconBalance,
});
};
11 changes: 2 additions & 9 deletions lib/protocol/helpers/withdrawal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { expect } from "chai";
import { ZeroAddress } from "ethers";

import { certainAddress, ether, impersonate, log, trace } from "lib";
Expand Down Expand Up @@ -33,20 +32,14 @@ export const unpauseWithdrawalQueue = async (ctx: ProtocolContext) => {
export const finalizeWithdrawalQueue = async (ctx: ProtocolContext) => {
const { lido, withdrawalQueue } = ctx.contracts;

const seed = Math.random() * 1000000;

const ethHolder = await impersonate(certainAddress(`withdrawalQueue:eth:whale:${seed}`), ether("100000"));
const stEthHolder = await impersonate(certainAddress(`withdrawalQueue:stEth:whale:${seed}`), ether("100000"));
const ethHolder = await impersonate(certainAddress("withdrawalQueue:eth:whale"), ether("100000"));
const stEthHolder = await impersonate(certainAddress("withdrawalQueue:stEth:whale"), ether("100000"));
const stEthHolderAmount = ether("10000");

// Here sendTransaction is used to validate native way of submitting ETH for stETH
const tx = await stEthHolder.sendTransaction({ to: lido.address, value: stEthHolderAmount });
await trace("stEthHolder.sendTransaction", tx);

const stEthHolderBalance = await lido.balanceOf(stEthHolder.address);

expect(stEthHolderBalance).to.approximately(stEthHolderAmount, 10n, "stETH balance increased");

let lastFinalizedRequestId = await withdrawalQueue.getLastFinalizedRequestId();
let lastRequestId = await withdrawalQueue.getLastRequestId();

Expand Down
3 changes: 1 addition & 2 deletions lib/protocol/provision.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { log } from "lib";

import {
depositAndReportValidators,
ensureHashConsensusInitialEpoch,
ensureOracleCommitteeMembers,
ensureStakeLimit,
Expand Down Expand Up @@ -33,7 +32,7 @@ export const provision = async (ctx: ProtocolContext) => {

await norEnsureOperators(ctx, 3n, 5n);
await sdvtEnsureOperators(ctx, 3n, 5n);
await depositAndReportValidators(ctx);

await finalizeWithdrawalQueue(ctx);

await ensureStakeLimit(ctx);
Expand Down
10 changes: 5 additions & 5 deletions test/integration/protocol-happy-path.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe("Protocol Happy Path", () => {

let uncountedStETHShares: bigint;
let amountWithRewards: bigint;
let depositCount: bigint;

before(async () => {
ctx = await getProtocolContext();
Expand Down Expand Up @@ -220,7 +221,7 @@ describe("Protocol Happy Path", () => {
const dsmSigner = await impersonate(depositSecurityModule.address, ether("100"));
const stakingModules = await stakingRouter.getStakingModules();

let depositCount = 0n;
depositCount = 0n;
let expectedBufferedEtherAfterDeposit = bufferedEtherBeforeDeposit;
for (const module of stakingModules) {
const depositTx = await lido.connect(dsmSigner).deposit(MAX_DEPOSIT, module.id, ZERO_HASH);
Expand Down Expand Up @@ -294,11 +295,10 @@ describe("Protocol Happy Path", () => {

const treasuryBalanceBeforeRebase = await lido.sharesOf(treasuryAddress);

// Stranger deposited 100 ETH, enough to deposit 3 validators, need to reflect this in the report
// 0.01 ETH is added to the clDiff to simulate some rewards
// 0.001 – to simulate rewards
const reportData: Partial<OracleReportOptions> = {
clDiff: ether("96.01"),
clAppearedValidators: 3n,
clDiff: ether("32") * depositCount + ether("0.001"),
clAppearedValidators: depositCount,
};

const { reportTx, extraDataTx } = (await report(ctx, reportData)) as {
Expand Down

0 comments on commit eef9523

Please sign in to comment.