Skip to content

Commit

Permalink
feat: contract based binary search for call gas limit estimate (#214) (
Browse files Browse the repository at this point in the history
…#217)

Co-authored-by: nikhilkumar1612 <[email protected]>
  • Loading branch information
0xSulpiride and nikhilkumar1612 authored Jul 16, 2024
1 parent 44f7ef7 commit 5d8e008
Show file tree
Hide file tree
Showing 21 changed files with 711 additions and 64 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"packages/*"
],
"npmClient": "yarn",
"version": "2.0.6",
"version": "2.0.7",
"stream": "true",
"command": {
"version": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "root",
"private": true,
"version": "2.0.6",
"version": "2.0.7",
"engines": {
"node": ">=18.0.0"
},
Expand Down
10 changes: 5 additions & 5 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publishConfig": {
"access": "public"
},
"version": "2.0.6",
"version": "2.0.7",
"description": "The API module of Etherspot bundler client",
"author": "Etherspot",
"homepage": "https://https://github.com/etherspot/skandha#readme",
Expand Down Expand Up @@ -34,10 +34,10 @@
"dependencies": {
"@fastify/cors": "9.0.1",
"@fastify/websocket": "10.0.1",
"@skandha/executor": "^2.0.6",
"@skandha/monitoring": "^2.0.6",
"@skandha/types": "^2.0.6",
"@skandha/utils": "^2.0.6",
"@skandha/executor": "^2.0.7",
"@skandha/monitoring": "^2.0.7",
"@skandha/types": "^2.0.7",
"@skandha/utils": "^2.0.7",
"class-transformer": "0.5.1",
"class-validator": "0.14.1",
"ethers": "5.7.2",
Expand Down
14 changes: 7 additions & 7 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publishConfig": {
"access": "public"
},
"version": "2.0.6",
"version": "2.0.7",
"description": "> TODO: description",
"author": "zincoshine <[email protected]>",
"homepage": "https://https://github.com/etherspot/skandha#readme",
Expand Down Expand Up @@ -40,12 +40,12 @@
"@libp2p/peer-id-factory": "2.0.1",
"@libp2p/prometheus-metrics": "1.1.3",
"@multiformats/multiaddr": "12.1.3",
"@skandha/api": "^2.0.6",
"@skandha/db": "^2.0.6",
"@skandha/executor": "^2.0.6",
"@skandha/monitoring": "^2.0.6",
"@skandha/node": "^2.0.6",
"@skandha/types": "^2.0.6",
"@skandha/api": "^2.0.7",
"@skandha/db": "^2.0.7",
"@skandha/executor": "^2.0.7",
"@skandha/monitoring": "^2.0.7",
"@skandha/node": "^2.0.7",
"@skandha/types": "^2.0.7",
"find-up": "5.0.0",
"got": "12.5.3",
"js-yaml": "4.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publishConfig": {
"access": "public"
},
"version": "2.0.6",
"version": "2.0.7",
"description": "The DB module of Etherspot bundler client",
"author": "Etherspot",
"homepage": "https://github.com/etherspot/etherspot-bundler#readme",
Expand Down Expand Up @@ -34,7 +34,7 @@
"dependencies": {
"@chainsafe/ssz": "0.10.1",
"@farcaster/rocksdb": "5.5.0",
"@skandha/types": "^2.0.6"
"@skandha/types": "^2.0.7"
},
"devDependencies": {
"@types/rocksdb": "3.0.1",
Expand Down
10 changes: 5 additions & 5 deletions packages/executor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publishConfig": {
"access": "public"
},
"version": "2.0.6",
"version": "2.0.7",
"description": "The Relayer module of Etherspot bundler client",
"author": "Etherspot",
"homepage": "https://https://github.com/etherspot/skandha#readme",
Expand Down Expand Up @@ -35,10 +35,10 @@
},
"dependencies": {
"@flashbots/ethers-provider-bundle": "0.6.2",
"@skandha/monitoring": "^2.0.6",
"@skandha/params": "^2.0.6",
"@skandha/types": "^2.0.6",
"@skandha/utils": "^2.0.6",
"@skandha/monitoring": "^2.0.7",
"@skandha/params": "^2.0.7",
"@skandha/types": "^2.0.7",
"@skandha/utils": "^2.0.7",
"async-mutex": "0.4.0",
"ethers": "5.7.2",
"strict-event-emitter-types": "2.0.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/executor/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,8 @@ export interface KnownEntities {
accounts: string[];
otherEntities: string[];
}

export interface ExecutionResultAndCallGasLimit {
returnInfo: ExecutionResult;
callGasLimit: BigNumber;
}
16 changes: 8 additions & 8 deletions packages/executor/src/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ export class Eth {
userOp.signature = ECDSA_DUMMY_SIGNATURE;
}

const returnInfo = await this.userOpValidationService.validateForEstimation(
userOp,
entryPoint
);
// eslint-disable-next-line prefer-const
let { returnInfo, callGasLimit } =
await this.userOpValidationService.validateForEstimation(
userOp,
entryPoint
);

// eslint-disable-next-line prefer-const
let { preOpGas, validAfter, validUntil, paid } = returnInfo;
let { preOpGas, validAfter, validUntil } = returnInfo;

const verificationGasLimit = BigNumber.from(preOpGas)
.sub(userOp.preVerificationGas)
Expand All @@ -194,9 +196,7 @@ export class Eth {

// calculate callGasLimit based on paid fee
const { cglMarkup } = this.config;
const totalGas: BigNumber = BigNumber.from(paid).div(userOp.maxFeePerGas);
let callGasLimit = totalGas
.sub(preOpGas)
callGasLimit = callGasLimit
.mul(10000 + this.config.cglMarkupPercent)
.div(10000) // % markup
.add(cglMarkup || 0);
Expand Down
2 changes: 2 additions & 0 deletions packages/executor/src/services/EntryPointService/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export const DefaultGasOverheads = {
bundleSize: 1,
sigSize: 65,
};

export const IMPLEMENTATION_ADDRESS_MARKER = "0xA13dB4eCfbce0586E57D1AeE224FbE64706E8cd3";
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ export function decodeRevertReason(
}
}

export function decodeTargetData(data: string) {
try {
const methodSig = data.slice(0, 10);
const dataParams = "0x" + data.slice(10);
if(methodSig === "0x8c83589a") {
const res = ethers.utils.defaultAbiCoder.decode(
["uint256", "uint256"],
dataParams
);
return res;
}
throw Error("Error decoding target data");
} catch (error) {
throw error;
}
}

// not sure why ethers fail to decode revert reasons, not even "Error()" (and obviously, not custom errors)
export function rethrowWithRevertReason(e: Error): never {
throw new Error(decodeRevertReason(e, false) as any);
Expand Down
45 changes: 39 additions & 6 deletions packages/executor/src/services/EntryPointService/versions/0.0.7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import {
UserOperationByHashResponse,
} from "@skandha/types/lib/api/interfaces";
import { deepHexlify } from "@skandha/utils/lib/hexlify";
import {
CallGasEstimationProxy__factory,
_deployedBytecode as _callGasEstimationProxyDeployedBytecode,
} from "@skandha/types/lib/contracts/EPv7/factories/core/CallGasEstimationProxy__factory";
import { CallGasEstimationProxy } from "@skandha/types/lib/contracts/EPv7/core/CallGasEstimationProxy";
import {
encodeUserOp,
mergeValidationDataValues,
Expand All @@ -35,13 +40,20 @@ import {
StakeInfo,
UserOpValidationResult,
} from "../../../interfaces";
import { DefaultGasOverheads } from "../constants";
import {
DefaultGasOverheads,
IMPLEMENTATION_ADDRESS_MARKER,
} from "../constants";
import { StateOverrides } from "../interfaces";
import { decodeRevertReason } from "../utils/decodeRevertReason";
import {
decodeRevertReason,
decodeTargetData,
} from "../utils/decodeRevertReason";
import { getUserOpGasLimit } from "../../BundlingService/utils";
import { IEntryPointService } from "./base";

const entryPointSimulations = IEntryPointSimulations__factory.createInterface();
const callGasEstimateProxy = CallGasEstimationProxy__factory.createInterface();

export class EntryPointV7Service implements IEntryPointService {
contract: EntryPoint;
Expand Down Expand Up @@ -71,10 +83,21 @@ export class EntryPointV7Service implements IEntryPointService {
)
: undefined;

const [data, stateOverrides] = this.encodeSimulateHandleOp(
const estimateCallGasArgs: CallGasEstimationProxy.EstimateCallGasArgsStruct =
{
userOp: packUserOp(userOp),
isContinuation: true,
maxGas: "20000000",
minGas: "21000",
rounding: "500",
};

const [data] = this.encodeSimulateHandleOp(
userOp,
AddressZero,
BytesZero
this.address,
callGasEstimateProxy.encodeFunctionData("estimateCallGas", [
estimateCallGasArgs,
])
);

const tx: providers.TransactionRequest = {
Expand All @@ -83,6 +106,15 @@ export class EntryPointV7Service implements IEntryPointService {
gasLimit,
};

const stateOverrides: StateOverrides = {
[this.address]: {
code: _callGasEstimationProxyDeployedBytecode,
},
[IMPLEMENTATION_ADDRESS_MARKER]: {
code: _deployedBytecode,
},
};

try {
const simulationResult = await this.provider.send("eth_call", [
tx,
Expand All @@ -93,7 +125,8 @@ export class EntryPointV7Service implements IEntryPointService {
"simulateHandleOp",
simulationResult
);
return res[0];
const [callGasLimit] = decodeTargetData(res[0].targetResult);
return { returnInfo: res[0], callGasLimit: callGasLimit };
} catch (error: any) {
console.log(error);
const err = decodeRevertReason(error);
Expand Down
3 changes: 2 additions & 1 deletion packages/executor/src/services/UserOpValidation/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UserOperation } from "@skandha/types/lib/contracts/UserOperation";
import { Config } from "../../config";
import {
ExecutionResult,
ExecutionResultAndCallGasLimit,
NetworkConfig,
UserOpValidationResult,
} from "../../interfaces";
Expand Down Expand Up @@ -62,7 +63,7 @@ export class UserOpValidationService {
async validateForEstimation(
userOp: UserOperation,
entryPoint: string
): Promise<ExecutionResult> {
): Promise<ExecutionResultAndCallGasLimit> {
return await this.estimationService.estimateUserOp(userOp, entryPoint);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { providers } from "ethers";
import { Logger } from "@skandha/types/lib";
import { UserOperation } from "@skandha/types/lib/contracts/UserOperation";
import { ExecutionResult } from "../../../interfaces";
import { ExecutionResultAndCallGasLimit } from "../../../interfaces";
import { EntryPointService } from "../../EntryPointService";
import { mergeValidationDataValues } from "../../EntryPointService/utils";

Expand All @@ -15,22 +15,23 @@ export class EstimationService {
async estimateUserOp(
userOp: UserOperation,
entryPoint: string
): Promise<ExecutionResult> {
const returnInfo = await this.entryPointService.simulateHandleOp(
entryPoint,
userOp
);
): Promise<ExecutionResultAndCallGasLimit> {
const { returnInfo, callGasLimit } =
await this.entryPointService.simulateHandleOp(entryPoint, userOp);
const { validAfter, validUntil } = mergeValidationDataValues(
returnInfo.accountValidationData,
returnInfo.paymasterValidationData
);
return {
preOpGas: returnInfo.preOpGas,
paid: returnInfo.paid,
validAfter: validAfter,
validUntil: validUntil,
targetSuccess: returnInfo.targetSuccess,
targetResult: returnInfo.targetResult,
returnInfo: {
preOpGas: returnInfo.preOpGas,
paid: returnInfo.paid,
validAfter: validAfter,
validUntil: validUntil,
targetSuccess: returnInfo.targetSuccess,
targetResult: returnInfo.targetResult,
},
callGasLimit,
};
}
}
4 changes: 2 additions & 2 deletions packages/monitoring/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publishConfig": {
"access": "public"
},
"version": "2.0.6",
"version": "2.0.7",
"description": "The Monitoring module of Etherspot bundler client",
"author": "Etherspot",
"homepage": "https://github.com/etherspot/etherspot-bundler#readme",
Expand Down Expand Up @@ -32,7 +32,7 @@
"check-readme": "typescript-docs-verifier"
},
"dependencies": {
"@skandha/types": "^2.0.6",
"@skandha/types": "^2.0.7",
"prom-client": "15.1.0"
}
}
16 changes: 8 additions & 8 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publishConfig": {
"access": "public"
},
"version": "2.0.6",
"version": "2.0.7",
"description": "The bundler node module of Etherspot bundler client",
"author": "Etherspot",
"homepage": "https://https://github.com/etherspot/skandha#readme",
Expand Down Expand Up @@ -56,13 +56,13 @@
"@libp2p/prometheus-metrics": "1.1.3",
"@libp2p/tcp": "6.1.0",
"@multiformats/multiaddr": "11.4.0",
"@skandha/api": "^2.0.6",
"@skandha/db": "^2.0.6",
"@skandha/executor": "^2.0.6",
"@skandha/monitoring": "^2.0.6",
"@skandha/params": "^2.0.6",
"@skandha/types": "^2.0.6",
"@skandha/utils": "^2.0.6",
"@skandha/api": "^2.0.7",
"@skandha/db": "^2.0.7",
"@skandha/executor": "^2.0.7",
"@skandha/monitoring": "^2.0.7",
"@skandha/params": "^2.0.7",
"@skandha/types": "^2.0.7",
"@skandha/utils": "^2.0.7",
"abstract-leveldown": "7.2.0",
"datastore-core": "8.0.1",
"ethers": "5.7.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/params/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publishConfig": {
"access": "public"
},
"version": "2.0.6",
"version": "2.0.7",
"description": "Various bundler parameters",
"author": "Etherspot",
"homepage": "https://github.com/etherspot/skandha#readme",
Expand All @@ -28,8 +28,8 @@
"@arbitrum/sdk": "3.1.4",
"@chainsafe/ssz": "0.10.1",
"@mantleio/sdk": "0.2.1",
"@skandha/types": "^2.0.6",
"@skandha/utils": "^2.0.6",
"@skandha/types": "^2.0.7",
"@skandha/utils": "^2.0.7",
"ethers": "5.7.2"
},
"scripts": {
Expand Down
Loading

0 comments on commit 5d8e008

Please sign in to comment.