From a2cd779a908a8243e8b0893a5bfd2dafc0bb6d8a Mon Sep 17 00:00:00 2001 From: shahafn Date: Mon, 2 Oct 2023 13:34:03 +0300 Subject: [PATCH] Staked account rep (#150) * Tracking staked account reputation * Adding clearMempool, clearReputation debug rpc calls --- packages/bundler/package.json | 2 +- packages/bundler/src/BundlerConfig.ts | 6 +++++- packages/bundler/src/BundlerServer.ts | 8 ++++++++ packages/bundler/src/DebugMethodHandler.ts | 8 ++++++++ packages/bundler/src/modules/EventsManager.ts | 2 +- packages/bundler/src/modules/MempoolManager.ts | 12 +++++++++--- packages/bundler/src/modules/ReputationManager.ts | 5 +++-- 7 files changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/bundler/package.json b/packages/bundler/package.json index 238cd8e0..a698119d 100644 --- a/packages/bundler/package.json +++ b/packages/bundler/package.json @@ -1,6 +1,6 @@ { "name": "@account-abstraction/bundler", - "version": "0.6.1", + "version": "0.6.2", "license": "MIT", "private": true, "files": [ diff --git a/packages/bundler/src/BundlerConfig.ts b/packages/bundler/src/BundlerConfig.ts index ffdecf3f..0b808915 100644 --- a/packages/bundler/src/BundlerConfig.ts +++ b/packages/bundler/src/BundlerConfig.ts @@ -1,6 +1,8 @@ // TODO: consider adopting config-loading approach from hardhat to allow code in config file import ow from 'ow' +const MIN_UNSTAKE_DELAY = 86400 +const MIN_STAKE_VALUE = 1e18.toString() export interface BundlerConfig { beneficiary: string entryPoint: string @@ -48,5 +50,7 @@ export const bundlerConfigDefault: Partial = { port: '3000', entryPoint: '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789', unsafe: false, - conditionalRpc: false + conditionalRpc: false, + minStake: MIN_STAKE_VALUE, + minUnstakeDelay: MIN_UNSTAKE_DELAY } diff --git a/packages/bundler/src/BundlerServer.ts b/packages/bundler/src/BundlerServer.ts index bda1f810..de17bbda 100644 --- a/packages/bundler/src/BundlerServer.ts +++ b/packages/bundler/src/BundlerServer.ts @@ -185,6 +185,10 @@ export class BundlerServer { case 'debug_bundler_dumpMempool': result = await this.debugHandler.dumpMempool() break + case 'debug_bundler_clearMempool': + this.debugHandler.clearMempool() + result = 'ok' + break case 'debug_bundler_setReputation': await this.debugHandler.setReputation(params[0]) result = 'ok' @@ -192,6 +196,10 @@ export class BundlerServer { case 'debug_bundler_dumpReputation': result = await this.debugHandler.dumpReputation() break + case 'debug_bundler_clearReputation': + this.debugHandler.clearReputation() + result = 'ok' + break case 'debug_bundler_setBundlingMode': await this.debugHandler.setBundlingMode(params[0]) result = 'ok' diff --git a/packages/bundler/src/DebugMethodHandler.ts b/packages/bundler/src/DebugMethodHandler.ts index 0d0fb72f..3027b986 100644 --- a/packages/bundler/src/DebugMethodHandler.ts +++ b/packages/bundler/src/DebugMethodHandler.ts @@ -50,6 +50,10 @@ export class DebugMethodHandler { return this.mempoolMgr.dump() } + clearMempool (): void { + this.mempoolMgr.clearState() + } + setReputation (param: any): ReputationDump { return this.repManager.setReputation(param) } @@ -57,4 +61,8 @@ export class DebugMethodHandler { dumpReputation (): ReputationDump { return this.repManager.dump() } + + clearReputation (): void { + this.repManager.clearState() + } } diff --git a/packages/bundler/src/modules/EventsManager.ts b/packages/bundler/src/modules/EventsManager.ts index 8e773158..22e13802 100644 --- a/packages/bundler/src/modules/EventsManager.ts +++ b/packages/bundler/src/modules/EventsManager.ts @@ -91,7 +91,7 @@ export class EventsManager { } _includedAddress (data: string | null): void { - if (data != null && data.length > 42) { + if (data != null && data.length >= 42) { const addr = data.slice(0, 42) this.reputationManager.updateIncludedStatus(addr) } diff --git a/packages/bundler/src/modules/MempoolManager.ts b/packages/bundler/src/modules/MempoolManager.ts index ea9a113b..44e480e1 100644 --- a/packages/bundler/src/modules/MempoolManager.ts +++ b/packages/bundler/src/modules/MempoolManager.ts @@ -1,6 +1,6 @@ import { BigNumber, BigNumberish } from 'ethers' import { getAddr } from './moduleUtils' -import { requireCond } from '../utils' +import { requireCond, RpcError } from '../utils' import { ReputationManager } from './ReputationManager' import Debug from 'debug' import { ReferencedCodeHashes, StakeInfo, UserOperation, ValidationErrors } from './Types' @@ -100,10 +100,16 @@ export class MempoolManager { this.checkReputation(senderInfo, paymasterInfo, factoryInfo, aggregatorInfo) this.mempool.push(entry) } - this.updateSeenStatus(aggregatorInfo?.addr, userOp) + this.updateSeenStatus(aggregatorInfo?.addr, userOp, senderInfo) } - private updateSeenStatus (aggregator: string | undefined, userOp: UserOperation): void { + private updateSeenStatus (aggregator: string | undefined, userOp: UserOperation, senderInfo: StakeInfo): void { + try { + this.reputationManager.checkStake('account', senderInfo) + this.reputationManager.updateSeenStatus(userOp.sender) + } catch (e: any) { + if (!(e instanceof RpcError)) throw e + } this.reputationManager.updateSeenStatus(aggregator) this.reputationManager.updateSeenStatus(getAddr(userOp.paymasterAndData)) this.reputationManager.updateSeenStatus(getAddr(userOp.initCode)) diff --git a/packages/bundler/src/modules/ReputationManager.ts b/packages/bundler/src/modules/ReputationManager.ts index 72fd9f27..b9f79dc2 100644 --- a/packages/bundler/src/modules/ReputationManager.ts +++ b/packages/bundler/src/modules/ReputationManager.ts @@ -21,8 +21,8 @@ export interface ReputationParams { export const BundlerReputationParams: ReputationParams = { minInclusionDenominator: 10, - throttlingSlack: 5, - banSlack: 10 + throttlingSlack: 10, + banSlack: 50 } export const NonBundlerReputationParams: ReputationParams = { @@ -58,6 +58,7 @@ export class ReputationManager { * debug: dump reputation map (with updated "status" for each entry) */ dump (): ReputationDump { + Object.values(this.entries).forEach(entry => { entry.status = this.getStatus(entry.address) }) return Object.values(this.entries) }