Skip to content

Commit

Permalink
fix gas test
Browse files Browse the repository at this point in the history
  • Loading branch information
drortirosh committed Jan 7, 2024
1 parent f53ae17 commit 8ba5146
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
9 changes: 5 additions & 4 deletions contracts/core/EntryPointSimulations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ contract EntryPointSimulations is EntryPoint, IEntryPointSimulations {
// empiric test showed that without this wrapper, simulation depositTo costs less..
function depositTo(address account) public override(IStakeManager, StakeManager) payable {
unchecked{
uint g = gasleft();
uint x = block.number;
while (g - gasleft() < 120) {
x = x + block.number;
// silly code, to waste some gas to make sure depositTo is always little more
// expensive than on-chain call
uint x = 1;
while (x < 5) {
x++;
}
}
StakeManager.depositTo(account);
Expand Down
58 changes: 37 additions & 21 deletions test/entrypoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,19 @@ describe('EntryPoint', function () {

describe('without stake', () => {
it('should fail to stake without value', async () => {
await expect(entryPoint.addStake(2)).to.revertedWith('no stake specified')
expect(await entryPoint.addStake(2)
.catch(e => decodeRevertReason(e)))
.to.match(/no stake specified/)
})
it('should fail to stake without delay', async () => {
await expect(entryPoint.addStake(0, { value: ONE_ETH })).to.revertedWith('must specify unstake delay')
expect(await entryPoint.addStake(0, { value: ONE_ETH })
.catch(e => decodeRevertReason(e)))
.to.match(/must specify unstake delay/)
})
it('should fail to unlock', async () => {
await expect(entryPoint.unlockStake()).to.revertedWith('not staked')
expect(await entryPoint.unlockStake()
.catch(e => decodeRevertReason(e)))
.to.match(/not staked/)
})
})
describe('with stake of 2 eth', () => {
Expand Down Expand Up @@ -162,7 +168,9 @@ describe('EntryPoint', function () {
await expect(entryPoint.withdrawStake(AddressZero)).to.revertedWith('Stake withdrawal is not due')
})
it('should fail to unlock again', async () => {
await expect(entryPoint.unlockStake()).to.revertedWith('already unstaking')
expect(await entryPoint.unlockStake()
.catch(e => decodeRevertReason(e)))
.to.match(/already unstaking/)
})
describe('after unstake delay', () => {
before(async () => {
Expand Down Expand Up @@ -190,7 +198,9 @@ describe('EntryPoint', function () {
})

it('should fail to unlock again', async () => {
await expect(entryPoint.unlockStake()).to.revertedWith('already unstaking')
expect(await entryPoint.unlockStake()
.catch(e => decodeRevertReason(e)))
.to.match(/already unstaking/)
})
it('should succeed to withdraw', async () => {
const { stake } = await entryPoint.getDepositInfo(addr)
Expand Down Expand Up @@ -558,8 +568,14 @@ describe('EntryPoint', function () {
const beneficiaryAddress = createAddress()

// "warmup" userop, for better gas calculation, below
await entryPoint.handleOps([await fillAndSign({ sender: account.address, callData: accountExec.data }, accountOwner, entryPoint)], beneficiaryAddress)
await entryPoint.handleOps([await fillAndSign({ sender: account.address, callData: accountExec.data }, accountOwner, entryPoint)], beneficiaryAddress)
await entryPoint.handleOps([await fillAndSign({
sender: account.address,
callData: accountExec.data
}, accountOwner, entryPoint)], beneficiaryAddress)
await entryPoint.handleOps([await fillAndSign({
sender: account.address,
callData: accountExec.data
}, accountOwner, entryPoint)], beneficiaryAddress)

const op1 = await fillAndSign({
sender: account.address,
Expand Down Expand Up @@ -785,7 +801,7 @@ describe('EntryPoint', function () {
const rcpt = await ret.wait()
const hash = await entryPoint.getUserOpHash(createOp)
await expect(ret).to.emit(entryPoint, 'AccountDeployed')
// eslint-disable-next-line @typescript-eslint/no-base-to-string
// eslint-disable-next-line @typescript-eslint/no-base-to-string
.withArgs(hash, createOp.sender, toChecksumAddress(createOp.initCode.toString().slice(0, 42)), AddressZero)

await calcGasUsage(rcpt!, entryPoint, beneficiaryAddress)
Expand All @@ -809,11 +825,11 @@ describe('EntryPoint', function () {
return
}
/**
* attempt a batch:
* 1. create account1 + "initialize" (by calling counter.count())
* 2. account2.exec(counter.count()
* (account created in advance)
*/
* attempt a batch:
* 1. create account1 + "initialize" (by calling counter.count())
* 2. account2.exec(counter.count()
* (account created in advance)
*/
let counter: TestCounter
let accountExecCounterFromEntryPoint: PopulatedTransaction
const beneficiaryAddress = createAddress()
Expand Down Expand Up @@ -988,14 +1004,14 @@ describe('EntryPoint', function () {
}).filter(ev => ev != null)
// expected "SignatureAggregatorChanged" before every switch of aggregator
expect(events).to.eql([
`agg(${aggregator.address})`,
`userOp(${userOp1.sender})`,
`userOp(${userOp2.sender})`,
`agg(${aggregator3.address})`,
`userOp(${userOp_agg3.sender})`,
`agg(${AddressZero})`,
`userOp(${userOp_noAgg.sender})`,
`agg(${AddressZero})`
`agg(${aggregator.address})`,
`userOp(${userOp1.sender})`,
`userOp(${userOp2.sender})`,
`agg(${aggregator3.address})`,
`userOp(${userOp_agg3.sender})`,
`agg(${AddressZero})`,
`userOp(${userOp_noAgg.sender})`,
`agg(${AddressZero})`
])
})

Expand Down
2 changes: 1 addition & 1 deletion test/entrypointsimulations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('EntryPointSimulations', function () {

function costInRange (simCost: BigNumber, epCost: BigNumber, message: string): void {
const diff = simCost.sub(epCost).toNumber()
const max = 350
const max = 330
expect(diff).to.be.within(0, max,
`${message} cost ${simCost.toNumber()} should be (up to ${max}) above ep cost ${epCost.toNumber()}`)
}
Expand Down

0 comments on commit 8ba5146

Please sign in to comment.