@@ -22,6 +22,13 @@ import (
2222 "github.com/holiman/uint256"
2323)
2424
25+ var (
26+ ErrInvalidInclusionRange = errors .New ("invalid inclusion range" )
27+ ErrInvalidBlockNumber = errors .New ("invalid block number" )
28+ ErrExceedsMaxBlock = errors .New ("block number exceeds max block" )
29+ ErrEmptyTxs = errors .New ("empty transactions" )
30+ )
31+
2532type BuilderConfig struct {
2633 ChainConfig * params.ChainConfig
2734 Engine consensus.Engine
@@ -77,26 +84,92 @@ func NewBuilder(config *BuilderConfig, args *BuilderArgs) (*Builder, error) {
7784 return b , nil
7885}
7986
80- type SBundle struct {
81- BlockNumber * big.Int `json:"blockNumber,omitempty"` // if BlockNumber is set it must match DecryptionCondition!
82- MaxBlock * big.Int `json:"maxBlock,omitempty"`
83- Txs types.Transactions `json:"txs"`
84- RevertingHashes []common.Hash `json:"revertingHashes,omitempty"`
85- RefundPercent * int `json:"percent,omitempty"`
86- }
87-
88- func (b * Builder ) AddTransaction (txn * types.Transaction ) (* suavextypes.SimulateTransactionResult , error ) {
87+ func (b * Builder ) addTransaction (txn * types.Transaction , env * environment ) (* suavextypes.SimulateTransactionResult , error ) {
8988 // If the context is not set, the logs will not be recorded
9089 b .env .state .SetTxContext (txn .Hash (), b .env .tcount )
9190
92- logs , err := b .wrk .commitTransaction (b .env , txn )
91+ prevGas := env .header .GasUsed
92+ logs , err := b .wrk .commitTransaction (env , txn )
9393 if err != nil {
9494 return & suavextypes.SimulateTransactionResult {
9595 Error : err .Error (),
9696 Success : false ,
97- }, nil
97+ }, err
98+ }
99+ egp := env .header .GasUsed - prevGas
100+ return receiptToSimResult (& types.Receipt {Logs : logs }, egp ), nil
101+ }
102+
103+ func (b * Builder ) AddTransaction (txn * types.Transaction ) (* suavextypes.SimulateTransactionResult , error ) {
104+ res , _ := b .addTransaction (txn , b .env )
105+ return res , nil
106+ }
107+
108+ func (b * Builder ) AddTransactions (txns types.Transactions ) ([]* suavextypes.SimulateTransactionResult , error ) {
109+ results := make ([]* suavextypes.SimulateTransactionResult , 0 )
110+ snap := b .env .copy ()
111+
112+ for _ , txn := range txns {
113+ res , err := b .addTransaction (txn , snap )
114+ results = append (results , res )
115+ if err != nil {
116+ return results , nil
117+ }
98118 }
99- return receiptToSimResult (& types.Receipt {Logs : logs }), nil
119+ b .env = snap
120+ return results , nil
121+ }
122+
123+ func (b * Builder ) addBundle (bundle * suavextypes.Bundle , env * environment ) (* suavextypes.SimulateBundleResult , error ) {
124+ if err := checkBundleParams (b .env .header .Number , bundle ); err != nil {
125+ return & suavextypes.SimulateBundleResult {
126+ Error : err .Error (),
127+ Success : false ,
128+ }, err
129+ }
130+
131+ revertingHashes := bundle .RevertingHashesMap ()
132+ egp := uint64 (0 )
133+
134+ var results []* suavextypes.SimulateTransactionResult
135+ for _ , txn := range bundle .Txs {
136+ result , err := b .addTransaction (txn , env )
137+ results = append (results , result )
138+ if err != nil {
139+ if _ , ok := revertingHashes [txn .Hash ()]; ok {
140+ // continue if the transaction is in the reverting hashes
141+ continue
142+ }
143+ return & suavextypes.SimulateBundleResult {
144+ Error : err .Error (),
145+ SimulateTransactionResults : results ,
146+ Success : false ,
147+ }, err
148+ }
149+ egp += result .Egp
150+ }
151+
152+ return & suavextypes.SimulateBundleResult {
153+ Egp : egp ,
154+ SimulateTransactionResults : results ,
155+ Success : true ,
156+ }, nil
157+ }
158+
159+ func (b * Builder ) AddBundles (bundles []* suavextypes.Bundle ) ([]* suavextypes.SimulateBundleResult , error ) {
160+ var results []* suavextypes.SimulateBundleResult
161+ snap := b .env .copy ()
162+
163+ for _ , bundle := range bundles {
164+ result , err := b .addBundle (bundle , snap )
165+ results = append (results , result )
166+ if err != nil {
167+ return results , nil
168+ }
169+ }
170+
171+ b .env = snap
172+ return results , nil
100173}
101174
102175func (b * Builder ) FillPending () error {
@@ -139,8 +212,8 @@ func (b *Builder) Bid(builderPubKey phase0.BLSPubKey) (*suavextypes.SubmitBlockR
139212
140213 blockBidMsg := builderV1.BidTrace {
141214 Slot : b .args .Slot ,
142- ParentHash : phase0 . Hash32 ( payload .ParentHash ) ,
143- BlockHash : phase0 . Hash32 ( payload .BlockHash ) ,
215+ ParentHash : payload .ParentHash ,
216+ BlockHash : payload .BlockHash ,
144217 BuilderPubkey : builderPubKey ,
145218 ProposerPubkey : phase0 .BLSPubKey (proposerPubkey ),
146219 ProposerFeeRecipient : bellatrix .ExecutionAddress (b .args .FeeRecipient ),
@@ -169,8 +242,9 @@ func (b *Builder) Bid(builderPubKey phase0.BLSPubKey) (*suavextypes.SubmitBlockR
169242 return & bidRequest , nil
170243}
171244
172- func receiptToSimResult (receipt * types.Receipt ) * suavextypes.SimulateTransactionResult {
245+ func receiptToSimResult (receipt * types.Receipt , egp uint64 ) * suavextypes.SimulateTransactionResult {
173246 result := & suavextypes.SimulateTransactionResult {
247+ Egp : egp ,
174248 Success : true ,
175249 Logs : []* suavextypes.SimulatedLog {},
176250 }
@@ -223,3 +297,33 @@ func executableDataToDenebExecutionPayload(data *engine.ExecutableData) (*deneb.
223297 Withdrawals : withdrawalData ,
224298 }, nil
225299}
300+
301+ func checkBundleParams (currentBlockNumber * big.Int , bundle * suavextypes.Bundle ) error {
302+ if bundle .BlockNumber != nil && bundle .MaxBlock != nil && bundle .BlockNumber .Cmp (bundle .MaxBlock ) > 0 {
303+ return ErrInvalidInclusionRange
304+ }
305+
306+ // check inclusion target if BlockNumber is set
307+ if bundle .BlockNumber != nil {
308+ if bundle .MaxBlock == nil && currentBlockNumber .Cmp (bundle .BlockNumber ) != 0 {
309+ return ErrInvalidBlockNumber
310+ }
311+
312+ if bundle .MaxBlock != nil {
313+ if currentBlockNumber .Cmp (bundle .MaxBlock ) > 0 {
314+ return ErrExceedsMaxBlock
315+ }
316+
317+ if currentBlockNumber .Cmp (bundle .BlockNumber ) < 0 {
318+ return ErrInvalidBlockNumber
319+ }
320+ }
321+ }
322+
323+ // check if the bundle has transactions
324+ if bundle .Txs == nil || bundle .Txs .Len () == 0 {
325+ return ErrEmptyTxs
326+ }
327+
328+ return nil
329+ }
0 commit comments