Skip to content

Commit

Permalink
added append time calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
hubchub committed May 4, 2023
1 parent f10daa8 commit 6b16f47
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
17 changes: 11 additions & 6 deletions core/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,14 @@ func (sl *Slice) Append(header *types.Header, domPendingHeader *types.Header, do
log.Trace("Entropy Calculations", "header", header.Hash(), "S", common.BigBitsToBits(s), "DeltaS", common.BigBitsToBits(header.CalcDeltaS()), "IntrinsicS", common.BigBitsToBits(header.CalcIntrinsicS()))

time11 := common.PrettyDuration(time.Since(start))
//Append has succeeded write the batch

// Append has succeeded write the batch
if err := batch.Write(); err != nil {
return nil, err
}
time12 := common.PrettyDuration(time.Since(start))
sl.writeToPhCacheAndPickPhHead(pendingHeaderWithTermini)
appendFinished := time.Since(start)
time12 := common.PrettyDuration(appendFinished)
sl.writeToPhCacheAndPickPhHead(pendingHeaderWithTermini, &appendFinished)

// Relay the new pendingHeader
go sl.relayPh(pendingHeaderWithTermini, domOrigin, block.Location())
Expand Down Expand Up @@ -486,7 +488,7 @@ func (sl *Slice) updatePhCacheFromDom(pendingHeader types.PendingHeader, termini
for _, i := range indices {
combinedPendingHeader = sl.combinePendingHeader(pendingHeader.Header, combinedPendingHeader, i, false)
}
sl.writeToPhCacheAndPickPhHead(types.PendingHeader{Header: combinedPendingHeader, Termini: localPendingHeader.Termini})
sl.writeToPhCacheAndPickPhHead(types.PendingHeader{Header: combinedPendingHeader, Termini: localPendingHeader.Termini}, nil)

return nil
}
Expand All @@ -495,7 +497,7 @@ func (sl *Slice) updatePhCacheFromDom(pendingHeader types.PendingHeader, termini
}

// writePhCache dom writes a given pendingHeaderWithTermini to the cache with the terminus used as the key.
func (sl *Slice) writeToPhCacheAndPickPhHead(pendingHeaderWithTermini types.PendingHeader) {
func (sl *Slice) writeToPhCacheAndPickPhHead(pendingHeaderWithTermini types.PendingHeader, appendTime *time.Duration) {
bestPh, exist := sl.phCache[sl.bestPhKey]
if !exist {
log.Error("BestPh Key does not exist for", "key", sl.bestPhKey)
Expand All @@ -520,10 +522,13 @@ func (sl *Slice) writeToPhCacheAndPickPhHead(pendingHeaderWithTermini types.Pend

// Pick a phCache Head
block := sl.hc.GetBlockByHash(pendingHeaderWithTermini.Header.ParentHash())
block.SetAppendTime(*appendTime)
if sl.poem(newPhEntropy, oldBestPhEntropy) {
sl.bestPhKey = pendingHeaderWithTermini.Termini[c_terminusIndex]
sl.hc.SetCurrentHeader(block.Header())
sl.hc.chainHeadFeed.Send(ChainHeadEvent{Block: block})
if appendTime != nil { // If appendTime is nil, it means this was called from updatePhCacheFromDom
sl.hc.chainHeadFeed.Send(ChainHeadEvent{Block: block})
}
log.Debug("Choosing new pending header", "Ph Number:", pendingHeaderWithTermini.Header.NumberArray())
}
}
Expand Down
20 changes: 18 additions & 2 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,9 @@ type Block struct {
subManifest BlockManifest

// caches
hash atomic.Value
size atomic.Value
hash atomic.Value
size atomic.Value
appendTime atomic.Value

// These fields are used by package eth to track
// inter-peer block relay.
Expand Down Expand Up @@ -1023,6 +1024,21 @@ func (b *Block) Hash() common.Hash {
return v
}

// GetAppendTime returns the appendTime of the block
// The appendTime is computed on the first call and cached thereafter.
func (b *Block) GetAppendTime() time.Duration {
if appendTime := b.appendTime.Load(); appendTime != nil {
if val, ok := appendTime.(time.Duration); ok {
return val
}
}
return -1
}

func (b *Block) SetAppendTime(appendTime time.Duration) {
b.appendTime.Store(appendTime)
}

type Blocks []*Block

// PendingHeader stores the header and termini value associated with the header.
Expand Down
4 changes: 4 additions & 0 deletions quaistats/quaistats.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ type blockStats struct {
Chain string `json:"chain"`
ChainID uint64 `json:"chainId"`
Tps uint64 `json:"tps"`
AppendTime time.Duration `json:"appendTime"`
}

type blockCacheDto struct {
Expand Down Expand Up @@ -689,6 +690,8 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {

tps := s.computeTps(block)

appendTime := block.GetAppendTime()

return &blockStats{
Number: header.Number(),
Hash: header.Hash(),
Expand All @@ -709,6 +712,7 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
Chain: common.NodeLocation.Name(),
ChainID: s.chainID.Uint64(),
Tps: tps,
AppendTime: appendTime,
}
}

Expand Down

0 comments on commit 6b16f47

Please sign in to comment.