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

Add a test for storage trie determinism #2914

Merged
merged 12 commits into from
Feb 11, 2025
13 changes: 13 additions & 0 deletions system_tests/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func (tc *TestClient) SendWaitTestTransactions(t *testing.T, txs []*types.Transa
return SendWaitTestTransactions(t, tc.ctx, tc.Client, txs)
}

func (tc *TestClient) DeployBigMap(t *testing.T, auth bind.TransactOpts) (common.Address, *mocksgen.BigMap) {
return deployBigMap(t, tc.ctx, auth, tc.Client)
}

func (tc *TestClient) DeploySimple(t *testing.T, auth bind.TransactOpts) (common.Address, *mocksgen.Simple) {
return deploySimple(t, tc.ctx, auth, tc.Client)
}
Expand Down Expand Up @@ -1713,6 +1717,15 @@ func getDeadlineTimeout(t *testing.T, defaultTimeout time.Duration) time.Duratio
return timeout
}

func deployBigMap(t *testing.T, ctx context.Context, auth bind.TransactOpts, client *ethclient.Client,
) (common.Address, *mocksgen.BigMap) {
addr, tx, bigMap, err := mocksgen.DeployBigMap(&auth, client)
Require(t, err, "could not deploy BigMap.sol contract")
_, err = EnsureTxSucceeded(ctx, client, tx)
Require(t, err)
return addr, bigMap
}

func deploySimple(
t *testing.T, ctx context.Context, auth bind.TransactOpts, client *ethclient.Client,
) (common.Address, *mocksgen.Simple) {
Expand Down
85 changes: 85 additions & 0 deletions system_tests/storage_trie_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2025, Offchain Labs, Inc.
// For license information, see:
// https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md

package arbtest

import (
"context"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/core/rawdb"

"github.com/offchainlabs/nitro/util/arbmath"
"github.com/offchainlabs/nitro/validator/valnode"
)

func TestStorageTrie(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var withL1 = true
builder := NewNodeBuilder(ctx).DefaultConfig(t, withL1)

// This test tests validates blocks at the end.
// For now, validation only works with HashScheme set.
builder.execConfig.Caching.StateScheme = rawdb.HashScheme
builder.nodeConfig.BlockValidator.Enable = false
builder.nodeConfig.Staker.Enable = true
builder.nodeConfig.BatchPoster.Enable = true
builder.nodeConfig.ParentChainReader.Enable = true
builder.nodeConfig.ParentChainReader.OldHeaderTimeout = 10 * time.Minute

valConf := valnode.TestValidationConfig
valConf.UseJit = true
_, valStack := createTestValidationNode(t, ctx, &valConf)
configByValidationNode(builder.nodeConfig, valStack)

cleanup := builder.Build(t)
defer cleanup()

ownerTxOpts := builder.L2Info.GetDefaultTransactOpts("Owner", ctx)
_, bigMap := builder.L2.DeployBigMap(t, ownerTxOpts)

// Store enough values to use just over 32M gas
toAdd := big.NewInt(1420)
// In the first transaction, don't clear any values.
toClear := big.NewInt(0)

userTxOpts := builder.L2Info.GetDefaultTransactOpts("Faucet", ctx)
tx, err := bigMap.ClearAndAddValues(&userTxOpts, toClear, toAdd)
Require(t, err)

receipt, err := builder.L2.EnsureTxSucceeded(tx)
Require(t, err)
tx1BlockNum := receipt.BlockNumber.Uint64()

want := uint64(20_000_000)
got := receipt.GasUsed - receipt.GasUsedForL1
if got < want {
t.Errorf("Want at least GasUsed: %d: got: %d", want, got)
}

// Clear about 75% of them, and add another 10%
toClear = arbmath.BigDiv(arbmath.BigMul(toAdd, big.NewInt(75)), big.NewInt(100))
toAdd = arbmath.BigDiv(arbmath.BigMul(toAdd, big.NewInt(10)), big.NewInt(100))

tx, err = bigMap.ClearAndAddValues(&userTxOpts, toClear, toAdd)
Require(t, err)

receipt, err = builder.L2.EnsureTxSucceeded(tx)
Require(t, err)
tx2BlockNum := receipt.BlockNumber.Uint64()

if tx2BlockNum <= tx1BlockNum {
t.Errorf("Expected tx2BlockNum > tx1BlockNum: %d <= %d", tx2BlockNum, tx1BlockNum)
} else {
t.Logf("tx2BlockNum > tx1BlockNum: %d > %d", tx2BlockNum, tx1BlockNum)
}

// Ensures that the validator gets the same results as the executor
validateBlockRange(t, []uint64{receipt.BlockNumber.Uint64()}, true, builder)
}
Loading