Svarog 171deded4e
Implement GetBlocks RPC command (#1514)
* Remove BlockHexes from GetBlocks request and response

* Add GetBlocks RPC

* Include the selectedTip's anticone in GetBlocks

* Add Anticone to fakeRelayInvsContext

* Include verbose data only if it was requested + Add comments to HandleGetBlocks

* Allow antiPastHashesBetween to receive unrelated low and high hashes

* Convert to/from protowire GetBlocksResponse with no verbose data correctly

* Removed NextLowHash

* Update GetBlocks in rpc_client

* Validate in consensus.Anticone that blockHash exists

Co-authored-by: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com>
2021-02-10 18:27:04 +02:00

56 lines
1.2 KiB
Go

package dagtraversalmanager
import (
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/hashset"
)
func (dtm *dagTraversalManager) Anticone(blockHash *externalapi.DomainHash) ([]*externalapi.DomainHash, error) {
anticone := []*externalapi.DomainHash{}
queue, err := dtm.consensusStateStore.Tips(dtm.databaseContext)
if err != nil {
return nil, err
}
visited := hashset.New()
for len(queue) > 0 {
var current *externalapi.DomainHash
current, queue = queue[0], queue[1:]
if visited.Contains(current) {
continue
}
visited.Add(current)
currentIsAncestorOfBlock, err := dtm.dagTopologyManager.IsAncestorOf(current, blockHash)
if err != nil {
return nil, err
}
if currentIsAncestorOfBlock {
continue
}
blockIsAncestorOfCurrent, err := dtm.dagTopologyManager.IsAncestorOf(blockHash, current)
if err != nil {
return nil, err
}
if !blockIsAncestorOfCurrent {
anticone = append(anticone, current)
}
currentParents, err := dtm.dagTopologyManager.Parents(current)
if err != nil {
return nil, err
}
for _, parent := range currentParents {
queue = append(queue, parent)
}
}
return anticone, nil
}