Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBac
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit)
}

func NewSimulatedBackendChain(database ethdb.Database, blockchain *core.BlockChain) *SimulatedBackend {
backend := &SimulatedBackend{
database: database,
blockchain: blockchain,
config: blockchain.Config(),
}

filterBackend := &filterBackend{database, blockchain, backend}
backend.filterSystem = filters.NewFilterSystem(filterBackend, filters.Config{})
backend.events = filters.NewEventSystem(backend.filterSystem, false)
backend.rollback(blockchain.CurrentBlock())

return backend
}

// Close terminates the underlying blockchain's update loop.
func (b *SimulatedBackend) Close() error {
b.blockchain.Stop()
Expand Down
10 changes: 5 additions & 5 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type IRelay interface {
}

type IBuilder interface {
OnPayloadAttribute(attrs *BuilderPayloadAttributes) error
OnPayloadAttribute(attrs *types.BuilderPayloadAttributes) error
Start() error
Stop() error
}
Expand All @@ -63,7 +63,7 @@ type Builder struct {

slotMu sync.Mutex
slot uint64
slotAttrs []BuilderPayloadAttributes
slotAttrs []types.BuilderPayloadAttributes
slotCtx context.Context
slotCtxCancel context.CancelFunc
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (b *Builder) Stop() error {
return nil
}

func (b *Builder) onSealedBlock(block *types.Block, ordersClosedAt time.Time, sealedAt time.Time, commitedBundles []types.SimulatedBundle, allBundles []types.SimulatedBundle, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *BuilderPayloadAttributes) error {
func (b *Builder) onSealedBlock(block *types.Block, ordersClosedAt time.Time, sealedAt time.Time, commitedBundles []types.SimulatedBundle, allBundles []types.SimulatedBundle, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *types.BuilderPayloadAttributes) error {
executableData := beacon.BlockToExecutableData(block)
payload, err := executableDataToExecutionPayload(executableData)
if err != nil {
Expand Down Expand Up @@ -157,7 +157,7 @@ func (b *Builder) onSealedBlock(block *types.Block, ordersClosedAt time.Time, se
return nil
}

func (b *Builder) OnPayloadAttribute(attrs *BuilderPayloadAttributes) error {
func (b *Builder) OnPayloadAttribute(attrs *types.BuilderPayloadAttributes) error {
if attrs == nil {
return nil
}
Expand Down Expand Up @@ -222,7 +222,7 @@ type blockQueueEntry struct {
allBundles []types.SimulatedBundle
}

func (b *Builder) runBuildingJob(slotCtx context.Context, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *BuilderPayloadAttributes) {
func (b *Builder) runBuildingJob(slotCtx context.Context, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *types.BuilderPayloadAttributes) {
ctx, cancel := context.WithTimeout(slotCtx, 12*time.Second)
defer cancel()

Expand Down
2 changes: 1 addition & 1 deletion builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestOnPayloadAttributes(t *testing.T) {
require.NoError(t, err)
testBlock.Profit = big.NewInt(10)

testPayloadAttributes := &BuilderPayloadAttributes{
testPayloadAttributes := &types.BuilderPayloadAttributes{
Timestamp: hexutil.Uint64(104),
Random: common.Hash{0x05, 0x10},
SuggestedFeeRecipient: common.Address{0x04, 0x10},
Expand Down
6 changes: 3 additions & 3 deletions builder/eth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type IEthereumService interface {
BuildBlock(attrs *BuilderPayloadAttributes, sealedBlockCallback miner.BlockHookFn) error
BuildBlock(attrs *types.BuilderPayloadAttributes, sealedBlockCallback miner.BlockHookFn) error
GetBlockByHash(hash common.Hash) *types.Block
Synced() bool
}
Expand All @@ -26,7 +26,7 @@ type testEthereumService struct {
testAllBundles []types.SimulatedBundle
}

func (t *testEthereumService) BuildBlock(attrs *BuilderPayloadAttributes, sealedBlockCallback miner.BlockHookFn) error {
func (t *testEthereumService) BuildBlock(attrs *types.BuilderPayloadAttributes, sealedBlockCallback miner.BlockHookFn) error {
sealedBlockCallback(t.testBlock, time.Now(), t.testBundlesMerged, t.testAllBundles)
return nil
}
Expand All @@ -43,7 +43,7 @@ func NewEthereumService(eth *eth.Ethereum) *EthereumService {
return &EthereumService{eth: eth}
}

func (s *EthereumService) BuildBlock(attrs *BuilderPayloadAttributes, sealedBlockCallback miner.BlockHookFn) error {
func (s *EthereumService) BuildBlock(attrs *types.BuilderPayloadAttributes, sealedBlockCallback miner.BlockHookFn) error {
// 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, sealedBlockCallback)
Expand Down
2 changes: 1 addition & 1 deletion builder/eth_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestBuildBlock(t *testing.T) {

parent := ethservice.BlockChain().CurrentBlock()

testPayloadAttributes := &BuilderPayloadAttributes{
testPayloadAttributes := &types.BuilderPayloadAttributes{
Timestamp: hexutil.Uint64(parent.Time() + 1),
Random: common.Hash{0x05, 0x10},
SuggestedFeeRecipient: common.Address{0x04, 0x10},
Expand Down
4 changes: 2 additions & 2 deletions builder/local_relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestGetHeader(t *testing.T) {
require.Equal(t, ``, rr.Body.String())
require.Equal(t, 204, rr.Code)

backend.OnPayloadAttribute(&BuilderPayloadAttributes{})
backend.OnPayloadAttribute(&types.BuilderPayloadAttributes{})
time.Sleep(2 * time.Second)

path = fmt.Sprintf("/eth/v1/builder/header/%d/%s/%s", 0, forkchoiceData.ParentHash.Hex(), validator.Pk.String())
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestGetPayload(t *testing.T) {
backend, relay, validator := newTestBackend(t, forkchoiceData, forkchoiceBlock)

registerValidator(t, validator, relay)
backend.OnPayloadAttribute(&BuilderPayloadAttributes{})
backend.OnPayloadAttribute(&types.BuilderPayloadAttributes{})
time.Sleep(2 * time.Second)

path := fmt.Sprintf("/eth/v1/builder/header/%d/%s/%s", 0, forkchoiceData.ParentHash.Hex(), validator.Pk.String())
Expand Down
44 changes: 18 additions & 26 deletions builder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ const (
_PathGetPayload = "/eth/v1/builder/blinded_blocks"
)

type BuilderPayloadAttributes struct {
Timestamp hexutil.Uint64 `json:"timestamp"`
Random common.Hash `json:"prevRandao"`
SuggestedFeeRecipient common.Address `json:"suggestedFeeRecipient,omitempty"`
Slot uint64 `json:"slot"`
HeadHash common.Hash `json:"blockHash"`
GasLimit uint64
}

type Service struct {
srv *http.Server
builder IBuilder
Expand All @@ -64,7 +55,7 @@ func (s *Service) Stop() error {
return nil
}

func (s *Service) PayloadAttributes(payloadAttributes *BuilderPayloadAttributes) error {
func (s *Service) PayloadAttributes(payloadAttributes *types.BuilderPayloadAttributes) error {
return s.builder.OnPayloadAttribute(payloadAttributes)
}

Expand All @@ -83,7 +74,7 @@ func getRouter(localRelay *LocalRelay) http.Handler {
return loggedRouter
}

func NewService(listenAddr string, localRelay *LocalRelay, builder *Builder) *Service {
func NewService(listenAddr string, localRelay *LocalRelay, builder IBuilder) *Service {
var srv *http.Server
if localRelay != nil {
srv = &http.Server{
Expand All @@ -105,26 +96,11 @@ func NewService(listenAddr string, localRelay *LocalRelay, builder *Builder) *Se
}

func Register(stack *node.Node, backend *eth.Ethereum, cfg *Config) error {
envRelaySkBytes, err := hexutil.Decode(cfg.RelaySecretKey)
if err != nil {
return errors.New("incorrect builder API secret key provided")
}

relaySk, err := bls.SecretKeyFromBytes(envRelaySkBytes[:])
if err != nil {
return errors.New("incorrect builder API secret key provided")
}

envBuilderSkBytes, err := hexutil.Decode(cfg.BuilderSecretKey)
if err != nil {
return errors.New("incorrect builder API secret key provided")
}

builderSk, err := bls.SecretKeyFromBytes(envBuilderSkBytes[:])
if err != nil {
return errors.New("incorrect builder API secret key provided")
}

genesisForkVersionBytes, err := hexutil.Decode(cfg.GenesisForkVersion)
if err != nil {
return fmt.Errorf("invalid genesisForkVersion: %w", err)
Expand All @@ -148,6 +124,16 @@ func Register(stack *node.Node, backend *eth.Ethereum, cfg *Config) error {

var localRelay *LocalRelay
if cfg.EnableLocalRelay {
envRelaySkBytes, err := hexutil.Decode(cfg.RelaySecretKey)
if err != nil {
return errors.New("incorrect builder API secret key provided")
}

relaySk, err := bls.SecretKeyFromBytes(envRelaySkBytes[:])
if err != nil {
return errors.New("incorrect builder API secret key provided")
}

localRelay = NewLocalRelay(relaySk, beaconClient, builderSigningDomain, proposerSigningDomain, ForkData{cfg.GenesisForkVersion, cfg.BellatrixForkVersion, cfg.GenesisValidatorsRoot}, cfg.EnableValidatorChecks)
}

Expand Down Expand Up @@ -203,6 +189,12 @@ func Register(stack *node.Node, backend *eth.Ethereum, cfg *Config) error {
}

ethereumService := NewEthereumService(backend)

builderSk, err := bls.SecretKeyFromBytes(envBuilderSkBytes[:])
if err != nil {
return errors.New("incorrect builder API secret key provided")
}

builderBackend := NewBuilder(builderSk, ds, relay, builderSigningDomain, ethereumService, cfg.DryRun, validator)
builderService := NewService(cfg.ListenAddr, localRelay, builderBackend)

Expand Down
3 changes: 1 addition & 2 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/accounts/scwallet"
"github.com/ethereum/go-ethereum/accounts/usbwallet"
builder "github.com/ethereum/go-ethereum/builder"
"github.com/ethereum/go-ethereum/builder"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/core/rawdb"
blockvalidationapi "github.com/ethereum/go-ethereum/eth/block-validation"
Expand Down Expand Up @@ -171,7 +171,6 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
override := ctx.Bool(utils.OverrideTerminalTotalDifficultyPassed.Name)
cfg.Eth.OverrideTerminalTotalDifficultyPassed = &override
}

// Construct the backend service.
backend, eth := utils.RegisterEthService(stack, &cfg.Eth, &cfg.Builder)

Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
builder "github.com/ethereum/go-ethereum/builder"
"github.com/ethereum/go-ethereum/builder"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/fdlimit"
"github.com/ethereum/go-ethereum/consensus"
Expand Down
15 changes: 15 additions & 0 deletions core/types/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package types

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)

type BuilderPayloadAttributes struct {
Timestamp hexutil.Uint64 `json:"timestamp"`
Random common.Hash `json:"prevRandao"`
SuggestedFeeRecipient common.Address `json:"suggestedFeeRecipient,omitempty"`
Slot uint64 `json:"slot"`
HeadHash common.Hash `json:"blockHash"`
GasLimit uint64
}
50 changes: 39 additions & 11 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,39 @@ func (s TxByNonce) Len() int { return len(s) }
func (s TxByNonce) Less(i, j int) bool { return s[i].Nonce() < s[j].Nonce() }
func (s TxByNonce) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

type _Order interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why aren't bundles and related types in a separate file like builder.go?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular type is used internally by TxWithMinerFee so this is I think the more fitting place

AsTx() *Transaction
AsBundle() *SimulatedBundle
}

type _TxOrder struct {
tx *Transaction
}

func (o _TxOrder) AsTx() *Transaction { return o.tx }
func (o _TxOrder) AsBundle() *SimulatedBundle { return nil }

type _BundleOrder struct {
bundle *SimulatedBundle
}

func (o _BundleOrder) AsTx() *Transaction { return nil }
func (o _BundleOrder) AsBundle() *SimulatedBundle { return o.bundle }

// TxWithMinerFee wraps a transaction with its gas price or effective miner gasTipCap
type TxWithMinerFee struct {
Tx *Transaction
Bundle *SimulatedBundle
order _Order
minerFee *big.Int
}

func (t *TxWithMinerFee) Tx() *Transaction {
return t.order.AsTx()
}

func (t *TxWithMinerFee) Bundle() *SimulatedBundle {
return t.order.AsBundle()
}

// NewTxWithMinerFee creates a wrapped transaction, calculating the effective
// miner gasTipCap if a base fee is provided.
// Returns error in case of a negative effective miner gasTipCap.
Expand All @@ -475,7 +501,7 @@ func NewTxWithMinerFee(tx *Transaction, baseFee *big.Int) (*TxWithMinerFee, erro
return nil, err
}
return &TxWithMinerFee{
Tx: tx,
order: _TxOrder{tx},
minerFee: minerFee,
}, nil
}
Expand All @@ -484,7 +510,7 @@ func NewTxWithMinerFee(tx *Transaction, baseFee *big.Int) (*TxWithMinerFee, erro
func NewBundleWithMinerFee(bundle *SimulatedBundle, baseFee *big.Int) (*TxWithMinerFee, error) {
minerFee := bundle.MevGasPrice
return &TxWithMinerFee{
Bundle: bundle,
order: _BundleOrder{bundle},
minerFee: minerFee,
}, nil
}
Expand All @@ -499,13 +525,14 @@ func (s TxByPriceAndTime) Less(i, j int) bool {
// deterministic sorting
cmp := s[i].minerFee.Cmp(s[j].minerFee)
if cmp == 0 {
if s[i].Tx != nil && s[j].Tx != nil {
return s[i].Tx.time.Before(s[j].Tx.time)
} else if s[i].Bundle != nil && s[j].Bundle != nil {
return s[i].Bundle.TotalGasUsed <= s[j].Bundle.TotalGasUsed
} else if s[i].Bundle != nil {
if s[i].Tx() != nil && s[j].Tx() != nil {
return s[i].Tx().time.Before(s[j].Tx().time)
} else if s[i].Bundle() != nil && s[j].Bundle() != nil {
return s[i].Bundle().TotalGasUsed <= s[j].Bundle().TotalGasUsed
} else if s[i].Bundle() != nil {
return false
}

return true
}
return cmp > 0
Expand Down Expand Up @@ -549,6 +576,7 @@ func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]Transa
}
heads = append(heads, wrapped)
}

for from, accTxs := range txs {
acc, _ := Sender(signer, accTxs[0])
wrapped, err := NewTxWithMinerFee(accTxs[0], baseFee)
Expand Down Expand Up @@ -594,8 +622,8 @@ func (t *TransactionsByPriceAndNonce) Peek() *TxWithMinerFee {

// Shift replaces the current best head with the next one from the same account.
func (t *TransactionsByPriceAndNonce) Shift() {
if t.heads[0].Tx != nil {
acc, _ := Sender(t.signer, t.heads[0].Tx)
if tx := t.heads[0].Tx(); tx != nil {
acc, _ := Sender(t.signer, tx)
if txs, ok := t.txs[acc]; ok && len(txs) > 0 {
if wrapped, err := NewTxWithMinerFee(txs[0], t.baseFee); err == nil {
t.heads[0], t.txs[acc] = wrapped, txs[1:]
Expand Down
4 changes: 2 additions & 2 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func testTransactionPriceNonceSort(t *testing.T, baseFee *big.Int) {

txs := Transactions{}
for tx := txset.Peek(); tx != nil; tx = txset.Peek() {
txs = append(txs, tx.Tx)
txs = append(txs, tx.Tx())
txset.Shift()
}
if len(txs) != expectedCount {
Expand Down Expand Up @@ -381,7 +381,7 @@ func TestTransactionTimeSort(t *testing.T) {

txs := Transactions{}
for tx := txset.Peek(); tx != nil; tx = txset.Peek() {
txs = append(txs, tx.Tx)
txs = append(txs, tx.Tx())
txset.Shift()
}
if len(txs) != len(keys) {
Expand Down
6 changes: 5 additions & 1 deletion eth/tracers/logger/account_touch_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ type AccountTouchTracer struct {
// including tx sender and tx.to from the top level call
func NewAccountTouchTracer() *AccountTouchTracer {
return &AccountTouchTracer{
touched: map[common.Address]struct{}{},
touched: make(map[common.Address]struct{}),
}
}

func (t *AccountTouchTracer) TouchedAddressesSet() map[common.Address]struct{} {
return t.touched
}

func (t *AccountTouchTracer) TouchedAddresses() []common.Address {
result := make([]common.Address, 0, len(t.touched))

Expand Down
Loading