Skip to content

Commit

Permalink
update to 1d model / add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danyalprout committed Apr 28, 2024
1 parent 606074d commit de6e7f2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 101 deletions.
7 changes: 7 additions & 0 deletions op-e2e/actions/ecotone_fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@ func TestEcotoneNetworkUpgradeTransactions(gt *testing.T) {
initialL1BlockAddress, err := ethCl.StorageAt(context.Background(), predeploys.L1BlockAddr, genesis.ImplementationSlot, nil)
require.NoError(t, err)

e, err := gasPriceOracle.IsEcotone(nil)
require.False(t, e)

// Build to the ecotone block
sequencer.ActBuildL2ToEcotone(t)

e, err = gasPriceOracle.IsEcotone(nil)
require.NoError(t, err)
require.True(t, e)

// get latest block
latestBlock, err := ethCl.BlockByNumber(context.Background(), nil)
require.NoError(t, err)
Expand Down
97 changes: 29 additions & 68 deletions op-e2e/actions/fjord_fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,54 @@ import (
)

var (
fjordGasPriceOracleCodeHash = common.HexToHash("0x9ceff82dc9f9bf592dc3954dde0ce8466864229caa8916fffc4005e2fde3d589")
fjordGasPriceOracleCodeHash = common.HexToHash("0xd932e30ee61cd4af3aa471df75293d2c4c3da7aac54ca3bfd5e0b65f4d7f5fa5")
// https://basescan.org/tx/0x8debb2fe54200183fb8baa3c6dbd8e6ec2e4f7a4add87416cd60336b8326d16a
txHex = "02f875822105819b8405709fb884057d460082e97f94273ca93a52b817294830ed7572aa591ccfa647fd80881249c58b0021fb3fc080a05bb08ccfd68f83392e446dac64d88a2d28e7072c06502dfabc4a77e77b5c7913a05878d53dd4ebba4f6367e572d524dffcabeec3abb1d8725ee3ac5dc32e1852e3"

costTxSizeCoef int64 = -88_664
costFastlzCoef int64 = 1_031_462
costIntercept int64 = -27_321_890
costFastlzCoef int64 = 836_500
costIntercept int64 = -42_585_600
)

func TestFjordNetworkUpgradeTransactions(gt *testing.T) {
t := NewDefaultTesting(gt)
dp := e2eutils.MakeDeployParams(t, defaultRollupTestParams)
genesisBlock := hexutil.Uint64(0)
fjordOffset := hexutil.Uint64(2)
ecotoneOffset := hexutil.Uint64(4)
fjordOffset := hexutil.Uint64(8)

dp.DeployConfig.L1CancunTimeOffset = &genesisBlock // can be removed once Cancun on L1 is the default

// Activate all forks at genesis, and schedule Fjord the block after
dp.DeployConfig.L2GenesisRegolithTimeOffset = &genesisBlock
dp.DeployConfig.L2GenesisCanyonTimeOffset = &genesisBlock
dp.DeployConfig.L2GenesisDeltaTimeOffset = &genesisBlock
dp.DeployConfig.L2GenesisEcotoneTimeOffset = &genesisBlock
dp.DeployConfig.L2GenesisEcotoneTimeOffset = &ecotoneOffset
dp.DeployConfig.L2GenesisFjordTimeOffset = &fjordOffset

require.NoError(t, dp.DeployConfig.Check(), "must have valid config")

sd := e2eutils.Setup(t, dp, defaultAlloc)
log := testlog.Logger(t, log.LvlDebug)
_, _, _, sequencer, engine, verifier, _, _ := setupReorgTestActors(t, dp, sd, log)
ethCl := engine.EthClient()

// build a single block to move away from the genesis with 0-values in L1Block contract
sequencer.ActL2StartBlock(t)
sequencer.ActL2EndBlock(t)

// start op-nodes
sequencer.ActL2PipelineFull(t)
verifier.ActL2PipelineFull(t)

// Get current implementations addresses (by slot) for L1Block + GasPriceOracle
initialGasPriceOracleAddress, err := ethCl.StorageAt(context.Background(), predeploys.GasPriceOracleAddr, genesis.ImplementationSlot, nil)
require.NoError(t, err)

// Get gas price from oracle
gasPriceOracle, err := bindings.NewGasPriceOracleCaller(predeploys.GasPriceOracleAddr, ethCl)
require.NoError(t, err)

// Build to the Fjord block
sequencer.ActBuildL2ToEcotone(t)

// Get current implementations addresses (by slot) for L1Block + GasPriceOracle
initialGasPriceOracleAddress, err := ethCl.StorageAt(context.Background(), predeploys.GasPriceOracleAddr, genesis.ImplementationSlot, nil)
require.NoError(t, err)

sequencer.ActBuildL2ToFjord(t)

// get latest block
Expand Down Expand Up @@ -102,17 +106,21 @@ func TestFjordNetworkUpgradeTransactions(gt *testing.T) {
require.NoError(t, err)
require.True(t, isFjord)

// Check GetL1Fee is updated
// Check GetL1GasUsed is updated
txData, err := hex.DecodeString(txHex)
require.NoError(t, err)

used, err := gasPriceOracle.GetL1Fee(&bind.CallOpts{}, txData)
l1GasUsed, err := gasPriceOracle.GetL1GasUsed(&bind.CallOpts{}, txData)
require.NoError(t, err)

fastLzLength := types.FlzCompressLen(txData)
fastLzSize := types.FlzCompressLen(txData)
require.Equal(gt, l1GasUsed.Uint64(), uint64(fastLzSize+68)*16)

cost := fjordL1Cost(t, gasPriceOracle, int64(fastLzLength), int64(len(txData)))
// Check that GetL1Fee takes into account fast LZ
used, err := gasPriceOracle.GetL1Fee(&bind.CallOpts{}, txData)
require.NoError(t, err)

cost := fjordL1Cost(t, gasPriceOracle, int64(fastLzSize))
require.Equal(t, cost.Uint64(), used.Uint64())

// Check that L1FeeUppberBound works
Expand All @@ -121,68 +129,22 @@ func TestFjordNetworkUpgradeTransactions(gt *testing.T) {

flzUpperBound := len(txData) + len(txData)/255 + 16

upperBoundCost := fjordL1Cost(t, gasPriceOracle, int64(flzUpperBound), int64(len(txData)))
upperBoundCost := fjordL1Cost(t, gasPriceOracle, int64(flzUpperBound))
require.Equal(t, upperBoundCost.Uint64(), upperBound.Uint64())
}

func FuzzFastLz(f *testing.F) {
f.Fuzz(func(gt *testing.T, data []byte) {
if len(data) <= 71 {
return
}

t := NewDefaultTesting(gt)
dp := e2eutils.MakeDeployParams(t, defaultRollupTestParams)
genesisBlock := hexutil.Uint64(0)

dp.DeployConfig.L1CancunTimeOffset = &genesisBlock // can be removed once Cancun on L1 is the default

// Activate all forks at genesis, and schedule Fjord the block after
dp.DeployConfig.L2GenesisRegolithTimeOffset = &genesisBlock
dp.DeployConfig.L2GenesisCanyonTimeOffset = &genesisBlock
dp.DeployConfig.L2GenesisDeltaTimeOffset = &genesisBlock
dp.DeployConfig.L2GenesisEcotoneTimeOffset = &genesisBlock
dp.DeployConfig.L2GenesisFjordTimeOffset = &genesisBlock

require.NoError(t, dp.DeployConfig.Check(), "must have valid config")

sd := e2eutils.Setup(t, dp, defaultAlloc)
log := testlog.Logger(t, log.LvlDebug)
_, _, _, sequencer, engine, verifier, _, _ := setupReorgTestActors(t, dp, sd, log)
ethCl := engine.EthClient()

// start op-nodes
sequencer.ActL2PipelineFull(t)
verifier.ActL2PipelineFull(t)

// Build to the Fjord block
sequencer.ActBuildL2ToFjord(t)

// Get gas price from oracle
gasPriceOracle, err := bindings.NewGasPriceOracleCaller(predeploys.GasPriceOracleAddr, ethCl)
require.NoError(t, err)

used, err := gasPriceOracle.GetL1Fee(&bind.CallOpts{}, data)
require.NoError(t, err)

fastLzLength := types.FlzCompressLen(data)
cost := fjordL1Cost(t, gasPriceOracle, int64(fastLzLength), int64(len(data)))

require.Equal(t, cost.Uint64(), used.Uint64())
})
}

// The new cost function:
// l1BaseFeeScaled = l1BaseFeeScalar * l1BaseFee * 16
// l1BlobFeeScaled = l1BlobFeeScalar * l1BlobBaseFee
// l1FeeScaled = l1BaseFeeScaled + l1BlobFeeScaled
// ((intercept + fastlzCoef*fastlzLength + uncompressedTxCoef*uncompressedTxSize) * l1FeeScaled) / 1e12
// ((intercept + fastlzCoef*max(fastlzLength, 71)) * l1FeeScaled) / 1e6
func fjordL1Cost(
t require.TestingT,
gasPriceOracle *bindings.GasPriceOracleCaller,
fastLzLength,
unsignedTxSize int64,
fastLzLength int64,
) *big.Int {
fastLzLength = max(fastLzLength, 71)

baseFeeScalar, err := gasPriceOracle.BaseFeeScalar(nil)
require.NoError(t, err)
l1BaseFee, err := gasPriceOracle.L1BaseFee(nil)
Expand All @@ -198,7 +160,6 @@ func fjordL1Cost(

cost := new(big.Int).Mul(new(big.Int).SetInt64(costFastlzCoef), new(big.Int).SetInt64(fastLzLength+68))
cost = new(big.Int).Add(cost, new(big.Int).SetInt64(costIntercept))
cost = new(big.Int).Add(cost, new(big.Int).Mul(new(big.Int).SetInt64(costTxSizeCoef), new(big.Int).SetInt64(unsignedTxSize+68)))
require.True(t, cost.Sign() >= 0)

cost = new(big.Int).Mul(cost, feeScaled)
Expand Down
Loading

0 comments on commit de6e7f2

Please sign in to comment.