Skip to content

Commit

Permalink
fix gas price oracle test
Browse files Browse the repository at this point in the history
  • Loading branch information
angel-ding-cb committed Feb 28, 2024
1 parent fbdba16 commit c3e8709
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions op-e2e/gas_price_oracle_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package op_e2e

import (
"encoding/binary"
"encoding/hex"
"fmt"
"io/fs"
"math/big"
"os"
Expand All @@ -19,7 +22,47 @@ import (
"github.com/ethereum-optimism/optimism/op-bindings/predeploys"
)

func inputsToHex(inputs []interface{}) ([]byte, error) {
resultBytes := []byte{}
for _, input := range inputs {
switch v := input.(type) {
case int32:
bytes := make([]byte, 4)
binary.BigEndian.PutUint32(bytes, uint32(v))
resultBytes = append(resultBytes, bytes...)
case uint32:
bytes := make([]byte, 4)
binary.BigEndian.PutUint32(bytes, v)
resultBytes = append(resultBytes, bytes...)
case uint64:
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, v)
resultBytes = append(resultBytes, bytes...)
default:
return nil, fmt.Errorf("unsupported type: %T", v)
}
}
// Print the hex-encoded string of 28 bytes
return resultBytes, nil
}

func TestGasPriceOracle(t *testing.T) {

var (
sequenceNumber uint64 = 0
blobFeeScalar uint32 = 1_250_000
baseFeeScalar uint32 = 11_111
costTxSizeCoef int32 = -88_664
costFastlzCoef int32 = 1_031_462
costIntercept int32 = -27_321_890
)

inputs := []interface{}{costIntercept, costFastlzCoef, costTxSizeCoef, baseFeeScalar, blobFeeScalar, sequenceNumber}
payload, err := inputsToHex(inputs)
assert.NoError(t, err)

byteResult := append(make([]byte, 4), payload...)

backend := backends.NewSimulatedBackend(map[common.Address]core.GenesisAccount{
predeploys.GasPriceOracleAddr: {
Code: common.FromHex(bindings.GasPriceOracleDeployedBin),
Expand All @@ -31,6 +74,12 @@ func TestGasPriceOracle(t *testing.T) {
predeploys.L1BlockAddr: {
Code: common.FromHex(bindings.L1BlockDeployedBin),
Balance: big.NewInt(0),
Storage: map[common.Hash]common.Hash{
common.HexToHash("0x1"): common.HexToHash("0x01"), // l1BaseFee 1
common.HexToHash("0x3"): common.HexToHash(hex.EncodeToString(byteResult)), // all constants
common.HexToHash("0x7"): common.HexToHash("0x01"), // l1BlobBaseFee 1

},
},
}, math.MaxUint64)

Expand All @@ -47,14 +96,28 @@ func TestGasPriceOracle(t *testing.T) {
return err
}

used, err := caller.GetL1GasUsed(&bind.CallOpts{}, b)
used, err := caller.GetL1Fee(&bind.CallOpts{}, b)
if err != nil {
return err
}

expected := (types.FlzCompressLen(b) + 68) * 16
assert.Equal(t, used.Uint64(), uint64(expected), path)
l1BaseFee, err := caller.L1BaseFee(&bind.CallOpts{})

if err != nil {
return err
}

l1BaseFeeScaled := uint64(baseFeeScalar) * l1BaseFee.Uint64() * 16
l1BlobBaseFee, err := caller.BlobBaseFee(&bind.CallOpts{})
if err != nil {
return err
}

l1BlobFeeScaled := uint64(blobFeeScalar) * l1BlobBaseFee.Uint64()
l1FeeScaled := l1BaseFeeScaled + l1BlobFeeScaled
fastLzLength := types.FlzCompressLen(b)
expected := ((uint64(costIntercept) + uint64(costFastlzCoef)*uint64(fastLzLength+68) + uint64(costTxSizeCoef)*uint64(len(b)+68)) * uint64(l1FeeScaled)) / 1e12
assert.Equal(t, used.Uint64(), uint64(expected), path)
atLeastOnce = true
return nil
})
Expand Down

0 comments on commit c3e8709

Please sign in to comment.