Skip to content

Commit

Permalink
fix(rollup sync service): remove syscall.Kill (#636)
Browse files Browse the repository at this point in the history
* fix(rollup sync service): remove syscall.Kill

* bump version
  • Loading branch information
colinlyguo authored Feb 21, 2024
1 parent ee381b2 commit 483949b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func New(stack *node.Node, config *ethconfig.Config, l1Client sync_service.EthCl

if config.EnableRollupVerify {
// initialize and start rollup event sync service
eth.rollupSyncService, err = rollup_sync_service.NewRollupSyncService(context.Background(), chainConfig, eth.chainDb, l1Client, eth.blockchain, stack.Config().L1DeploymentBlock)
eth.rollupSyncService, err = rollup_sync_service.NewRollupSyncService(context.Background(), chainConfig, eth.chainDb, l1Client, eth.blockchain, stack)
if err != nil {
return nil, fmt.Errorf("cannot initialize rollup event sync service: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 1 // Minor version component of the current release
VersionPatch = 16 // Patch version component of the current release
VersionPatch = 17 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down
26 changes: 14 additions & 12 deletions rollup/rollup_sync_service/rollup_sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"reflect"
"syscall"
"time"

"github.com/scroll-tech/go-ethereum/accounts/abi"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/node"
"github.com/scroll-tech/go-ethereum/params"

"github.com/scroll-tech/go-ethereum/rollup/rcfg"
Expand Down Expand Up @@ -54,9 +54,10 @@ type RollupSyncService struct {
l1RevertBatchEventSignature common.Hash
l1FinalizeBatchEventSignature common.Hash
bc *core.BlockChain
stack *node.Node
}

func NewRollupSyncService(ctx context.Context, genesisConfig *params.ChainConfig, db ethdb.Database, l1Client sync_service.EthClient, bc *core.BlockChain, l1DeploymentBlock uint64) (*RollupSyncService, error) {
func NewRollupSyncService(ctx context.Context, genesisConfig *params.ChainConfig, db ethdb.Database, l1Client sync_service.EthClient, bc *core.BlockChain, stack *node.Node) (*RollupSyncService, error) {
// terminate if the caller does not provide an L1 client (e.g. in tests)
if l1Client == nil || (reflect.ValueOf(l1Client).Kind() == reflect.Ptr && reflect.ValueOf(l1Client).IsNil()) {
log.Warn("No L1 client provided, L1 rollup sync service will not run")
Expand All @@ -80,8 +81,8 @@ func NewRollupSyncService(ctx context.Context, genesisConfig *params.ChainConfig
// Initialize the latestProcessedBlock with the block just before the L1 deployment block.
// This serves as a default value when there's no L1 rollup events synced in the database.
var latestProcessedBlock uint64
if l1DeploymentBlock > 0 {
latestProcessedBlock = l1DeploymentBlock - 1
if stack.Config().L1DeploymentBlock > 0 {
latestProcessedBlock = stack.Config().L1DeploymentBlock - 1
}

block := rawdb.ReadRollupEventSyncedL1BlockNumber(db)
Expand All @@ -103,6 +104,7 @@ func NewRollupSyncService(ctx context.Context, genesisConfig *params.ChainConfig
l1RevertBatchEventSignature: scrollChainABI.Events["RevertBatch"].ID,
l1FinalizeBatchEventSignature: scrollChainABI.Events["FinalizeBatch"].ID,
bc: bc,
stack: stack,
}

return &service, nil
Expand Down Expand Up @@ -223,7 +225,7 @@ func (s *RollupSyncService) parseAndUpdateRollupEventLogs(logs []types.Log, endB
return fmt.Errorf("failed to get local node info, batch index: %v, err: %w", batchIndex, err)
}

endBlock, finalizedBatchMeta, err := validateBatch(event, parentBatchMeta, chunks)
endBlock, finalizedBatchMeta, err := validateBatch(event, parentBatchMeta, chunks, s.stack)
if err != nil {
return fmt.Errorf("fatal: validateBatch failed: finalize event: %v, err: %w", event, err)
}
Expand Down Expand Up @@ -375,7 +377,7 @@ func (s *RollupSyncService) decodeChunkBlockRanges(txData []byte) ([]*rawdb.Chun
// validateBatch verifies the consistency between the L1 contract and L2 node data.
// The function will terminate the node and exit if any consistency check fails.
// It returns the number of the end block, a finalized batch meta data, and an error if any.
func validateBatch(event *L1FinalizeBatchEvent, parentBatchMeta *rawdb.FinalizedBatchMeta, chunks []*Chunk) (uint64, *rawdb.FinalizedBatchMeta, error) {
func validateBatch(event *L1FinalizeBatchEvent, parentBatchMeta *rawdb.FinalizedBatchMeta, chunks []*Chunk, stack *node.Node) (uint64, *rawdb.FinalizedBatchMeta, error) {
if len(chunks) == 0 {
return 0, nil, fmt.Errorf("invalid argument: length of chunks is 0, batch index: %v", event.BatchIndex.Uint64())
}
Expand All @@ -395,15 +397,15 @@ func validateBatch(event *L1FinalizeBatchEvent, parentBatchMeta *rawdb.Finalized
localStateRoot := endBlock.Header.Root
if localStateRoot != event.StateRoot {
log.Error("State root mismatch", "batch index", event.BatchIndex.Uint64(), "start block", startBlock.Header.Number.Uint64(), "end block", endBlock.Header.Number.Uint64(), "parent batch hash", parentBatchMeta.BatchHash.Hex(), "l1 finalized state root", event.StateRoot.Hex(), "l2 state root", localStateRoot.Hex())
syscall.Kill(os.Getpid(), syscall.SIGTERM)
return 0, nil, fmt.Errorf("state root mismatch")
stack.Close()
os.Exit(1)
}

localWithdrawRoot := endBlock.WithdrawRoot
if localWithdrawRoot != event.WithdrawRoot {
log.Error("Withdraw root mismatch", "batch index", event.BatchIndex.Uint64(), "start block", startBlock.Header.Number.Uint64(), "end block", endBlock.Header.Number.Uint64(), "parent batch hash", parentBatchMeta.BatchHash.Hex(), "l1 finalized withdraw root", event.WithdrawRoot.Hex(), "l2 withdraw root", localWithdrawRoot.Hex())
syscall.Kill(os.Getpid(), syscall.SIGTERM)
return 0, nil, fmt.Errorf("withdraw root mismatch")
stack.Close()
os.Exit(1)
}

// Note: All params for NewBatchHeader are calculated locally based on the block data.
Expand All @@ -422,8 +424,8 @@ func validateBatch(event *L1FinalizeBatchEvent, parentBatchMeta *rawdb.Finalized
log.Error("marshal chunks failed", "err", err)
}
log.Error("Chunks", "chunks", string(chunksJson))
syscall.Kill(os.Getpid(), syscall.SIGTERM)
return 0, nil, fmt.Errorf("batch hash mismatch")
stack.Close()
os.Exit(1)
}

totalL1MessagePopped := parentBatchMeta.TotalL1MessagePopped
Expand Down
19 changes: 15 additions & 4 deletions rollup/rollup_sync_service/rollup_sync_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/scroll-tech/go-ethereum/core/rawdb"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/ethdb/memorydb"
"github.com/scroll-tech/go-ethereum/node"
"github.com/scroll-tech/go-ethereum/params"
)

Expand All @@ -32,7 +33,12 @@ func TestRollupSyncServiceStartAndStop(t *testing.T) {
db := rawdb.NewDatabase(memorydb.New())
l1Client := &mockEthClient{}
bc := &core.BlockChain{}
service, err := NewRollupSyncService(context.Background(), genesisConfig, db, l1Client, bc, 1)
stack, err := node.New(&node.DefaultConfig)
if err != nil {
t.Fatalf("Failed to new P2P node: %v", err)
}
defer stack.Close()
service, err := NewRollupSyncService(context.Background(), genesisConfig, db, l1Client, bc, stack)
if err != nil {
t.Fatalf("Failed to new rollup sync service: %v", err)
}
Expand Down Expand Up @@ -112,7 +118,12 @@ func TestGetChunkRanges(t *testing.T) {
commitBatchRLP: rlpData,
}
bc := &core.BlockChain{}
service, err := NewRollupSyncService(context.Background(), genesisConfig, db, l1Client, bc, 1)
stack, err := node.New(&node.DefaultConfig)
if err != nil {
t.Fatalf("Failed to new P2P node: %v", err)
}
defer stack.Close()
service, err := NewRollupSyncService(context.Background(), genesisConfig, db, l1Client, bc, stack)
if err != nil {
t.Fatalf("Failed to new rollup sync service: %v", err)
}
Expand Down Expand Up @@ -169,7 +180,7 @@ func TestValidateBatch(t *testing.T) {
StateRoot: chunk3.Blocks[len(chunk3.Blocks)-1].Header.Root,
WithdrawRoot: chunk3.Blocks[len(chunk3.Blocks)-1].WithdrawRoot,
}
endBlock1, finalizedBatchMeta1, err := validateBatch(event1, parentBatchMeta1, []*Chunk{chunk1, chunk2, chunk3})
endBlock1, finalizedBatchMeta1, err := validateBatch(event1, parentBatchMeta1, []*Chunk{chunk1, chunk2, chunk3}, nil)
assert.NoError(t, err)
assert.Equal(t, uint64(13), endBlock1)

Expand All @@ -193,7 +204,7 @@ func TestValidateBatch(t *testing.T) {
StateRoot: chunk4.Blocks[len(chunk4.Blocks)-1].Header.Root,
WithdrawRoot: chunk4.Blocks[len(chunk4.Blocks)-1].WithdrawRoot,
}
endBlock2, finalizedBatchMeta2, err := validateBatch(event2, parentBatchMeta2, []*Chunk{chunk4})
endBlock2, finalizedBatchMeta2, err := validateBatch(event2, parentBatchMeta2, []*Chunk{chunk4}, nil)
assert.NoError(t, err)
assert.Equal(t, uint64(17), endBlock2)

Expand Down

0 comments on commit 483949b

Please sign in to comment.