Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Feb 18, 2024
1 parent da2aed8 commit 11e7c5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
6 changes: 6 additions & 0 deletions core/txpool/bundlepool/bundlepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ func (bgp *BundleGasPricer) Push(gasPrice *big.Int) {

func (bgp *BundleGasPricer) retire() {
now := time.Now()
// make sure the first element is not expired
for !bgp.queue.Empty() {
v, _ := bgp.queue.Peek()
info := v
Expand All @@ -366,6 +367,11 @@ func (bgp *BundleGasPricer) retire() {
}
bgp.queue.Pop()
}
// only keep one element
length := bgp.queue.Size()
for i := 1; i < length; i++ {
bgp.queue.Remove(i)
}
}

// LatestBundleGasPrice is a method to get the latest-cached bundle gas price.
Expand Down
57 changes: 37 additions & 20 deletions miner/bidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"sync"
"time"

"golang.org/x/exp/slices"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -23,6 +21,8 @@ import (
"github.com/ethereum/go-ethereum/rpc"
)

const maxBid int64 = 3

var (
dialer = &net.Dialer{
Timeout: time.Second,
Expand Down Expand Up @@ -52,7 +52,7 @@ type BidderConfig struct {
Enable bool
Validators []ValidatorConfig
Account common.Address
DelayLeftOver time.Duration
DelayLeftOver uint64
}

type Bidder struct {
Expand All @@ -62,7 +62,7 @@ type Bidder struct {

validators map[common.Address]*ethclient.Client // validator address -> ethclient.Client

works map[int64][]*environment
bestWorks map[int64]*environment

newBidCh chan *environment
exitCh chan struct{}
Expand All @@ -78,7 +78,7 @@ func NewBidder(config *BidderConfig, engine consensus.Engine, chain *core.BlockC
engine: engine,
chain: chain,
validators: make(map[common.Address]*ethclient.Client),
works: make(map[int64][]*environment),
bestWorks: make(map[int64]*environment),
newBidCh: make(chan *environment, 10),
exitCh: make(chan struct{}),
}
Expand Down Expand Up @@ -111,28 +111,36 @@ func (b *Bidder) mainLoop() {
defer timer.Stop()
<-timer.C // discard the initial tick

currentHeight := b.chain.CurrentBlock().Number.Int64()
var (
bidNum int64 = 0
bidUntil time.Time
currentHeight = b.chain.CurrentBlock().Number.Int64()
)
for {
select {
case work := <-b.newBidCh:
if work.header.Number.Int64() > currentHeight {
bidNum = 0
bidUntil = time.Unix(int64(work.header.Time+b.chain.Config().Parlia.Period-b.config.DelayLeftOver), 0)
currentHeight = work.header.Number.Int64()
nextHeaderTimestamp := work.header.Time + b.chain.Config().Parlia.Period
timer.Reset(time.Until(time.Unix(int64(nextHeaderTimestamp), 0).Add(-b.config.DelayLeftOver)))

if time.Now().After(bidUntil) {
timer.Reset(0)
} else {
timer.Reset(bidUntil.Sub(time.Now()) / time.Duration(maxBid))
}
}
if bidNum < maxBid && b.isBestWork(work) {
b.bestWorks[work.header.Number.Int64()] = work
}
b.works[work.header.Number.Int64()] = append(b.works[work.header.Number.Int64()], work)
case <-timer.C:
works := b.works[currentHeight]
slices.SortStableFunc(works, func(i, j *environment) int {
return j.profit.Cmp(i.profit)
})

for i, work := range works {
// only bid for the top 3 most profitable works
if i >= 3 {
break
if b.bestWorks[currentHeight] != nil {
b.bid(b.bestWorks[currentHeight])
b.bestWorks[currentHeight] = nil
bidNum++
if bidNum < maxBid && time.Now().Before(bidUntil) {
timer.Reset(bidUntil.Sub(time.Now()) / time.Duration(maxBid-bidNum))
}
b.bid(work)
}
case <-b.exitCh:
return
Expand Down Expand Up @@ -203,7 +211,7 @@ func (b *Bidder) bid(work *environment) {
BlockNumber: parent.Number.Uint64() + 1,
ParentHash: parent.Hash(),
GasUsed: work.header.GasUsed,
GasFee: work.blockReward.Uint64(),
GasFee: work.profit.Uint64(),
Txs: txs,
// TODO: decide builderFee according to realtime traffic and validator commission
}
Expand Down Expand Up @@ -231,6 +239,15 @@ func (b *Bidder) bid(work *environment) {
return
}

// isBestWork returns the work is better than the current best work
func (b *Bidder) isBestWork(work *environment) bool {
if work.profit == nil {
return false
}

return b.bestWorks[work.header.Number.Int64()].profit.Cmp(work.profit) < 0
}

// signBid signs the bid with builder's account
func (b *Bidder) signBid(bid *types.Bid) ([]byte, error) {
bz, err := rlp.EncodeToBytes(bid)
Expand Down
1 change: 0 additions & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,6 @@ func (w *worker) commitTransaction(env *environment, tx *txpool.Transaction, rec

gasUsed := new(big.Int).SetUint64(receipt.GasUsed)
env.profit.Add(env.profit, gasUsed.Mul(gasUsed, tx.Tx.GasPrice()))
env.blockReward.Add(env.blockReward, gasUsed.Mul(gasUsed, tx.Tx.GasPrice()))

return receipt.Logs, nil
}
Expand Down
1 change: 0 additions & 1 deletion miner/worker_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (w *worker) commitWorkV2(interruptCh chan int32, timestamp int64) {
// fillTransactions retrieves the pending bundles and transactions from the txpool and fills them
// into the given sealing block. The selection and ordering strategy can be extended in the future.
func (w *worker) fillTransactionsAndBundles(interruptCh chan int32, env *environment, stopTimer *time.Timer) error {
env.blockReward = new(big.Int)
env.profit = new(big.Int)

var (
Expand Down

0 comments on commit 11e7c5b

Please sign in to comment.