mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-07 14:46:44 +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
|
// past median time of a block
|
||||||
type PastMedianTimeManager interface {
|
type PastMedianTimeManager interface {
|
||||||
PastMedianTime(stagingArea *StagingArea, blockHash *externalapi.DomainHash) (int64, error)
|
PastMedianTime(stagingArea *StagingArea, blockHash *externalapi.DomainHash) (int64, error)
|
||||||
|
InvalidateVirtualPastMedianTimeCache()
|
||||||
}
|
}
|
||||||
|
@ -184,6 +184,8 @@ func (bp *blockProcessor) validateAndInsertBlock(stagingArea *model.StagingArea,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bp.pastMedianTimeManager.InvalidateVirtualPastMedianTimeCache()
|
||||||
|
|
||||||
bp.blockLogger.LogBlock(block)
|
bp.blockLogger.LogBlock(block)
|
||||||
|
|
||||||
return &externalapi.BlockInsertionResult{
|
return &externalapi.BlockInsertionResult{
|
||||||
|
@ -23,6 +23,8 @@ type pastMedianTimeManager struct {
|
|||||||
ghostdagDataStore model.GHOSTDAGDataStore
|
ghostdagDataStore model.GHOSTDAGDataStore
|
||||||
|
|
||||||
genesisHash *externalapi.DomainHash
|
genesisHash *externalapi.DomainHash
|
||||||
|
|
||||||
|
virtualPastMedianTimeCache int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// New instantiates a new PastMedianTimeManager
|
// New instantiates a new PastMedianTimeManager
|
||||||
@ -47,6 +49,9 @@ func New(timestampDeviationTolerance int,
|
|||||||
|
|
||||||
// PastMedianTime returns the past median time for some block
|
// PastMedianTime returns the past median time for some block
|
||||||
func (pmtm *pastMedianTimeManager) PastMedianTime(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (int64, error) {
|
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)
|
window, err := pmtm.dagTraversalManager.BlockWindow(stagingArea, blockHash, 2*pmtm.timestampDeviationTolerance-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -59,7 +64,16 @@ func (pmtm *pastMedianTimeManager) PastMedianTime(stagingArea *model.StagingArea
|
|||||||
return header.TimeInMilliseconds(), nil
|
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(
|
func (pmtm *pastMedianTimeManager) windowMedianTimestamp(
|
||||||
@ -82,3 +96,7 @@ func (pmtm *pastMedianTimeManager) windowMedianTimestamp(
|
|||||||
|
|
||||||
return timestamps[len(timestamps)/2], nil
|
return timestamps[len(timestamps)/2], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pmtm *pastMedianTimeManager) InvalidateVirtualPastMedianTimeCache() {
|
||||||
|
pmtm.virtualPastMedianTimeCache = 0
|
||||||
|
}
|
||||||
|
@ -23,6 +23,10 @@ type mocPastMedianTimeManager struct {
|
|||||||
pastMedianTimeForTest int64
|
pastMedianTimeForTest int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mdf *mocPastMedianTimeManager) InvalidateVirtualPastMedianTimeCache() {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
// PastMedianTime returns the past median time for the test.
|
// PastMedianTime returns the past median time for the test.
|
||||||
func (mdf *mocPastMedianTimeManager) PastMedianTime(*model.StagingArea, *externalapi.DomainHash) (int64, error) {
|
func (mdf *mocPastMedianTimeManager) PastMedianTime(*model.StagingArea, *externalapi.DomainHash) (int64, error) {
|
||||||
return mdf.pastMedianTimeForTest, nil
|
return mdf.pastMedianTimeForTest, nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user