Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
exclude CL withdrawals from profit calculation (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush authored and avalonche committed Mar 11, 2024
1 parent e0b7604 commit 66fc7a0
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 21 deletions.
2 changes: 2 additions & 0 deletions builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Config struct {
SecondaryRemoteRelayEndpoints []string `toml:",omitempty"`
ValidationBlocklist string `toml:",omitempty"`
ValidationUseCoinbaseDiff bool `toml:",omitempty"`
ValidationExcludeWithdrawals bool `toml:",omitempty"`
BuilderRateLimitDuration string `toml:",omitempty"`
BuilderRateLimitMaxBurst int `toml:",omitempty"`
BuilderRateLimitResubmitInterval string `toml:",omitempty"`
Expand Down Expand Up @@ -52,6 +53,7 @@ var DefaultConfig = Config{
SecondaryRemoteRelayEndpoints: nil,
ValidationBlocklist: "",
ValidationUseCoinbaseDiff: false,
ValidationExcludeWithdrawals: false,
BuilderRateLimitDuration: RateLimitIntervalDefault.String(),
BuilderRateLimitMaxBurst: RateLimitBurstDefault,
DiscardRevertibleTxOnErr: false,
Expand Down
2 changes: 1 addition & 1 deletion builder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func Register(stack *node.Node, backend *eth.Ethereum, cfg *Config) error {
return fmt.Errorf("failed to load validation blocklist %w", err)
}
}
validator = blockvalidation.NewBlockValidationAPI(backend, accessVerifier, cfg.ValidationUseCoinbaseDiff)
validator = blockvalidation.NewBlockValidationAPI(backend, accessVerifier, cfg.ValidationUseCoinbaseDiff, cfg.ValidationExcludeWithdrawals)
}

// Set up builder rate limiter based on environment variables or CLI flags.
Expand Down
3 changes: 3 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
if ctx.IsSet(utils.BuilderBlockValidationUseBalanceDiff.Name) {
bvConfig.UseBalanceDiffProfit = ctx.Bool(utils.BuilderBlockValidationUseBalanceDiff.Name)
}
if ctx.IsSet(utils.BuilderBlockValidationExcludeWithdrawals.Name) {
bvConfig.ExcludeWithdrawals = ctx.Bool(utils.BuilderBlockValidationExcludeWithdrawals.Name)
}

if err := blockvalidationapi.Register(stack, eth, bvConfig); err != nil {
utils.Fatalf("Failed to register the Block Validation API: %v", err)
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ var (
utils.BuilderEnableValidatorChecks,
utils.BuilderBlockValidationBlacklistSourceFilePath,
utils.BuilderBlockValidationUseBalanceDiff,
utils.BuilderBlockValidationExcludeWithdrawals,
utils.BuilderEnableLocalRelay,
utils.BuilderSecondsInSlot,
utils.BuilderSlotsInEpoch,
Expand Down
7 changes: 7 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,12 @@ var (
Value: false,
Category: flags.BuilderCategory,
}
BuilderBlockValidationExcludeWithdrawals = &cli.BoolFlag{
Name: "builder.validation_exclude_withdrawals",
Usage: "Block validation API will exclude CL withdrawals to the fee recipient from the balance delta.",
Value: false,
Category: flags.BuilderCategory,
}
BuilderEnableLocalRelay = &cli.BoolFlag{
Name: "builder.local_relay",
Usage: "Enable the local relay",
Expand Down Expand Up @@ -1624,6 +1630,7 @@ func SetBuilderConfig(ctx *cli.Context, cfg *builder.Config) {
cfg.ValidationBlocklist = ctx.String(BuilderBlockValidationBlacklistSourceFilePath.Name)
}
cfg.ValidationUseCoinbaseDiff = ctx.Bool(BuilderBlockValidationUseBalanceDiff.Name)
cfg.ValidationExcludeWithdrawals = ctx.Bool(BuilderBlockValidationExcludeWithdrawals.Name)
cfg.BuilderRateLimitDuration = ctx.String(BuilderRateLimitDuration.Name)
cfg.BuilderRateLimitMaxBurst = ctx.Int(BuilderRateLimitMaxBurst.Name)
cfg.BuilderSubmissionOffset = ctx.Duration(BuilderSubmissionOffset.Name)
Expand Down
11 changes: 10 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2453,7 +2453,8 @@ func (bc *BlockChain) SetBlockValidatorAndProcessorForTesting(v Validator, p Pro
// It returns nil if the payload is valid, otherwise it returns an error.
// - `useBalanceDiffProfit` if set to false, proposer payment is assumed to be in the last transaction of the block
// otherwise we use proposer balance changes after the block to calculate proposer payment (see details in the code)
func (bc *BlockChain) ValidatePayload(block *types.Block, feeRecipient common.Address, expectedProfit *big.Int, registeredGasLimit uint64, vmConfig vm.Config, useBalanceDiffProfit bool) error {
// - `excludeWithdrawals` if set to true, withdrawals to the fee recipient are excluded from the balance change
func (bc *BlockChain) ValidatePayload(block *types.Block, feeRecipient common.Address, expectedProfit *big.Int, registeredGasLimit uint64, vmConfig vm.Config, useBalanceDiffProfit, excludeWithdrawals bool) error {
header := block.Header()
if err := bc.engine.VerifyHeader(bc, header); err != nil {
return err
Expand Down Expand Up @@ -2495,6 +2496,14 @@ func (bc *BlockChain) ValidatePayload(block *types.Block, feeRecipient common.Ad

feeRecipientBalanceDelta := new(uint256.Int).Set(statedb.GetBalance(feeRecipient))
feeRecipientBalanceDelta.Sub(feeRecipientBalanceDelta, feeRecipientBalanceBefore)
if excludeWithdrawals {
for _, w := range block.Withdrawals() {
if w.Address == feeRecipient {
amount := new(uint256.Int).Mul(new(uint256.Int).SetUint64(w.Amount), uint256.NewInt(params.GWei))
feeRecipientBalanceDelta.Sub(feeRecipientBalanceDelta, amount)
}
}
}

if bc.Config().IsShanghai(header.Number, header.Time) {
if header.WithdrawalsHash == nil {
Expand Down
2 changes: 1 addition & 1 deletion core/txpool/blobpool/blobpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1664,4 +1664,4 @@ func (p *BlobPool) Status(hash common.Hash) txpool.TxStatus {

func (p *BlobPool) IsPrivateTxHash(hash common.Hash) bool {
return p.privateTxs.Contains(hash)
}
}
12 changes: 6 additions & 6 deletions core/txpool/blobpool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import (

// Config are the configuration parameters of the blob transaction pool.
type Config struct {
Datadir string // Data directory containing the currently executable blobs
Datacap uint64 // Soft-cap of database storage (hard cap is larger due to overhead)
PriceBump uint64 // Minimum price bump percentage to replace an already existing nonce
Datadir string // Data directory containing the currently executable blobs
Datacap uint64 // Soft-cap of database storage (hard cap is larger due to overhead)
PriceBump uint64 // Minimum price bump percentage to replace an already existing nonce
PrivateTxLifetime time.Duration // Maximum amount of time to keep private transactions private
}

// DefaultConfig contains the default configurations for the transaction pool.
var DefaultConfig = Config{
Datadir: "blobpool",
Datacap: 10 * 1024 * 1024 * 1024 / 4, // TODO(karalabe): /4 handicap for rollout, gradually bump back up to 10GB
PriceBump: 100, // either have patience or be aggressive, no mushy ground
Datadir: "blobpool",
Datacap: 10 * 1024 * 1024 * 1024 / 4, // TODO(karalabe): /4 handicap for rollout, gradually bump back up to 10GB
PriceBump: 100, // either have patience or be aggressive, no mushy ground
PrivateTxLifetime: 3 * 24 * time.Hour,
}

Expand Down
2 changes: 1 addition & 1 deletion core/txpool/subpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type SubPool interface {
Add(txs []*types.Transaction, local bool, sync bool, private bool) []error

IsPrivateTxHash(hash common.Hash) bool

// Pending retrieves all currently processable transactions, grouped by origin
// account and sorted by nonce.
//
Expand Down
11 changes: 8 additions & 3 deletions eth/block-validation/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type BlockValidationConfig struct {
BlacklistSourceFilePath string
// If set to true, proposer payment is calculated as a balance difference of the fee recipient.
UseBalanceDiffProfit bool
// If set to true, withdrawals to the fee recipient are excluded from the balance difference.
ExcludeWithdrawals bool
}

// Register adds catalyst APIs to the full node.
Expand All @@ -109,7 +111,7 @@ func Register(stack *node.Node, backend *eth.Ethereum, cfg BlockValidationConfig
stack.RegisterAPIs([]rpc.API{
{
Namespace: "flashbots",
Service: NewBlockValidationAPI(backend, accessVerifier, cfg.UseBalanceDiffProfit),
Service: NewBlockValidationAPI(backend, accessVerifier, cfg.UseBalanceDiffProfit, cfg.ExcludeWithdrawals),
},
})
return nil
Expand All @@ -120,15 +122,18 @@ type BlockValidationAPI struct {
accessVerifier *AccessVerifier
// If set to true, proposer payment is calculated as a balance difference of the fee recipient.
useBalanceDiffProfit bool
// If set to true, withdrawals to the fee recipient are excluded from the balance delta.
excludeWithdrawals bool
}

// NewConsensusAPI creates a new consensus api for the given backend.
// The underlying blockchain needs to have a valid terminal total difficulty set.
func NewBlockValidationAPI(eth *eth.Ethereum, accessVerifier *AccessVerifier, useBalanceDiffProfit bool) *BlockValidationAPI {
func NewBlockValidationAPI(eth *eth.Ethereum, accessVerifier *AccessVerifier, useBalanceDiffProfit, excludeWithdrawals bool) *BlockValidationAPI {
return &BlockValidationAPI{
eth: eth,
accessVerifier: accessVerifier,
useBalanceDiffProfit: useBalanceDiffProfit,
excludeWithdrawals: excludeWithdrawals,
}
}

Expand Down Expand Up @@ -278,7 +283,7 @@ func (api *BlockValidationAPI) validateBlock(block *types.Block, msg *builderApi
vmconfig = vm.Config{Tracer: tracer}
}

err := api.eth.BlockChain().ValidatePayload(block, feeRecipient, expectedProfit, registeredGasLimit, vmconfig, api.useBalanceDiffProfit)
err := api.eth.BlockChain().ValidatePayload(block, feeRecipient, expectedProfit, registeredGasLimit, vmconfig, api.useBalanceDiffProfit, api.excludeWithdrawals)
if err != nil {
return err
}
Expand Down
110 changes: 104 additions & 6 deletions eth/block-validation/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestValidateBuilderSubmissionV1(t *testing.T) {
ethservice.Merger().ReachTTD()
defer n.Close()

api := NewBlockValidationAPI(ethservice, nil, true)
api := NewBlockValidationAPI(ethservice, nil, true, true)
parent := preMergeBlocks[len(preMergeBlocks)-1]

api.eth.APIBackend.Miner().SetEtherbase(testValidatorAddr)
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestValidateBuilderSubmissionV2(t *testing.T) {
ethservice.Merger().ReachTTD()
defer n.Close()

api := NewBlockValidationAPI(ethservice, nil, true)
api := NewBlockValidationAPI(ethservice, nil, true, true)
parent := preMergeBlocks[len(preMergeBlocks)-1]

api.eth.APIBackend.Miner().SetEtherbase(testBuilderAddr)
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestValidateBuilderSubmissionV3(t *testing.T) {
ethservice.Merger().ReachTTD()
defer n.Close()

api := NewBlockValidationAPI(ethservice, nil, true)
api := NewBlockValidationAPI(ethservice, nil, true, false)
parent := ethservice.BlockChain().CurrentHeader()

api.eth.APIBackend.Miner().SetEtherbase(testBuilderAddr)
Expand Down Expand Up @@ -855,7 +855,7 @@ func TestValidateBuilderSubmissionV2_CoinbasePaymentDefault(t *testing.T) {
ethservice.Merger().ReachTTD()
defer n.Close()

api := NewBlockValidationAPI(ethservice, nil, true)
api := NewBlockValidationAPI(ethservice, nil, true, true)

baseFee := eip1559.CalcBaseFee(ethservice.BlockChain().Config(), lastBlock.Header())
txs := make(types.Transactions, 0)
Expand Down Expand Up @@ -967,8 +967,8 @@ func TestValidateBuilderSubmissionV2_Blocklist(t *testing.T) {
},
}

apiWithBlock := NewBlockValidationAPI(ethservice, accessVerifier, true)
apiNoBlock := NewBlockValidationAPI(ethservice, nil, true)
apiWithBlock := NewBlockValidationAPI(ethservice, accessVerifier, true, true)
apiNoBlock := NewBlockValidationAPI(ethservice, nil, true, true)

baseFee := eip1559.CalcBaseFee(ethservice.BlockChain().Config(), lastBlock.Header())
blockedTxs := make(types.Transactions, 0)
Expand Down Expand Up @@ -1014,3 +1014,101 @@ func TestValidateBuilderSubmissionV2_Blocklist(t *testing.T) {
})
}
}

// This tests payment when the proposer fee recipient receives CL withdrawal.
func TestValidateBuilderSubmissionV2_ExcludeWithdrawals(t *testing.T) {
genesis, preMergeBlocks := generatePreMergeChain(20)
lastBlock := preMergeBlocks[len(preMergeBlocks)-1]
time := lastBlock.Time() + 5
genesis.Config.ShanghaiTime = &time
n, ethservice := startEthService(t, genesis, preMergeBlocks)
ethservice.Merger().ReachTTD()
defer n.Close()

api := NewBlockValidationAPI(ethservice, nil, true, true)

baseFee := eip1559.CalcBaseFee(ethservice.BlockChain().Config(), lastBlock.Header())
txs := make(types.Transactions, 0)

statedb, _ := ethservice.BlockChain().StateAt(lastBlock.Root())
nonce := statedb.GetNonce(testAddr)
signer := types.LatestSigner(ethservice.BlockChain().Config())

expectedProfit := uint64(0)

tx1, _ := types.SignTx(types.NewTransaction(nonce, common.Address{0x16}, big.NewInt(10), 21000, big.NewInt(2*baseFee.Int64()), nil), signer, testKey)
txs = append(txs, tx1)
expectedProfit += 21000 * baseFee.Uint64()

// this tx will use 56996 gas
tx2, _ := types.SignTx(types.NewContractCreation(nonce+1, new(big.Int), 1000000, big.NewInt(2*baseFee.Int64()), logCode), signer, testKey)
txs = append(txs, tx2)
expectedProfit += 56996 * baseFee.Uint64()

tx3, _ := types.SignTx(types.NewTransaction(nonce+2, testAddr, big.NewInt(10), 21000, baseFee, nil), signer, testKey)
txs = append(txs, tx3)

// this transaction sends 7 wei to the proposer fee recipient, this should count as a profit
tx4, _ := types.SignTx(types.NewTransaction(nonce+3, testValidatorAddr, big.NewInt(7), 21000, baseFee, nil), signer, testKey)
txs = append(txs, tx4)
expectedProfit += 7

withdrawals := []*types.Withdrawal{
{
Index: 0,
Validator: 1,
Amount: 100,
Address: testAddr,
},
{
Index: 1,
Validator: 1,
Amount: 17,
Address: testValidatorAddr,
},
{
Index: 1,
Validator: 1,
Amount: 21,
Address: testValidatorAddr,
},
}
withdrawalsRoot := types.DeriveSha(types.Withdrawals(withdrawals), trie.NewStackTrie(nil))

buildBlockArgs := buildBlockArgs{
parentHash: lastBlock.Hash(),
parentRoot: lastBlock.Root(),
feeRecipient: testValidatorAddr,
txs: txs,
random: common.Hash{},
number: lastBlock.NumberU64() + 1,
gasLimit: lastBlock.GasLimit(),
timestamp: lastBlock.Time() + 5,
extraData: nil,
baseFeePerGas: baseFee,
withdrawals: withdrawals,
}

execData, err := buildBlock(buildBlockArgs, ethservice.BlockChain())
require.NoError(t, err)

value := big.NewInt(int64(expectedProfit))

req, err := executableDataToBlockValidationRequest(execData, testValidatorAddr, value, withdrawalsRoot)
require.NoError(t, err)
require.NoError(t, api.ValidateBuilderSubmissionV2(req))

// try to claim less profit than expected, should work
value.SetUint64(expectedProfit - 1)

req, err = executableDataToBlockValidationRequest(execData, testValidatorAddr, value, withdrawalsRoot)
require.NoError(t, err)
require.NoError(t, api.ValidateBuilderSubmissionV2(req))

// try to claim more profit than expected, should fail
value.SetUint64(expectedProfit + 1)

req, err = executableDataToBlockValidationRequest(execData, testValidatorAddr, value, withdrawalsRoot)
require.NoError(t, err)
require.ErrorContains(t, api.ValidateBuilderSubmissionV2(req), "payment")
}
2 changes: 1 addition & 1 deletion eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type txPool interface {
// can decide whether to receive notifications only for newly seen transactions
// or also for reorged out ones.
SubscribeTransactions(ch chan<- core.NewTxsEvent, reorgs bool) event.Subscription

// IsPrivateTxHash indicates if the transaction hash should not
// be broadcast on public channels
IsPrivateTxHash(hash common.Hash) bool
Expand Down
3 changes: 2 additions & 1 deletion internal/ethapi/sbundle_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"math/big"
"time"

"github.com/ethereum/go-ethereum/consensus/misc/eip4844"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
Expand Down

0 comments on commit 66fc7a0

Please sign in to comment.