From 7348142f334e669d963fe6dc3ee0747bc9d76bf8 Mon Sep 17 00:00:00 2001
From: galaio <12880651+galaio@users.noreply.github.com>
Date: Wed, 24 Jul 2024 14:58:15 +0800
Subject: [PATCH] TxDAG: support TxDAG transfer, it can be used in QA
 performance testing; (#10)

* txdag: support txdag transfer in extra;

* txdag: support txdag transfer in extra;

---------

Co-authored-by: galaio <galaio@users.noreply.github.com>
---
 beacon/engine/types.go           |  8 +++++---
 core/blockchain.go               | 10 +++++++++-
 core/parallel_state_processor.go |  8 ++++++++
 miner/worker.go                  | 31 ++++++++++++++++++++++++++++++-
 4 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/beacon/engine/types.go b/beacon/engine/types.go
index 9a3ea8d077..aef01e7f5f 100644
--- a/beacon/engine/types.go
+++ b/beacon/engine/types.go
@@ -217,9 +217,11 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
 	if err != nil {
 		return nil, err
 	}
-	if len(params.ExtraData) > 32 {
-		return nil, fmt.Errorf("invalid extradata length: %v", len(params.ExtraData))
-	}
+
+	// TODO(galaio): need hardfork, skip check
+	//if len(params.ExtraData) > 32 {
+	//	return nil, fmt.Errorf("invalid extradata length: %v", len(params.ExtraData))
+	//}
 	if len(params.LogsBloom) != 256 {
 		return nil, fmt.Errorf("invalid logsBloom length: %v", len(params.LogsBloom))
 	}
diff --git a/core/blockchain.go b/core/blockchain.go
index e689d6923b..65c126e793 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -1950,7 +1950,15 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
 				if err != nil {
 					return it.index, err
 				}
-				log.Info("Insert chain", "block", block.NumberU64(), "txDAG", txDAG)
+				log.Info("Insert chain", "block", block.NumberU64(), "txDAG", txDAG.Type())
+			}
+			// TODO(galaio): need hardfork
+			if bc.chainConfig.Optimism != nil && len(block.Header().Extra) > 0 {
+				txDAG, err := types.DecodeTxDAG(block.Header().Extra)
+				if err != nil {
+					return it.index, err
+				}
+				log.Info("Insert chain", "block", block.NumberU64(), "txDAG", txDAG.Type())
 			}
 
 			// Enable prefetching to pull in trie node paths while processing transactions
diff --git a/core/parallel_state_processor.go b/core/parallel_state_processor.go
index e0338f28c9..9fe363f151 100644
--- a/core/parallel_state_processor.go
+++ b/core/parallel_state_processor.go
@@ -741,6 +741,14 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat
 			return nil, nil, 0, err
 		}
 	}
+	// TODO(galaio): need hardfork
+	if p.bc.chainConfig.Optimism != nil && len(block.Header().Extra) > 0 {
+		txDAG, err = types.DecodeTxDAG(block.Header().Extra)
+		if err != nil {
+			return nil, nil, 0, err
+		}
+		log.Info("dispatch chain with", "block", block.NumberU64(), "txDAG", txDAG.Type())
+	}
 	// From now on, entering parallel execution.
 	p.doStaticDispatchV2(p.allTxReqs, txDAG) // todo: put txReqs in unit?
 
diff --git a/miner/worker.go b/miner/worker.go
index dbb706d369..9769f260b4 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -1347,6 +1347,25 @@ func (w *worker) generateWork(genParams *generateParams) *newPayloadResult {
 		return &newPayloadResult{err: fmt.Errorf("empty block root")}
 	}
 
+	// Because the TxDAG appends after sidecar, so we only enable after cancun
+	if w.chainConfig.IsCancun(block.Number(), block.Time()) && w.chainConfig.Optimism == nil {
+		txDAG, _ := work.state.MVStates2TxDAG()
+		rawTxDAG, err := types.EncodeTxDAG(txDAG)
+		if err != nil {
+			return &newPayloadResult{err: err}
+		}
+		block = block.WithTxDAG(rawTxDAG)
+	}
+
+	// TODO(galaio): need hardfork
+	if w.chainConfig.Optimism != nil {
+		txDAG, _ := work.state.MVStates2TxDAG()
+		rawTxDAG, err := types.EncodeTxDAG(txDAG)
+		if err != nil {
+			return &newPayloadResult{err: err}
+		}
+		block.Header().Extra = rawTxDAG
+	}
 	assembleBlockTimer.UpdateSince(start)
 	log.Debug("assembleBlockTimer", "duration", common.PrettyDuration(time.Since(start)), "parentHash", genParams.parentHash)
 
@@ -1454,7 +1473,7 @@ func (w *worker) commit(env *environment, interval func(), update bool, start ti
 		}
 
 		// Because the TxDAG appends after sidecar, so we only enable after cancun
-		if w.chainConfig.IsCancun(env.header.Number, env.header.Time) {
+		if w.chainConfig.IsCancun(env.header.Number, env.header.Time) && w.chainConfig.Optimism == nil {
 			for i := len(env.txs); i < len(block.Transactions()); i++ {
 				env.state.RecordSystemTxRWSet(i)
 			}
@@ -1466,6 +1485,16 @@ func (w *worker) commit(env *environment, interval func(), update bool, start ti
 			block = block.WithTxDAG(rawTxDAG)
 		}
 
+		// TODO(galaio): need hardfork
+		if w.chainConfig.Optimism != nil {
+			txDAG, _ := env.state.MVStates2TxDAG()
+			rawTxDAG, err := types.EncodeTxDAG(txDAG)
+			if err != nil {
+				return err
+			}
+			block.Header().Extra = rawTxDAG
+		}
+
 		// If we're post merge, just ignore
 		if !w.isTTDReached(block.Header()) {
 			select {