Skip to content

Commit

Permalink
Change new constants to uint256 for gas optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSparksCode committed Feb 28, 2024
1 parent 71e9bb5 commit fa0e194
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
61 changes: 28 additions & 33 deletions contracts/auction-handler/FastLaneAuctionHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ contract FastLaneAuctionHandler is FastLaneAuctionHandlerEvents {

/// @notice Map[validator] = % payment to validator in a bid with refund
mapping(address => int256) private validatorsRefundShareMap;
uint24 internal constant VALIDATOR_SHARE_BASE = 1_000_000;
uint24 internal constant STAKE_RATIO = 50_000; // 5%

uint256 internal constant VALIDATOR_SHARE_BASE = 1_000_000;
uint256 internal constant STAKE_RATIO = 50_000; // 5%

// TODO: update to point to the correct address
address internal excessBalanceRecipient = address(0);
Expand Down Expand Up @@ -255,16 +255,15 @@ contract FastLaneAuctionHandler is FastLaneAuctionHandlerEvents {
/// @notice Submits a fast bid
/// @dev Will not revert
/// @param fastGasPrice Bonus gasPrice rate that Searcher commits to pay to validator for gas used by searcher's call
/// @param executeOnLoss Boolean flag that enables Searcher calls to execute even if they lost the auction.
/// @param executeOnLoss Boolean flag that enables Searcher calls to execute even if they lost the auction.
/// @param searcherToAddress Searcher contract address to be called on its `fastLaneCall` function.
/// @param searcherCallData callData to be passed to `_searcherToAddress.fastLaneCall(fastPrice,msg.sender,callData)`
function submitFastBid(
uint256 fastGasPrice, // surplus gasprice commited to be paid at the end of execution
bool executeOnLoss, // If true, execute even if searcher lost auction
address searcherToAddress,
bytes calldata searcherCallData
bytes calldata searcherCallData
) external payable onlyEOA nonReentrant {

if (searcherToAddress == address(this) || searcherToAddress == msg.sender) revert RelaySearcherWrongParams();

PGAData memory existing_bid = fulfilledPGAMap[block.number];
Expand All @@ -274,30 +273,31 @@ contract FastLaneAuctionHandler is FastLaneAuctionHandlerEvents {
bool alreadyPaid = existing_bid.paid;

// NOTE: These checks help mitigate the damage to searchers caused by relay error and adversarial validators by reverting
// early if the transactions are not sequenced pursuant to auction rules.
// early if the transactions are not sequenced pursuant to auction rules.

// Do not execute if a fastBid tx with a lower gasPrice was executed prior to this tx in the same block.
// NOTE: This edge case should only be achieveable via validator manipulation or erratic searcher nonce management
// Do not execute if a fastBid tx with a lower gasPrice was executed prior to this tx in the same block.
// NOTE: This edge case should only be achieveable via validator manipulation or erratic searcher nonce management
if (lowestGasPrice != 0 && lowestGasPrice < tx.gasprice) {
emit RelayInvestigateOutcome(block.coinbase, msg.sender, block.number, lowestFastPrice, fastGasPrice, lowestGasPrice, tx.gasprice);

// Do not execute if a fastBid tx with a lower bid amount was executed prior to this tx in the same block.
emit RelayInvestigateOutcome(
block.coinbase, msg.sender, block.number, lowestFastPrice, fastGasPrice, lowestGasPrice, tx.gasprice
);

// Do not execute if a fastBid tx with a lower bid amount was executed prior to this tx in the same block.
} else if (lowestTotalPrice != 0 && lowestTotalPrice <= fastGasPrice + tx.gasprice) {
emit RelayInvestigateOutcome(block.coinbase, msg.sender, block.number, lowestFastPrice, fastGasPrice, lowestGasPrice, tx.gasprice);

// Execute the tx if there are no issues w/ ordering.
// Execute the tx if the searcher enabled executeOnLoss or if the searcher won
} else if (executeOnLoss || !alreadyPaid) {
emit RelayInvestigateOutcome(
block.coinbase, msg.sender, block.number, lowestFastPrice, fastGasPrice, lowestGasPrice, tx.gasprice
);

// Execute the tx if there are no issues w/ ordering.
// Execute the tx if the searcher enabled executeOnLoss or if the searcher won
} else if (executeOnLoss || !alreadyPaid) {
// Use a try/catch pattern so that tx.gasprice and bidAmount can be saved to verify that
// proper transaction ordering is being followed.
try this.fastBidWrapper{value: msg.value}(
msg.sender, fastGasPrice, searcherToAddress, searcherCallData
) returns (uint256 bidAmount) {

// proper transaction ordering is being followed.
try this.fastBidWrapper{value: msg.value}(msg.sender, fastGasPrice, searcherToAddress, searcherCallData)
returns (uint256 bidAmount) {
// Mark this auction as being complete to provide quicker reverts for subsequent searchers
fulfilledPGAMap[block.number] = PGAData({
lowestGasPrice: uint64(tx.gasprice),
lowestGasPrice: uint64(tx.gasprice),
lowestFastPrice: uint64(fastGasPrice),
lowestTotalPrice: uint64(fastGasPrice + tx.gasprice),
paid: true
Expand All @@ -306,27 +306,22 @@ contract FastLaneAuctionHandler is FastLaneAuctionHandlerEvents {
emit RelayFastBid(msg.sender, block.coinbase, true, bidAmount, searcherToAddress);

return; // return early so that we don't refund the searcher's msg.value

} catch {
// Update the auction to provide quicker reverts for subsequent searchers
fulfilledPGAMap[block.number] = PGAData({
lowestGasPrice: uint64(tx.gasprice),
lowestGasPrice: uint64(tx.gasprice),
lowestFastPrice: uint64(fastGasPrice),
lowestTotalPrice: uint64(fastGasPrice + tx.gasprice),
paid: alreadyPaid // carry forward any previous wins in the block
});

emit RelayFastBid(msg.sender, block.coinbase, false, 0, searcherToAddress);

}
}

if (msg.value > 0) {
// Refund the searcher any msg.value for failed txs.
SafeTransferLib.safeTransferETH(
msg.sender,
msg.value
);
// Refund the searcher any msg.value for failed txs.
SafeTransferLib.safeTransferETH(msg.sender, msg.value);
}
}

Expand Down Expand Up @@ -519,11 +514,11 @@ contract FastLaneAuctionHandler is FastLaneAuctionHandlerEvents {
/// @param _amount Amount to calculates cuts from
/// @param _share Share bps
/// @return validatorCut Validator cut
function _calculateValidatorShare(uint256 _amount, uint24 _share) internal pure returns (uint256 validatorCut) {
function _calculateValidatorShare(uint256 _amount, uint256 _share) internal pure returns (uint256 validatorCut) {
validatorCut = (_amount * (VALIDATOR_SHARE_BASE - _share)) / VALIDATOR_SHARE_BASE;
}

function getCurrentStakeRatio() public view returns (uint24) {
function getCurrentStakeRatio() public view returns (uint256) {
return STAKE_RATIO;
}

Expand Down
9 changes: 6 additions & 3 deletions contracts/interfaces/IFastLaneAuctionHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ interface IFastLaneAuctionHandler {
address searcherToAddress,
bytes memory searcherCallData
) external payable;
function submitFastBid(uint256 fastGasPrice, bool executeOnLoss, address searcherToAddress, bytes memory searcherCallData)
external
payable;
function submitFastBid(
uint256 fastGasPrice,
bool executeOnLoss,
address searcherToAddress,
bytes memory searcherCallData
) external payable;
function submitFlashBid(
uint256 bidAmount,
bytes32 oppTxHash,
Expand Down
2 changes: 1 addition & 1 deletion test/PFL_AuctionHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ contract PFLAuctionHandlerTest is PFLHelper, FastLaneAuctionHandlerEvents, Test
emit RelayProcessingExcessBalance(address(0), excessBalance);

vm.prank(VALIDATOR1);

uint256 returnedAmountPaid = PFR.collectFees();
uint256 actualAmountPaid = VALIDATOR1.balance - balanceBefore;
uint256 excessRecipientBalanceAfter = EXCESS_RECIPIENT.balance;
Expand Down

0 comments on commit fa0e194

Please sign in to comment.