Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuildParents and ValidateHeaderInContext improvement #2058

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
// a given set of direct parents
type BlockParentBuilder interface {
BuildParents(stagingArea *StagingArea,
daaScore uint64,
directParentHashes []*externalapi.DomainHash) ([]externalapi.BlockLevelParents, error)
}
6 changes: 3 additions & 3 deletions domain/consensus/processes/blockbuilder/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (bb *blockBuilder) buildHeader(stagingArea *model.StagingArea, transactions
return nil, err
}

parents, err := bb.newBlockParents(stagingArea, daaScore)
parents, err := bb.newBlockParents(stagingArea)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -240,12 +240,12 @@ func (bb *blockBuilder) buildHeader(stagingArea *model.StagingArea, transactions
), nil
}

func (bb *blockBuilder) newBlockParents(stagingArea *model.StagingArea, daaScore uint64) ([]externalapi.BlockLevelParents, error) {
func (bb *blockBuilder) newBlockParents(stagingArea *model.StagingArea) ([]externalapi.BlockLevelParents, error) {
virtualBlockRelations, err := bb.blockRelationStore.BlockRelation(bb.databaseContext, stagingArea, model.VirtualBlockHash)
if err != nil {
return nil, err
}
return bb.blockParentBuilder.BuildParents(stagingArea, daaScore, virtualBlockRelations.Parents)
return bb.blockParentBuilder.BuildParents(stagingArea, virtualBlockRelations.Parents)
}

func (bb *blockBuilder) newBlockTime(stagingArea *model.StagingArea) (int64, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (bb *testBlockBuilder) buildUTXOInvalidHeader(stagingArea *model.StagingAre
return nil, err
}

parents, err := bb.blockParentBuilder.BuildParents(stagingArea, daaScore, parentHashes)
parents, err := bb.blockParentBuilder.BuildParents(stagingArea, parentHashes)
if err != nil {
return nil, err
}
Expand Down
23 changes: 10 additions & 13 deletions domain/consensus/processes/blockparentbuilder/blockparentbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ func New(
}

func (bpb *blockParentBuilder) BuildParents(stagingArea *model.StagingArea,
daaScore uint64, directParentHashes []*externalapi.DomainHash) ([]externalapi.BlockLevelParents, error) {

// Late on we'll mutate direct parent hashes, so we first clone it.
directParentHashesCopy := make([]*externalapi.DomainHash, len(directParentHashes))
copy(directParentHashesCopy, directParentHashes)
directParentHashes []*externalapi.DomainHash) ([]externalapi.BlockLevelParents, error) {

pruningPoint, err := bpb.pruningStore.PruningPoint(bpb.databaseContext, stagingArea)
if err != nil {
Expand All @@ -64,10 +60,10 @@ func (bpb *blockParentBuilder) BuildParents(stagingArea *model.StagingArea,
// considered as a valid candidate.
// This is why we sort the direct parent headers in a way that the first one will be
// in the future of the pruning point.
directParentHeaders := make([]externalapi.BlockHeader, len(directParentHashesCopy))
directParentHeaders := make([]externalapi.BlockHeader, len(directParentHashes))
firstParentInFutureOfPruningPointIndex := 0
foundFirstParentInFutureOfPruningPoint := false
for i, directParentHash := range directParentHashesCopy {
for i, directParentHash := range directParentHashes {
isInFutureOfPruningPoint, err := bpb.dagTopologyManager.IsAncestorOf(stagingArea, pruningPoint, directParentHash)
if err != nil {
return nil, err
Expand All @@ -86,16 +82,17 @@ func (bpb *blockParentBuilder) BuildParents(stagingArea *model.StagingArea,
return nil, errors.New("BuildParents should get at least one parent in the future of the pruning point")
}

oldFirstDirectParent := directParentHashesCopy[0]
directParentHashesCopy[0] = directParentHashesCopy[firstParentInFutureOfPruningPointIndex]
directParentHashesCopy[firstParentInFutureOfPruningPointIndex] = oldFirstDirectParent

for i, directParentHash := range directParentHashesCopy {
for i, directParentHash := range directParentHashes {
directParentHeader, err := bpb.blockHeaderStore.BlockHeader(bpb.databaseContext, stagingArea, directParentHash)
if err != nil {
return nil, err
}
directParentHeaders[i] = directParentHeader
if i > 0 && i == firstParentInFutureOfPruningPointIndex {
directParentHeaders[i] = directParentHeaders[0]
directParentHeaders[0] = directParentHeader
} else {
directParentHeaders[i] = directParentHeader
}
}

type blockToReferences map[externalapi.DomainHash][]*externalapi.DomainHash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,23 @@ func (v *blockValidator) ValidateHeaderInContext(stagingArea *model.StagingArea,
return err
}

ghostdagData, err := v.ghostdagDataStores[0].Get(v.databaseContext, stagingArea, blockHash, false)
if err != nil {
return err
}

if !hasValidatedHeader {
var logErr error
log.Debug(logger.NewLogClosure(func() string {
var ghostdagData *externalapi.BlockGHOSTDAGData
ghostdagData, logErr = v.ghostdagDataStores[0].Get(v.databaseContext, stagingArea, blockHash, false)
if err != nil {
return ""
}

return fmt.Sprintf("block %s blue score is %d", blockHash, ghostdagData.BlueScore())
}))

if logErr != nil {
return logErr
}
}

err = v.validateMedianTime(stagingArea, header)
if err != nil {
return err
}

err = v.checkMergeSizeLimit(stagingArea, blockHash)
err = v.checkMergeSizeLimit(stagingArea, ghostdagData)
if err != nil {
return err
}
Expand All @@ -67,14 +61,12 @@ func (v *blockValidator) ValidateHeaderInContext(stagingArea *model.StagingArea,
return err
}
}

if !isBlockWithTrustedData {
err = v.checkIndirectParents(stagingArea, header)
if err != nil {
return err
}
}

err = v.mergeDepthManager.CheckBoundedMergeDepth(stagingArea, blockHash, isBlockWithTrustedData)
if err != nil {
return err
Expand All @@ -85,12 +77,12 @@ func (v *blockValidator) ValidateHeaderInContext(stagingArea *model.StagingArea,
return err
}

err = v.checkBlueWork(stagingArea, blockHash, header)
err = v.checkBlueWork(stagingArea, ghostdagData, header)
if err != nil {
return err
}

err = v.checkHeaderBlueScore(stagingArea, blockHash, header)
err = v.checkHeaderBlueScore(stagingArea, ghostdagData, header)
if err != nil {
return err
}
Expand Down Expand Up @@ -174,12 +166,7 @@ func (v *blockValidator) validateMedianTime(stagingArea *model.StagingArea, head
return nil
}

func (v *blockValidator) checkMergeSizeLimit(stagingArea *model.StagingArea, hash *externalapi.DomainHash) error {
ghostdagData, err := v.ghostdagDataStores[0].Get(v.databaseContext, stagingArea, hash, false)
if err != nil {
return err
}

func (v *blockValidator) checkMergeSizeLimit(stagingArea *model.StagingArea, ghostdagData *externalapi.BlockGHOSTDAGData) error {
mergeSetSize := len(ghostdagData.MergeSetBlues()) + len(ghostdagData.MergeSetReds())

if uint64(mergeSetSize) > v.mergeSetSizeLimit {
Expand All @@ -191,7 +178,7 @@ func (v *blockValidator) checkMergeSizeLimit(stagingArea *model.StagingArea, has
}

func (v *blockValidator) checkIndirectParents(stagingArea *model.StagingArea, header externalapi.BlockHeader) error {
expectedParents, err := v.blockParentBuilder.BuildParents(stagingArea, header.DAAScore(), header.DirectParents())
expectedParents, err := v.blockParentBuilder.BuildParents(stagingArea, header.DirectParents())
if err != nil {
return err
}
Expand All @@ -216,30 +203,17 @@ func (v *blockValidator) checkDAAScore(stagingArea *model.StagingArea, blockHash
return nil
}

func (v *blockValidator) checkBlueWork(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash,
header externalapi.BlockHeader) error {

ghostdagData, err := v.ghostdagDataStores[0].Get(v.databaseContext, stagingArea, blockHash, false)
if err != nil {
return err
}
func (v *blockValidator) checkBlueWork(stagingArea *model.StagingArea, ghostdagData *externalapi.BlockGHOSTDAGData, header externalapi.BlockHeader) error {
expectedBlueWork := ghostdagData.BlueWork()
if header.BlueWork().Cmp(expectedBlueWork) != 0 {
return errors.Wrapf(ruleerrors.ErrUnexpectedBlueWork, "block blue work of %d is not the expected value of %d", header.BlueWork(), expectedBlueWork)
}
return nil
}

func (v *blockValidator) checkHeaderBlueScore(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash,
header externalapi.BlockHeader) error {

ghostdagData, err := v.ghostdagDataStores[0].Get(v.databaseContext, stagingArea, blockHash, false)
if err != nil {
return err
}
func (v *blockValidator) checkHeaderBlueScore(stagingArea *model.StagingArea, ghostdagData *externalapi.BlockGHOSTDAGData, header externalapi.BlockHeader) error {
if header.BlueScore() != ghostdagData.BlueScore() {
return errors.Wrapf(ruleerrors.ErrUnexpectedBlueWork, "block blue work of %d is not the expected "+
"value of %d", header.BlueWork(), ghostdagData.BlueScore())
return errors.Wrapf(ruleerrors.ErrUnexpectedBlueWork, "block blue score of %d is not the expected value of %d", header.BlueScore(), ghostdagData.BlueScore())
}
return nil
}