From 02d5fb29cf653ffcba33f16afe1c62d23665d3b4 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Tue, 29 Dec 2020 12:07:05 +0200 Subject: [PATCH] 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 --- app/protocol/flowcontext/sync_rate.go | 11 ++++++++--- app/rpc/manager.go | 10 ++++++++-- app/rpc/rpchandlers/get_selected_tip_hash.go | 4 +--- domain/consensus/consensus.go | 4 ++-- domain/consensus/finality_test.go | 16 ++++++++-------- domain/consensus/model/externalapi/consensus.go | 2 +- .../virtual_selected_parent_blue_score_test.go | 4 ++-- 7 files changed, 30 insertions(+), 21 deletions(-) diff --git a/app/protocol/flowcontext/sync_rate.go b/app/protocol/flowcontext/sync_rate.go index f581c520a..b96036c60 100644 --- a/app/protocol/flowcontext/sync_rate.go +++ b/app/protocol/flowcontext/sync_rate.go @@ -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 } diff --git a/app/rpc/manager.go b/app/rpc/manager.go index 403061282..1d050ad69 100644 --- a/app/rpc/manager.go +++ b/app/rpc/manager.go @@ -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) } diff --git a/app/rpc/rpchandlers/get_selected_tip_hash.go b/app/rpc/rpchandlers/get_selected_tip_hash.go index 3831b98d0..8bdf4b8f9 100644 --- a/app/rpc/rpchandlers/get_selected_tip_hash.go +++ b/app/rpc/rpchandlers/get_selected_tip_hash.go @@ -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 } diff --git a/domain/consensus/consensus.go b/domain/consensus/consensus.go index 97bd4e97c..7bff7e698 100644 --- a/domain/consensus/consensus.go +++ b/domain/consensus/consensus.go @@ -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) { diff --git a/domain/consensus/finality_test.go b/domain/consensus/finality_test.go index 4fc06b148..a428127c7 100644 --- a/domain/consensus/finality_test.go +++ b/domain/consensus/finality_test.go @@ -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 diff --git a/domain/consensus/model/externalapi/consensus.go b/domain/consensus/model/externalapi/consensus.go index 7df5b99e7..7ac38e21f 100644 --- a/domain/consensus/model/externalapi/consensus.go +++ b/domain/consensus/model/externalapi/consensus.go @@ -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) diff --git a/testing/integration/virtual_selected_parent_blue_score_test.go b/testing/integration/virtual_selected_parent_blue_score_test.go index b005da809..f3ce0969e 100644 --- a/testing/integration/virtual_selected_parent_blue_score_test.go +++ b/testing/integration/virtual_selected_parent_blue_score_test.go @@ -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) } }