kaspad/domain/consensus/model/externalapi/blocklevelparents.go
Ori Newman afaac28da1
Validate each level parents (#1827)
* Create BlockParentBuilder.

* Implement BuildParents.

* Explictly set level 0 blocks to be the same as direct parents.

* Add checkIndirectParents to validateBlockHeaderInContext.

* Fix test_block_builder.go and BlockLevelParents::Equal.

* Don't check indirect parents for blocks with trusted data.

* Handle pruned blocks when building block level parents.

* Fix bad deletions from unprocessedXxxParents.

* Fix merge errors.

* Fix bad pruning point parent replaces.

* Fix duplicates in newBlockLevelParents.

* Skip checkIndirectParents

* Get rid of staging constant IDs

* Fix BuildParents

* Fix tests

* Add comments

* Change order of directParentHashes

* Get rid of maybeAddDirectParentParents

* Add comments

* Add blockToReferences type

* Use ParentsAtLevel

Co-authored-by: stasatdaglabs <stas@daglabs.com>
2021-09-13 14:22:00 +03:00

64 lines
1.6 KiB
Go

package externalapi
// BlockLevelParents represent the parents within a single super-block level
// See https://github.com/kaspanet/research/issues/3 for further details
type BlockLevelParents []*DomainHash
// Equal returns true if this BlockLevelParents is equal to `other`
func (sl BlockLevelParents) Equal(other BlockLevelParents) bool {
if len(sl) != len(other) {
return false
}
for _, thisHash := range sl {
found := false
for _, otherHash := range other {
if thisHash.Equal(otherHash) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// Clone creates a clone of this BlockLevelParents
func (sl BlockLevelParents) Clone() BlockLevelParents {
return CloneHashes(sl)
}
// Contains returns true if this BlockLevelParents contains the given blockHash
func (sl BlockLevelParents) Contains(blockHash *DomainHash) bool {
for _, blockLevelParent := range sl {
if blockLevelParent.Equal(blockHash) {
return true
}
}
return false
}
// ParentsEqual returns true if all the BlockLevelParents in `a` and `b` are
// equal pairwise
func ParentsEqual(a, b []BlockLevelParents) bool {
if len(a) != len(b) {
return false
}
for i, blockLevelParents := range a {
if !blockLevelParents.Equal(b[i]) {
return false
}
}
return true
}
// CloneParents creates a clone of the given BlockLevelParents slice
func CloneParents(parents []BlockLevelParents) []BlockLevelParents {
clone := make([]BlockLevelParents, len(parents))
for i, blockLevelParents := range parents {
clone[i] = blockLevelParents.Clone()
}
return clone
}