Skip to content

Commit

Permalink
feat(mono): add proposer estimate l1 gas used
Browse files Browse the repository at this point in the history
  • Loading branch information
luanxu-mxc committed Nov 15, 2023
1 parent c45e3e2 commit a7f72db
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
11 changes: 7 additions & 4 deletions packages/protocol/contracts/L1/MxcL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ contract MxcL1 is EssentialContract, ICrossChainSync, MxcEvents, MxcErrors {
uint256[100] private __gap;

modifier onlyEOA() {
uint size;
uint256 size;
address sender = msg.sender;
assembly { size := extcodesize(sender) }
assembly {
size := extcodesize(sender)
}
require(size == 0, "Only EOA accounts allowed");
_;
}
Expand Down Expand Up @@ -83,7 +85,7 @@ contract MxcL1 is EssentialContract, ICrossChainSync, MxcEvents, MxcErrors {
* `n` transactions in `txList`, then there will be up to `n + 1`
* transactions in the L2 block.
*/
function proposeBlock(bytes calldata input, bytes calldata txList)
function proposeBlock(bytes calldata input, bytes calldata txList, uint256 estimateGas)
external
nonReentrant onlyEOA
returns (MxcData.BlockMetadata memory meta)
Expand All @@ -95,7 +97,8 @@ contract MxcL1 is EssentialContract, ICrossChainSync, MxcEvents, MxcErrors {
config: config,
resolver: AddressResolver(this),
input: abi.decode(input, (MxcData.BlockMetadataInput)),
txList: txList
txList: txList,
estimateGas: estimateGas
});
if (config.maxVerificationsPerTx > 0) {
LibVerifying.verifyBlocks({
Expand Down
24 changes: 13 additions & 11 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interface IArbGasInfo {
function getMinimumGasPrice() external view returns (uint256);
}


library LibProposing {
using SafeCastUpgradeable for uint256;
using LibAddress for address;
Expand All @@ -44,7 +43,8 @@ library LibProposing {
MxcData.Config memory config,
AddressResolver resolver,
MxcData.BlockMetadataInput memory input,
bytes calldata txList
bytes calldata txList,
uint256 estimateGas
) internal returns (MxcData.BlockMetadata memory meta) {
uint256 gasStart = gasleft();

Expand All @@ -70,12 +70,12 @@ library LibProposing {
meta.gasLimit = input.gasLimit;
meta.beneficiary = input.beneficiary;
meta.treasury = resolver.resolve(config.chainId, "treasury", false);
// meta.depositsProcessed = LibEthDepositing.processDeposits(state, config, input.beneficiary);
// meta.depositsProcessed = LibEthDepositing.processDeposits(state, config, input.beneficiary);

uint256 proposeReward = LibTokenomics.getProposeReward(resolver, config, state);
meta.blockReward = uint256(state.proveMetaReward) * 1e16 + proposeReward;
state.proveMetaReward = 0;
if(meta.blockReward > 0) {
if (meta.blockReward > 0) {
LibTokenomics.mintReward(resolver, meta.blockReward);
}

Expand Down Expand Up @@ -106,22 +106,24 @@ library LibProposing {
state.accProposedAt += meta.timestamp;
}

uint256 l2G = input.gasLimit;
if(l2G < 21000) {
l2G = 21000;
if (input.gasLimit < 21000) {
input.gasLimit = 21000;
}
uint256 ethMxcPrice = 90000;
{
address oracle = resolver.resolve("oracle_ethmxc",true);
if(oracle != address(0)) {
address oracle = resolver.resolve("oracle_ethmxc", true);
if (oracle != address(0)) {
ethMxcPrice = uint256(AggregatorInterface(oracle).latestAnswer());
if(ethMxcPrice == 0) {
if (ethMxcPrice == 0) {
ethMxcPrice = 90000;
}
}
}
if (estimateGas < gasStart) {
estimateGas = gasStart;
}
// baseFee relay on arbitrum
meta.baseFee = (block.basefee * (gasStart - gasleft()) * 4 * ethMxcPrice / l2G) / 1 gwei;
meta.baseFee = (block.basefee * estimateGas * 4 * ethMxcPrice / input.gasLimit) / 1 gwei;

if (state.prevBaseFee != 0) {
if (meta.baseFee > (state.prevBaseFee * 105) / 100) {
Expand Down
29 changes: 29 additions & 0 deletions packages/protocol/script/UpgradeMXCL1.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "forge-std/Script.sol";
import "../contracts/common/AddressManager.sol";
import "../contracts/L1/MxcL1.sol";
import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import "../contracts/bridge/EtherVault.sol";
import "../contracts/bridge/TokenVault.sol";
import "../contracts/L1/MxcToken.sol";
import "../contracts/bridge/Bridge.sol";
import "../contracts/thirdparty/WETH9.sol";

contract UpgradeMXCL1 is Script {

uint256 public privateKey = vm.envUint("PRIVATE_KEY");

address payable public MXCL1 = payable(0x54D8864e8855A7B66eE42B8F2Eaa0F2E06bd641a);


function run() external {
vm.startBroadcast(privateKey);
TransparentUpgradeableProxy(MXCL1).upgradeTo(address(new ProxiedMxcL1()));

vm.stopBroadcast();

}

}
6 changes: 4 additions & 2 deletions packages/protocol/test/MxcL1TestBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ contract ArbSysTest is ArbSys {
return blockhash(blockNumber);
}
}

interface IArbGasInfo {
function getMinimumGasPrice() external view returns (uint256);
}

contract ArbGasInfoTest is IArbGasInfo {
function getMinimumGasPrice() external view returns (uint256) {
return 1e8;
Expand Down Expand Up @@ -94,7 +96,7 @@ abstract contract MxcL1TestBase is Test {
vm.etch(address(100), address(arbSysTest).code);
console2.log(ArbSys(arbSysTest).arbBlockNumber());
ArbGasInfoTest arbGasInfoTest = new ArbGasInfoTest();
vm.etch(address(108),address(arbGasInfoTest).code);
vm.etch(address(108), address(arbGasInfoTest).code);
}

function setUp() public virtual {
Expand Down Expand Up @@ -185,7 +187,7 @@ abstract contract MxcL1TestBase is Test {
meta.treasury = L2Treasury;

vm.prank(proposer, proposer);
meta = L1.proposeBlock(abi.encode(input), txList);
meta = L1.proposeBlock(abi.encode(input), txList, 0);
}

function proveBlock(
Expand Down

0 comments on commit a7f72db

Please sign in to comment.