Skip to content

Commit

Permalink
Merge branch 'master' into arbos-30-cleanup-escrow
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower committed Apr 30, 2024
2 parents 66a2454 + 112f1d3 commit 51df9da
Show file tree
Hide file tree
Showing 51 changed files with 413 additions and 218 deletions.
4 changes: 2 additions & 2 deletions arbnode/dataposter/data_poster.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ func (p *DataPoster) sendTx(ctx context.Context, prevTx *storage.QueuedTransacti
return fmt.Errorf("couldn't get preceding tx in DataPoster to check if should send tx with nonce %d: %w", newTx.FullTx.Nonce(), err)
}
if precedingTx != nil && // precedingTx == nil -> the actual preceding tx was already confirmed
precedingTx.FullTx.Type() != newTx.FullTx.Type() {
(precedingTx.FullTx.Type() != newTx.FullTx.Type() || !precedingTx.Sent) {
latestBlockNumber, err := p.client.BlockNumber(ctx)
if err != nil {
return fmt.Errorf("couldn't get block number in DataPoster to check if should send tx with nonce %d: %w", newTx.FullTx.Nonce(), err)
Expand All @@ -857,7 +857,7 @@ func (p *DataPoster) sendTx(ctx context.Context, prevTx *storage.QueuedTransacti
}

if precedingTx.FullTx.Nonce() > reorgResistantNonce {
log.Info("DataPoster is holding off on sending a transaction of different type to the previous transaction until the previous transaction has been included in a reorg resistant block (it remains queued and will be retried)", "nonce", newTx.FullTx.Nonce(), "prevType", precedingTx.FullTx.Type(), "type", newTx.FullTx.Type())
log.Info("DataPoster is avoiding creating a mempool nonce gap (the tx remains queued and will be retried)", "nonce", newTx.FullTx.Nonce(), "prevType", precedingTx.FullTx.Type(), "type", newTx.FullTx.Type(), "prevSent", precedingTx.Sent)
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion arbnode/inbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func TestTransactionStreamer(t *testing.T) {
Fail(t, "error getting block state", err)
}
haveBalance := state.GetBalance(acct)
if balance.Cmp(haveBalance) != 0 {
if balance.Cmp(haveBalance.ToBig()) != 0 {
t.Error("unexpected balance for account", acct, "; expected", balance, "got", haveBalance)
}
}
Expand Down
50 changes: 31 additions & 19 deletions arbos/arbosState/arbosstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,26 @@ import (
// persisted beyond the end of the test.)

type ArbosState struct {
arbosVersion uint64 // version of the ArbOS storage format and semantics
upgradeVersion storage.StorageBackedUint64 // version we're planning to upgrade to, or 0 if not planning to upgrade
upgradeTimestamp storage.StorageBackedUint64 // when to do the planned upgrade
networkFeeAccount storage.StorageBackedAddress
l1PricingState *l1pricing.L1PricingState
l2PricingState *l2pricing.L2PricingState
retryableState *retryables.RetryableState
addressTable *addressTable.AddressTable
chainOwners *addressSet.AddressSet
sendMerkle *merkleAccumulator.MerkleAccumulator
blockhashes *blockhash.Blockhashes
chainId storage.StorageBackedBigInt
chainConfig storage.StorageBackedBytes
genesisBlockNum storage.StorageBackedUint64
infraFeeAccount storage.StorageBackedAddress
brotliCompressionLevel storage.StorageBackedUint64 // brotli compression level used for pricing
backingStorage *storage.Storage
Burner burn.Burner
arbosVersion uint64 // version of the ArbOS storage format and semantics
maxArbosVersionSupported uint64 // maximum ArbOS version supported by this code
maxDebugArbosVersionSupported uint64 // maximum ArbOS version supported by this code in debug mode
upgradeVersion storage.StorageBackedUint64 // version we're planning to upgrade to, or 0 if not planning to upgrade
upgradeTimestamp storage.StorageBackedUint64 // when to do the planned upgrade
networkFeeAccount storage.StorageBackedAddress
l1PricingState *l1pricing.L1PricingState
l2PricingState *l2pricing.L2PricingState
retryableState *retryables.RetryableState
addressTable *addressTable.AddressTable
chainOwners *addressSet.AddressSet
sendMerkle *merkleAccumulator.MerkleAccumulator
blockhashes *blockhash.Blockhashes
chainId storage.StorageBackedBigInt
chainConfig storage.StorageBackedBytes
genesisBlockNum storage.StorageBackedUint64
infraFeeAccount storage.StorageBackedAddress
brotliCompressionLevel storage.StorageBackedUint64 // brotli compression level used for pricing
backingStorage *storage.Storage
Burner burn.Burner
}

var ErrUninitializedArbOS = errors.New("ArbOS uninitialized")
Expand All @@ -70,6 +72,8 @@ func OpenArbosState(stateDB vm.StateDB, burner burn.Burner) (*ArbosState, error)
}
return &ArbosState{
arbosVersion,
20,
20,
backingStorage.OpenStorageBackedUint64(uint64(upgradeVersionOffset)),
backingStorage.OpenStorageBackedUint64(uint64(upgradeTimestampOffset)),
backingStorage.OpenStorageBackedAddress(uint64(networkFeeAccountOffset)),
Expand Down Expand Up @@ -299,7 +303,7 @@ func (state *ArbosState) UpgradeArbosVersion(
case 10:
ensure(state.l1PricingState.SetL1FeesAvailable(stateDB.GetBalance(
l1pricing.L1PricerFundsPoolAddress,
)))
).ToBig()))
case 11:
// Update the PerBatchGasCost to a more accurate value compared to the old v6 default.
ensure(state.l1PricingState.SetPerBatchGasCost(l1pricing.InitialPerBatchGasCostV12))
Expand Down Expand Up @@ -413,6 +417,14 @@ func (state *ArbosState) RetryableState() *retryables.RetryableState {
return state.retryableState
}

func (state *ArbosState) MaxArbosVersionSupported() uint64 {
return state.maxArbosVersionSupported
}

func (state *ArbosState) MaxDebugArbosVersionSupported() uint64 {
return state.maxDebugArbosVersionSupported
}

func (state *ArbosState) L1PricingState() *l1pricing.L1PricingState {
return state.l1PricingState
}
Expand Down
2 changes: 1 addition & 1 deletion arbos/arbosState/initialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func checkAccounts(db *state.StateDB, arbState *ArbosState, accts []statetransfe
if db.GetNonce(addr) != acct.Nonce {
t.Fatal()
}
if db.GetBalance(addr).Cmp(acct.EthBalance) != 0 {
if db.GetBalance(addr).ToBig().Cmp(acct.EthBalance) != 0 {
t.Fatal()
}
if acct.ContractInfo != nil {
Expand Down
7 changes: 4 additions & 3 deletions arbos/arbosState/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/holiman/uint256"
"github.com/offchainlabs/nitro/arbos/arbostypes"
"github.com/offchainlabs/nitro/arbos/burn"
"github.com/offchainlabs/nitro/arbos/l2pricing"
Expand Down Expand Up @@ -142,7 +143,7 @@ func InitializeArbosInDatabase(db ethdb.Database, initData statetransfer.InitDat
if err != nil {
return common.Hash{}, err
}
statedb.SetBalance(account.Addr, account.EthBalance)
statedb.SetBalance(account.Addr, uint256.MustFromBig(account.EthBalance))
statedb.SetNonce(account.Addr, account.Nonce)
if account.ContractInfo != nil {
statedb.SetCode(account.Addr, account.ContractInfo.Code)
Expand Down Expand Up @@ -173,7 +174,7 @@ func initializeRetryables(statedb *state.StateDB, rs *retryables.RetryableState,
return err
}
if r.Timeout <= currentTimestamp {
statedb.AddBalance(r.Beneficiary, r.Callvalue)
statedb.AddBalance(r.Beneficiary, uint256.MustFromBig(r.Callvalue))
continue
}
retryablesList = append(retryablesList, r)
Expand All @@ -192,7 +193,7 @@ func initializeRetryables(statedb *state.StateDB, rs *retryables.RetryableState,
addr := r.To
to = &addr
}
statedb.AddBalance(retryables.RetryableEscrowAddress(r.Id), r.Callvalue)
statedb.AddBalance(retryables.RetryableEscrowAddress(r.Id), uint256.MustFromBig(r.Callvalue))
_, err := rs.CreateRetryable(r.Id, r.Timeout, r.From, to, r.Callvalue, r.Beneficiary, r.Calldata)
if err != nil {
return err
Expand Down
21 changes: 11 additions & 10 deletions arbos/l1pricing/l1PricingOldVersions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
package l1pricing

import (
"math"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/offchainlabs/nitro/arbos/util"
am "github.com/offchainlabs/nitro/util/arbmath"
"math"
"math/big"
)

func (ps *L1PricingState) _preversion10_UpdateForBatchPosterSpending(
Expand Down Expand Up @@ -105,8 +106,8 @@ func (ps *L1PricingState) _preversion10_UpdateForBatchPosterSpending(
// pay rewards, as much as possible
paymentForRewards := am.BigMulByUint(am.UintToBig(perUnitReward), unitsAllocated)
availableFunds := statedb.GetBalance(L1PricerFundsPoolAddress)
if am.BigLessThan(availableFunds, paymentForRewards) {
paymentForRewards = availableFunds
if am.BigLessThan(availableFunds.ToBig(), paymentForRewards) {
paymentForRewards = availableFunds.ToBig()
}
fundsDueForRewards = am.BigSub(fundsDueForRewards, paymentForRewards)
if err := ps.SetFundsDueForRewards(fundsDueForRewards); err != nil {
Expand All @@ -130,8 +131,8 @@ func (ps *L1PricingState) _preversion10_UpdateForBatchPosterSpending(
return err
}
balanceToTransfer := balanceDueToPoster
if am.BigLessThan(availableFunds, balanceToTransfer) {
balanceToTransfer = availableFunds
if am.BigLessThan(availableFunds.ToBig(), balanceToTransfer) {
balanceToTransfer = availableFunds.ToBig()
}
if balanceToTransfer.Sign() > 0 {
addrToPay, err := posterState.PayTo()
Expand Down Expand Up @@ -166,7 +167,7 @@ func (ps *L1PricingState) _preversion10_UpdateForBatchPosterSpending(
if err != nil {
return err
}
surplus := am.BigSub(statedb.GetBalance(L1PricerFundsPoolAddress), am.BigAdd(totalFundsDue, fundsDueForRewards))
surplus := am.BigSub(statedb.GetBalance(L1PricerFundsPoolAddress).ToBig(), am.BigAdd(totalFundsDue, fundsDueForRewards))

inertia, err := ps.Inertia()
if err != nil {
Expand Down Expand Up @@ -230,7 +231,7 @@ func (ps *L1PricingState) _preVersion2_UpdateForBatchPosterSpending(
if err != nil {
return err
}
oldSurplus := am.BigSub(statedb.GetBalance(L1PricerFundsPoolAddress), am.BigAdd(totalFundsDue, fundsDueForRewards))
oldSurplus := am.BigSub(statedb.GetBalance(L1PricerFundsPoolAddress).ToBig(), am.BigAdd(totalFundsDue, fundsDueForRewards))

// compute allocation fraction -- will allocate updateTimeDelta/timeDelta fraction of units and funds to this update
lastUpdateTime, err := ps.LastUpdateTime()
Expand Down Expand Up @@ -280,7 +281,7 @@ func (ps *L1PricingState) _preVersion2_UpdateForBatchPosterSpending(

// allocate funds to this update
collectedSinceUpdate := statedb.GetBalance(L1PricerFundsPoolAddress)
availableFunds := am.BigDivByUint(am.BigMulByUint(collectedSinceUpdate, allocationNumerator), allocationDenominator)
availableFunds := am.BigDivByUint(am.BigMulByUint(collectedSinceUpdate.ToBig(), allocationNumerator), allocationDenominator)

// pay rewards, as much as possible
paymentForRewards := am.BigMulByUint(am.UintToBig(perUnitReward), unitsAllocated)
Expand Down Expand Up @@ -356,7 +357,7 @@ func (ps *L1PricingState) _preVersion2_UpdateForBatchPosterSpending(
if err != nil {
return err
}
surplus := am.BigSub(statedb.GetBalance(L1PricerFundsPoolAddress), am.BigAdd(totalFundsDue, fundsDueForRewards))
surplus := am.BigSub(statedb.GetBalance(L1PricerFundsPoolAddress).ToBig(), am.BigAdd(totalFundsDue, fundsDueForRewards))

inertia, err := ps.Inertia()
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions arbos/l1pricing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/holiman/uint256"
"github.com/offchainlabs/nitro/arbos/arbosState"
"github.com/offchainlabs/nitro/arbos/l1pricing"
"github.com/offchainlabs/nitro/arbos/util"
Expand Down Expand Up @@ -171,7 +172,7 @@ func _testL1PricingFundsDue(t *testing.T, testParams *l1PricingTest, expectedRes
// create some fake collection
balanceAdded := big.NewInt(int64(testParams.fundsCollectedPerSecond * 3))
unitsAdded := testParams.unitsPerSecond * 3
evm.StateDB.AddBalance(l1pricing.L1PricerFundsPoolAddress, balanceAdded)
evm.StateDB.AddBalance(l1pricing.L1PricerFundsPoolAddress, uint256.MustFromBig(balanceAdded))
err = l1p.SetL1FeesAvailable(balanceAdded)
Require(t, err)
err = l1p.SetUnitsSinceUpdate(unitsAdded)
Expand All @@ -187,7 +188,7 @@ func _testL1PricingFundsDue(t *testing.T, testParams *l1PricingTest, expectedRes
)
Require(t, err)
rewardRecipientBalance := evm.StateDB.GetBalance(rewardAddress)
if !arbmath.BigEquals(rewardRecipientBalance, expectedResults.rewardRecipientBalance) {
if !arbmath.BigEquals(rewardRecipientBalance.ToBig(), expectedResults.rewardRecipientBalance) {
Fail(t, rewardRecipientBalance, expectedResults.rewardRecipientBalance)
}
unitsRemaining, err := l1p.UnitsSinceUpdate()
Expand All @@ -196,16 +197,16 @@ func _testL1PricingFundsDue(t *testing.T, testParams *l1PricingTest, expectedRes
Fail(t, unitsRemaining, expectedResults.unitsRemaining)
}
fundsReceived := evm.StateDB.GetBalance(firstPayTo)
if !arbmath.BigEquals(fundsReceived, expectedResults.fundsReceived) {
if !arbmath.BigEquals(fundsReceived.ToBig(), expectedResults.fundsReceived) {
Fail(t, fundsReceived, expectedResults.fundsReceived)
}
fundsStillHeld := evm.StateDB.GetBalance(l1pricing.L1PricerFundsPoolAddress)
if !arbmath.BigEquals(fundsStillHeld, expectedResults.fundsStillHeld) {
if !arbmath.BigEquals(fundsStillHeld.ToBig(), expectedResults.fundsStillHeld) {
Fail(t, fundsStillHeld, expectedResults.fundsStillHeld)
}
fundsAvail, err := l1p.L1FeesAvailable()
Require(t, err)
if fundsStillHeld.Cmp(fundsAvail) != 0 {
if fundsStillHeld.ToBig().Cmp(fundsAvail) != 0 {
Fail(t, fundsStillHeld, fundsAvail)
}
}
Expand Down
2 changes: 1 addition & 1 deletion arbos/retryables/retryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (rs *RetryableState) DeleteRetryable(id common.Hash, evm *vm.EVM, scenario
escrowAddress := RetryableEscrowAddress(id)
beneficiaryAddress := common.BytesToAddress(beneficiary[:])
amount := evm.StateDB.GetBalance(escrowAddress)
err = util.TransferBalance(&escrowAddress, &beneficiaryAddress, amount, evm, scenario, "escrow")
err = util.TransferBalance(&escrowAddress, &beneficiaryAddress, amount.ToBig(), evm, scenario, "escrow")
if err != nil {
return false, err
}
Expand Down
7 changes: 4 additions & 3 deletions arbos/tx_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"math/big"

"github.com/holiman/uint256"
"github.com/offchainlabs/nitro/arbos/l1pricing"

"github.com/offchainlabs/nitro/arbos/util"
Expand Down Expand Up @@ -145,7 +146,7 @@ func (p *TxProcessor) StartTxHook() (endTxNow bool, gasUsed uint64, err error, r
// This transfer is necessary because we don't actually invoke the EVM.
// Since MintBalance already called AddBalance on `from`,
// we don't have EIP-161 concerns around not touching `from`.
core.Transfer(evm.StateDB, from, *to, value)
core.Transfer(evm.StateDB, from, *to, uint256.MustFromBig(value))
return true, 0, nil, nil
case *types.ArbitrumInternalTx:
defer (startTracer())()
Expand Down Expand Up @@ -174,7 +175,7 @@ func (p *TxProcessor) StartTxHook() (endTxNow bool, gasUsed uint64, err error, r

// check that the user has enough balance to pay for the max submission fee
balanceAfterMint := evm.StateDB.GetBalance(tx.From)
if balanceAfterMint.Cmp(tx.MaxSubmissionFee) < 0 {
if balanceAfterMint.ToBig().Cmp(tx.MaxSubmissionFee) < 0 {
err := fmt.Errorf(
"insufficient funds for max submission fee: address %v have %v want %v",
tx.From, balanceAfterMint, tx.MaxSubmissionFee,
Expand Down Expand Up @@ -258,7 +259,7 @@ func (p *TxProcessor) StartTxHook() (endTxNow bool, gasUsed uint64, err error, r

maxGasCost := arbmath.BigMulByUint(tx.GasFeeCap, usergas)
maxFeePerGasTooLow := arbmath.BigLessThan(tx.GasFeeCap, effectiveBaseFee)
if arbmath.BigLessThan(balance, maxGasCost) || usergas < params.TxGas || maxFeePerGasTooLow {
if arbmath.BigLessThan(balance.ToBig(), maxGasCost) || usergas < params.TxGas || maxFeePerGasTooLow {
// User either specified too low of a gas fee cap, didn't have enough balance to pay for gas,
// or the specified gas limit is below the minimum transaction gas cost.
// Either way, attempt to refund the gas costs, since we're not doing the auto-redeem.
Expand Down
4 changes: 2 additions & 2 deletions arbos/util/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewTracingInfo(evm *vm.EVM, from, to common.Address, scenario TracingScenar
return &TracingInfo{
Tracer: evm.Config.Tracer,
Scenario: scenario,
Contract: vm.NewContract(addressHolder{to}, addressHolder{from}, big.NewInt(0), 0),
Contract: vm.NewContract(addressHolder{to}, addressHolder{from}, uint256.NewInt(0), 0),
Depth: evm.Depth(),
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func (info *TracingInfo) MockCall(input []byte, gas uint64, from, to common.Addr
tracer := info.Tracer
depth := info.Depth

contract := vm.NewContract(addressHolder{to}, addressHolder{from}, amount, gas)
contract := vm.NewContract(addressHolder{to}, addressHolder{from}, uint256.MustFromBig(amount), gas)

scope := &vm.ScopeContext{
Memory: TracingMemoryFromBytes(input),
Expand Down
11 changes: 6 additions & 5 deletions arbos/util/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/log"
"github.com/holiman/uint256"
"github.com/offchainlabs/nitro/util/arbmath"
)

Expand All @@ -29,17 +30,17 @@ func TransferBalance(
}
if from != nil {
balance := evm.StateDB.GetBalance(*from)
if arbmath.BigLessThan(balance, amount) {
if arbmath.BigLessThan(balance.ToBig(), amount) {
return fmt.Errorf("%w: addr %v have %v want %v", vm.ErrInsufficientBalance, *from, balance, amount)
}
evm.StateDB.SubBalance(*from, amount)
evm.StateDB.SubBalance(*from, uint256.MustFromBig(amount))
if evm.Context.ArbOSVersion >= 30 {
// ensure the from account is "touched" for EIP-161
evm.StateDB.AddBalance(*from, common.Big0)
evm.StateDB.AddBalance(*from, &uint256.Int{})
}
}
if to != nil {
evm.StateDB.AddBalance(*to, amount)
evm.StateDB.AddBalance(*to, uint256.MustFromBig(amount))
}
if tracer := evm.Config.Tracer; tracer != nil {
if evm.Depth() != 0 && scenario != TracingDuringEVM {
Expand All @@ -63,7 +64,7 @@ func TransferBalance(
info := &TracingInfo{
Tracer: evm.Config.Tracer,
Scenario: scenario,
Contract: vm.NewContract(addressHolder{*to}, addressHolder{*from}, big.NewInt(0), 0),
Contract: vm.NewContract(addressHolder{*to}, addressHolder{*from}, uint256.NewInt(0), 0),
Depth: evm.Depth(),
}
info.MockCall([]byte{}, 0, *from, *to, amount)
Expand Down
Loading

0 comments on commit 51df9da

Please sign in to comment.