From 2cd097dffb3042d669ff0a14ede7d844fea160c6 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Thu, 22 Feb 2024 12:40:41 +0000 Subject: [PATCH] Expose Bid as function --- miner/builder.go | 11 ++--------- miner/builder_test.go | 20 ++++++++++++++++++++ suave/builder/api/api.go | 11 ++++++++++- suave/builder/api/api_client.go | 7 +++++-- suave/builder/api/api_server.go | 7 ++++--- suave/builder/api/api_test.go | 5 +++-- suave/builder/session_manager.go | 25 +++++++++---------------- 7 files changed, 53 insertions(+), 33 deletions(-) diff --git a/miner/builder.go b/miner/builder.go index 036e65a1cd..578e2a09e5 100644 --- a/miner/builder.go +++ b/miner/builder.go @@ -117,7 +117,7 @@ func (b *Builder) BuildBlock() (*types.Block, error) { return block, nil } -func (b *Builder) Bid(builderPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error) { +func (b *Builder) Bid(builderPubKey phase0.BLSPubKey) (*suavextypes.SubmitBlockRequest, error) { work := b.env if b.block == nil { @@ -157,7 +157,7 @@ func (b *Builder) Bid(builderPubKey phase0.BLSPubKey) (*SubmitBlockRequest, erro return nil, err } - bidRequest := SubmitBlockRequest{ + bidRequest := suavextypes.SubmitBlockRequest{ Root: phase0.Root(root), SubmitBlockRequest: denebBuilder.SubmitBlockRequest{ Message: &blockBidMsg, @@ -169,13 +169,6 @@ func (b *Builder) Bid(builderPubKey phase0.BLSPubKey) (*SubmitBlockRequest, erro return &bidRequest, nil } -// SubmitBlockRequest is an extension of the builder.SubmitBlockRequest with the root -// of the bid that needs to be signed -type SubmitBlockRequest struct { - denebBuilder.SubmitBlockRequest - Root phase0.Root -} - func receiptToSimResult(receipt *types.Receipt) *suavextypes.SimulateTransactionResult { result := &suavextypes.SimulateTransactionResult{ Success: true, diff --git a/miner/builder_test.go b/miner/builder_test.go index dc615f579c..d0e89ff3d4 100644 --- a/miner/builder_test.go +++ b/miner/builder_test.go @@ -104,6 +104,26 @@ func TestBuilder_ContractWithLogs(t *testing.T) { require.Equal(t, simResult.Logs[0].Topics[0], suaveExample1Artifact.Abi.Events["SomeEvent"].ID) } +func TestBuilder_Bid(t *testing.T) { + t.Parallel() + + config, _ := newMockBuilderConfig(t) + + builder, err := NewBuilder(config, &BuilderArgs{}) + require.NoError(t, err) + + _, err = builder.Bid([48]byte{}) + require.Error(t, err, "cannot create bid without block") + + _, err = builder.BuildBlock() + require.NoError(t, err) + + req, err := builder.Bid([48]byte{}) + require.NoError(t, err) + + fmt.Println("-- req --", req) +} + func newMockBuilderConfig(t *testing.T) (*BuilderConfig, *testWorkerBackend) { var ( db = rawdb.NewMemoryDatabase() diff --git a/suave/builder/api/api.go b/suave/builder/api/api.go index adc9064527..41376fede6 100644 --- a/suave/builder/api/api.go +++ b/suave/builder/api/api.go @@ -4,6 +4,8 @@ import ( "context" "math/big" + denebBuilder "github.com/attestantio/go-builder-client/api/deneb" + "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" @@ -65,9 +67,16 @@ type simulateLogMarshaling struct { Data hexutil.Bytes } +// SubmitBlockRequest is an extension of the builder.SubmitBlockRequest with the root +// of the bid that needs to be signed +type SubmitBlockRequest struct { + denebBuilder.SubmitBlockRequest + Root phase0.Root +} + type API interface { NewSession(ctx context.Context, args *BuildBlockArgs) (string, error) AddTransaction(ctx context.Context, sessionId string, tx *types.Transaction) (*SimulateTransactionResult, error) BuildBlock(ctx context.Context, sessionId string) error - Bid(ctx context.Context, sessioId string, blsPubKey string) error + Bid(ctx context.Context, sessioId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error) } diff --git a/suave/builder/api/api_client.go b/suave/builder/api/api_client.go index f2a7147397..9f75c04a62 100644 --- a/suave/builder/api/api_client.go +++ b/suave/builder/api/api_client.go @@ -3,6 +3,7 @@ package api import ( "context" + "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc" ) @@ -45,6 +46,8 @@ func (a *APIClient) BuildBlock(ctx context.Context, sessionId string) error { return a.rpc.CallContext(ctx, nil, "suavex_buildBlock", sessionId) } -func (a *APIClient) Bid(ctx context.Context, sessioId string, blsPubKey string) error { - return a.rpc.CallContext(ctx, nil, "suavex_bid", sessioId, blsPubKey) +func (a *APIClient) Bid(ctx context.Context, sessioId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error) { + var req *SubmitBlockRequest + err := a.rpc.CallContext(ctx, &req, "suavex_bid", sessioId, blsPubKey) + return req, err } diff --git a/suave/builder/api/api_server.go b/suave/builder/api/api_server.go index ef0333bc90..233240bb3d 100644 --- a/suave/builder/api/api_server.go +++ b/suave/builder/api/api_server.go @@ -3,6 +3,7 @@ package api import ( "context" + "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/ethereum/go-ethereum/core/types" ) @@ -13,7 +14,7 @@ type SessionManager interface { NewSession(context.Context, *BuildBlockArgs) (string, error) AddTransaction(sessionId string, tx *types.Transaction) (*SimulateTransactionResult, error) BuildBlock(sessionId string) error - Bid(sessionId string, blsPubKey string) error + Bid(sessionId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error) } func NewServer(s SessionManager) *Server { @@ -39,8 +40,8 @@ func (s *Server) BuildBlock(ctx context.Context, sessionId string) error { return s.sessionMngr.BuildBlock(sessionId) } -func (s *Server) Bid(ctx context.Context, sessioId string, blsPubKey string) error { - return nil +func (s *Server) Bid(ctx context.Context, sessionId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error) { + return s.sessionMngr.Bid(sessionId, blsPubKey) } // TODO: Remove diff --git a/suave/builder/api/api_test.go b/suave/builder/api/api_test.go index ecd97763bc..0c29477ac1 100644 --- a/suave/builder/api/api_test.go +++ b/suave/builder/api/api_test.go @@ -5,6 +5,7 @@ import ( "math/big" "testing" + "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc" @@ -46,6 +47,6 @@ func (nullSessionManager) BuildBlock(sessionId string) error { return nil } -func (nullSessionManager) Bid(sessioId string, blsPubKey string) error { - return nil +func (nullSessionManager) Bid(sessioId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error) { + return nil, nil } diff --git a/suave/builder/session_manager.go b/suave/builder/session_manager.go index a8d1281615..cef7126591 100644 --- a/suave/builder/session_manager.go +++ b/suave/builder/session_manager.go @@ -7,10 +7,10 @@ import ( "sync" "time" + "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/txpool" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/miner" @@ -19,21 +19,6 @@ import ( "github.com/google/uuid" ) -// blockchain is the minimum interface to the blockchain -// required to build a block -type blockchain interface { - core.ChainContext - - // Header returns the current tip of the chain - CurrentHeader() *types.Header - - // StateAt returns the state at the given root - StateAt(root common.Hash) (*state.StateDB, error) - - // Config returns the chain config - Config() *params.ChainConfig -} - type Config struct { GasCeil uint64 SessionIdleTimeout time.Duration @@ -174,6 +159,14 @@ func (s *SessionManager) BuildBlock(sessionId string) error { return err } +func (s *SessionManager) Bid(sessionId string, blsPubKey phase0.BLSPubKey) (*api.SubmitBlockRequest, error) { + builder, err := s.getSession(sessionId) + if err != nil { + return nil, err + } + return builder.Bid(blsPubKey) +} + // CalcBaseFee calculates the basefee of the header. func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { // If the current block is the first EIP-1559 block, return the InitialBaseFee.