Fix notifyVirtualSelectedParentBlueScoreChanged to show the selected tip blue score instead of the virtual's (#1309)

* Fix notifyVirtualSelectedParentBlueScoreChanged to show the selected tip blue score instead of the virtual's

* Fix ShouldMine() to fetch selected tip header
This commit is contained in:
Ori Newman 2020-12-29 12:07:05 +02:00 committed by GitHub
parent 48278bd1c0
commit 02d5fb29cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 21 deletions

View File

@ -65,14 +65,19 @@ func (f *FlowContext) ShouldMine() (bool, error) {
return false, err
}
virtualSelectedParentHeader, err := f.domain.Consensus().GetBlockHeader(virtualSelectedParent)
if err != nil {
return false, err
}
now := mstime.Now().UnixMilliseconds()
if now-virtualSelectedParent.Header.TimeInMilliseconds < maxSelectedParentTimeDiffToAllowMiningInMilliSeconds {
if now-virtualSelectedParentHeader.TimeInMilliseconds < maxSelectedParentTimeDiffToAllowMiningInMilliSeconds {
log.Debugf("The selected tip timestamp is recent (%d), so ShouldMine returns true",
virtualSelectedParent.Header.TimeInMilliseconds)
virtualSelectedParentHeader.TimeInMilliseconds)
return true, nil
}
log.Debugf("The selected tip timestamp is old (%d), so ShouldMine returns false",
virtualSelectedParent.Header.TimeInMilliseconds)
virtualSelectedParentHeader.TimeInMilliseconds)
return false, nil
}

View File

@ -106,11 +106,17 @@ func (m *Manager) notifyVirtualSelectedParentBlueScoreChanged() error {
onEnd := logger.LogAndMeasureExecutionTime(log, "RPCManager.NotifyVirtualSelectedParentBlueScoreChanged")
defer onEnd()
virtualInfo, err := m.context.Domain.Consensus().GetVirtualInfo()
virtualSelectedParent, err := m.context.Domain.Consensus().GetVirtualSelectedParent()
if err != nil {
return err
}
notification := appmessage.NewVirtualSelectedParentBlueScoreChangedNotificationMessage(virtualInfo.BlueScore)
blockInfo, err := m.context.Domain.Consensus().GetBlockInfo(virtualSelectedParent)
if err != nil {
return err
}
notification := appmessage.NewVirtualSelectedParentBlueScoreChangedNotificationMessage(blockInfo.BlueScore)
return m.context.NotificationManager.NotifyVirtualSelectedParentBlueScoreChanged(notification)
}

View File

@ -3,7 +3,6 @@ package rpchandlers
import (
"github.com/kaspanet/kaspad/app/appmessage"
"github.com/kaspanet/kaspad/app/rpc/rpccontext"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
)
@ -14,8 +13,7 @@ func HandleGetSelectedTipHash(context *rpccontext.Context, _ *router.Router, _ a
return nil, err
}
response := appmessage.NewGetSelectedTipHashResponseMessage(
consensushashing.BlockHash(selectedTip).String())
response := appmessage.NewGetSelectedTipHashResponseMessage(selectedTip.String())
return response, nil
}

View File

@ -229,7 +229,7 @@ func (s *consensus) ValidateAndInsertPruningPoint(newPruningPoint *externalapi.D
return s.blockProcessor.ValidateAndInsertPruningPoint(newPruningPoint, serializedUTXOSet)
}
func (s *consensus) GetVirtualSelectedParent() (*externalapi.DomainBlock, error) {
func (s *consensus) GetVirtualSelectedParent() (*externalapi.DomainHash, error) {
s.lock.Lock()
defer s.lock.Unlock()
@ -237,7 +237,7 @@ func (s *consensus) GetVirtualSelectedParent() (*externalapi.DomainBlock, error)
if err != nil {
return nil, err
}
return s.blockStore.Block(s.databaseContext, virtualGHOSTDAGData.SelectedParent())
return virtualGHOSTDAGData.SelectedParent(), nil
}
func (s *consensus) Tips() ([]*externalapi.DomainHash, error) {

View File

@ -107,7 +107,7 @@ func TestFinality(t *testing.T) {
if err != nil {
t.Fatalf("TestFinality: Failed getting virtual selectedParent: %v", err)
}
if !consensushashing.BlockHash(selectedTip).Equal(sideChainTipHash) {
if !selectedTip.Equal(sideChainTipHash) {
t.Fatalf("Overtaking block in side-chain is not selectedTip")
}
@ -145,7 +145,7 @@ func TestFinality(t *testing.T) {
if err != nil {
t.Fatalf("TestFinality: Failed getting virtual selectedParent: %v", err)
}
selectedTipGhostDagData, err := consensus.GHOSTDAGDataStore().Get(consensus.DatabaseContext(), consensushashing.BlockHash(selectedTip))
selectedTipGhostDagData, err := consensus.GHOSTDAGDataStore().Get(consensus.DatabaseContext(), selectedTip)
if err != nil {
t.Fatalf("TestFinality: Failed getting the ghost dag data of the selected tip: %v", err)
}
@ -329,8 +329,8 @@ func TestBoundedMergeDepth(t *testing.T) {
t.Fatalf("TestBoundedMergeDepth: Failed getting the virtual selected parent %v", err)
}
if !consensushashing.BlockHash(virtualSelectedParent).Equal(consensushashing.BlockHash(pointAtBlueKosherizing)) {
t.Fatalf("TestBoundedMergeDepth: Expected %s to be the selectedTip but found %s instead", consensushashing.BlockHash(pointAtBlueKosherizing), consensushashing.BlockHash(virtualSelectedParent))
if !virtualSelectedParent.Equal(consensushashing.BlockHash(pointAtBlueKosherizing)) {
t.Fatalf("TestBoundedMergeDepth: Expected %s to be the selectedTip but found %s instead", consensushashing.BlockHash(pointAtBlueKosherizing), virtualSelectedParent)
}
// Now let's make the kosherizing block red and try to merge again
@ -346,8 +346,8 @@ func TestBoundedMergeDepth(t *testing.T) {
t.Fatalf("TestBoundedMergeDepth: Failed getting the virtual selected parent %v", err)
}
if !consensushashing.BlockHash(virtualSelectedParent).Equal(tip) {
t.Fatalf("TestBoundedMergeDepth: Expected %s to be the selectedTip but found %s instead", tip, consensushashing.BlockHash(virtualSelectedParent))
if !virtualSelectedParent.Equal(tip) {
t.Fatalf("TestBoundedMergeDepth: Expected %s to be the selectedTip but found %s instead", tip, virtualSelectedParent)
}
virtualGhotDagData, err = consensusReal.GHOSTDAGDataStore().Get(consensusReal.DatabaseContext(), model.VirtualBlockHash)
@ -382,8 +382,8 @@ func TestBoundedMergeDepth(t *testing.T) {
t.Fatalf("TestBoundedMergeDepth: Failed getting the virtual selected parent %v", err)
}
if !consensushashing.BlockHash(virtualSelectedParent).Equal(consensushashing.BlockHash(transitiveBlueKosherizing)) {
t.Fatalf("TestBoundedMergeDepth: Expected %s to be the selectedTip but found %s instead", consensushashing.BlockHash(transitiveBlueKosherizing), consensushashing.BlockHash(virtualSelectedParent))
if !virtualSelectedParent.Equal(consensushashing.BlockHash(transitiveBlueKosherizing)) {
t.Fatalf("TestBoundedMergeDepth: Expected %s to be the selectedTip but found %s instead", consensushashing.BlockHash(transitiveBlueKosherizing), virtualSelectedParent)
}
// Lets validate the status of all the interesting blocks

View File

@ -16,7 +16,7 @@ type Consensus interface {
GetPruningPointUTXOSet(expectedPruningPointHash *DomainHash) ([]byte, error)
PruningPoint() (*DomainHash, error)
ValidateAndInsertPruningPoint(newPruningPoint *DomainBlock, serializedUTXOSet []byte) error
GetVirtualSelectedParent() (*DomainBlock, error)
GetVirtualSelectedParent() (*DomainHash, error)
CreateBlockLocator(lowHash, highHash *DomainHash, limit uint32) (BlockLocator, error)
FindNextBlockLocatorBoundaries(blockLocator BlockLocator) (lowHash, highHash *DomainHash, err error)
GetSyncInfo() (*SyncInfo, error)

View File

@ -44,9 +44,9 @@ func TestVirtualSelectedParentBlueScore(t *testing.T) {
for i := 0; i < blockAmountToMine; i++ {
mineNextBlock(t, kaspad)
notification := <-onVirtualSelectedParentBlueScoreChangedChan
if notification.VirtualSelectedParentBlueScore != 2+uint64(i) {
if notification.VirtualSelectedParentBlueScore != 1+uint64(i) {
t.Fatalf("Unexpected virtual selected parent blue score. Want: %d, got: %d",
2+uint64(i), notification.VirtualSelectedParentBlueScore)
1+uint64(i), notification.VirtualSelectedParentBlueScore)
}
}