Skip to content

Commit

Permalink
use ctx instead
Browse files Browse the repository at this point in the history
  • Loading branch information
marshabl committed Oct 31, 2024
1 parent bfec934 commit c093028
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 37 deletions.
4 changes: 4 additions & 0 deletions src/contracts/atlas/Atlas.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
30 changes: 25 additions & 5 deletions src/contracts/atlas/Escrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion src/contracts/atlas/SafetyLocks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ abstract contract SafetyLocks is Storage {
pure
returns (Context memory)
{
Reimbursement[] memory reimbursements;

return Context({
executionEnvironment: executionEnvironment,
userOpHash: userOpHash,
Expand All @@ -76,7 +78,8 @@ abstract contract SafetyLocks is Storage {
solverOutcome: 0,
bidFind: false,
isSimulation: isSimulation,
callDepth: 0
callDepth: 0,
reimbursements: reimbursements
});
}
}
24 changes: 2 additions & 22 deletions src/contracts/common/ExecutionEnvironment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 0 additions & 5 deletions src/contracts/types/EscrowTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/contracts/types/LockTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion test/ExecutionBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -85,7 +86,8 @@ contract ExecutionBaseTest is BaseTest {
solverOutcome: 2,
bidFind: true,
isSimulation: false,
callDepth: 1
callDepth: 1,
reimbursements: reimbursements
});

firstSet = abi.encodePacked(
Expand Down
4 changes: 3 additions & 1 deletion test/GasAccounting.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ contract MockGasAccounting is TestAtlas, BaseTest {
view
returns (Context memory ctx)
{
Reimbursement[] memory reimbursements;
ctx = Context({
executionEnvironment: _activeEnvironment(),
userOpHash: bytes32(0),
Expand All @@ -106,7 +107,8 @@ contract MockGasAccounting is TestAtlas, BaseTest {
solverOutcome: 0,
bidFind: false,
isSimulation: false,
callDepth: 0
callDepth: 0,
reimbursements: reimbursements
});
}

Expand Down
4 changes: 3 additions & 1 deletion test/Permit69.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ contract Permit69Test is BaseTest {
callConfig: mockCallConfig
});

Reimbursement[] memory reimbursements;
ctx = Context({
executionEnvironment: mockExecutionEnvAddress,
userOpHash: bytes32(0),
Expand All @@ -77,7 +78,8 @@ contract Permit69Test is BaseTest {
solverOutcome: 0,
bidFind: false,
isSimulation: false,
callDepth: 0
callDepth: 0,
reimbursements: reimbursements
});

mockAtlas.setLock(mockExecutionEnvAddress, mockCallConfig);
Expand Down
4 changes: 3 additions & 1 deletion test/libraries/SafetyBits.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ contract SafetyBitsTest is Test {
pure
returns (Context memory)
{
Reimbursement[] memory reimbursements;
return Context({
executionEnvironment: executionEnvironment,
userOpHash: userOpHash,
Expand All @@ -48,7 +49,8 @@ contract SafetyBitsTest is Test {
solverOutcome: 0,
bidFind: false,
isSimulation: isSimulation,
callDepth: 0
callDepth: 0,
reimbursements: reimbursements
});
}

Expand Down

0 comments on commit c093028

Please sign in to comment.