Skip to content

Commit

Permalink
Merge branch 'master' into go-1.18-lint-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hkalodner authored Mar 28, 2022
2 parents 4890745 + eb048fc commit 1cd9504
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 72 deletions.
3 changes: 2 additions & 1 deletion arbnode/inbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/offchainlabs/nitro/statetransfer"

"github.com/offchainlabs/nitro/arbos/util"
nitroutil "github.com/offchainlabs/nitro/util"
"github.com/offchainlabs/nitro/util/testhelpers"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -90,7 +91,7 @@ func TestTransactionStreamer(t *testing.T) {
var blockStates []blockTestState
blockStates = append(blockStates, blockTestState{
balances: map[common.Address]*big.Int{
rewrittenOwnerAddress: new(big.Int).Mul(maxExpectedGasCost, big.NewInt(1_000_000)),
rewrittenOwnerAddress: new(big.Int).Mul(maxExpectedGasCost, big.NewInt(int64(nitroutil.NormalizeL2GasForL1GasInitial(1_000_000, params.GWei)))),
},
accounts: []common.Address{rewrittenOwnerAddress},
numMessages: 1,
Expand Down
2 changes: 1 addition & 1 deletion arbos/block_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var EmitTicketCreatedEvent func(*vm.EVM, [32]byte) error

func createNewHeader(prevHeader *types.Header, l1info *L1Info, state *arbosState.ArbosState, chainConfig *params.ChainConfig) *types.Header {
l2Pricing := state.L2PricingState()
baseFee, err := l2Pricing.GasPriceWei()
baseFee, err := l2Pricing.BaseFeeWei()
state.Restrict(err)

var lastBlockHash common.Hash
Expand Down
51 changes: 19 additions & 32 deletions arbos/l2pricing/l2pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type L2PricingState struct {
rateEstimateInertia storage.StorageBackedUint64
speedLimitPerSecond storage.StorageBackedUint64
maxPerBlockGasLimit storage.StorageBackedUint64
gasPriceWei storage.StorageBackedBigInt
minGasPriceWei storage.StorageBackedBigInt
baseFeeWei storage.StorageBackedBigInt
minBaseFeeWei storage.StorageBackedBigInt
}

const (
Expand All @@ -38,8 +38,8 @@ const (
rateEstimateInertiaOffset
speedLimitPerSecondOffset
maxPerBlockGasLimitOffset
gasPriceWeiOffset
minGasPriceWeiOffset
baseFeeWeiOffset
minBaseFeeWeiOffset
)

const GethBlockGasLimit = 1 << 63
Expand All @@ -54,8 +54,8 @@ func InitializeL2PricingState(sto *storage.Storage) error {
_ = sto.SetUint64ByUint64(rateEstimateInertiaOffset, InitialRateEstimateInertia)
_ = sto.SetUint64ByUint64(speedLimitPerSecondOffset, InitialSpeedLimitPerSecond)
_ = sto.SetUint64ByUint64(maxPerBlockGasLimitOffset, InitialPerBlockGasLimit)
_ = sto.SetUint64ByUint64(gasPriceWeiOffset, InitialBaseFeeWei)
return sto.SetUint64ByUint64(minGasPriceWeiOffset, InitialMinimumGasPriceWei)
_ = sto.SetUint64ByUint64(baseFeeWeiOffset, InitialBaseFeeWei)
return sto.SetUint64ByUint64(minBaseFeeWeiOffset, InitialMinimumBaseFeeWei)
}

func OpenL2PricingState(sto *storage.Storage) *L2PricingState {
Expand All @@ -70,8 +70,8 @@ func OpenL2PricingState(sto *storage.Storage) *L2PricingState {
sto.OpenStorageBackedUint64(rateEstimateInertiaOffset),
sto.OpenStorageBackedUint64(speedLimitPerSecondOffset),
sto.OpenStorageBackedUint64(maxPerBlockGasLimitOffset),
sto.OpenStorageBackedBigInt(gasPriceWeiOffset),
sto.OpenStorageBackedBigInt(minGasPriceWeiOffset),
sto.OpenStorageBackedBigInt(baseFeeWeiOffset),
sto.OpenStorageBackedBigInt(minBaseFeeWeiOffset),
}
}

Expand Down Expand Up @@ -148,36 +148,23 @@ func (ps *L2PricingState) SetRateEstimateInertia(inertia uint64) error {
return ps.rateEstimateInertia.Set(inertia)
}

func (ps *L2PricingState) GasPriceWei() (*big.Int, error) {
return ps.gasPriceWei.Get()
func (ps *L2PricingState) BaseFeeWei() (*big.Int, error) {
return ps.baseFeeWei.Get()
}

func (ps *L2PricingState) SetGasPriceWei(val *big.Int) error {
return ps.gasPriceWei.Set(val)
func (ps *L2PricingState) SetBaseFeeWei(val *big.Int) error {
return ps.baseFeeWei.Set(val)
}

func (ps *L2PricingState) MinGasPriceWei() (*big.Int, error) {
return ps.minGasPriceWei.Get()
func (ps *L2PricingState) MinBaseFeeWei() (*big.Int, error) {
return ps.minBaseFeeWei.Get()
}

func (ps *L2PricingState) SetMinGasPriceWei(val *big.Int) error {
err := ps.minGasPriceWei.Set(val)
if err != nil {
return err
}

// Check if the current gas price is below the new minimum.
curGasPrice, err := ps.gasPriceWei.Get()
if err != nil {
return err
}
if arbmath.BigLessThan(curGasPrice, val) {
// The current gas price is less than the new minimum. Override it.
return ps.gasPriceWei.Set(val)
} else {
// The current gas price is greater than the new minimum. Ignore it.
return nil
}
func (ps *L2PricingState) SetMinBaseFeeWei(val *big.Int) error {
// This modifies the "minimum basefee" parameter, but doesn't modify the current basefee.
// If this increases the minimum basefee, then the basefee might be below the minimum for a little while.
// If so, the basefee will increase by up to a factor of two per block, until it reaches the minimum.
return ps.minBaseFeeWei.Set(val)
}

func (ps *L2PricingState) SpeedLimitPerSecond() (uint64, error) {
Expand Down
4 changes: 2 additions & 2 deletions arbos/l2pricing/l2pricing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ func getGasPool(t *testing.T, pricing *L2PricingState) int64 {
}

func getPrice(t *testing.T, pricing *L2PricingState) uint64 {
value, err := pricing.GasPriceWei()
value, err := pricing.BaseFeeWei()
Require(t, err)
return arbmath.BigToUintOrPanic(value)
}

func getMinPrice(t *testing.T, pricing *L2PricingState) uint64 {
value, err := pricing.MinGasPriceWei()
value, err := pricing.MinBaseFeeWei()
Require(t, err)
return arbmath.BigToUintOrPanic(value)
}
Expand Down
10 changes: 5 additions & 5 deletions arbos/l2pricing/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

const InitialSpeedLimitPerSecond = 1000000
const InitialPerBlockGasLimit uint64 = 20 * 1000000
const InitialMinimumGasPriceWei = 1 * params.GWei
const InitialBaseFeeWei = InitialMinimumGasPriceWei
const InitialMinimumBaseFeeWei = params.GWei / 10
const InitialBaseFeeWei = InitialMinimumBaseFeeWei
const InitialGasPoolSeconds = 10 * 60
const InitialRateEstimateInertia = 60

Expand Down Expand Up @@ -97,8 +97,8 @@ func (ps *L2PricingState) UpdatePricingModel(header *types.Header, timePassed ui
//
exp := (averageOfRatios - arbmath.OneInBips) * arbmath.Bips(timePassed) / 120 // limit to EIP 1559's max rate
price := arbmath.BigMulByBips(header.BaseFee, arbmath.ApproxExpBasisPoints(exp))
maxPrice := arbmath.BigMulByInt(header.BaseFee, 2)
minPrice, _ := ps.MinGasPriceWei()
maxPrice := arbmath.BigMulByInt(header.BaseFee, params.ElasticityMultiplier)
minPrice, _ := ps.MinBaseFeeWei()

p := func(args ...interface{}) {
if debug {
Expand All @@ -118,7 +118,7 @@ func (ps *L2PricingState) UpdatePricingModel(header *types.Header, timePassed ui
log.Warn("ArbOS tried to 2x the price", "price", price, "bound", maxPrice)
price = maxPrice
}
_ = ps.SetGasPriceWei(price)
_ = ps.SetBaseFeeWei(price)
_ = ps.SetGasPool(newGasPool)
ps.SetGasPoolLastBlock(newGasPool)
}
Expand Down
2 changes: 1 addition & 1 deletion arbos/tx_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (p *TxProcessor) GasChargingHook(gasRemaining *uint64) (*common.Address, er
// This will help the user pad the total they'll pay in case the price rises a bit.
// Note, reducing the poster cost will increase share the network fee gets, not reduce the total.

minGasPrice, _ := p.state.L2PricingState().MinGasPriceWei()
minGasPrice, _ := p.state.L2PricingState().MinBaseFeeWei()

adjustedPrice := arbmath.BigMulByFrac(gasPrice, 7, 8) // assume congestion
if arbmath.BigLessThan(adjustedPrice, minGasPrice) {
Expand Down
2 changes: 1 addition & 1 deletion precompiles/ArbGasInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (con ArbGasInfo) GetGasAccountingParams(c ctx, evm mech) (huge, huge, huge,

// Get the minimum gas price needed for a transaction to succeed
func (con ArbGasInfo) GetMinimumGasPrice(c ctx, evm mech) (huge, error) {
return c.state.L2PricingState().MinGasPriceWei()
return c.state.L2PricingState().MinBaseFeeWei()
}

// Get the number of seconds worth of the speed limit the gas pool contains
Expand Down
10 changes: 5 additions & 5 deletions precompiles/ArbOwner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func (con ArbOwner) SetL1BaseFeeEstimateInertia(c ctx, evm mech, inertia uint64)
}

// Sets the L2 gas price directly, bypassing the pool calculus
func (con ArbOwner) SetL2GasPrice(c ctx, evm mech, priceInWei huge) error {
return c.state.L2PricingState().SetGasPriceWei(priceInWei)
func (con ArbOwner) SetL2BaseFee(c ctx, evm mech, priceInWei huge) error {
return c.state.L2PricingState().SetBaseFeeWei(priceInWei)
}

// Sets the minimum gas price needed for a transaction to succeed
func (con ArbOwner) SetMinimumGasPrice(c ctx, evm mech, priceInWei huge) error {
return c.state.L2PricingState().SetMinGasPriceWei(priceInWei)
// Sets the minimum base fee needed for a transaction to succeed
func (con ArbOwner) SetMinimumL2BaseFee(c ctx, evm mech, priceInWei huge) error {
return c.state.L2PricingState().SetMinBaseFeeWei(priceInWei)
}

// Sets the computational speed limit for the chain
Expand Down
8 changes: 4 additions & 4 deletions solgen/src/precompiles/ArbOwner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ interface ArbOwner {
/// @notice Set how slowly ArbOS updates its estimate of the L1 basefee
function setL1BaseFeeEstimateInertia(uint64 inertia) external;

/// @notice Set the L2 gas price directly, bypassing the pool calculus
function setL2GasPrice(uint256 priceInWei) external;
/// @notice Set the L2 basefee directly, bypassing the pool calculus
function setL2BaseFee(uint256 priceInWei) external;

/// @notice Set the minimum gas price needed for a transaction to succeed
function setMinimumGasPrice(uint256 priceInWei) external;
/// @notice Set the minimum basefee needed for a transaction to succeed
function setMinimumL2BaseFee(uint256 priceInWei) external;

/// @notice Set the computational speed limit for the chain
function setSpeedLimit(uint64 limit) external;
Expand Down
30 changes: 21 additions & 9 deletions system_tests/estimation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,33 @@ func TestEstimate(t *testing.T) {
// set the gas price
arbOwner, err := precompilesgen.NewArbOwner(common.HexToAddress("0x70"), client)
Require(t, err, "could not deploy ArbOwner contract")
tx, err := arbOwner.SetMinimumGasPrice(&auth, gasPrice)
tx, err := arbOwner.SetMinimumL2BaseFee(&auth, gasPrice)
Require(t, err, "could not set L2 gas price")
_, err = arbutil.EnsureTxSucceeded(ctx, client, tx)
Require(t, err)

// make an empty block to let the gas price update
TransferBalance(t, "Owner", "Owner", common.Big0, l2info, client, ctx)

// get the gas price
// connect to arbGasInfo precompile
arbGasInfo, err := precompilesgen.NewArbGasInfo(common.HexToAddress("0x6c"), client)
Require(t, err, "could not deploy contract")
_, _, _, _, _, setPrice, err := arbGasInfo.GetPricesInWei(&bind.CallOpts{})
Require(t, err, "could not get L2 gas price")
if gasPrice.Cmp(setPrice) != 0 {
Fail(t, "L2 gas price was not set correctly", gasPrice, setPrice)

// wait for price to come to equilibrium
equilibrated := false
numTriesLeft := 20
for !equilibrated && numTriesLeft > 0 {
// make an empty block to let the gas price update
l2info.GasPrice = new(big.Int).Mul(l2info.GasPrice, big.NewInt(2))
TransferBalance(t, "Owner", "Owner", common.Big0, l2info, client, ctx)

// check if the price has equilibrated
_, _, _, _, _, setPrice, err := arbGasInfo.GetPricesInWei(&bind.CallOpts{})
Require(t, err, "could not get L2 gas price")
if gasPrice.Cmp(setPrice) == 0 {
equilibrated = true
}
numTriesLeft--
}
if !equilibrated {
Fail(t, "L2 gas price did not converge", gasPrice)
}

initialBalance, err := client.BalanceAt(ctx, auth.From, nil)
Expand Down
7 changes: 4 additions & 3 deletions system_tests/retryable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package arbtest

import (
"context"
"github.com/offchainlabs/nitro/arbos/l2pricing"
"math/big"
"testing"
"time"
Expand Down Expand Up @@ -129,7 +130,7 @@ func TestSubmitRetryableImmediateSuccess(t *testing.T) {
beneficiaryAddress,
beneficiaryAddress,
arbmath.UintToBig(estimate),
big.NewInt(params.InitialBaseFee*2),
big.NewInt(l2pricing.InitialBaseFeeWei*2),
[]byte{0x32, 0x42, 0x32, 0x88},
)
Require(t, err)
Expand Down Expand Up @@ -179,7 +180,7 @@ func TestSubmitRetryableFailThenRetry(t *testing.T) {
beneficiaryAddress,
// send enough L2 gas for intrinsic but not compute
big.NewInt(int64(params.TxGas+params.TxDataNonZeroGasEIP2028*4)),
big.NewInt(params.InitialBaseFee*2),
big.NewInt(l2pricing.InitialBaseFeeWei*2),
simpleABI.Methods["increment"].ID,
)
Require(t, err)
Expand Down Expand Up @@ -272,7 +273,7 @@ func TestSubmissionGasCosts(t *testing.T) {
feeRefundAddress,
beneficiaryAddress,
retryableGas,
big.NewInt(params.InitialBaseFee*2),
big.NewInt(l2pricing.InitialBaseFeeWei*2),
retryableCallData,
)
Require(t, err)
Expand Down
8 changes: 5 additions & 3 deletions system_tests/seqinbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"bytes"
"context"
"fmt"
"github.com/offchainlabs/nitro/arbos/l2pricing"
"github.com/offchainlabs/nitro/util"
"math/big"
"math/rand"
"testing"
Expand Down Expand Up @@ -156,7 +158,7 @@ func testSequencerInboxReaderImpl(t *testing.T, validator bool) {
sourceNum := rand.Int() % len(state.accounts)
source := state.accounts[sourceNum]
amount := new(big.Int).SetUint64(uint64(rand.Int()) % state.balances[source].Uint64())
reserveAmount := new(big.Int).SetUint64(params.InitialBaseFee * 10000000)
reserveAmount := new(big.Int).SetUint64(l2pricing.InitialBaseFeeWei * 100000000)
if state.balances[source].Cmp(new(big.Int).Add(amount, reserveAmount)) < 0 {
// Leave enough funds for gas
amount = big.NewInt(1)
Expand All @@ -176,8 +178,8 @@ func testSequencerInboxReaderImpl(t *testing.T, validator bool) {

rawTx := &types.DynamicFeeTx{
To: &dest,
Gas: 210000,
GasFeeCap: big.NewInt(params.InitialBaseFee * 2),
Gas: util.NormalizeL2GasForL1GasInitial(210000, params.GWei),
GasFeeCap: big.NewInt(l2pricing.InitialBaseFeeWei * 2),
Value: amount,
Nonce: state.nonces[source],
}
Expand Down
6 changes: 4 additions & 2 deletions system_tests/test_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"crypto/ecdsa"
"errors"
"github.com/offchainlabs/nitro/arbos/l2pricing"
"github.com/offchainlabs/nitro/util"
"math/big"
"testing"

Expand Down Expand Up @@ -46,8 +48,8 @@ func NewBlockChainTestInfo(t *testing.T, signer types.Signer, gasPrice *big.Int,
}

func NewArbTestInfo(t *testing.T, chainId *big.Int) *BlockchainTestInfo {
var transferGas uint64 = 300_000 // include room for aggregator L1 costs
arbinfo := NewBlockChainTestInfo(t, types.NewArbitrumSigner(types.NewLondonSigner(chainId)), big.NewInt(params.InitialBaseFee*2), transferGas)
var transferGas uint64 = util.NormalizeL2GasForL1GasInitial(300_000, params.GWei) // include room for aggregator L1 costs
arbinfo := NewBlockChainTestInfo(t, types.NewArbitrumSigner(types.NewLondonSigner(chainId)), big.NewInt(l2pricing.InitialBaseFeeWei*2), transferGas)
arbinfo.GenerateGenesysAccount("Owner", new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9)))
arbinfo.GenerateGenesysAccount("Faucet", new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9)))
return arbinfo
Expand Down
7 changes: 4 additions & 3 deletions system_tests/twonodeslong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package arbtest

import (
"context"
"github.com/offchainlabs/nitro/arbos/l2pricing"
"io/ioutil"
"math/big"
"math/rand"
Expand Down Expand Up @@ -70,11 +71,11 @@ func testTwoNodesLong(t *testing.T, dasModeStr string) {
l2info.GenerateAccount("ErrorTxSender")

SendWaitTestTransactions(t, ctx, l2client, []*types.Transaction{
l2info.PrepareTx("Faucet", "ErrorTxSender", l2info.TransferGas, big.NewInt(params.InitialBaseFee*int64(l2info.TransferGas)), nil),
l2info.PrepareTx("Faucet", "ErrorTxSender", l2info.TransferGas, big.NewInt(l2pricing.InitialBaseFeeWei*int64(l2info.TransferGas)), nil),
})

delayedMsgsToSendMax := big.NewInt(int64(largeLoops * avgDelayedMessagesPerLoop * 10))
delayedFaucetNeeds := new(big.Int).Mul(new(big.Int).Add(fundsPerDelayed, new(big.Int).SetUint64(params.InitialBaseFee*100000)), delayedMsgsToSendMax)
delayedFaucetNeeds := new(big.Int).Mul(new(big.Int).Add(fundsPerDelayed, new(big.Int).SetUint64(l2pricing.InitialBaseFeeWei*100000)), delayedMsgsToSendMax)
SendWaitTestTransactions(t, ctx, l2client, []*types.Transaction{
l2info.PrepareTx("Faucet", "DelayedFaucet", l2info.TransferGas, delayedFaucetNeeds, nil),
})
Expand All @@ -84,7 +85,7 @@ func testTwoNodesLong(t *testing.T, dasModeStr string) {
if delayedFaucetBalance.Cmp(delayedFaucetNeeds) != 0 {
t.Fatalf("Unexpected balance, has %v, expects %v", delayedFaucetBalance, delayedFaucetNeeds)
}
t.Logf("DelayedFaucet has %v, per delayd: %v, baseprice: %v", delayedFaucetBalance, fundsPerDelayed, params.InitialBaseFee)
t.Logf("DelayedFaucet has %v, per delayd: %v, baseprice: %v", delayedFaucetBalance, fundsPerDelayed, l2pricing.InitialBaseFeeWei)

if avgTotalL1MessagesPerLoop < avgDelayedMessagesPerLoop {
Fail(t, "bad params, avgTotalL1MessagesPerLoop should include avgDelayedMessagesPerLoop")
Expand Down
11 changes: 11 additions & 0 deletions util/normalizeGas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package util

import (
"github.com/offchainlabs/nitro/arbos/l2pricing"
)

// This function, for testing, adjusts an L2 gas amount that represents L1 gas spending, to compensate for
// the difference between the assumed L2 base fee and the actual initial L2 base fee.
func NormalizeL2GasForL1GasInitial(l2gas uint64, assumedL2Basefee uint64) uint64 {
return l2gas * assumedL2Basefee / l2pricing.InitialBaseFeeWei
}

0 comments on commit 1cd9504

Please sign in to comment.