From 9923fd696a1cecbe1e537e1b2f71cab6aa0fd7ff Mon Sep 17 00:00:00 2001 From: halo3mic Date: Wed, 10 Apr 2024 20:06:28 +0000 Subject: [PATCH] propagate BlobsBundle on build calls --- eth/api_backend.go | 4 +- internal/ethapi/api_test.go | 8 ++-- internal/ethapi/backend.go | 4 +- internal/ethapi/transaction_args_test.go | 8 ++-- miner/builder_legacy.go | 48 ++++++++++++++--------- miner/miner.go | 4 +- suave/backends/eth_backend_server.go | 14 +++---- suave/backends/eth_backend_server_test.go | 8 ++-- 8 files changed, 54 insertions(+), 44 deletions(-) diff --git a/eth/api_backend.go b/eth/api_backend.go index 28aea7317416..c7a84cd17bcb 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -426,11 +426,11 @@ func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Blo return b.eth.stateAtTransaction(ctx, block, txIndex, reexec) } -func (b *EthAPIBackend) BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) { +func (b *EthAPIBackend) BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { return b.eth.Miner().BuildBlockFromTxs(ctx, buildArgs, txs) } -func (b *EthAPIBackend) BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) { +func (b *EthAPIBackend) BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { return b.eth.Miner().BuildBlockFromBundles(ctx, buildArgs, bundles) } diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index 719573130c3b..790d656cfaf2 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -621,18 +621,18 @@ func (b testBackend) ServiceFilter(ctx context.Context, session *bloombits.Match panic("implement me") } -func (n *testBackend) BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) { +func (n *testBackend) BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { block := types.NewBlock(&types.Header{GasUsed: 1000, BaseFee: big.NewInt(1)}, txs, nil, nil, trie.NewStackTrie(nil)) - return block, big.NewInt(11000), nil + return block, big.NewInt(11000), nil, nil } -func (n *testBackend) BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) { +func (n *testBackend) BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { var txs types.Transactions for _, bundle := range bundles { txs = append(txs, bundle.Txs...) } block := types.NewBlock(&types.Header{GasUsed: 1000, BaseFee: big.NewInt(1)}, txs, nil, nil, trie.NewStackTrie(nil)) - return block, big.NewInt(11000), nil + return block, big.NewInt(11000), nil, nil } func (n *testBackend) Call(ctx context.Context, contractAddr common.Address, input []byte) ([]byte, error) { diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index e4fdb43cee41..c1e95755b496 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -98,8 +98,8 @@ type Backend interface { ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) // SUAVE Execution Methods - BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) - BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) + BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) + BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) } func GetAPIs(apiBackend Backend) []rpc.API { diff --git a/internal/ethapi/transaction_args_test.go b/internal/ethapi/transaction_args_test.go index f72a56a6c6c3..87c729052c7b 100644 --- a/internal/ethapi/transaction_args_test.go +++ b/internal/ethapi/transaction_args_test.go @@ -402,12 +402,12 @@ func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) func (b *backendMock) Engine() consensus.Engine { return nil } -func (n *backendMock) BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) { - return nil, nil, nil +func (n *backendMock) BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { + return nil, nil, nil, nil } -func (n *backendMock) BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) { - return nil, nil, nil +func (n *backendMock) BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { + return nil, nil, nil, nil } func (n *backendMock) Call(ctx context.Context, contractAddr common.Address, input []byte) ([]byte, error) { diff --git a/miner/builder_legacy.go b/miner/builder_legacy.go index f7354ea21561..d5bd183cbed9 100644 --- a/miner/builder_legacy.go +++ b/miner/builder_legacy.go @@ -72,7 +72,7 @@ func (miner *Miner) commitPendingTxs(work *environment) error { return nil } -func (miner *Miner) buildBlockFromTxs(ctx context.Context, args *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) { +func (miner *Miner) buildBlockFromTxs(ctx context.Context, args *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { params := &generateParams{ timestamp: args.Timestamp, forceTime: true, @@ -88,36 +88,37 @@ func (miner *Miner) buildBlockFromTxs(ctx context.Context, args *types.BuildBloc work, err := miner.prepareWork(params) if err != nil { - return nil, nil, err + return nil, nil, nil, err } profitPre := work.state.GetBalance(args.FeeRecipient) if err := miner.rawCommitTransactions(work, txs); err != nil { - return nil, nil, err + return nil, nil, nil, err } if args.FillPending { if err := miner.commitPendingTxs(work); err != nil { - return nil, nil, err + return nil, nil, nil, err } } profitPost := work.state.GetBalance(args.FeeRecipient) // TODO : Is it okay to set Uncle List to nil? body := types.Body{Transactions: work.txs, Withdrawals: params.withdrawals} + sidecars := envSidecars(work) block, err := miner.engine.FinalizeAndAssemble(miner.chain, work.header, work.state, &body, work.receipts) if err != nil { - return nil, nil, err + return nil, nil, nil, err } blockProfit := new(big.Int).Sub(profitPost.ToBig(), profitPre.ToBig()) - return block, blockProfit, nil + return block, blockProfit, sidecars, nil } -func (miner *Miner) buildBlockFromBundles(ctx context.Context, args *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) { +func (miner *Miner) buildBlockFromBundles(ctx context.Context, args *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { // create ephemeral addr and private key for payment txn ephemeralPrivKey, err := crypto.GenerateKey() if err != nil { - return nil, nil, err + return nil, nil, nil, err } ephemeralAddr := crypto.PubkeyToAddress(ephemeralPrivKey.PublicKey) @@ -136,7 +137,7 @@ func (miner *Miner) buildBlockFromBundles(ctx context.Context, args *types.Build work, err := miner.prepareWork(params) if err != nil { - return nil, nil, err + return nil, nil, nil, err } // Assume static 28000 gas transfers for both mev-share and proposer payments @@ -150,7 +151,7 @@ func (miner *Miner) buildBlockFromBundles(ctx context.Context, args *types.Build // apply bundle profitPreBundle := work.state.GetBalance(params.coinbase) if err := miner.rawCommitTransactions(work, bundle.Txs); err != nil { - return nil, nil, err + return nil, nil, nil, err } profitPostBundle := work.state.GetBalance(params.coinbase) @@ -173,7 +174,7 @@ func (miner *Miner) buildBlockFromBundles(ctx context.Context, args *types.Build userTx := bundle.Txs[0] // NOTE : assumes first txn is refund recipient refundAddr, err := types.Sender(types.LatestSignerForChainID(userTx.ChainId()), userTx) if err != nil { - return nil, nil, err + return nil, nil, nil, err } paymentTx, err := types.SignTx(types.NewTx(&types.LegacyTx{ Nonce: currNonce, @@ -184,18 +185,18 @@ func (miner *Miner) buildBlockFromBundles(ctx context.Context, args *types.Build }), work.signer, ephemeralPrivKey) if err != nil { - return nil, nil, err + return nil, nil, nil, err } // commit payment txn if err := miner.rawCommitTransactions(work, types.Transactions{paymentTx}); err != nil { - return nil, nil, err + return nil, nil, nil, err } } } if args.FillPending { if err := miner.commitPendingTxs(work); err != nil { - return nil, nil, err + return nil, nil, nil, err } } @@ -213,20 +214,31 @@ func (miner *Miner) buildBlockFromBundles(ctx context.Context, args *types.Build GasPrice: work.header.BaseFee, }), work.signer, ephemeralPrivKey) if err != nil { - return nil, nil, fmt.Errorf("could not sign proposer payment: %w", err) + return nil, nil, nil, fmt.Errorf("could not sign proposer payment: %w", err) } // commit payment txn if err := miner.rawCommitTransactions(work, types.Transactions{paymentTx}); err != nil { - return nil, nil, fmt.Errorf("could not sign proposer payment: %w", err) + return nil, nil, nil, fmt.Errorf("could not sign proposer payment: %w", err) } log.Info("buildBlockFromBundles", "num_bundles", len(bundles), "num_txns", len(work.txs), "profit", proposerProfit) // TODO : Is it okay to set Uncle List to nil? body := types.Body{Transactions: work.txs, Withdrawals: params.withdrawals} + sidecars := envSidecars(work) block, err := miner.engine.FinalizeAndAssemble(miner.chain, work.header, work.state, &body, work.receipts) if err != nil { - return nil, nil, err + return nil, nil, nil, err } - return block, proposerProfit, nil + return block, proposerProfit, sidecars, nil +} + +func envSidecars(env *environment) []*types.BlobTxSidecar { + sidecars := []*types.BlobTxSidecar{} + for _, tx := range env.txs { + if tx.Type() == 0x03 { + sidecars = append(sidecars, env.sidecars[len(sidecars)]) + } + } + return sidecars } diff --git a/miner/miner.go b/miner/miner.go index 0218f4c8532a..18c8ca22b154 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -165,10 +165,10 @@ func (miner *Miner) getPending() *newPayloadResult { return ret } -func (miner *Miner) BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) { +func (miner *Miner) BuildBlockFromTxs(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { return miner.buildBlockFromTxs(ctx, buildArgs, txs) } -func (miner *Miner) BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) { +func (miner *Miner) BuildBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { return miner.buildBlockFromBundles(ctx, buildArgs, bundles) } diff --git a/suave/backends/eth_backend_server.go b/suave/backends/eth_backend_server.go index 571f4a4eddce..759a8fa0cf78 100644 --- a/suave/backends/eth_backend_server.go +++ b/suave/backends/eth_backend_server.go @@ -23,8 +23,8 @@ var _ EthBackend = &EthBackendServer{} // to resolve the EthBackend server queries type EthBackendServerBackend interface { CurrentHeader() *types.Header - BuildBlockFromTxs(ctx context.Context, buildArgs *suave.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) - BuildBlockFromBundles(ctx context.Context, buildArgs *suave.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) + BuildBlockFromTxs(ctx context.Context, buildArgs *suave.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) + BuildBlockFromBundles(ctx context.Context, buildArgs *suave.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) Call(ctx context.Context, contractAddr common.Address, input []byte) ([]byte, error) } @@ -51,13 +51,12 @@ func (e *EthBackendServer) BuildEthBlock(ctx context.Context, buildArgs *types.B } } - block, profit, err := e.b.BuildBlockFromTxs(ctx, buildArgs, txs) + block, profit, scs, err := e.b.BuildBlockFromTxs(ctx, buildArgs, txs) if err != nil { return nil, err } - // TODO: we're not adding blobs, but this is not where you would do it anyways - return engine.BlockToExecutableData(block, profit, nil), nil + return engine.BlockToExecutableData(block, profit, scs), nil } func (e *EthBackendServer) BuildEthBlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*engine.ExecutionPayloadEnvelope, error) { @@ -75,13 +74,12 @@ func (e *EthBackendServer) BuildEthBlockFromBundles(ctx context.Context, buildAr } } - block, profit, err := e.b.BuildBlockFromBundles(ctx, buildArgs, bundles) + block, profit, scs, err := e.b.BuildBlockFromBundles(ctx, buildArgs, bundles) if err != nil { return nil, err } - // TODO: we're not adding blobs, but this is not where you would do it anyways - return engine.BlockToExecutableData(block, profit, nil), nil + return engine.BlockToExecutableData(block, profit, scs), nil } func (e *EthBackendServer) Call(ctx context.Context, contractAddr common.Address, input []byte) ([]byte, error) { diff --git a/suave/backends/eth_backend_server_test.go b/suave/backends/eth_backend_server_test.go index 60bc0d70b64b..7e34ab0e8121 100644 --- a/suave/backends/eth_backend_server_test.go +++ b/suave/backends/eth_backend_server_test.go @@ -39,18 +39,18 @@ func (n *mockBackend) CurrentHeader() *types.Header { return &types.Header{} } -func (n *mockBackend) BuildBlockFromTxs(ctx context.Context, buildArgs *suave.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) { +func (n *mockBackend) BuildBlockFromTxs(ctx context.Context, buildArgs *suave.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { block := types.NewBlock(&types.Header{GasUsed: 1000, BaseFee: big.NewInt(1)}, txs, nil, nil, trie.NewStackTrie(nil)) - return block, big.NewInt(11000), nil + return block, big.NewInt(11000), nil, nil } -func (n *mockBackend) BuildBlockFromBundles(ctx context.Context, buildArgs *suave.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) { +func (n *mockBackend) BuildBlockFromBundles(ctx context.Context, buildArgs *suave.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, []*types.BlobTxSidecar, error) { var txs types.Transactions for _, bundle := range bundles { txs = append(txs, bundle.Txs...) } block := types.NewBlock(&types.Header{GasUsed: 1000, BaseFee: big.NewInt(1)}, txs, nil, nil, trie.NewStackTrie(nil)) - return block, big.NewInt(11000), nil + return block, big.NewInt(11000), nil, nil } func (n *mockBackend) Call(ctx context.Context, contractAddr common.Address, input []byte) ([]byte, error) {