Skip to content

Commit

Permalink
Addjust profit calculation and use the blocks channel directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruteri authored and avalonche committed Mar 8, 2023
1 parent 1d78769 commit 643b3b6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
11 changes: 8 additions & 3 deletions builder/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package builder

import (
"errors"
_ "os"

"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -74,13 +75,17 @@ func (b *Builder) OnPayloadAttribute(attrs *BuilderPayloadAttributes) error {
attrs.GasLimit = vd.GasLimit

if b.eth.Synced() {
block := b.eth.GetBlockByHash(attrs.HeadHash)
if block == nil {
parentBlock := b.eth.GetBlockByHash(attrs.HeadHash)
if parentBlock == nil {
log.Info("Block hash not found in blocktree", "head block hash", attrs.HeadHash)
return err
}

executableData := b.eth.BuildBlock(attrs)
executableData, block := b.eth.BuildBlock(attrs)
if executableData == nil || block == nil {
log.Error("did not receive the payload")
return errors.New("could not build block")
}
payload, err := executableDataToExecutionPayload(executableData)
if err != nil {
log.Error("could not format execution payload", "err", err)
Expand Down
30 changes: 21 additions & 9 deletions builder/eth_service.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package builder

import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/catalyst"
"github.com/ethereum/go-ethereum/log"
)

type IEthereumService interface {
BuildBlock(attrs *BuilderPayloadAttributes) *beacon.ExecutableDataV1
BuildBlock(attrs *BuilderPayloadAttributes) (*beacon.ExecutableDataV1, *types.Block)
GetBlockByHash(hash common.Hash) *types.Block
Synced() bool
}
Expand All @@ -21,8 +22,8 @@ type testEthereumService struct {
testBlock *types.Block
}

func (t *testEthereumService) BuildBlock(attrs *BuilderPayloadAttributes) *beacon.ExecutableDataV1 {
return t.testExecutableData
func (t *testEthereumService) BuildBlock(attrs *BuilderPayloadAttributes) (*beacon.ExecutableDataV1, *types.Block) {
return t.testExecutableData, t.testBlock
}

func (t *testEthereumService) GetBlockByHash(hash common.Hash) *types.Block { return t.testBlock }
Expand All @@ -37,18 +38,29 @@ func NewEthereumService(eth *eth.Ethereum) *EthereumService {
return &EthereumService{eth: eth}
}

func (s *EthereumService) BuildBlock(attrs *BuilderPayloadAttributes) *beacon.ExecutableDataV1 {
func (s *EthereumService) BuildBlock(attrs *BuilderPayloadAttributes) (*beacon.ExecutableDataV1, *types.Block) {
// Send a request to generate a full block in the background.
// The result can be obtained via the returned channel.
resCh, err := s.eth.Miner().GetSealingBlockAsync(attrs.HeadHash, uint64(attrs.Timestamp), attrs.SuggestedFeeRecipient, attrs.GasLimit, attrs.Random, false)
if err != nil {
log.Error("Failed to create async sealing payload", "err", err)
return nil
return nil, nil
}

resultPayload := catalyst.NewPayload(resCh)
executableData, _ := resultPayload.Resolve()
return executableData
timer := time.NewTimer(4 * time.Second)
defer timer.Stop()

select {
case block := <-resCh:
if block == nil {
log.Error("received nil block from sealing work")
return nil, nil
}
return beacon.BlockToExecutableData(block), block
case <-timer.C:
log.Error("timeout waiting for block", "parent hash", attrs.HeadHash, "slot", attrs.Slot)
return nil, nil
}
}

func (s *EthereumService) GetBlockByHash(hash common.Hash) *types.Block {
Expand Down
5 changes: 4 additions & 1 deletion builder/eth_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ func TestBuildBlock(t *testing.T) {
}

service := NewEthereumService(ethservice)
executableData := service.BuildBlock(testPayloadAttributes)
executableData, block := service.BuildBlock(testPayloadAttributes)

require.Equal(t, common.Address{0x04, 0x10}, executableData.FeeRecipient)
require.Equal(t, common.Hash{0x05, 0x10}, executableData.Random)
require.Equal(t, parent.Hash(), executableData.ParentHash)
require.Equal(t, parent.Time()+1, executableData.Timestamp)
require.Equal(t, block.ParentHash(), parent.Hash())
require.Equal(t, block.Hash(), executableData.BlockHash)
require.Equal(t, block.Profit.Uint64(), uint64(0))
}

0 comments on commit 643b3b6

Please sign in to comment.