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

Adds more tests related to precompiles #2709

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
33e4c2d
Adds comments on precompile methods without tests
diegoximenes Sep 24, 2024
ec8d071
Updates comments on precompile methods without tests
diegoximenes Sep 24, 2024
e3bc3a9
Test for ArbAddressTable.AddressExists
diegoximenes Sep 24, 2024
4ea3cb6
Do not test deprecated methods in ArbAggregator
diegoximenes Sep 25, 2024
72e4541
Do not test simple ArbDebug methods
diegoximenes Sep 25, 2024
2bd1a50
Tests for ArbFunctionTable
diegoximenes Sep 25, 2024
548e8bd
Tests for ArbInfo
diegoximenes Sep 25, 2024
c8d63c3
Removes TODOs from ArbWasmCache
diegoximenes Sep 25, 2024
4f7c94a
Removes TODOs from ArbWasm
diegoximenes Sep 25, 2024
d7eb09f
Tests for ArbStatistics
diegoximenes Sep 25, 2024
29ef7bc
Do not test simple ArbosActs methods
diegoximenes Sep 25, 2024
49aabb7
Tests for ArbSys.GetStorageGasAvailable and ArbSys.ArbOSVersion
diegoximenes Sep 25, 2024
56a6912
Do not test ArbSys.MapL1SenderContractAddressToL2Alias
diegoximenes Sep 25, 2024
25396a8
Test for ArbOwnerPublic.GetBrotliCompressionLevel
diegoximenes Sep 25, 2024
6c71ab9
Do not test ArbOwnerPublic.RectifyChainOwner
diegoximenes Sep 25, 2024
38b64b1
Comment to check ArbOwner later
diegoximenes Sep 25, 2024
982227a
TestGetLifetime
diegoximenes Sep 25, 2024
d52f32a
TestArbGasInfoAndArbOwner
diegoximenes Sep 26, 2024
4c1cb26
Test for GetNetworkFeeAccount
diegoximenes Sep 26, 2024
11da6ec
Test for SetL1PricingInertia
diegoximenes Sep 26, 2024
b4e46ca
Test for GetGasAccountingParams. Fixes GetGasBacklogTolerance test
diegoximenes Sep 26, 2024
26910d7
Test for ArbOwner.SetL2BaseFee
diegoximenes Sep 26, 2024
e489105
More ArbGasInfo tests
diegoximenes Sep 26, 2024
c676dc5
ArbRetryableTx.GetBeneficiary test
diegoximenes Sep 26, 2024
7e4bff8
GetPricesInArbGas test
diegoximenes Sep 26, 2024
10ae887
GetCurrentTxL1GasFees test
diegoximenes Sep 27, 2024
b445c21
GetCurrentRedeemer test
diegoximenes Sep 27, 2024
2b13f47
ArbRetryableTx.Cancel test
diegoximenes Sep 27, 2024
6d21681
ArbRetryableTx.Keepalive test
diegoximenes Sep 27, 2024
83d96c2
Fixes address comparison
diegoximenes Sep 27, 2024
b06be3e
Adds missing t.Parallel()
diegoximenes Sep 27, 2024
45799c8
Fixes comments
diegoximenes Sep 27, 2024
b8120d6
Improves arbSys.ArbOSVersion test
diegoximenes Sep 27, 2024
a6bab58
Fixes comment
diegoximenes Sep 27, 2024
dcaa36a
Fixes comment
diegoximenes Sep 27, 2024
01c7cae
Separate long setter/getter test in multiple test functions
diegoximenes Oct 1, 2024
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
12 changes: 12 additions & 0 deletions precompiles/ArbAddressTable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func TestAddressTable1(t *testing.T) {

addr := common.BytesToAddress(crypto.Keccak256([]byte{})[:20])

exists, err := atab.AddressExists(context, evm, addr)
Require(t, err)
if exists {
t.Fatal("Address shouldn't exist")
}

// register addr
slot, err := atab.Register(context, evm, addr)
Require(t, err)
Expand All @@ -61,6 +67,12 @@ func TestAddressTable1(t *testing.T) {
t.Fatal()
}

exists, err = atab.AddressExists(context, evm, addr)
Require(t, err)
if !exists {
t.Fatal("Address should exist")
}

// verify Lookup of addr returns 0
index, err := atab.Lookup(context, evm, addr)
Require(t, err)
Expand Down
37 changes: 37 additions & 0 deletions precompiles/ArbFunctionTable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

package precompiles

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

func TestArbFunctionTable(t *testing.T) {
t.Parallel()

evm := newMockEVMForTesting()
ftab := ArbFunctionTable{}
context := testContext(common.Address{}, evm)

addr := common.BytesToAddress(crypto.Keccak256([]byte{})[:20])

// should be a noop
err := ftab.Upload(context, evm, []byte{0, 0, 0, 0})
Require(t, err)

size, err := ftab.Size(context, evm, addr)
Require(t, err)
if size.Cmp(big.NewInt(0)) != 0 {
t.Fatal("Size should be 0")
}

_, _, _, err = ftab.Get(context, evm, addr, big.NewInt(10))
if err == nil {
t.Fatal("Should error")
}
}
140 changes: 140 additions & 0 deletions precompiles/ArbGasInfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

package precompiles

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/offchainlabs/nitro/arbos/arbosState"
"github.com/offchainlabs/nitro/arbos/burn"
"github.com/offchainlabs/nitro/arbos/storage"
"github.com/offchainlabs/nitro/arbos/util"
"github.com/offchainlabs/nitro/util/testhelpers"
)

func setupArbGasInfo(
t *testing.T,
) (
*vm.EVM,
*arbosState.ArbosState,
*Context,
*ArbGasInfo,
) {
evm := newMockEVMForTesting()
caller := common.BytesToAddress(crypto.Keccak256([]byte{})[:20])
tracer := util.NewTracingInfo(evm, testhelpers.RandomAddress(), types.ArbosAddress, util.TracingDuringEVM)
state, err := arbosState.OpenArbosState(evm.StateDB, burn.NewSystemBurner(tracer, false))
Require(t, err)

arbGasInfo := &ArbGasInfo{}
callCtx := testContext(caller, evm)

return evm, state, callCtx, arbGasInfo
}

func TestGetGasBacklog(t *testing.T) {
t.Parallel()

evm, state, callCtx, arbGasInfo := setupArbGasInfo(t)

backlog := uint64(1000)
err := state.L2PricingState().SetGasBacklog(backlog)
Require(t, err)
retrievedBacklog, err := arbGasInfo.GetGasBacklog(callCtx, evm)
Require(t, err)
if retrievedBacklog != backlog {
t.Fatal("expected backlog to be", backlog, "but got", retrievedBacklog)
}
}

func TestGetL1PricingUpdateTime(t *testing.T) {
t.Parallel()

evm, state, callCtx, arbGasInfo := setupArbGasInfo(t)

lastUpdateTime := uint64(1001)
err := state.L1PricingState().SetLastUpdateTime(lastUpdateTime)
Require(t, err)
retrievedLastUpdateTime, err := arbGasInfo.GetLastL1PricingUpdateTime(callCtx, evm)
Require(t, err)
if retrievedLastUpdateTime != lastUpdateTime {
t.Fatal("expected last update time to be", lastUpdateTime, "but got", retrievedLastUpdateTime)
}
}

func TestGetL1PricingFundsDueForRewards(t *testing.T) {
t.Parallel()

evm, state, callCtx, arbGasInfo := setupArbGasInfo(t)

fundsDueForRewards := big.NewInt(1002)
err := state.L1PricingState().SetFundsDueForRewards(fundsDueForRewards)
Require(t, err)
retrievedFundsDueForRewards, err := arbGasInfo.GetL1PricingFundsDueForRewards(callCtx, evm)
Require(t, err)
if retrievedFundsDueForRewards.Cmp(fundsDueForRewards) != 0 {
t.Fatal("expected funds due for rewards to be", fundsDueForRewards, "but got", retrievedFundsDueForRewards)
}
}

func TestGetL1PricingUnitsSinceUpdate(t *testing.T) {
t.Parallel()

evm, state, callCtx, arbGasInfo := setupArbGasInfo(t)

pricingUnitsSinceUpdate := uint64(1003)
err := state.L1PricingState().SetUnitsSinceUpdate(pricingUnitsSinceUpdate)
Require(t, err)
retrievedPricingUnitsSinceUpdate, err := arbGasInfo.GetL1PricingUnitsSinceUpdate(callCtx, evm)
Require(t, err)
if retrievedPricingUnitsSinceUpdate != pricingUnitsSinceUpdate {
t.Fatal("expected pricing units since update to be", pricingUnitsSinceUpdate, "but got", retrievedPricingUnitsSinceUpdate)
}
}

func TestGetLastL1PricingSurplus(t *testing.T) {
t.Parallel()

evm, state, callCtx, arbGasInfo := setupArbGasInfo(t)

lastSurplus := big.NewInt(1004)
err := state.L1PricingState().SetLastSurplus(lastSurplus, params.ArbosVersion_Stylus)
Require(t, err)
retrievedLastSurplus, err := arbGasInfo.GetLastL1PricingSurplus(callCtx, evm)
Require(t, err)
if retrievedLastSurplus.Cmp(lastSurplus) != 0 {
t.Fatal("expected last surplus to be", lastSurplus, "but got", retrievedLastSurplus)
}
}

func TestGetPricesInArbGas(t *testing.T) {
t.Parallel()

evm := newMockEVMForTesting()
caller := common.BytesToAddress(crypto.Keccak256([]byte{})[:20])
arbGasInfo := &ArbGasInfo{}
callCtx := testContext(caller, evm)

evm.Context.BaseFee = big.NewInt(1005)
expectedGasPerL2Tx := big.NewInt(111442786069)
expectedGasForL1Calldata := big.NewInt(796019900)
expectedStorageArbGas := big.NewInt(int64(storage.StorageWriteCost))
gasPerL2Tx, gasForL1Calldata, storageArbGas, err := arbGasInfo.GetPricesInArbGas(callCtx, evm)
Require(t, err)
if gasPerL2Tx.Cmp(expectedGasPerL2Tx) != 0 {
t.Fatal("expected gas per L2 tx to be", expectedGasPerL2Tx, "but got", gasPerL2Tx)
}
if gasForL1Calldata.Cmp(expectedGasForL1Calldata) != 0 {
t.Fatal("expected gas for L1 calldata to be", expectedGasForL1Calldata, "but got", gasForL1Calldata)
}
if storageArbGas.Cmp(expectedStorageArbGas) != 0 {
t.Fatal("expected storage arb gas to be", expectedStorageArbGas, "but got", storageArbGas)
}
}
17 changes: 17 additions & 0 deletions precompiles/ArbOwner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ func TestArbOwner(t *testing.T) {
if avail.Cmp(deposited) != 0 {
Fail(t, avail, deposited)
}

err = prec.SetNetworkFeeAccount(callCtx, evm, addr1)
Require(t, err)
retrievedNetworkFeeAccount, err := prec.GetNetworkFeeAccount(callCtx, evm)
Require(t, err)
if retrievedNetworkFeeAccount.Cmp(addr1) != 0 {
Fail(t, "Expected", addr1, "got", retrievedNetworkFeeAccount)
}

l2BaseFee := big.NewInt(123)
err = prec.SetL2BaseFee(callCtx, evm, l2BaseFee)
Require(t, err)
retrievedL2BaseFee, err := state.L2PricingState().BaseFeeWei()
Require(t, err)
if l2BaseFee.Cmp(retrievedL2BaseFee) != 0 {
Fail(t, "Expected", l2BaseFee, "got", retrievedL2BaseFee)
}
}

func TestArbOwnerSetChainConfig(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion precompiles/ArbRetryableTx.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ func (con ArbRetryableTx) GetTimeout(c ctx, evm mech, ticketId bytes32) (huge, e

// Keepalive adds one lifetime period to the ticket's expiry
func (con ArbRetryableTx) Keepalive(c ctx, evm mech, ticketId bytes32) (huge, error) {

// charge for the expiry update
retryableState := c.State.RetryableState()
nbytes, err := retryableState.RetryableSizeBytes(ticketId, evm.Context.Time)
Expand Down
51 changes: 50 additions & 1 deletion precompiles/ArbRetryableTx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,45 @@ import (
"math/big"
"testing"

"github.com/offchainlabs/nitro/arbos"
"github.com/offchainlabs/nitro/arbos/retryables"
"github.com/offchainlabs/nitro/arbos/storage"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/vm"
templates "github.com/offchainlabs/nitro/solgen/go/precompilesgen"
)

func TestRetryableRedeem(t *testing.T) {
func newMockEVMForTestingWithCurrentRefundTo(currentRefundTo *common.Address) *vm.EVM {
evm := newMockEVMForTesting()
txProcessor := arbos.NewTxProcessor(evm, &core.Message{})
txProcessor.CurrentRefundTo = currentRefundTo
evm.ProcessingHook = txProcessor
return evm
}

func TestGetLifetimeAndCurrentRedeemer(t *testing.T) {
currentRefundTo := common.HexToAddress("0x030405")

evm := newMockEVMForTestingWithCurrentRefundTo(&currentRefundTo)
retryableTx := ArbRetryableTx{}
context := testContext(common.Address{}, evm)

lifetime, err := retryableTx.GetLifetime(context, evm)
Require(t, err)
if lifetime.Cmp(big.NewInt(retryables.RetryableLifetimeSeconds)) != 0 {
t.Fatal("Expected to be ", retryables.RetryableLifetimeSeconds, " but got ", lifetime)
}

currentRedeemer, err := retryableTx.GetCurrentRedeemer(context, evm)
Require(t, err)
if currentRefundTo.Cmp(currentRedeemer) != 0 {
t.Fatal("Expected to be ", currentRefundTo, " but got ", currentRedeemer)
}
}

func TestRetryableRedeemAndGetBeneficiary(t *testing.T) {
evm := newMockEVMForTesting()
precompileCtx := testContext(common.Address{}, evm)

Expand Down Expand Up @@ -62,4 +94,21 @@ func TestRetryableRedeem(t *testing.T) {
// to handle both cases, and some will be left over in this test's use case.
Fail(t, "didn't consume all the expected gas")
}

getBeneficiaryCallData, err := retryABI.Pack("getBeneficiary", id)
Require(t, err)
retrievedBeneficiary, _, err := Precompiles()[retryAddress].Call(
getBeneficiaryCallData,
retryAddress,
retryAddress,
common.Address{},
big.NewInt(0),
false,
1000000,
evm,
)
Require(t, err)
if common.BytesToAddress(retrievedBeneficiary).Cmp(beneficiary) != 0 {
Fail(t, "expected beneficiary to be ", beneficiary, " but got ", common.BytesToAddress(retrievedBeneficiary))
}
}
22 changes: 22 additions & 0 deletions precompiles/ArbStatistics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

package precompiles

import (
"testing"

"github.com/ethereum/go-ethereum/common"
)

func TestArbStatistics(t *testing.T) {
evm := newMockEVMForTesting()
stats := ArbStatistics{}
context := testContext(common.Address{}, evm)

blockNum, _, _, _, _, _, err := stats.GetStats(context, evm)
Require(t, err)
if blockNum.Cmp(evm.Context.BlockNumber) != 0 {
t.Error("Unexpected block number")
}
}
Loading
Loading