Skip to content
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
8 changes: 4 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ func (bc *BlockChain) GetJustifiedNumber(header *types.Header) uint64 {
}

// getFinalizedNumber returns the highest finalized number before the specific block.
func (bc *BlockChain) getFinalizedNumber(header *types.Header) uint64 {
func (bc *BlockChain) GetFinalizedNumber(header *types.Header) uint64 {
if p, ok := bc.engine.(consensus.PoSA); ok {
if finalizedHeader := p.GetFinalizedHeader(bc, header); finalizedHeader != nil {
return finalizedHeader.Number.Uint64()
Expand Down Expand Up @@ -747,7 +747,7 @@ func (bc *BlockChain) loadLastState() error {
bc.currentBlock.Store(headBlock.Header())
headBlockGauge.Update(int64(headBlock.NumberU64()))
justifiedBlockGauge.Update(int64(bc.GetJustifiedNumber(headBlock.Header())))
finalizedBlockGauge.Update(int64(bc.getFinalizedNumber(headBlock.Header())))
finalizedBlockGauge.Update(int64(bc.GetFinalizedNumber(headBlock.Header())))

// Restore the last known head header
headHeader := headBlock.Header()
Expand Down Expand Up @@ -1196,7 +1196,7 @@ func (bc *BlockChain) SnapSyncCommitHead(hash common.Hash) error {
bc.currentBlock.Store(block.Header())
headBlockGauge.Update(int64(block.NumberU64()))
justifiedBlockGauge.Update(int64(bc.GetJustifiedNumber(block.Header())))
finalizedBlockGauge.Update(int64(bc.getFinalizedNumber(block.Header())))
finalizedBlockGauge.Update(int64(bc.GetFinalizedNumber(block.Header())))
bc.chainmu.Unlock()

// Destroy any existing state snapshot and regenerate it in the background,
Expand Down Expand Up @@ -1337,7 +1337,7 @@ func (bc *BlockChain) writeHeadBlock(block *types.Block) {
bc.currentBlock.Store(block.Header())
headBlockGauge.Update(int64(block.NumberU64()))
justifiedBlockGauge.Update(int64(bc.GetJustifiedNumber(block.Header())))
finalizedBlockGauge.Update(int64(bc.getFinalizedNumber(block.Header())))
finalizedBlockGauge.Update(int64(bc.GetFinalizedNumber(block.Header())))
}

// stopWithoutSaving stops the blockchain service. If any imports are currently in progress
Expand Down
8 changes: 4 additions & 4 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewHeaderChain(chainDb ethdb.Database, config *params.ChainConfig, engine c
hc.currentHeaderHash = hc.CurrentHeader().Hash()
headHeaderGauge.Update(hc.CurrentHeader().Number.Int64())
justifiedBlockGauge.Update(int64(hc.GetJustifiedNumber(hc.CurrentHeader())))
finalizedBlockGauge.Update(int64(hc.getFinalizedNumber(hc.CurrentHeader())))
finalizedBlockGauge.Update(int64(hc.GetFinalizedNumber(hc.CurrentHeader())))

return hc, nil
}
Expand All @@ -116,7 +116,7 @@ func (hc *HeaderChain) GetJustifiedNumber(header *types.Header) uint64 {
}

// getFinalizedNumber returns the highest finalized number before the specific block.
func (hc *HeaderChain) getFinalizedNumber(header *types.Header) uint64 {
func (hc *HeaderChain) GetFinalizedNumber(header *types.Header) uint64 {
if p, ok := hc.engine.(consensus.PoSA); ok {
if finalizedHeader := p.GetFinalizedHeader(hc, header); finalizedHeader != nil {
return finalizedHeader.Number.Uint64()
Expand Down Expand Up @@ -585,7 +585,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
hc.currentHeaderHash = head.Hash()
headHeaderGauge.Update(head.Number.Int64())
justifiedBlockGauge.Update(int64(hc.GetJustifiedNumber(head)))
finalizedBlockGauge.Update(int64(hc.getFinalizedNumber(head)))
finalizedBlockGauge.Update(int64(hc.GetFinalizedNumber(head)))
}

type (
Expand Down Expand Up @@ -673,7 +673,7 @@ func (hc *HeaderChain) setHead(headBlock uint64, headTime uint64, updateFn Updat
hc.currentHeaderHash = parentHash
headHeaderGauge.Update(parent.Number.Int64())
justifiedBlockGauge.Update(int64(hc.GetJustifiedNumber(parent)))
finalizedBlockGauge.Update(int64(hc.getFinalizedNumber(parent)))
finalizedBlockGauge.Update(int64(hc.GetFinalizedNumber(parent)))

// If this is the first iteration, wipe any leftover data upwards too so
// we don't end up with dangling daps in the database
Expand Down
56 changes: 37 additions & 19 deletions miner/bid_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type bidSimulator struct {

simBidMu sync.RWMutex
simulatingBid map[common.Hash]*BidRuntime // prevBlockHash -> bidRuntime, in the process of simulation
bidsToSim map[uint64][]*BidRuntime // blockNumber --> bidRuntime list, used to discard envs

maxBidsPerBuilder uint32 // Maximum number of bids allowed per builder per block
}
Expand Down Expand Up @@ -143,6 +144,7 @@ func newBidSimulator(
bestBid: make(map[common.Hash]*BidRuntime),
bestBidToRun: make(map[common.Hash]*types.Bid),
simulatingBid: make(map[common.Hash]*BidRuntime),
bidsToSim: make(map[uint64][]*BidRuntime),
}
if delayLeftOver != nil {
b.delayLeftOver = *delayLeftOver
Expand Down Expand Up @@ -265,12 +267,6 @@ func (b *bidSimulator) SetBestBid(prevBlockHash common.Hash, bid *BidRuntime) {
b.bestBidMu.Lock()
defer b.bestBidMu.Unlock()

// must discard the environment of the last best bid, otherwise it will cause memory leak
last := b.bestBid[prevBlockHash]
if last != nil && last.env != nil {
last.env.discard()
}

b.bestBid[prevBlockHash] = bid
}

Expand Down Expand Up @@ -330,6 +326,21 @@ func (b *bidSimulator) RemoveSimulatingBid(prevBlockHash common.Hash) {
delete(b.simulatingBid, prevBlockHash)
}

func (b *bidSimulator) AddBidToSim(bidRuntime *BidRuntime) {
b.simBidMu.Lock()
defer b.simBidMu.Unlock()

if bidRuntime == nil || bidRuntime.bid == nil {
return
}

blockNumber := bidRuntime.bid.BlockNumber
if _, ok := b.bidsToSim[blockNumber]; !ok {
b.bidsToSim[blockNumber] = make([]*BidRuntime, 3)
}
b.bidsToSim[blockNumber] = append(b.bidsToSim[blockNumber], bidRuntime)
}

func (b *bidSimulator) mainLoop() {
defer b.chainHeadSub.Unsubscribe()

Expand Down Expand Up @@ -501,32 +512,42 @@ func (b *bidSimulator) clearLoop() {
delete(b.pending, blockNumber)
b.pendingMu.Unlock()

b.bestBidMu.Lock()
if bid, ok := b.bestBid[parentHash]; ok {
bid.env.discard()
clearThreshold := b.chain.GetFinalizedNumber(b.chain.GetHeaderByHash(parentHash))
if blockNumber > b.chain.TriesInMemory() {
clearThreshold = max(clearThreshold, blockNumber-b.chain.TriesInMemory())
}
delete(b.bestBid, parentHash)

b.bestBidMu.Lock()
for k, v := range b.bestBid {
if v.bid.BlockNumber <= blockNumber-b.chain.TriesInMemory() {
v.env.discard()
if v.bid.BlockNumber <= clearThreshold {
delete(b.bestBid, k)
}
}
delete(b.bestBidToRun, parentHash)
for k, v := range b.bestBidToRun {
if v.BlockNumber <= blockNumber-b.chain.TriesInMemory() {
if v.BlockNumber <= clearThreshold {
delete(b.bestBidToRun, k)
}
}
b.bestBidMu.Unlock()

b.simBidMu.Lock()
for k, v := range b.simulatingBid {
if v.bid.BlockNumber <= blockNumber-b.chain.TriesInMemory() {
v.env.discard()
if v.bid.BlockNumber <= clearThreshold {
delete(b.simulatingBid, k)
}
}
for blockNumber, bidList := range b.bidsToSim {
if blockNumber <= clearThreshold {
for _, bid := range bidList {
if bid.env != nil {
// envs for simulating only discard here
bid.env.discard()
}
}
}
delete(b.bidsToSim, blockNumber)
}
b.simBidMu.Unlock()
}

Expand Down Expand Up @@ -631,10 +652,6 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {

if bidRuntime.env != nil {
logCtx = append(logCtx, "gasLimit", bidRuntime.env.header.GasLimit)

if err != nil || !success {
bidRuntime.env.discard()
}
}

if err != nil {
Expand Down Expand Up @@ -675,6 +692,7 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {
}, false); err != nil {
return
}
b.AddBidToSim(bidRuntime)

// if the left time is not enough to do simulation, return
delay := b.engine.Delay(b.chain, bidRuntime.env.header, &b.delayLeftOver)
Expand Down