Skip to content

Commit

Permalink
test(rollup): add commit and finalize genesis batch (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo authored Jan 22, 2024
1 parent 9bb4868 commit 44c5f1c
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 19 deletions.
4 changes: 2 additions & 2 deletions rollup/cmd/mock_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func (b *MockApp) MockConfig(store bool) error {
}

cfg.L1Config.Endpoint = base.L1gethImg.Endpoint()
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1gethImg.Endpoint()
cfg.L2Config.Endpoint = base.L2gethImg.Endpoint()
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2gethImg.Endpoint()
cfg.L2Config.Endpoint = base.L2gethImg.Endpoint()
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1gethImg.Endpoint()
cfg.DBConfig.DSN = base.DBImg.Endpoint()
b.Config = cfg

Expand Down
10 changes: 5 additions & 5 deletions rollup/conf/config.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"l1_config": {
"confirmations": "0x6",
"endpoint": "DUMMY_ENDPOINT",
"endpoint": "https://rpc.ankr.com/eth",
"l1_message_queue_address": "0x0000000000000000000000000000000000000000",
"scroll_chain_address": "0x0000000000000000000000000000000000000000",
"start_height": 0,
"relayer_config": {
"gas_price_oracle_address": "0x0000000000000000000000000000000000000000",
"sender_config": {
"endpoint": "https://sepolia-rpc.scroll.io",
"endpoint": "https://rpc.scroll.io",
"check_pending_time": 2,
"check_balance_time": 100,
"escalate_blocks": 100,
Expand All @@ -29,22 +29,22 @@
},
"l2_config": {
"confirmations": "0x1",
"endpoint": "https://sepolia-rpc.scroll.io",
"endpoint": "https://rpc.scroll.io",
"l2_messenger_address": "0x0000000000000000000000000000000000000000",
"l2_message_queue_address": "0x0000000000000000000000000000000000000000",
"relayer_config": {
"rollup_contract_address": "0x0000000000000000000000000000000000000000",
"gas_price_oracle_address": "0x0000000000000000000000000000000000000000",
"sender_config": {
"endpoint": "https://sepolia-rpc.scroll.io",
"endpoint": "https://rpc.ankr.com/eth",
"check_pending_time": 10,
"check_balance_time": 100,
"escalate_blocks": 100,
"confirmations": "0x6",
"escalate_multiple_num": 11,
"escalate_multiple_den": 10,
"max_gas_price": 10000000000,
"tx_type": "LegacyTx",
"tx_type": "DynamicFeeTx",
"min_balance": 100000000000000000000,
"pending_limit": 10
},
Expand Down
5 changes: 5 additions & 0 deletions rollup/internal/controller/relayer/relayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/stretchr/testify/assert"

"scroll-tech/common/database"
Expand Down Expand Up @@ -40,6 +41,10 @@ var (
)

func setupEnv(t *testing.T) {
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
glogger.Verbosity(log.LvlInfo)
log.Root().SetHandler(glogger)

// Load config.
var err error
cfg, err = config.NewConfig("../../../conf/config.json")
Expand Down
8 changes: 4 additions & 4 deletions rollup/internal/controller/sender/estimategas.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *Sender) estimateGasLimit(contract *common.Address, data []byte, gasPric
return gasLimitWithoutAccessList, nil, fmt.Errorf(errStr)
}

// Heuristically fine-tune accessList because 'to' address is automatically included in the access list by the Ethereum protocol: https://github.com/ethereum/go-ethereum/blob/v1.13.10/core/state/statedb.go#L1322
// Fine-tune accessList because 'to' address is automatically included in the access list by the Ethereum protocol: https://github.com/ethereum/go-ethereum/blob/v1.13.10/core/state/statedb.go#L1322
// This function returns a gas estimation because GO SDK does not support access list: https://github.com/ethereum/go-ethereum/blob/v1.13.10/ethclient/ethclient.go#L642
accessList, gasLimitWithAccessList = finetuneAccessList(accessList, gasLimitWithAccessList, msg.To)

Expand All @@ -122,11 +122,11 @@ func finetuneAccessList(accessList *types.AccessList, gasLimitWithAccessList uin
var newAccessList types.AccessList
for _, entry := range *accessList {
if entry.Address == *to && len(entry.StorageKeys) < 24 {
// This is a heuristic method, we check if number of slots is < 24.
// If so, we remove it and adjust the gas limit estimate accordingly.
// Based on: https://arxiv.org/pdf/2312.06574.pdf
// We remove the address and respective storage keys as long as the number of storage keys < 24.
// This removal helps in preventing double-counting of the 'to' address in access list calculations.
gasLimitWithAccessList -= 2400
// Assuming each slot saves 100 gas units as access list storage key cost (1900 gas) plus warm sload (100 gas) is cheaper than cold sload (2100 gas).
// Each storage key saves 100 gas units.
gasLimitWithAccessList += uint64(100 * len(entry.StorageKeys))
} else {
// Otherwise, keep the entry in the new access list.
Expand Down
10 changes: 8 additions & 2 deletions rollup/internal/controller/sender/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/ecdsa"
"errors"
"math/big"
"os"
"strconv"
"testing"

Expand All @@ -14,6 +15,7 @@ import (
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/crypto"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -44,6 +46,10 @@ func TestMain(m *testing.M) {
}

func setupEnv(t *testing.T) {
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
glogger.Verbosity(log.LvlInfo)
log.Root().SetHandler(glogger)

var err error
cfg, err = config.NewConfig("../../../conf/config.json")
assert.NoError(t, err)
Expand Down Expand Up @@ -194,12 +200,12 @@ func testAccessListTransactionGasLimit(t *testing.T) {

gasLimit, accessList, err := s.estimateGasLimit(&scrollChainAddress, data, big.NewInt(100000000000), big.NewInt(100000000000), big.NewInt(100000000000), big.NewInt(0), true)
assert.NoError(t, err)
assert.Equal(t, uint64(43450), gasLimit)
assert.Equal(t, uint64(43472), gasLimit)
assert.NotNil(t, accessList)

gasLimit, accessList, err = s.estimateGasLimit(&scrollChainAddress, data, big.NewInt(100000000000), big.NewInt(100000000000), big.NewInt(100000000000), big.NewInt(0), false)
assert.NoError(t, err)
assert.Equal(t, uint64(43927), gasLimit)
assert.Equal(t, uint64(43949), gasLimit)
assert.Nil(t, accessList)

s.Stop()
Expand Down
5 changes: 5 additions & 0 deletions rollup/internal/controller/watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"

Expand Down Expand Up @@ -33,6 +34,10 @@ var (
)

func setupEnv(t *testing.T) (err error) {
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
glogger.Verbosity(log.LvlInfo)
log.Root().SetHandler(glogger)

// Load config.
cfg, err = config.NewConfig("../../../conf/config.json")
assert.NoError(t, err)
Expand Down
4 changes: 4 additions & 0 deletions rollup/mock_bridge/MockBridgeL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ contract MockBridgeL1 {
* Functions from ScrollChain *
******************************/

/// @notice Import layer 2 genesis block
function importGenesisBatch(bytes calldata _batchHeader, bytes32 _stateRoot) external {
}

function commitBatch(
uint8 /*version*/,
bytes calldata _parentBatchHeader,
Expand Down
13 changes: 7 additions & 6 deletions rollup/tests/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"math/big"
"net/http"
"os"
"strconv"
"strings"
"testing"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"

Expand Down Expand Up @@ -68,6 +70,10 @@ func TestMain(m *testing.M) {
}

func setupEnv(t *testing.T) {
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
glogger.Verbosity(log.LvlInfo)
log.Root().SetHandler(glogger)

base.RunImages(t)

var err error
Expand Down Expand Up @@ -138,15 +144,10 @@ func TestFunction(t *testing.T) {
t.Run("TestProcessStartEnableMetrics", testProcessStartEnableMetrics)

// l1 rollup and watch rollup events
t.Run("TestCommitAndFinalizeGenesisBatch", testCommitAndFinalizeGenesisBatch)
t.Run("TestCommitBatchAndFinalizeBatch", testCommitBatchAndFinalizeBatch)

// l1 message

// l2 message
// TODO: add a "user relay l2msg Succeed" test

// l1/l2 gas oracle
t.Run("TestImportL1GasPrice", testImportL1GasPrice)
t.Run("TestImportL2GasPrice", testImportL2GasPrice)

}
28 changes: 28 additions & 0 deletions rollup/tests/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ import (
"scroll-tech/rollup/internal/orm"
)

func testCommitAndFinalizeGenesisBatch(t *testing.T) {
db := setupDB(t)
defer database.CloseDB(db)

prepareContracts(t)

l2Cfg := rollupApp.Config.L2Config
l2Relayer, err := relayer.NewLayer2Relayer(context.Background(), l2Client, db, l2Cfg.RelayerConfig, true, nil)
assert.NoError(t, err)
assert.NotNil(t, l2Relayer)

genesisChunkHash := common.HexToHash("0x00e076380b00a3749816fcc9a2a576b529952ef463222a54544d21b7d434dfe1")
chunkOrm := orm.NewChunk(db)
dbChunk, err := chunkOrm.GetChunksInRange(context.Background(), 0, 0)
assert.NoError(t, err)
assert.Len(t, dbChunk, 1)
assert.Equal(t, genesisChunkHash.String(), dbChunk[0].Hash)
assert.Equal(t, types.ProvingTaskVerified, types.ProvingStatus(dbChunk[0].ProvingStatus))

genesisBatchHash := common.HexToHash("0x2d214b024f5337d83a5681f88575ab225f345ec2e4e3ce53cf4dc4b0cb5c96b1")
batchOrm := orm.NewBatch(db)
batch, err := batchOrm.GetBatchByIndex(context.Background(), 0)
assert.NoError(t, err)
assert.Equal(t, genesisBatchHash.String(), batch.Hash)
assert.Equal(t, types.ProvingTaskVerified, types.ProvingStatus(batch.ProvingStatus))
assert.Equal(t, types.RollupFinalized, types.RollupStatus(batch.RollupStatus))
}

func testCommitBatchAndFinalizeBatch(t *testing.T) {
db := setupDB(t)
defer database.CloseDB(db)
Expand Down

0 comments on commit 44c5f1c

Please sign in to comment.