From c09302872b459ffe006a9bbd87907131c7365605 Mon Sep 17 00:00:00 2001 From: Blair Marshall Date: Thu, 31 Oct 2024 16:08:53 -0400 Subject: [PATCH] use ctx instead --- src/contracts/atlas/Atlas.sol | 4 +++ src/contracts/atlas/Escrow.sol | 30 +++++++++++++++---- src/contracts/atlas/SafetyLocks.sol | 5 +++- src/contracts/common/ExecutionEnvironment.sol | 24 ++------------- src/contracts/types/EscrowTypes.sol | 5 ---- src/contracts/types/LockTypes.sol | 6 ++++ test/ExecutionBase.t.sol | 4 ++- test/GasAccounting.t.sol | 4 ++- test/Permit69.t.sol | 4 ++- test/libraries/SafetyBits.t.sol | 4 ++- 10 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/contracts/atlas/Atlas.sol b/src/contracts/atlas/Atlas.sol index c17d08258..194289737 100644 --- a/src/contracts/atlas/Atlas.sol +++ b/src/contracts/atlas/Atlas.sol @@ -19,6 +19,8 @@ import { SafetyBits } from "src/contracts/libraries/SafetyBits.sol"; import { IL2GasCalculator } from "src/contracts/interfaces/IL2GasCalculator.sol"; import { IDAppControl } from "src/contracts/interfaces/IDAppControl.sol"; +import "forge-std/Test.sol"; + /// @title Atlas V1 /// @author FastLane Labs /// @notice The Execution Abstraction protocol. @@ -146,6 +148,8 @@ contract Atlas is Escrow, Factory { _returnData = _executePreOpsCall(ctx, dConfig, userOp); } + console.log(ctx.reimbursements[0].reimburser); + // UserOp Call _returnData = _executeUserOperation(ctx, dConfig, userOp, _returnData); diff --git a/src/contracts/atlas/Escrow.sol b/src/contracts/atlas/Escrow.sol index 77647490f..8e476480d 100644 --- a/src/contracts/atlas/Escrow.sol +++ b/src/contracts/atlas/Escrow.sol @@ -57,26 +57,46 @@ abstract contract Escrow is AtlETH { withLockPhase(ExecutionPhase.PreOps) returns (bytes memory) { + uint256 gasBefore = gasleft(); + (bool _success, bytes memory _data) = ctx.executionEnvironment.call( abi.encodePacked( abi.encodeCall(IExecutionEnvironment.preOpsWrapper, userOp), ctx.setAndPack(ExecutionPhase.PreOps) ) ); - // Decode _data as a tuple of (Reimbursement, bytes) - (Reimbursement memory reimbursement, bytes memory preOpsData) = abi.decode(_data, (Reimbursement, bytes)); - console.log(reimbursement.reimburser); + uint256 gasUsed = gasBefore - gasleft(); + + Reimbursement memory newReimbursement = Reimbursement({ + gasUsed: gasUsed, + reimburser: 0x1234567890123456789012345678901234567890 + }); + + // Create a new memory array with an additional slot + Reimbursement[] memory updatedReimbursements = new Reimbursement[](ctx.reimbursements.length + 1); + + // Copy existing elements + for (uint i = 0; i < ctx.reimbursements.length; i++) { + updatedReimbursements[i] = ctx.reimbursements[i]; + } + + // Add the new reimbursement at the end + updatedReimbursements[ctx.reimbursements.length] = newReimbursement; + + // Update the context with the new array + ctx.reimbursements = updatedReimbursements; if (_success) { if (dConfig.callConfig.needsPreOpsReturnData()) { - return abi.decode(preOpsData, (bytes)); + return abi.decode(_data, (bytes)); } else { return new bytes(0); } } if (ctx.isSimulation) revert PreOpsSimFail(); - revert PreOpsFail(); + // revert PreOpsFail(); + return new bytes(0); } /// @notice Executes the user operation logic defined in the Execution Environment. diff --git a/src/contracts/atlas/SafetyLocks.sol b/src/contracts/atlas/SafetyLocks.sol index 2b3d66b92..7492a7b1b 100644 --- a/src/contracts/atlas/SafetyLocks.sol +++ b/src/contracts/atlas/SafetyLocks.sol @@ -64,6 +64,8 @@ abstract contract SafetyLocks is Storage { pure returns (Context memory) { + Reimbursement[] memory reimbursements; + return Context({ executionEnvironment: executionEnvironment, userOpHash: userOpHash, @@ -76,7 +78,8 @@ abstract contract SafetyLocks is Storage { solverOutcome: 0, bidFind: false, isSimulation: isSimulation, - callDepth: 0 + callDepth: 0, + reimbursements: reimbursements }); } } diff --git a/src/contracts/common/ExecutionEnvironment.sol b/src/contracts/common/ExecutionEnvironment.sol index 37498d9f6..b818e2c22 100644 --- a/src/contracts/common/ExecutionEnvironment.sol +++ b/src/contracts/common/ExecutionEnvironment.sol @@ -15,8 +15,6 @@ import "src/contracts/types/SolverOperation.sol"; import "src/contracts/types/UserOperation.sol"; import "src/contracts/types/EscrowTypes.sol"; -import "forge-std/Test.sol"; - /// @title ExecutionEnvironment /// @author FastLane Labs /// @notice An Execution Environment contract is deployed for each unique combination of User address x DAppControl @@ -48,32 +46,14 @@ contract ExecutionEnvironment is Base { onlyAtlasEnvironment returns (bytes memory) { - uint256 gasBefore = gasleft(); - bytes memory _preOpsData = _forward(abi.encodeCall(IDAppControl.preOpsCall, userOp)); bool _success; (_success, _preOpsData) = _control().delegatecall(_preOpsData); - uint256 gasUsed = gasBefore - gasleft(); - - // if (!_success) revert AtlasErrors.PreOpsDelegatecallFail(); - // Create reimbursement struct based on the success of the delegate call - Reimbursement memory reimbursement = Reimbursement({ - gasUsed: gasUsed, - reimburser: 0x1234567890123456789012345678901234567890 //need to get this from somewhere - }); - - // If the delegate call failed, encode the reimbursement data only - if (!_success) { - _preOpsData = abi.encode(reimbursement, bytes("")); - } else { - _preOpsData = abi.decode(_preOpsData, (bytes)); - _preOpsData = abi.encode(reimbursement, _preOpsData); - } + if (!_success) revert AtlasErrors.PreOpsDelegatecallFail(); - // _preOpsData = abi.decode(_preOpsData, (bytes)); - // console.logBytes(_preOpsData); + _preOpsData = abi.decode(_preOpsData, (bytes)); return _preOpsData; } diff --git a/src/contracts/types/EscrowTypes.sol b/src/contracts/types/EscrowTypes.sol index e6fde55cf..7568cb582 100644 --- a/src/contracts/types/EscrowTypes.sol +++ b/src/contracts/types/EscrowTypes.sol @@ -24,11 +24,6 @@ struct SolverTracker { bool invertsBidValue; } -struct Reimbursement { - uint256 gasUsed; - address reimburser; -} - /// @title SolverOutcome /// @notice Enum for SolverOutcome /// @dev Multiple SolverOutcomes can be used to represent the outcome of a solver call diff --git a/src/contracts/types/LockTypes.sol b/src/contracts/types/LockTypes.sol index f5831ff6d..3fda8d9dd 100644 --- a/src/contracts/types/LockTypes.sol +++ b/src/contracts/types/LockTypes.sol @@ -14,6 +14,12 @@ struct Context { bool bidFind; bool isSimulation; address bundler; + Reimbursement[] reimbursements; // array of reimbursements +} + +struct Reimbursement { + uint256 gasUsed; + address reimburser; } enum ExecutionPhase { diff --git a/test/ExecutionBase.t.sol b/test/ExecutionBase.t.sol index 5cb1a0b11..86b18f189 100644 --- a/test/ExecutionBase.t.sol +++ b/test/ExecutionBase.t.sol @@ -73,6 +73,7 @@ contract ExecutionBaseTest is BaseTest { pure returns (bytes memory firstSet, Context memory _ctx) { + Reimbursement[] memory reimbursements; _ctx = Context({ executionEnvironment: address(123), userOpHash: bytes32(uint256(456)), @@ -85,7 +86,8 @@ contract ExecutionBaseTest is BaseTest { solverOutcome: 2, bidFind: true, isSimulation: false, - callDepth: 1 + callDepth: 1, + reimbursements: reimbursements }); firstSet = abi.encodePacked( diff --git a/test/GasAccounting.t.sol b/test/GasAccounting.t.sol index 39f62bfb0..d62944925 100644 --- a/test/GasAccounting.t.sol +++ b/test/GasAccounting.t.sol @@ -94,6 +94,7 @@ contract MockGasAccounting is TestAtlas, BaseTest { view returns (Context memory ctx) { + Reimbursement[] memory reimbursements; ctx = Context({ executionEnvironment: _activeEnvironment(), userOpHash: bytes32(0), @@ -106,7 +107,8 @@ contract MockGasAccounting is TestAtlas, BaseTest { solverOutcome: 0, bidFind: false, isSimulation: false, - callDepth: 0 + callDepth: 0, + reimbursements: reimbursements }); } diff --git a/test/Permit69.t.sol b/test/Permit69.t.sol index b5b47f4c8..43839b0c5 100644 --- a/test/Permit69.t.sol +++ b/test/Permit69.t.sol @@ -65,6 +65,7 @@ contract Permit69Test is BaseTest { callConfig: mockCallConfig }); + Reimbursement[] memory reimbursements; ctx = Context({ executionEnvironment: mockExecutionEnvAddress, userOpHash: bytes32(0), @@ -77,7 +78,8 @@ contract Permit69Test is BaseTest { solverOutcome: 0, bidFind: false, isSimulation: false, - callDepth: 0 + callDepth: 0, + reimbursements: reimbursements }); mockAtlas.setLock(mockExecutionEnvAddress, mockCallConfig); diff --git a/test/libraries/SafetyBits.t.sol b/test/libraries/SafetyBits.t.sol index 52ba6c979..3e5bfc5c8 100644 --- a/test/libraries/SafetyBits.t.sol +++ b/test/libraries/SafetyBits.t.sol @@ -36,6 +36,7 @@ contract SafetyBitsTest is Test { pure returns (Context memory) { + Reimbursement[] memory reimbursements; return Context({ executionEnvironment: executionEnvironment, userOpHash: userOpHash, @@ -48,7 +49,8 @@ contract SafetyBitsTest is Test { solverOutcome: 0, bidFind: false, isSimulation: isSimulation, - callDepth: 0 + callDepth: 0, + reimbursements: reimbursements }); }