mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-04 13:16:43 +00:00
Cache virtual past median time (#1775)
* Add cache for virtual pastMedianTime * Implement InvalidateVirtualPastMedianTimeCache for mocPastMedianTimeManager
This commit is contained in:
parent
2f7a1395e7
commit
f7cce5cb39
@ -6,4 +6,5 @@ import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
// past median time of a block
|
||||
type PastMedianTimeManager interface {
|
||||
PastMedianTime(stagingArea *StagingArea, blockHash *externalapi.DomainHash) (int64, error)
|
||||
InvalidateVirtualPastMedianTimeCache()
|
||||
}
|
||||
|
@ -184,6 +184,8 @@ func (bp *blockProcessor) validateAndInsertBlock(stagingArea *model.StagingArea,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bp.pastMedianTimeManager.InvalidateVirtualPastMedianTimeCache()
|
||||
|
||||
bp.blockLogger.LogBlock(block)
|
||||
|
||||
return &externalapi.BlockInsertionResult{
|
||||
|
@ -23,6 +23,8 @@ type pastMedianTimeManager struct {
|
||||
ghostdagDataStore model.GHOSTDAGDataStore
|
||||
|
||||
genesisHash *externalapi.DomainHash
|
||||
|
||||
virtualPastMedianTimeCache int64
|
||||
}
|
||||
|
||||
// New instantiates a new PastMedianTimeManager
|
||||
@ -47,6 +49,9 @@ func New(timestampDeviationTolerance int,
|
||||
|
||||
// PastMedianTime returns the past median time for some block
|
||||
func (pmtm *pastMedianTimeManager) PastMedianTime(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (int64, error) {
|
||||
if blockHash == model.VirtualBlockHash && pmtm.virtualPastMedianTimeCache != 0 {
|
||||
return pmtm.virtualPastMedianTimeCache, nil
|
||||
}
|
||||
window, err := pmtm.dagTraversalManager.BlockWindow(stagingArea, blockHash, 2*pmtm.timestampDeviationTolerance-1)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@ -59,7 +64,16 @@ func (pmtm *pastMedianTimeManager) PastMedianTime(stagingArea *model.StagingArea
|
||||
return header.TimeInMilliseconds(), nil
|
||||
}
|
||||
|
||||
return pmtm.windowMedianTimestamp(stagingArea, window)
|
||||
pastMedianTime, err := pmtm.windowMedianTimestamp(stagingArea, window)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if blockHash == model.VirtualBlockHash {
|
||||
pmtm.virtualPastMedianTimeCache = pastMedianTime
|
||||
}
|
||||
|
||||
return pastMedianTime, nil
|
||||
}
|
||||
|
||||
func (pmtm *pastMedianTimeManager) windowMedianTimestamp(
|
||||
@ -82,3 +96,7 @@ func (pmtm *pastMedianTimeManager) windowMedianTimestamp(
|
||||
|
||||
return timestamps[len(timestamps)/2], nil
|
||||
}
|
||||
|
||||
func (pmtm *pastMedianTimeManager) InvalidateVirtualPastMedianTimeCache() {
|
||||
pmtm.virtualPastMedianTimeCache = 0
|
||||
}
|
||||
|
@ -23,6 +23,10 @@ type mocPastMedianTimeManager struct {
|
||||
pastMedianTimeForTest int64
|
||||
}
|
||||
|
||||
func (mdf *mocPastMedianTimeManager) InvalidateVirtualPastMedianTimeCache() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
// PastMedianTime returns the past median time for the test.
|
||||
func (mdf *mocPastMedianTimeManager) PastMedianTime(*model.StagingArea, *externalapi.DomainHash) (int64, error) {
|
||||
return mdf.pastMedianTimeForTest, nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user