Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease the fee because of increased number of transactions #119

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions SystemConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"L1_GAS_PER_PUBDATA_BYTE": 17,
"BATCH_OVERHEAD_L2_GAS": 1200000,
"BATCH_OVERHEAD_L1_GAS": 1000000,
"MAX_TRANSACTIONS_IN_BATCH": 1024,
"BOOTLOADER_TX_ENCODING_SPACE": 8740224,
"MAX_TRANSACTIONS_IN_BATCH": 10000,
"BOOTLOADER_TX_ENCODING_SPACE": 9090400,
"L1_TX_INTRINSIC_L2_GAS": 167157,
"L1_TX_INTRINSIC_PUBDATA": 88,
"L1_TX_MIN_L2_GAS_BASE": 173484,
Expand Down
4 changes: 1 addition & 3 deletions ethereum/contracts/dev-contracts/test/MailboxFacetTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ contract MailboxFacetTest is MailboxFacet {
s.feeParams = _feeParams;
}

function getL2GasPrice(
uint256 _l1GasPrice
) external view returns (uint256) {
function getL2GasPrice(uint256 _l1GasPrice) external view returns (uint256) {
return _deriveL2GasPrice(_l1GasPrice, REQUIRED_L2_GAS_PRICE_PER_PUBDATA);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ contract TransactionValidatorTest {
TransactionValidator.validateUpgradeTransaction(_transaction);
}

function getOverheadForTransaction(
uint256 _encodingLength
) external pure returns (uint256) {
function getOverheadForTransaction(uint256 _encodingLength) external pure returns (uint256) {
return TransactionValidator.getOverheadForTransaction(_encodingLength);
}
}
2 changes: 1 addition & 1 deletion ethereum/contracts/zksync/Config.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ uint256 constant PACKED_L2_BLOCK_TIMESTAMP_MASK = 0xffffffffffffffffffffffffffff
/// need to pay to compensate for the batch being closed.
/// @dev It is expected that the L1 contracts will enforce that the L2 gas price will be high enough to compensate
/// the operator in case the batch is closed because of tx slots filling up.
uint256 constant TX_SLOT_OVERHEAD_L2_GAS = 80000;
uint256 constant TX_SLOT_OVERHEAD_L2_GAS = 10000;

/// @dev The overhead for each byte of the bootloader memory that the encoding of the transaction.
/// It is roughly equal to 80kk/BOOTLOADER_MEMORY_FOR_TXS, i.e. how many gas would an L1->L2 transaction
Expand Down
73 changes: 44 additions & 29 deletions ethereum/test/unit_tests/mailbox_test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { expect } from "chai";
import * as hardhat from "hardhat";
import { Action, facetCut, diamondCut } from "../../src.ts/diamondCut";
import { MailboxFacet, MockExecutorFacet, Forwarder, MailboxFacetTestFactory, MailboxFacetTest } from "../../typechain";
import { MailboxFacetFactory, MockExecutorFacetFactory, DiamondInitFactory, ForwarderFactory } from "../../typechain";
import type { MailboxFacet, MockExecutorFacet, Forwarder, MailboxFacetTest } from "../../typechain";
import {
MailboxFacetTestFactory,
MailboxFacetFactory,
MockExecutorFacetFactory,
DiamondInitFactory,
ForwarderFactory,
} from "../../typechain";
import {
DEFAULT_REVERT_REASON,
getCallRevertReason,
Expand Down Expand Up @@ -210,7 +216,9 @@ describe("Mailbox tests", function () {
let testContract: MailboxFacetTest;
const TEST_GAS_PRICES = [];

async function testOnAllGasPrices(testFunc: (price: ethers.BigNumber) => ethers.utils.Deferrable<ethers.BigNumber>) {
async function testOnAllGasPrices(
testFunc: (price: ethers.BigNumber) => ethers.utils.Deferrable<ethers.BigNumber>
) {
for (const gasPrice of TEST_GAS_PRICES) {
expect(await testContract.getL2GasPrice(gasPrice)).to.eq(testFunc(gasPrice));
}
Expand All @@ -223,43 +231,49 @@ describe("Mailbox tests", function () {

// Generating 10 more gas prices for test suit
let priceGwei = 0.001;
while(priceGwei < 10000) {
while (priceGwei < 10000) {
priceGwei *= 2;
const priceWei = ethers.utils.parseUnits(priceGwei.toString(), 'gwei');
const priceWei = ethers.utils.parseUnits(priceGwei.toString(), "gwei");
TEST_GAS_PRICES.push(priceWei);
}
});

it("Should allow simulating old behaviour", async () => {
// Simulating old L2 gas price calculations might be helpful for migration between the systems
await (await testContract.setFeeParams({
...defaultFeeParams(),
pubdataPricingMode: PubdataPricingMode.Rollup,
batchOverheadL1Gas: 0,
minimalL2GasPrice: 500_000_000,
})).wait();
await (
await testContract.setFeeParams({
...defaultFeeParams(),
pubdataPricingMode: PubdataPricingMode.Rollup,
batchOverheadL1Gas: 0,
minimalL2GasPrice: 500_000_000,
})
).wait();

// Testing the logic under low / medium / high L1 gas price
testOnAllGasPrices(expectedLegacyL2GasPrice);
});

it("Should allow free pubdata", async () => {
await (await testContract.setFeeParams({
...defaultFeeParams(),
pubdataPricingMode: PubdataPricingMode.Validium,
batchOverheadL1Gas: 0
})).wait();

await (
await testContract.setFeeParams({
...defaultFeeParams(),
pubdataPricingMode: PubdataPricingMode.Validium,
batchOverheadL1Gas: 0,
})
).wait();

// The gas price per pubdata is still constant, however, the L2 gas price is always equal to the minimalL2GasPrice
testOnAllGasPrices(() => {
return ethers.BigNumber.from(defaultFeeParams().minimalL2GasPrice);
})
});
});

it("Should work fine in general case", async () => {
await (await testContract.setFeeParams({
...defaultFeeParams(),
})).wait();
await (
await testContract.setFeeParams({
...defaultFeeParams(),
})
).wait();

testOnAllGasPrices(calculateL2GasPrice);
});
Expand Down Expand Up @@ -363,7 +377,7 @@ function calculateL2GasPrice(l1GasPrice: ethers.BigNumber) {

let pubdataPriceETH = ethers.BigNumber.from(0);
if (feeParams.pubdataPricingMode === PubdataPricingMode.Rollup) {
pubdataPriceETH = l1GasPrice.mul(17);
pubdataPriceETH = l1GasPrice.mul(17);
}

const batchOverheadETH = l1GasPrice.mul(feeParams.batchOverheadL1Gas);
Expand All @@ -379,19 +393,20 @@ function calculateL2GasPrice(l1GasPrice: ethers.BigNumber) {
return minL2GasPriceETH;
}


function expectedLegacyL2GasPrice(l1GasPrice: ethers.BigNumberish) {
// In the previous release the following code was used to calculate the L2 gas price for L1->L2 transactions:
//
//
// uint256 pubdataPriceETH = L1_GAS_PER_PUBDATA_BYTE * _l1GasPrice;
// uint256 minL2GasPriceETH = (pubdataPriceETH + _gasPricePerPubdata - 1) / _gasPricePerPubdata;
// return Math.max(FAIR_L2_GAS_PRICE, minL2GasPriceETH);
//
//

const pubdataPriceETH = ethers.BigNumber.from(l1GasPrice).mul(17);
const gasPricePerPubdata = ethers.BigNumber.from(REQUIRED_L2_GAS_PRICE_PER_PUBDATA);
const gasPricePerPubdata = ethers.BigNumber.from(REQUIRED_L2_GAS_PRICE_PER_PUBDATA);
const FAIR_L2_GAS_PRICE = 500_000_000; // 0.5 gwei
const minL2GasPirceETH = ethers.BigNumber.from(pubdataPriceETH.add(gasPricePerPubdata).sub(1)).div(gasPricePerPubdata);
const minL2GasPirceETH = ethers.BigNumber.from(pubdataPriceETH.add(gasPricePerPubdata).sub(1)).div(
gasPricePerPubdata
);

return ethers.BigNumber.from(Math.max(FAIR_L2_GAS_PRICE, minL2GasPirceETH.toNumber()))
return ethers.BigNumber.from(Math.max(FAIR_L2_GAS_PRICE, minL2GasPirceETH.toNumber()));
}
24 changes: 11 additions & 13 deletions ethereum/test/unit_tests/transaction_validator_test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ describe("TransactionValidator tests", function () {
);
expect(result).equal("uk");

const result = await getCallRevertReason(
tester.validateL1ToL2Transaction(
createTestTransaction({}),
500000,
1
)
);
const result = await getCallRevertReason(tester.validateL1ToL2Transaction(createTestTransaction({}), 500000, 1));
expect(result).equal("uk");
});

Expand All @@ -72,12 +66,16 @@ describe("TransactionValidator tests", function () {
});

it("Should allow large transactions if the caller is fine with it", async () => {
// This transaction could publish 2B bytes of pubdata & has 2B gas, which is more than would be typically
// This transaction could publish 2B bytes of pubdata & has 2B gas, which is more than would be typically
// allowed in the production system
await tester.validateL1ToL2Transaction(createTestTransaction({
gasPergasPerPubdataByteLimit: 1,
gasLimit: 2_000_000_000,
}), 2_000_000_000, 2_000_000_000);
await tester.validateL1ToL2Transaction(
createTestTransaction({
gasPergasPerPubdataByteLimit: 1,
gasLimit: 2_000_000_000,
}),
2_000_000_000,
2_000_000_000
);
});
});

Expand Down Expand Up @@ -245,7 +243,7 @@ function createTestTransaction(overrides) {

function getCorrectOverheadForTransaction(len: number) {
const MEMORY_BYTE_OVERHEAD = 10;
const TX_SLOT_OVERHEAD = 80000;
const TX_SLOT_OVERHEAD = 10000;

return Math.max(len * MEMORY_BYTE_OVERHEAD, TX_SLOT_OVERHEAD);
}
Loading