Skip to content

Commit

Permalink
feat: add api and util to help builder (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Raina authored Mar 5, 2024
1 parent 2cab6be commit a5e99d3
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 12 deletions.
23 changes: 23 additions & 0 deletions common/bidutil/bidutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package bidutil

import (
"time"

"github.com/ethereum/go-ethereum/core/types"
)

// BidBetterBefore returns the time when the next bid better be received, considering the delay and bid simulation.
// BidBetterBefore is earlier than BidMustBefore.
func BidBetterBefore(parentHeader *types.Header, blockPeriod uint64, delayLeftOver, simulationLeftOver time.Duration) time.Time {
nextHeaderTime := BidMustBefore(parentHeader, blockPeriod, delayLeftOver)
nextHeaderTime = nextHeaderTime.Add(-simulationLeftOver)
return nextHeaderTime
}

// BidMustBefore returns the time when the next bid must be received,
// only considering the consensus delay but not bid simulation duration.
func BidMustBefore(parentHeader *types.Header, blockPeriod uint64, delayLeftOver time.Duration) time.Time {
nextHeaderTime := time.Unix(int64(parentHeader.Time+blockPeriod), 0)
nextHeaderTime = nextHeaderTime.Add(-delayLeftOver)
return nextHeaderTime
}
6 changes: 6 additions & 0 deletions core/types/bid.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/big"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -176,3 +177,8 @@ type BidIssue struct {
BidHash common.Hash
Message string
}

type MevParams struct {
ValidatorCommission int64 // 100 means 1%
BidSimulationLeftOver time.Duration
}
8 changes: 8 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ func (b *EthAPIBackend) MevRunning() bool {
return b.Miner().MevRunning()
}

func (b *EthAPIBackend) MevParams() types.MevParams {
return b.Miner().MevParams()
}

func (b *EthAPIBackend) StartMev() {
b.Miner().StartMev()
}
Expand All @@ -457,6 +461,10 @@ func (b *EthAPIBackend) SendBid(ctx context.Context, bid *types.BidArgs) (common
return b.Miner().SendBid(ctx, bid)
}

func (b *EthAPIBackend) BestBidGasFee(parentHash common.Hash) *big.Int {
return b.Miner().BestPackedBlockReward(parentHash)
}

func (b *EthAPIBackend) MinerInTurn() bool {
return b.Miner().InTurn()
}
9 changes: 9 additions & 0 deletions internal/ethapi/api_mev.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ethapi
import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -96,6 +97,14 @@ func (m *MevAPI) SendBid(ctx context.Context, args types.BidArgs) (common.Hash,
return m.b.SendBid(ctx, &args)
}

func (m *MevAPI) BestBidGasFee(_ context.Context, parentHash common.Hash) *big.Int {
return m.b.BestBidGasFee(parentHash)
}

func (m *MevAPI) Params() types.MevParams {
return m.b.MevParams()
}

// Running returns true if mev is running
func (m *MevAPI) Running() bool {
return m.b.MevRunning()
Expand Down
9 changes: 8 additions & 1 deletion internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,10 @@ func (b testBackend) ServiceFilter(ctx context.Context, session *bloombits.Match
panic("implement me")
}

func (b *testBackend) MevRunning() bool { return false }
func (b *testBackend) MevRunning() bool { return false }
func (b *testBackend) MevParams() types.MevParams {
return types.MevParams{}
}
func (b *testBackend) StartMev() {}
func (b *testBackend) StopMev() {}
func (b *testBackend) AddBuilder(builder common.Address, builderUrl string) error { return nil }
Expand All @@ -556,6 +559,10 @@ func (b *testBackend) SendBid(ctx context.Context, bid *types.BidArgs) (common.H
panic("implement me")
}
func (b *testBackend) MinerInTurn() bool { return false }
func (b *testBackend) BestBidGasFee(parentHash common.Hash) *big.Int {
//TODO implement me
panic("implement me")
}

func TestEstimateGas(t *testing.T) {
t.Parallel()
Expand Down
4 changes: 4 additions & 0 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ type Backend interface {

// MevRunning return true if mev is running
MevRunning() bool
// MevParams returns the static params of mev
MevParams() types.MevParams
// StartMev starts mev
StartMev()
// StopMev stops mev
Expand All @@ -114,6 +116,8 @@ type Backend interface {
RemoveBuilder(builder common.Address) error
// SendBid receives bid from the builders.
SendBid(ctx context.Context, bid *types.BidArgs) (common.Hash, error)
// BestBidGasFee returns the gas fee of the best bid for the given parent hash.
BestBidGasFee(parentHash common.Hash) *big.Int
// MinerInTurn returns true if the validator is in turn to propose the block.
MinerInTurn() bool
}
Expand Down
9 changes: 8 additions & 1 deletion internal/ethapi/transaction_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,10 @@ func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent)

func (b *backendMock) Engine() consensus.Engine { return nil }

func (b *backendMock) MevRunning() bool { return false }
func (b *backendMock) MevRunning() bool { return false }
func (b *backendMock) MevParams() types.MevParams {
return types.MevParams{}
}
func (b *backendMock) StartMev() {}
func (b *backendMock) StopMev() {}
func (b *backendMock) AddBuilder(builder common.Address, builderUrl string) error { return nil }
Expand All @@ -361,3 +364,7 @@ func (b *backendMock) SendBid(ctx context.Context, bid *types.BidArgs) (common.H
panic("implement me")
}
func (b *backendMock) MinerInTurn() bool { return false }
func (b *backendMock) BestBidGasFee(parentHash common.Hash) *big.Int {
//TODO implement me
panic("implement me")
}
9 changes: 7 additions & 2 deletions miner/bid_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/bidutil"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -389,8 +390,12 @@ func (b *bidSimulator) newBidLoop() {

func (b *bidSimulator) bidMustBefore(parentHash common.Hash) time.Time {
parentHeader := b.chain.GetHeaderByHash(parentHash)
nextHeaderTimestamp := parentHeader.Time + b.chainConfig.Parlia.Period
return time.Unix(int64(nextHeaderTimestamp), 0).Add(-b.delayLeftOver)
return bidutil.BidMustBefore(parentHeader, b.chainConfig.Parlia.Period, b.delayLeftOver)
}

func (b *bidSimulator) bidBetterBefore(parentHash common.Hash) time.Time {
parentHeader := b.chain.GetHeaderByHash(parentHash)
return bidutil.BidBetterBefore(parentHeader, b.chainConfig.Parlia.Period, b.delayLeftOver, b.config.BidSimulationLeftOver)
}

func (b *bidSimulator) clearLoop() {
Expand Down
2 changes: 1 addition & 1 deletion miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var DefaultConfig = Config{
NewPayloadTimeout: 2 * time.Second,
DelayLeftOver: 50 * time.Millisecond,

Mev: MevConfig{Enabled: false},
Mev: DefaultMevConfig,
}

// Miner creates blocks and searches for proof-of-work values.
Expand Down
38 changes: 31 additions & 7 deletions miner/miner_mev.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ type BuilderConfig struct {
}

type MevConfig struct {
Enabled bool // Whether to enable Mev or not
SentryURL string // The url of Mev sentry
Builders []BuilderConfig // The list of builders
Enabled bool // Whether to enable Mev or not
SentryURL string // The url of Mev sentry
Builders []BuilderConfig // The list of builders
ValidatorCommission int64 // 100 means 1%
BidSimulationLeftOver time.Duration
}

ValidatorCommission int64 // 100 means 1%
var DefaultMevConfig = MevConfig{
Enabled: false,
SentryURL: "",
Builders: nil,
ValidatorCommission: 100,
BidSimulationLeftOver: 100 * time.Millisecond,
}

// MevRunning return true if mev is running.
Expand Down Expand Up @@ -69,11 +77,11 @@ func (miner *Miner) SendBid(ctx context.Context, bidArgs *types.BidArgs) (common
return common.Hash{}, types.NewInvalidBidError(fmt.Sprintf("fail to convert bidArgs to bid, %v", err))
}

bidMustBefore := miner.bidSimulator.bidMustBefore(bidArgs.RawBid.ParentHash)
timeout := time.Until(bidMustBefore)
bidBetterBefore := miner.bidSimulator.bidBetterBefore(bidArgs.RawBid.ParentHash)
timeout := time.Until(bidBetterBefore)

if timeout <= 0 {
return common.Hash{}, fmt.Errorf("too late, expected befor %s, appeared %s later", bidMustBefore,
return common.Hash{}, fmt.Errorf("too late, expected befor %s, appeared %s later", bidBetterBefore,
common.PrettyDuration(timeout))
}

Expand All @@ -85,3 +93,19 @@ func (miner *Miner) SendBid(ctx context.Context, bidArgs *types.BidArgs) (common

return bid.Hash(), nil
}

func (miner *Miner) BestPackedBlockReward(parentHash common.Hash) *big.Int {
bidRuntime := miner.bidSimulator.GetBestBid(parentHash)
if bidRuntime == nil {
return nil
}

return bidRuntime.packedBlockReward
}

func (miner *Miner) MevParams() types.MevParams {
return types.MevParams{
ValidatorCommission: miner.worker.config.Mev.ValidatorCommission,
BidSimulationLeftOver: miner.worker.config.Mev.BidSimulationLeftOver,
}
}

0 comments on commit a5e99d3

Please sign in to comment.