From 08769ead2bd6c7adc064595eafbf17c1ee3f488b Mon Sep 17 00:00:00 2001 From: Mars Date: Tue, 21 May 2024 12:24:41 +0800 Subject: [PATCH] dev: ensure consistency in BPS bundle result (#2479) * dev: ensure consistency in BPS bundle result * fix: remove env operation once the sim is discarded & rename --- core/types/bid.go | 53 +++++++++++++++++++++++++----------------- miner/bid_simulator.go | 16 ++++++------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/core/types/bid.go b/core/types/bid.go index a0d50239a8..e65ab0f003 100644 --- a/core/types/bid.go +++ b/core/types/bid.go @@ -6,6 +6,8 @@ import ( "sync/atomic" "time" + mapset "github.com/deckarep/golang-set/v2" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" @@ -40,6 +42,12 @@ func (b *BidArgs) ToBid(builder common.Address, signer Signer) (*Bid, error) { return nil, err } + if len(b.RawBid.UnRevertible) > len(txs) { + return nil, fmt.Errorf("expect NonRevertible no more than %d", len(txs)) + } + unRevertibleHashes := mapset.NewThreadUnsafeSetWithSize[common.Hash](len(b.RawBid.UnRevertible)) + unRevertibleHashes.Append(b.RawBid.UnRevertible...) + if len(b.PayBidTx) != 0 { var payBidTx = new(Transaction) err = payBidTx.UnmarshalBinary(b.PayBidTx) @@ -51,14 +59,15 @@ func (b *BidArgs) ToBid(builder common.Address, signer Signer) (*Bid, error) { } bid := &Bid{ - Builder: builder, - BlockNumber: b.RawBid.BlockNumber, - ParentHash: b.RawBid.ParentHash, - Txs: txs, - GasUsed: b.RawBid.GasUsed + b.PayBidTxGasUsed, - GasFee: b.RawBid.GasFee, - BuilderFee: b.RawBid.BuilderFee, - rawBid: *b.RawBid, + Builder: builder, + BlockNumber: b.RawBid.BlockNumber, + ParentHash: b.RawBid.ParentHash, + Txs: txs, + UnRevertible: unRevertibleHashes, + GasUsed: b.RawBid.GasUsed + b.PayBidTxGasUsed, + GasFee: b.RawBid.GasFee, + BuilderFee: b.RawBid.BuilderFee, + rawBid: *b.RawBid, } if bid.BuilderFee == nil { @@ -70,12 +79,13 @@ func (b *BidArgs) ToBid(builder common.Address, signer Signer) (*Bid, error) { // RawBid represents a raw bid from builder directly. type RawBid struct { - BlockNumber uint64 `json:"blockNumber"` - ParentHash common.Hash `json:"parentHash"` - Txs []hexutil.Bytes `json:"txs"` - GasUsed uint64 `json:"gasUsed"` - GasFee *big.Int `json:"gasFee"` - BuilderFee *big.Int `json:"builderFee"` + BlockNumber uint64 `json:"blockNumber"` + ParentHash common.Hash `json:"parentHash"` + Txs []hexutil.Bytes `json:"txs"` + UnRevertible []common.Hash `json:"unRevertible"` + GasUsed uint64 `json:"gasUsed"` + GasFee *big.Int `json:"gasFee"` + BuilderFee *big.Int `json:"builderFee"` hash atomic.Value } @@ -154,13 +164,14 @@ func (b *RawBid) Hash() common.Hash { // Bid represents a bid. type Bid struct { - Builder common.Address - BlockNumber uint64 - ParentHash common.Hash - Txs Transactions - GasUsed uint64 - GasFee *big.Int - BuilderFee *big.Int + Builder common.Address + BlockNumber uint64 + ParentHash common.Hash + Txs Transactions + UnRevertible mapset.Set[common.Hash] + GasUsed uint64 + GasFee *big.Int + BuilderFee *big.Int rawBid RawBid } diff --git a/miner/bid_simulator.go b/miner/bid_simulator.go index 75e6d63e99..aa2afc8124 100644 --- a/miner/bid_simulator.go +++ b/miner/bid_simulator.go @@ -613,7 +613,7 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) { break } - err = bidRuntime.commitTransaction(b.chain, b.chainConfig, tx) + err = bidRuntime.commitTransaction(b.chain, b.chainConfig, tx, bidRuntime.bid.UnRevertible.Contains(tx.Hash())) if err != nil { log.Error("BidSimulator: failed to commit tx", "bidHash", bidRuntime.bid.Hash(), "tx", tx.Hash(), "err", err) err = fmt.Errorf("invalid tx in bid, %v", err) @@ -668,7 +668,7 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) { // commit payBidTx at the end of the block bidRuntime.env.gasPool.AddGas(params.PayBidTxGasLimit) - err = bidRuntime.commitTransaction(b.chain, b.chainConfig, payBidTx) + err = bidRuntime.commitTransaction(b.chain, b.chainConfig, payBidTx, true) if err != nil { log.Error("BidSimulator: failed to commit tx", "builder", bidRuntime.bid.Builder, "bidHash", bidRuntime.bid.Hash(), "tx", payBidTx.Hash(), "err", err) @@ -749,12 +749,10 @@ func (r *BidRuntime) packReward(validatorCommission uint64) { r.packedValidatorReward.Sub(r.packedValidatorReward, r.bid.BuilderFee) } -func (r *BidRuntime) commitTransaction(chain *core.BlockChain, chainConfig *params.ChainConfig, tx *types.Transaction) error { +func (r *BidRuntime) commitTransaction(chain *core.BlockChain, chainConfig *params.ChainConfig, tx *types.Transaction, unRevertible bool) error { var ( - env = r.env - snap = env.state.Snapshot() - gp = env.gasPool.Gas() - sc *types.BlobSidecar + env = r.env + sc *types.BlobSidecar ) // Start executing the transaction @@ -777,9 +775,9 @@ func (r *BidRuntime) commitTransaction(chain *core.BlockChain, chainConfig *para receipt, err := core.ApplyTransaction(chainConfig, chain, &env.coinbase, env.gasPool, env.state, env.header, tx, &env.header.GasUsed, *chain.GetVMConfig(), core.NewReceiptBloomGenerator()) if err != nil { - env.state.RevertToSnapshot(snap) - env.gasPool.SetGas(gp) return err + } else if unRevertible && receipt.Status == types.ReceiptStatusFailed { + return errors.New("no revertible transaction failed") } if tx.Type() == types.BlobTxType {