diff --git a/core/rawdb/accessors_rollup_batch.go b/core/rawdb/accessors_rollup_batch.go index ed88f39e1..dbce1f0b3 100644 --- a/core/rawdb/accessors_rollup_batch.go +++ b/core/rawdb/accessors_rollup_batch.go @@ -3,7 +3,6 @@ package rawdb import ( "bytes" "encoding/binary" - "math/big" "github.com/morph-l2/go-ethereum/common" "github.com/morph-l2/go-ethereum/core/types" @@ -12,26 +11,6 @@ import ( "github.com/morph-l2/go-ethereum/rlp" ) -func WriteRollupBatch(db ethdb.KeyValueWriter, batch *types.RollupBatch) { - bz, err := batch.Encode() - if err != nil { - log.Crit("failed to RLP encode batch", "batch index", batch.Index, "err", err) - } - if err = db.Put(RollupBatchKey(batch.Index), bz); err != nil { - log.Crit("failed to store batch", "batch index", batch.Index, "err", err) - } - - // stores batchHash -> batchIndex - if err = db.Put(RollupBatchIndexKey(batch.Hash), encodeBigEndian(batch.Index)); err != nil { - log.Crit("failed to store batch index", "batch hash", batch.Hash.Hex(), "batch index", batch.Index, "err", err) - } - - // stores latest batch index - if err = db.Put(rollupHeadBatchKey, encodeBigEndian(batch.Index)); err != nil { - log.Crit("failed to store latest batch index", "batch index", batch.Index, "err", err) - } -} - func ReadLatestBatchIndex(db ethdb.Reader) *uint64 { data, err := db.Get(rollupHeadBatchKey) if err != nil && isNotFoundErr(err) { @@ -76,17 +55,6 @@ func ReadRollupBatch(db ethdb.Reader, batchIndex uint64) (*types.RollupBatch, er return rb, nil } -func WriteBatchSignature(db ethdb.KeyValueWriter, batchHash common.Hash, signature types.BatchSignature) { - bz, err := rlp.EncodeToBytes(&signature) - if err != nil { - log.Crit("failed to RLP encode batch signature", "batch hash", batchHash, "signer index", signature.Signer, "err", err) - } - - if err = db.Put(RollupBatchSignatureSignerKey(batchHash, signature.Signer), bz); err != nil { - log.Crit("failed to store batch signature", "batch index", batchHash, "signer", signature.Signer, "err", err) - } -} - func ReadBatchSignatures(db ethdb.Database, batchHash common.Hash) ([]*types.BatchSignature, error) { prefix := RollupBatchSignatureKey(batchHash) it := db.NewIterator(prefix, nil) @@ -110,38 +78,3 @@ func ReadBatchSignatures(db ethdb.Database, batchHash common.Hash) ([]*types.Bat } return bss, nil } - -func WriteBatchL1DataFee(db ethdb.Database, batchIndex uint64, l1DataFee *big.Int) { - if err := db.Put(RollupBatchL1DataFeeKey(batchIndex), l1DataFee.Bytes()); err != nil { - log.Crit("failed to store batch l1DataFee", "batch index", batchIndex, "l1DataFee", l1DataFee.String(), "err", err) - } -} - -func WriteHeadBatchIndexHasFee(db ethdb.Database, batchIndex uint64) { - if err := db.Put(rollupBatchHeadBatchHasFeeKey, encodeBigEndian(batchIndex)); err != nil { - log.Crit("failed to store head batch which has fee collected", "batch index", batchIndex, "err", err) - } -} - -func ReadBatchL1DataFee(db ethdb.Database, batchIndex uint64) *big.Int { - data, err := db.Get(RollupBatchL1DataFeeKey(batchIndex)) - if err != nil && isNotFoundErr(err) { - return nil - } - if err != nil { - log.Crit("failed to read batch from database", "err", err) - } - return new(big.Int).SetBytes(data) -} - -func ReadLatestBatchIndexHasFee(db ethdb.Reader) *uint64 { - data, err := db.Get(rollupBatchHeadBatchHasFeeKey) - if err != nil && isNotFoundErr(err) { - return nil - } - if err != nil { - log.Crit("failed to read batchIndex from database", "err", err) - } - index := binary.BigEndian.Uint64(data) - return &index -} diff --git a/eth/api.go b/eth/api.go index 22b6f3ed9..de79fee59 100644 --- a/eth/api.go +++ b/eth/api.go @@ -609,8 +609,6 @@ type RPCRollupBatch struct { Sidecar types.BlobTxSidecar `json:"sidecar"` Signatures []RPCBatchSignature `json:"signatures"` - - CollectedL1Fee *hexutil.Big `json:"collectedL1Fee"` } type RPCBatchSignature struct { @@ -646,12 +644,6 @@ func (api *MorphAPI) GetRollupBatchByIndex(ctx context.Context, index uint64) (* sidecar = *rollupBatch.Sidecar } - var collectedL1Fee *hexutil.Big - l1DataFee := rawdb.ReadBatchL1DataFee(api.eth.ChainDb(), index) - if l1DataFee != nil { - collectedL1Fee = (*hexutil.Big)(l1DataFee) - } - return &RPCRollupBatch{ Version: rollupBatch.Version, Hash: rollupBatch.Hash, @@ -665,19 +657,9 @@ func (api *MorphAPI) GetRollupBatchByIndex(ctx context.Context, index uint64) (* NumL1Messages: rollupBatch.NumL1Messages, Sidecar: sidecar, Signatures: rpcSignatures, - CollectedL1Fee: collectedL1Fee, }, nil } -func (api *MorphAPI) GetRollupBatchL1FeeByIndex(ctx context.Context, index uint64) (*hexutil.Big, error) { - var collectedL1Fee *hexutil.Big - l1DataFee := rawdb.ReadBatchL1DataFee(api.eth.ChainDb(), index) - if l1DataFee != nil { - collectedL1Fee = (*hexutil.Big)(l1DataFee) - } - return collectedL1Fee, nil -} - // DiskAndHeaderRoot represents both the disk state root and header root for a block. type DiskAndHeaderRoot struct { DiskRoot common.Hash `json:"diskRoot"` diff --git a/eth/backend.go b/eth/backend.go index 409c04ad5..905876fbf 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -52,7 +52,6 @@ import ( "github.com/morph-l2/go-ethereum/p2p/enode" "github.com/morph-l2/go-ethereum/params" "github.com/morph-l2/go-ethereum/rlp" - "github.com/morph-l2/go-ethereum/rollup/batch" "github.com/morph-l2/go-ethereum/rpc" ) @@ -92,9 +91,6 @@ type Ethereum struct { p2pServer *p2p.Server - // Batch Handler - batchHandler *batch.Handler - lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase) } @@ -258,9 +254,6 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { eth.miner = miner.New(eth, config.Miner, eth.engine) eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData)) - // new batch handler - eth.batchHandler = batch.NewBatchHandler(chainDb, eth.blockchain) - eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil} if eth.APIBackend.allowUnprotectedTxs { log.Info("Unprotected transactions allowed") @@ -390,7 +383,6 @@ func (s *Ethereum) Downloader() *downloader.Downloader { return s.handler.downlo func (s *Ethereum) Synced() bool { return atomic.LoadUint32(&s.handler.acceptTxs) == 1 } func (s *Ethereum) ArchiveMode() bool { return s.config.NoPruning } func (s *Ethereum) BloomIndexer() *core.ChainIndexer { return s.bloomIndexer } -func (s *Ethereum) BatchHandler() *batch.Handler { return s.batchHandler } func (s *Ethereum) SyncMode() downloader.SyncMode { mode, _ := s.handler.chainSync.modeAndLocalHead() return mode @@ -413,9 +405,6 @@ func (s *Ethereum) Protocols() []p2p.Protocol { // Start implements node.Lifecycle, starting all internal goroutines needed by the // Ethereum protocol implementation. func (s *Ethereum) Start() error { - // start the batch handler - s.batchHandler.Start() - //eth.StartENRUpdater(s.blockchain, s.p2pServer.LocalNode()) // Start the bloom bits servicing goroutines @@ -447,7 +436,6 @@ func (s *Ethereum) Stop() error { close(s.closeBloomHandler) s.txPool.Stop() s.miner.Close() - s.batchHandler.Stop() s.blockchain.Stop() s.engine.Close() rawdb.PopUncleanShutdownMarker(s.chainDb) diff --git a/eth/catalyst/l2_api.go b/eth/catalyst/l2_api.go index 7c12b6582..a563b7e9e 100644 --- a/eth/catalyst/l2_api.go +++ b/eth/catalyst/l2_api.go @@ -132,7 +132,7 @@ func (api *l2ConsensusAPI) ValidateL2Block(params ExecutableL2Data) (*GenericRes return nil, fmt.Errorf("wrong parent hash: %s, expected parent hash is %s", params.ParentHash, parent.Hash()) } - block, err := api.executableDataToBlock(params, nil) + block, err := api.executableDataToBlock(params) if err != nil { return nil, err } @@ -179,7 +179,7 @@ func (api *l2ConsensusAPI) ValidateL2Block(params ExecutableL2Data) (*GenericRes }, nil } -func (api *l2ConsensusAPI) NewL2Block(params ExecutableL2Data, batchHash *common.Hash) (err error) { +func (api *l2ConsensusAPI) NewL2Block(params ExecutableL2Data) (err error) { api.newBlockLock.Lock() defer api.newBlockLock.Unlock() @@ -198,7 +198,7 @@ func (api *l2ConsensusAPI) NewL2Block(params ExecutableL2Data, batchHash *common return fmt.Errorf("wrong parent hash: %s, expected parent hash is %s", params.ParentHash, parent.Hash()) } - block, err := api.executableDataToBlock(params, batchHash) + block, err := api.executableDataToBlock(params) if err != nil { return err } @@ -263,16 +263,6 @@ func (api *l2ConsensusAPI) NewSafeL2Block(params SafeL2Data) (header *types.Head return header, api.eth.BlockChain().WriteStateAndSetHead(block, receipts, stateDB, procTime) } -func (api *l2ConsensusAPI) CommitBatch(batch types.RollupBatch, signatures []types.BatchSignature) error { - log.Info("commit batch", "batch index", batch.Index) - return api.eth.BatchHandler().ImportBatch(&batch, signatures) -} - -func (api *l2ConsensusAPI) AppendBatchSignature(batchHash common.Hash, signature types.BatchSignature) { - log.Info("append batch signature", "batch hash", fmt.Sprintf("%x", batchHash)) - api.eth.BatchHandler().ImportBatchSig(batchHash, signature) -} - func (api *l2ConsensusAPI) safeDataToBlock(params SafeL2Data) (*types.Block, error) { var batchHash common.Hash if params.BatchHash != nil { @@ -300,11 +290,7 @@ func (api *l2ConsensusAPI) safeDataToBlock(params SafeL2Data) (*types.Block, err return types.NewBlockWithHeader(header).WithBody(txs, nil), nil } -func (api *l2ConsensusAPI) executableDataToBlock(params ExecutableL2Data, batchHash *common.Hash) (*types.Block, error) { - var bh common.Hash - if batchHash != nil { - bh = *batchHash - } +func (api *l2ConsensusAPI) executableDataToBlock(params ExecutableL2Data) (*types.Block, error) { header := &types.Header{ ParentHash: params.ParentHash, Number: big.NewInt(int64(params.Number)), @@ -314,7 +300,6 @@ func (api *l2ConsensusAPI) executableDataToBlock(params ExecutableL2Data, batchH Coinbase: params.Miner, BaseFee: params.BaseFee, NextL1MsgIndex: params.NextL1MessageIndex, - BatchHash: bh, } header.Difficulty = l2.Difficulty header.Nonce = l2.Nonce diff --git a/eth/catalyst/l2_api_test.go b/eth/catalyst/l2_api_test.go index fcacb35fd..25e7cfe4e 100644 --- a/eth/catalyst/l2_api_test.go +++ b/eth/catalyst/l2_api_test.go @@ -188,7 +188,7 @@ func TestNewL2Block(t *testing.T) { LogsBloom: block.Bloom().Bytes(), } - err = api.NewL2Block(l2Data, nil) + err = api.NewL2Block(l2Data) require.NoError(t, err) currentState, err := ethService.BlockChain().State() @@ -205,7 +205,7 @@ func TestNewL2Block(t *testing.T) { validResp, err := api.ValidateL2Block(*resp) require.NoError(t, err) require.True(t, validResp.Success) - err = api.NewL2Block(*resp, nil) + err = api.NewL2Block(*resp) require.NoError(t, err) currentState, err = ethService.BlockChain().State() require.NoError(t, err) diff --git a/ethclient/authclient/engine.go b/ethclient/authclient/engine.go index 9298bc8bc..0c47c162f 100644 --- a/ethclient/authclient/engine.go +++ b/ethclient/authclient/engine.go @@ -38,8 +38,8 @@ func (ec *Client) ValidateL2Block(ctx context.Context, executableL2Data *catalys } // NewL2Block executes L2 Block, and set the block to chain -func (ec *Client) NewL2Block(ctx context.Context, executableL2Data *catalyst.ExecutableL2Data, batchHash *common.Hash) error { - return ec.c.CallContext(ctx, nil, "engine_newL2Block", executableL2Data, batchHash) +func (ec *Client) NewL2Block(ctx context.Context, executableL2Data *catalyst.ExecutableL2Data) error { + return ec.c.CallContext(ctx, nil, "engine_newL2Block", executableL2Data) } // NewSafeL2Block executes a safe L2 Block, and set the block to chain @@ -49,16 +49,6 @@ func (ec *Client) NewSafeL2Block(ctx context.Context, safeL2Data *catalyst.SafeL return &header, err } -// CommitBatch commit the batch, with the signatures -func (ec *Client) CommitBatch(ctx context.Context, batch *types.RollupBatch, signatures []types.BatchSignature) error { - return ec.c.CallContext(ctx, nil, "engine_commitBatch", batch, signatures) -} - -// AppendBlsSignature append a new bls signature to the batch -func (ec *Client) AppendBlsSignature(ctx context.Context, batchHash common.Hash, signature types.BatchSignature) error { - return ec.c.CallContext(ctx, nil, "engine_appendBatchSignature", batchHash, signature) -} - // SetBlockTags sets the safe and finalized block by hash func (ec *Client) SetBlockTags(ctx context.Context, safeBlockHash common.Hash, finalizedBlockHash common.Hash) error { return ec.c.CallContext(ctx, nil, "engine_setBlockTags", safeBlockHash, finalizedBlockHash) @@ -80,4 +70,3 @@ func (ec *Client) AssembleL2BlockV2(ctx context.Context, parentHash common.Hash, err := ec.c.CallContext(ctx, &result, "engine_assembleL2BlockV2", parentHash, timestamp, txs) return result, err } - diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 22f997b2e..9b7fbf43b 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -491,18 +491,6 @@ func (ec *Client) GetRollupBatchByIndex(ctx context.Context, batchIndex uint64) return rpcRollupBatch, err } -// GetRollupBatchL1FeeByIndex query the batch to be rollup by batchIndex -func (ec *Client) GetRollupBatchL1FeeByIndex(ctx context.Context, batchIndex uint64) (*big.Int, error) { - var fee *hexutil.Big - err := ec.c.CallContext(ctx, &fee, "morph_getRollupBatchL1FeeByIndex", batchIndex) - if err != nil { - return nil, err - } else if fee == nil { - return nil, nil - } - return (*big.Int)(fee), err -} - // State Access // NetworkID returns the network ID (also known as the chain ID) for this chain. diff --git a/rollup/batch/block_context.go b/rollup/batch/block_context.go deleted file mode 100644 index e18289a07..000000000 --- a/rollup/batch/block_context.go +++ /dev/null @@ -1,58 +0,0 @@ -package batch - -import ( - "bytes" - "encoding/binary" - "math/big" -) - -type BlockContext struct { - Number uint64 `json:"number" gencodec:"required"` - Timestamp uint64 `json:"timestamp" gencodec:"required"` - BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"` - GasLimit uint64 `json:"gasLimit" gencodec:"required"` - NumTxs uint16 `json:"numTxs"` - NumL1Txs uint16 `json:"numL1Txs"` -} - -func (wb *BlockContext) DecodeBlockContext(bc []byte) error { - reader := bytes.NewReader(bc) - bsBaseFee := make([]byte, 32) - if err := binary.Read(reader, binary.BigEndian, &wb.Number); err != nil { - return err - } - if err := binary.Read(reader, binary.BigEndian, &wb.Timestamp); err != nil { - return err - } - if err := binary.Read(reader, binary.BigEndian, &bsBaseFee); err != nil { - return err - } - wb.BaseFee = new(big.Int).SetBytes(bsBaseFee) - if err := binary.Read(reader, binary.BigEndian, &wb.GasLimit); err != nil { - return err - } - if err := binary.Read(reader, binary.BigEndian, &wb.NumTxs); err != nil { - return err - } - if err := binary.Read(reader, binary.BigEndian, &wb.NumL1Txs); err != nil { - return err - } - return nil -} - -func BlockContextsFromBytes(bz []byte) (bcs []BlockContext, err error) { - if len(bz) == 0 { - return nil, nil - } - blockCount := binary.BigEndian.Uint16(bz[:2]) - bz = bz[2:] - for i := 0; i < int(blockCount); i++ { - bc := new(BlockContext) - err = bc.DecodeBlockContext(bz[i*60 : i*60+60]) - if err != nil { - return nil, err - } - bcs = append(bcs, *bc) - } - return bcs, nil -} diff --git a/rollup/batch/handler.go b/rollup/batch/handler.go deleted file mode 100644 index 59f635b4d..000000000 --- a/rollup/batch/handler.go +++ /dev/null @@ -1,151 +0,0 @@ -package batch - -import ( - "math/big" - "sync" - - "github.com/morph-l2/go-ethereum/common" - "github.com/morph-l2/go-ethereum/core" - "github.com/morph-l2/go-ethereum/core/rawdb" - "github.com/morph-l2/go-ethereum/core/types" - "github.com/morph-l2/go-ethereum/ethdb" - "github.com/morph-l2/go-ethereum/log" -) - -const newBatchChanelBuffer = 5 - -type Handler struct { - db ethdb.Database - bc *core.BlockChain - latestBatchIndex uint64 - latestBatchIndexHasFee uint64 - newBatchCh chan *types.RollupBatch - - logger log.Logger - stopCh chan struct{} - wg sync.WaitGroup -} - -func NewBatchHandler(db ethdb.Database, bc *core.BlockChain) *Handler { - var ( - latestBatchIndex uint64 - latestBatchIndexHasFee uint64 - ) - index := rawdb.ReadLatestBatchIndex(db) - if index != nil { - latestBatchIndex = *index - } - index = rawdb.ReadLatestBatchIndexHasFee(db) - if index != nil { - latestBatchIndexHasFee = *index - } - logger := log.New("component", "batchHandler") - return &Handler{ - db: db, - bc: bc, - logger: logger, - - latestBatchIndex: latestBatchIndex, - latestBatchIndexHasFee: latestBatchIndexHasFee, - - newBatchCh: make(chan *types.RollupBatch, newBatchChanelBuffer), - stopCh: make(chan struct{}), - } -} - -func (h *Handler) Start() { - h.fillMissingFeeCalc() - go h.CollectL1FeeLoop() -} - -func (h *Handler) Stop() { - h.logger.Info("batch handler is stopping") - h.stopCh <- struct{}{} - h.wg.Wait() - h.logger.Info("batch handler is stopped") -} - -func (h *Handler) ImportBatch(batch *types.RollupBatch, signatures []types.BatchSignature) error { - h.newBatchCh <- batch - - dbBatch := h.db.NewBatch() - rawdb.WriteRollupBatch(dbBatch, batch) - for _, signature := range signatures { - rawdb.WriteBatchSignature(dbBatch, batch.Hash, signature) - } - return dbBatch.Write() -} - -func (h *Handler) ImportBatchSig(batchHash common.Hash, signature types.BatchSignature) { - rawdb.WriteBatchSignature(h.db, batchHash, signature) -} - -func (h *Handler) CollectL1FeeLoop() { - h.logger.Info("start to collect L1Fee for new batches") - h.wg.Add(1) - defer func() { - h.logger.Info("stop collecting L1Fee") - h.wg.Done() - }() - for { - select { - case newBatch := <-h.newBatchCh: - if h.latestBatchIndex < newBatch.Index { - h.latestBatchIndex = newBatch.Index - } - h.calculateL1FeeForBatch(newBatch.Index, newBatch.BlockContexts) - rawdb.WriteHeadBatchIndexHasFee(h.db, newBatch.Index) - h.latestBatchIndexHasFee++ - case <-h.stopCh: - close(h.newBatchCh) - return - } - } -} - -func (h *Handler) calculateL1FeeForBatch(index uint64, blockContextsBytes []byte) { - blockContexts, err := BlockContextsFromBytes(blockContextsBytes) - if err != nil { - h.logger.Error("failed to parse block contexts from bytes", "err", err) - return - } - - receivedL1Fee := big.NewInt(0) - for _, bc := range blockContexts { - hash := rawdb.ReadCanonicalHash(h.db, bc.Number) - receipts := h.bc.GetReceiptsByHash(hash) - for _, receipt := range receipts { - if receipt.L1Fee != nil { - receivedL1Fee = new(big.Int).Add(receivedL1Fee, receipt.L1Fee) - } - } - } - rawdb.WriteBatchL1DataFee(h.db, index, receivedL1Fee) - h.logger.Info("complete L1Fee calculation on batch", "index", index, "l1Fee", receivedL1Fee.String()) -} - -func (h *Handler) fillMissingFeeCalc() { - h.logger.Info("start L1Fee calculation on missing batches", "latestBatchIndex", h.latestBatchIndex, "latestBatchIndexHasFee", h.latestBatchIndexHasFee) - if h.latestBatchIndexHasFee == 0 || h.latestBatchIndexHasFee >= h.latestBatchIndex { - h.logger.Info("no missing batches found") - return - } - for h.latestBatchIndexHasFee != 0 && h.latestBatchIndexHasFee < h.latestBatchIndex { - processIndex := h.latestBatchIndexHasFee + 1 - batch, err := rawdb.ReadRollupBatch(h.db, processIndex) - if err != nil { - log.Error("failed fillMissingFeeCalc", "error", err) - return - } - if batch == nil { - // skip this batch if it is removed from db somehow - h.logger.Error("no batch found", "index", processIndex, "method", "fillMissingFeeCalc") - h.latestBatchIndexHasFee++ - continue - } - h.calculateL1FeeForBatch(processIndex, batch.BlockContexts) - h.latestBatchIndexHasFee++ - } - rawdb.WriteHeadBatchIndexHasFee(h.db, h.latestBatchIndex) - h.logger.Info("complete L1Fee calculation on missing batches") -}