Skip to content

Commit

Permalink
txdag: remove legacy TxDAG transfer logic; (bnb-chain#16)
Browse files Browse the repository at this point in the history
Co-authored-by: galaio <[email protected]>
  • Loading branch information
2 people authored and sunny2022da committed Jul 30, 2024
1 parent 09ef03b commit f2ff2a4
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 115 deletions.
8 changes: 3 additions & 5 deletions beacon/engine/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,9 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
if err != nil {
return nil, err
}

// 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.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))
}
Expand Down
7 changes: 0 additions & 7 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
if ancestorErr != nil {
return ancestorErr
}

// TODO(galaio): add more TxDAG hash when TxDAG in consensus, txDAG check here
if len(block.TxDAG()) > 0 {
if _, err := types.DecodeTxDAG(block.TxDAG()); err != nil {
return errors.New("wrong TxDAG in block body")
}
}
return nil
}

Expand Down
20 changes: 3 additions & 17 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,22 +1935,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
return it.index, err
}

// TODO(galaio): use txDAG in some accelerate scenarios, like state pre-fetcher.
//if bc.enableTxDAG && len(block.TxDAG()) > 0 {
// txDAG, err := types.DecodeTxDAG(block.TxDAG())
// if err != nil {
// return it.index, err
// }
// log.Info("Insert chain", "block", block.NumberU64(), "txDAG", txDAG)
//}
// TODO(galaio): need hardfork
if bc.enableTxDAG && 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())
}
// TODO(galaio): load TxDAG from block, use txDAG in some accelerate scenarios, like state pre-fetcher.
//if bc.enableTxDAG {}

// Enable prefetching to pull in trie node paths while processing transactions
statedb.StartPrefetcher("chain")
Expand Down Expand Up @@ -2850,13 +2836,13 @@ func (bc *BlockChain) SetupTxDAGGeneration(output string) {
}

// write handler
bc.txDAGWriteCh = make(chan TxDAGOutputItem, 10000)
go func() {
writeHandle, err := os.OpenFile(output, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
log.Error("OpenFile when open the txDAG output file", "file", output)
return
}
bc.txDAGWriteCh = make(chan TxDAGOutputItem, 10000)
defer writeHandle.Close()
for {
select {
Expand Down
18 changes: 2 additions & 16 deletions core/parallel_state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,28 +801,14 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat

var (
txDAG types.TxDAG
err error
)
if p.bc.enableTxDAG {
if len(block.TxDAG()) != 0 {
txDAG, err = types.DecodeTxDAG(block.TxDAG())
if err != nil {
return nil, nil, 0, err
}
}
// load cache txDAG from file
// TODO(galaio): load TxDAG from block
// or load cache txDAG from file
if txDAG == nil && len(p.bc.txDAGMapping) > 0 {
txDAG = p.bc.txDAGMapping[block.NumberU64()]
}
}
// TODO(galaio): need hardfork
if p.bc.enableTxDAG && 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?

Expand Down
21 changes: 0 additions & 21 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ type Block struct {
uncles []*Header
transactions Transactions
withdrawals Withdrawals
// TODO(galaio): package txDAG in consensus later
txDAG []byte

// caches
hash atomic.Value
Expand Down Expand Up @@ -425,10 +423,6 @@ func (b *Block) SanityCheck() error {
return b.header.SanityCheck()
}

func (b *Block) TxDAG() []byte {
return b.txDAG
}

type writeCounter uint64

func (c *writeCounter) Write(b []byte) (int, error) {
Expand Down Expand Up @@ -458,7 +452,6 @@ func (b *Block) WithSeal(header *Header) *Block {
transactions: b.transactions,
uncles: b.uncles,
withdrawals: b.withdrawals,
txDAG: b.txDAG,
}
}

Expand All @@ -469,7 +462,6 @@ func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
transactions: make([]*Transaction, len(transactions)),
uncles: make([]*Header, len(uncles)),
withdrawals: b.withdrawals,
txDAG: b.txDAG,
}
copy(block.transactions, transactions)
for i := range uncles {
Expand All @@ -484,7 +476,6 @@ func (b *Block) WithWithdrawals(withdrawals []*Withdrawal) *Block {
header: b.header,
transactions: b.transactions,
uncles: b.uncles,
txDAG: b.txDAG,
}
if withdrawals != nil {
block.withdrawals = make([]*Withdrawal, len(withdrawals))
Expand All @@ -493,18 +484,6 @@ func (b *Block) WithWithdrawals(withdrawals []*Withdrawal) *Block {
return block
}

// WithTxDAG returns a block containing the given txDAG.
func (b *Block) WithTxDAG(txDAG []byte) *Block {
block := &Block{
header: b.header,
transactions: b.transactions,
uncles: b.uncles,
withdrawals: b.withdrawals,
txDAG: txDAG,
}
return block
}

// Hash returns the keccak256 hash of b's header.
// The hash is computed on the first call and cached thereafter.
func (b *Block) Hash() common.Hash {
Expand Down
3 changes: 0 additions & 3 deletions eth/handler_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ func (h *ethHandler) Handle(peer *eth.Peer, packet eth.Packet) error {
return h.handleBlockAnnounces(peer, hashes, numbers)

case *eth.NewBlockPacket:
if len(packet.TxDAG) != 0 {
packet.Block = packet.Block.WithTxDAG(packet.TxDAG)
}
return h.handleBlockBroadcast(peer, packet.Block, packet.TD)

case *eth.NewPooledTransactionHashesPacket67:
Expand Down
1 change: 0 additions & 1 deletion eth/protocols/eth/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ func (p *Peer) SendNewBlock(block *types.Block, td *big.Int) error {
return p2p.Send(p.rw, NewBlockMsg, &NewBlockPacket{
Block: block,
TD: td,
TxDAG: block.TxDAG(),
})
}

Expand Down
3 changes: 0 additions & 3 deletions eth/protocols/eth/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ type BlockHeadersRLPPacket struct {
type NewBlockPacket struct {
Block *types.Block
TD *big.Int
TxDAG []byte `rlp:"optional"`
}

// sanityCheck verifies that the values are reasonable, as a DoS protection
Expand Down Expand Up @@ -239,8 +238,6 @@ type BlockBody struct {
Transactions []*types.Transaction // Transactions contained within a block
Uncles []*types.Header // Uncles contained within a block
Withdrawals []*types.Withdrawal `rlp:"optional"` // Withdrawals contained within a block
// TODO(galio): add block body later
//TxDAGs [][]byte `rlp:"optional"` // TxDAGs contained within a block
}

// Unpack retrieves the transactions and uncles from the range packet and returns
Expand Down
50 changes: 8 additions & 42 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1219,25 +1219,14 @@ 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.chain.TxDAGEnabled() && w.chainConfig.IsCancun(block.Number(), block.Time()) && w.chainConfig.Optimism == nil {
txDAG, _ := work.state.ResolveTxDAG([]common.Address{work.coinbase, params.OptimismBaseFeeRecipient, params.OptimismL1FeeRecipient})
rawTxDAG, err := types.EncodeTxDAG(txDAG)
if err != nil {
return &newPayloadResult{err: err}
}
block = block.WithTxDAG(rawTxDAG)
}

// TODO(galaio): need hardfork
if w.chain.TxDAGEnabled() && w.chainConfig.Optimism != nil {
txDAG, _ := work.state.ResolveTxDAG([]common.Address{work.coinbase, params.OptimismBaseFeeRecipient, params.OptimismL1FeeRecipient})
rawTxDAG, err := types.EncodeTxDAG(txDAG)
if err != nil {
return &newPayloadResult{err: err}
}
block.Header().Extra = rawTxDAG
}
// TODO(galaio): fulfill TxDAG to mined block
//if w.chain.TxDAGEnabled() && w.chainConfig.Optimism != nil {
// txDAG, _ := work.state.ResolveTxDAG([]common.Address{work.coinbase, params.OptimismBaseFeeRecipient, params.OptimismL1FeeRecipient})
// rawTxDAG, err := types.EncodeTxDAG(txDAG)
// if err != nil {
// return &newPayloadResult{err: err}
// }
//}

assembleBlockTimer.UpdateSince(start)
log.Debug("assembleBlockTimer", "duration", common.PrettyDuration(time.Since(start)), "parentHash", genParams.parentHash)
Expand Down Expand Up @@ -1347,29 +1336,6 @@ func (w *worker) commit(env *environment, interval func(), update bool, start ti
return err
}

// Because the TxDAG appends after sidecar, so we only enable after cancun
if w.chain.TxDAGEnabled() && 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)
}
txDAG, _ := env.state.ResolveTxDAG([]common.Address{env.coinbase, params.OptimismBaseFeeRecipient, params.OptimismL1FeeRecipient})
rawTxDAG, err := types.EncodeTxDAG(txDAG)
if err != nil {
return err
}
block = block.WithTxDAG(rawTxDAG)
}

// TODO(galaio): need hardfork
if w.chain.TxDAGEnabled() && w.chainConfig.Optimism != nil {
txDAG, _ := env.state.ResolveTxDAG([]common.Address{env.coinbase, params.OptimismBaseFeeRecipient, params.OptimismL1FeeRecipient})
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 {
Expand Down

0 comments on commit f2ff2a4

Please sign in to comment.