[NOD-669] Rename start/endHash -> low/highHash (#591)

* [NOD-669] Remove the "get" from getBlueBlocksBetween.

* [NOD-669] Remove the "Get" from GetBlueBlocksHeadersBetween.

* [NOD-669] In blueBlocksBetween, rename startHash to lowHash and stopHash to highHash.

* [NOD-669] Rename startHash to lowHash and stopHash to highHash in blockLocator logic.

* [NOD-669] Remove zeroHash logic in blockLocator.

* [NOD-669] Finish renaming startHash and stopHash in blockdag.

* [NOD-669] Rename startHash and stopHash in blockdag where I previously missed it.

* [NOD-669] Rename startHash and stopHash in blockdag where I previously missed it some more.

* [NOD-669] Rename startHash and stopHash in blockdag where I previously missed it some more some more.

* [NOD-669] Fix bad grammar in method names.

* [NOD-669] Rename lowHash to blockHash in SelectedParentChain.

* [NOD-669] Fix a comment.
This commit is contained in:
stasatdaglabs 2020-01-20 12:47:16 +02:00 committed by Svarog
parent 38b4749f20
commit 49418f4222
22 changed files with 227 additions and 227 deletions

View File

@ -21,66 +21,61 @@ import (
// [17 16 14 11 7 2 genesis]
type BlockLocator []*daghash.Hash
// BlockLocatorFromHashes returns a block locator from start and stop hash.
// BlockLocatorFromHashes returns a block locator from high and low hash.
// See BlockLocator for details on the algorithm used to create a block locator.
//
// In addition to the general algorithm referenced above, this function will
// return the block locator for the selected tip if the passed hash is not currently
// known.
//
// This function is safe for concurrent access.
func (dag *BlockDAG) BlockLocatorFromHashes(startHash, stopHash *daghash.Hash) (BlockLocator, error) {
func (dag *BlockDAG) BlockLocatorFromHashes(highHash, lowHash *daghash.Hash) (BlockLocator, error) {
dag.dagLock.RLock()
defer dag.dagLock.RUnlock()
startNode := dag.index.LookupNode(startHash)
var stopNode *blockNode
if !stopHash.IsEqual(&daghash.ZeroHash) {
stopNode = dag.index.LookupNode(stopHash)
}
return dag.blockLocator(startNode, stopNode)
highNode := dag.index.LookupNode(highHash)
lowNode := dag.index.LookupNode(lowHash)
return dag.blockLocator(highNode, lowNode)
}
// blockLocator returns a block locator for the passed start and stop nodes.
// The default value for the start node is the selected tip, and the default
// values of the stop node is the genesis block.
// blockLocator returns a block locator for the passed high and low nodes.
// The default value for the high node is the selected tip, and the default
// values of the low node is the genesis block.
//
// See the BlockLocator type comments for more details.
//
// This function MUST be called with the DAG state lock held (for reads).
func (dag *BlockDAG) blockLocator(startNode, stopNode *blockNode) (BlockLocator, error) {
func (dag *BlockDAG) blockLocator(highNode, lowNode *blockNode) (BlockLocator, error) {
// Use the selected tip if requested.
if startNode == nil {
startNode = dag.virtual.selectedParent
if highNode == nil {
highNode = dag.virtual.selectedParent
}
if stopNode == nil {
stopNode = dag.genesis
if lowNode == nil {
lowNode = dag.genesis
}
// We use the selected parent of the start node, so the
// block locator won't contain the start node.
startNode = startNode.selectedParent
// We use the selected parent of the high node, so the
// block locator won't contain the high node.
highNode = highNode.selectedParent
node := startNode
node := highNode
step := uint64(1)
locator := make(BlockLocator, 0)
for node != nil {
locator = append(locator, node.hash)
// Nothing more to add once the stop node has been added.
if node.blueScore <= stopNode.blueScore {
if node != stopNode {
return nil, errors.Errorf("startNode and stopNode are " +
// Nothing more to add once the low node has been added.
if node.blueScore <= lowNode.blueScore {
if node != lowNode {
return nil, errors.Errorf("highNode and lowNode are " +
"not in the same selected parent chain.")
}
break
}
// Calculate blueScore of previous node to include ensuring the
// final node is stopNode.
// final node is lowNode.
nextBlueScore := node.blueScore - step
if nextBlueScore < stopNode.blueScore {
nextBlueScore = stopNode.blueScore
if nextBlueScore < lowNode.blueScore {
nextBlueScore = lowNode.blueScore
}
// walk backwards through the nodes to the correct ancestor.
@ -99,21 +94,21 @@ func (dag *BlockDAG) blockLocator(startNode, stopNode *blockNode) (BlockLocator,
// sync peer.
//
// This function MUST be called with the DAG state lock held (for reads).
func (dag *BlockDAG) FindNextLocatorBoundaries(locator BlockLocator) (startHash, stopHash *daghash.Hash) {
func (dag *BlockDAG) FindNextLocatorBoundaries(locator BlockLocator) (highHash, lowHash *daghash.Hash) {
// Find the most recent locator block hash in the DAG. In the case none of
// the hashes in the locator are in the DAG, fall back to the genesis block.
stopNode := dag.genesis
lowNode := dag.genesis
nextBlockLocatorIndex := int64(len(locator) - 1)
for i, hash := range locator {
node := dag.index.LookupNode(hash)
if node != nil {
stopNode = node
lowNode = node
nextBlockLocatorIndex = int64(i) - 1
break
}
}
if nextBlockLocatorIndex < 0 {
return nil, stopNode.hash
return nil, lowNode.hash
}
return locator[nextBlockLocatorIndex], stopNode.hash
return locator[nextBlockLocatorIndex], lowNode.hash
}

View File

@ -1472,44 +1472,44 @@ func (dag *BlockDAG) IsInSelectedParentChain(blockHash *daghash.Hash) bool {
return dag.virtual.selectedParentChainSet.containsHash(blockHash)
}
// SelectedParentChain returns the selected parent chain starting from startHash (exclusive)
// up to the virtual (exclusive). If startHash is nil then the genesis block is used. If
// startHash is not within the select parent chain, go down its own selected parent chain,
// SelectedParentChain returns the selected parent chain starting from blockHash (exclusive)
// up to the virtual (exclusive). If blockHash is nil then the genesis block is used. If
// blockHash is not within the select parent chain, go down its own selected parent chain,
// while collecting each block hash in removedChainHashes, until reaching a block within
// the main selected parent chain.
//
// This method MUST be called with the DAG lock held
func (dag *BlockDAG) SelectedParentChain(startHash *daghash.Hash) ([]*daghash.Hash, []*daghash.Hash, error) {
if startHash == nil {
startHash = dag.genesis.hash
func (dag *BlockDAG) SelectedParentChain(blockHash *daghash.Hash) ([]*daghash.Hash, []*daghash.Hash, error) {
if blockHash == nil {
blockHash = dag.genesis.hash
}
if !dag.BlockExists(startHash) {
return nil, nil, errors.Errorf("startHash %s does not exist in the DAG", startHash)
if !dag.BlockExists(blockHash) {
return nil, nil, errors.Errorf("blockHash %s does not exist in the DAG", blockHash)
}
// If startHash is not in the selected parent chain, go down its selected parent chain
// If blockHash is not in the selected parent chain, go down its selected parent chain
// until we find a block that is in the main selected parent chain.
var removedChainHashes []*daghash.Hash
for !dag.IsInSelectedParentChain(startHash) {
removedChainHashes = append(removedChainHashes, startHash)
for !dag.IsInSelectedParentChain(blockHash) {
removedChainHashes = append(removedChainHashes, blockHash)
node := dag.index.LookupNode(startHash)
startHash = node.selectedParent.hash
node := dag.index.LookupNode(blockHash)
blockHash = node.selectedParent.hash
}
// Find the index of the startHash in the selectedParentChainSlice
startHashIndex := len(dag.virtual.selectedParentChainSlice) - 1
for startHashIndex >= 0 {
node := dag.virtual.selectedParentChainSlice[startHashIndex]
if node.hash.IsEqual(startHash) {
// Find the index of the blockHash in the selectedParentChainSlice
blockHashIndex := len(dag.virtual.selectedParentChainSlice) - 1
for blockHashIndex >= 0 {
node := dag.virtual.selectedParentChainSlice[blockHashIndex]
if node.hash.IsEqual(blockHash) {
break
}
startHashIndex--
blockHashIndex--
}
// Copy all the addedChainHashes starting from startHashIndex (exclusive)
addedChainHashes := make([]*daghash.Hash, len(dag.virtual.selectedParentChainSlice)-startHashIndex-1)
for i, node := range dag.virtual.selectedParentChainSlice[startHashIndex+1:] {
// Copy all the addedChainHashes starting from blockHashIndex (exclusive)
addedChainHashes := make([]*daghash.Hash, len(dag.virtual.selectedParentChainSlice)-blockHashIndex-1)
for i, node := range dag.virtual.selectedParentChainSlice[blockHashIndex+1:] {
addedChainHashes[i] = node.hash
}
@ -1593,13 +1593,13 @@ func (dag *BlockDAG) SelectedParentHash(blockHash *daghash.Hash) (*daghash.Hash,
return node.selectedParent.hash, nil
}
// getBlueBlocksHashesBetween returns the hashes of the blocks after the provided
// start hash until the provided stop hash is reached, or up to the
// blueBlockHashesBetween returns the hashes of the blocks after the provided
// low hash until the provided high hash is reached, or up to the
// provided max number of block hashes.
//
// This function MUST be called with the DAG state lock held (for reads).
func (dag *BlockDAG) getBlueBlocksHashesBetween(startHash, stopHash *daghash.Hash, maxHashes uint64) ([]*daghash.Hash, error) {
nodes, err := dag.getBlueBlocksBetween(startHash, stopHash, maxHashes)
func (dag *BlockDAG) blueBlockHashesBetween(lowHash, highHash *daghash.Hash, maxHashes uint64) ([]*daghash.Hash, error) {
nodes, err := dag.blueBlocksBetween(lowHash, highHash, maxHashes)
if err != nil {
return nil, err
}
@ -1610,44 +1610,44 @@ func (dag *BlockDAG) getBlueBlocksHashesBetween(startHash, stopHash *daghash.Has
return hashes, nil
}
func (dag *BlockDAG) getBlueBlocksBetween(startHash, stopHash *daghash.Hash, maxEntries uint64) ([]*blockNode, error) {
startNode := dag.index.LookupNode(startHash)
if startNode == nil {
return nil, errors.Errorf("Couldn't find start hash %s", startHash)
func (dag *BlockDAG) blueBlocksBetween(lowHash, highHash *daghash.Hash, maxEntries uint64) ([]*blockNode, error) {
lowNode := dag.index.LookupNode(lowHash)
if lowNode == nil {
return nil, errors.Errorf("Couldn't find low hash %s", lowHash)
}
stopNode := dag.index.LookupNode(stopHash)
if stopNode == nil {
return nil, errors.Errorf("Couldn't find stop hash %s", stopHash)
highNode := dag.index.LookupNode(highHash)
if highNode == nil {
return nil, errors.Errorf("Couldn't find high hash %s", highHash)
}
// In order to get no more then maxEntries of blue blocks from
// the future of the start node (including itself), we iterate
// the selected parent chain of the stopNode and add the blues
// each node (including the stopNode itself). This is why the
// the future of the lowNode (including itself), we iterate
// the selected parent chain of the highNode and add the blues
// each node (including the highNode itself). This is why the
// number of returned blocks will be
// stopNode.blueScore-startNode.blueScore+1.
// If stopNode.blueScore-startNode.blueScore+1 > maxEntries, we
// first iterate on the selected parent chain of the stop node
// until we find a new stop node
// where stopNode.blueScore-startNode.blueScore+1 <= maxEntries
// highNode.blueScore-lowNode.blueScore+1.
// If highNode.blueScore-lowNode.blueScore+1 > maxEntries, we
// first iterate on the selected parent chain of the highNode
// until we find a new highNode
// where highNode.blueScore-lowNode.blueScore+1 <= maxEntries
for stopNode.blueScore-startNode.blueScore+1 > maxEntries {
stopNode = stopNode.selectedParent
for highNode.blueScore-lowNode.blueScore+1 > maxEntries {
highNode = highNode.selectedParent
}
// Populate and return the found nodes.
nodes := make([]*blockNode, 0, stopNode.blueScore-startNode.blueScore+1)
nodes = append(nodes, stopNode)
current := stopNode
for current.blueScore > startNode.blueScore {
nodes := make([]*blockNode, 0, highNode.blueScore-lowNode.blueScore+1)
nodes = append(nodes, highNode)
current := highNode
for current.blueScore > lowNode.blueScore {
for _, blue := range current.blues {
nodes = append(nodes, blue)
}
current = current.selectedParent
}
if current != startNode {
return nil, errors.Errorf("the start hash is not found in the " +
"selected parent chain of the stop hash")
if current != lowNode {
return nil, errors.Errorf("the low hash is not found in the " +
"selected parent chain of the high hash")
}
reversedNodes := make([]*blockNode, len(nodes))
for i, node := range nodes {
@ -1656,14 +1656,14 @@ func (dag *BlockDAG) getBlueBlocksBetween(startHash, stopHash *daghash.Hash, max
return reversedNodes, nil
}
// GetBlueBlocksHashesBetween returns the hashes of the blue blocks after the
// provided start hash until the provided stop hash is reached, or up to the
// BlueBlockHashesBetween returns the hashes of the blue blocks after the
// provided low hash until the provided high hash is reached, or up to the
// provided max number of block hashes.
//
// This function is safe for concurrent access.
func (dag *BlockDAG) GetBlueBlocksHashesBetween(startHash, stopHash *daghash.Hash, maxHashes uint64) ([]*daghash.Hash, error) {
func (dag *BlockDAG) BlueBlockHashesBetween(lowHash, highHash *daghash.Hash, maxHashes uint64) ([]*daghash.Hash, error) {
dag.dagLock.RLock()
hashes, err := dag.getBlueBlocksHashesBetween(startHash, stopHash, maxHashes)
hashes, err := dag.blueBlockHashesBetween(lowHash, highHash, maxHashes)
if err != nil {
return nil, err
}
@ -1671,13 +1671,13 @@ func (dag *BlockDAG) GetBlueBlocksHashesBetween(startHash, stopHash *daghash.Has
return hashes, nil
}
// getBlueBlocksHeadersBetween returns the headers of the blue blocks after the
// provided start hash until the provided stop hash is reached, or up to the
// blueBlockHeadersBetween returns the headers of the blue blocks after the
// provided low hash until the provided high hash is reached, or up to the
// provided max number of block headers.
//
// This function MUST be called with the DAG state lock held (for reads).
func (dag *BlockDAG) getBlueBlocksHeadersBetween(startHash, stopHash *daghash.Hash, maxHeaders uint64) ([]*wire.BlockHeader, error) {
nodes, err := dag.getBlueBlocksBetween(startHash, stopHash, maxHeaders)
func (dag *BlockDAG) blueBlockHeadersBetween(lowHash, highHash *daghash.Hash, maxHeaders uint64) ([]*wire.BlockHeader, error) {
nodes, err := dag.blueBlocksBetween(lowHash, highHash, maxHeaders)
if err != nil {
return nil, err
}
@ -1689,17 +1689,17 @@ func (dag *BlockDAG) getBlueBlocksHeadersBetween(startHash, stopHash *daghash.Ha
}
// GetTopHeaders returns the top wire.MaxBlockHeadersPerMsg block headers ordered by height.
func (dag *BlockDAG) GetTopHeaders(startHash *daghash.Hash) ([]*wire.BlockHeader, error) {
startNode := &dag.virtual.blockNode
if startHash != nil {
startNode = dag.index.LookupNode(startHash)
if startNode == nil {
return nil, errors.Errorf("Couldn't find the start hash %s in the dag", startHash)
func (dag *BlockDAG) GetTopHeaders(highHash *daghash.Hash) ([]*wire.BlockHeader, error) {
highNode := &dag.virtual.blockNode
if highHash != nil {
highNode = dag.index.LookupNode(highHash)
if highNode == nil {
return nil, errors.Errorf("Couldn't find the high hash %s in the dag", highHash)
}
}
headers := make([]*wire.BlockHeader, 0, startNode.blueScore)
headers := make([]*wire.BlockHeader, 0, highNode.blueScore)
queue := newDownHeap()
queue.pushSet(startNode.parents)
queue.pushSet(highNode.parents)
visited := newSet()
for i := uint32(0); queue.Len() > 0 && len(headers) < wire.MaxBlockHeadersPerMsg; i++ {
@ -1734,14 +1734,14 @@ func (dag *BlockDAG) RUnlock() {
dag.dagLock.RUnlock()
}
// GetBlueBlocksHeadersBetween returns the headers of the blocks after the provided
// start hash until the provided stop hash is reached, or up to the
// BlueBlockHeadersBetween returns the headers of the blocks after the provided
// low hash until the provided high hash is reached, or up to the
// provided max number of block headers.
//
// This function is safe for concurrent access.
func (dag *BlockDAG) GetBlueBlocksHeadersBetween(startHash, stopHash *daghash.Hash) ([]*wire.BlockHeader, error) {
func (dag *BlockDAG) BlueBlockHeadersBetween(lowHash, highHash *daghash.Hash) ([]*wire.BlockHeader, error) {
dag.dagLock.RLock()
headers, err := dag.getBlueBlocksHeadersBetween(startHash, stopHash, wire.MaxBlockHeadersPerMsg)
headers, err := dag.blueBlockHeadersBetween(lowHash, highHash, wire.MaxBlockHeadersPerMsg)
if err != nil {
return nil, err
}

View File

@ -854,33 +854,33 @@ func (dag *BlockDAG) BlockByHash(hash *daghash.Hash) (*util.Block, error) {
return block, err
}
// BlockHashesFrom returns a slice of blocks starting from startHash
// ordered by blueScore. If startHash is nil then the genesis block is used.
// BlockHashesFrom returns a slice of blocks starting from lowHash
// ordered by blueScore. If lowHash is nil then the genesis block is used.
//
// This method MUST be called with the DAG lock held
func (dag *BlockDAG) BlockHashesFrom(startHash *daghash.Hash, limit int) ([]*daghash.Hash, error) {
func (dag *BlockDAG) BlockHashesFrom(lowHash *daghash.Hash, limit int) ([]*daghash.Hash, error) {
blockHashes := make([]*daghash.Hash, 0, limit)
if startHash == nil {
startHash = dag.genesis.hash
if lowHash == nil {
lowHash = dag.genesis.hash
// If we're starting from the beginning we should include the
// genesis hash in the result
blockHashes = append(blockHashes, dag.genesis.hash)
}
if !dag.BlockExists(startHash) {
return nil, errors.Errorf("block %s not found", startHash)
if !dag.BlockExists(lowHash) {
return nil, errors.Errorf("block %s not found", lowHash)
}
blueScore, err := dag.BlueScoreByBlockHash(startHash)
blueScore, err := dag.BlueScoreByBlockHash(lowHash)
if err != nil {
return nil, err
}
err = dag.index.db.View(func(dbTx database.Tx) error {
blockIndexBucket := dbTx.Metadata().Bucket(blockIndexBucketName)
startKey := BlockIndexKey(startHash, blueScore)
lowKey := BlockIndexKey(lowHash, blueScore)
cursor := blockIndexBucket.Cursor()
cursor.Seek(startKey)
cursor.Seek(lowKey)
for ok := cursor.Next(); ok; ok = cursor.Next() {
key := cursor.Key()
blockHash, err := blockHashFromBlockIndexKey(key)

View File

@ -686,7 +686,7 @@ func (sm *SyncManager) handleInvMsg(imsg *invMsg) {
// Request blocks after the first block's ancestor that exists
// in the selected path chain, one up to the
// final one the remote peer knows about.
peer.PushGetBlockLocatorMsg(iv.Hash, &daghash.ZeroHash)
peer.PushGetBlockLocatorMsg(iv.Hash, sm.dagParams.GenesisHash)
}
}
}

View File

@ -139,16 +139,16 @@ func messageSummary(msg wire.Message) string {
return invSummary(msg.InvList)
case *wire.MsgGetBlockInvs:
return fmt.Sprintf("start hash %s, stop hash %s", msg.StartHash,
msg.StopHash)
return fmt.Sprintf("low hash %s, high hash %s", msg.LowHash,
msg.HighHash)
case *wire.MsgGetHeaders:
return fmt.Sprintf("start hash %s, stop hash %s", msg.StartHash,
msg.StopHash)
case *wire.MsgGetBlockLocator:
return fmt.Sprintf("start hash %s, stop hash %s", msg.StartHash,
msg.StopHash)
return fmt.Sprintf("high hash %s, low hash %s", msg.HighHash,
msg.LowHash)
case *wire.MsgBlockLocator:
if len(msg.BlockLocatorHashes) > 0 {

View File

@ -413,13 +413,13 @@ type Peer struct {
sendHeadersPreferred bool // peer sent a sendheaders message
verAckReceived bool
knownInventory *mruInventoryMap
prevGetBlockInvsMtx sync.Mutex
prevGetBlockInvsStart *daghash.Hash
prevGetBlockInvsStop *daghash.Hash
prevGetHdrsMtx sync.Mutex
prevGetHdrsStart *daghash.Hash
prevGetHdrsStop *daghash.Hash
knownInventory *mruInventoryMap
prevGetBlockInvsMtx sync.Mutex
prevGetBlockInvsLow *daghash.Hash
prevGetBlockInvsHigh *daghash.Hash
prevGetHdrsMtx sync.Mutex
prevGetHdrsStart *daghash.Hash
prevGetHdrsStop *daghash.Hash
// These fields keep track of statistics for the peer and are protected
// by the statsMtx mutex.
@ -808,42 +808,42 @@ func (p *Peer) PushAddrMsg(addresses []*wire.NetAddress, subnetworkID *subnetwor
return msg.AddrList, nil
}
// PushGetBlockLocatorMsg sends a getlocator message for the provided start
// and stop hash.
// PushGetBlockLocatorMsg sends a getlocator message for the provided high
// and low hash.
//
// This function is safe for concurrent access.
func (p *Peer) PushGetBlockLocatorMsg(startHash, stopHash *daghash.Hash) {
msg := wire.NewMsgGetBlockLocator(startHash, stopHash)
func (p *Peer) PushGetBlockLocatorMsg(highHash, lowHash *daghash.Hash) {
msg := wire.NewMsgGetBlockLocator(highHash, lowHash)
p.QueueMessage(msg, nil)
}
// PushGetBlockInvsMsg sends a getblockinvs message for the provided block locator
// and stop hash. It will ignore back-to-back duplicate requests.
// and high hash. It will ignore back-to-back duplicate requests.
//
// This function is safe for concurrent access.
func (p *Peer) PushGetBlockInvsMsg(startHash, stopHash *daghash.Hash) error {
func (p *Peer) PushGetBlockInvsMsg(lowHash, highHash *daghash.Hash) error {
// Filter duplicate getblockinvs requests.
p.prevGetBlockInvsMtx.Lock()
isDuplicate := p.prevGetBlockInvsStop != nil && p.prevGetBlockInvsStart != nil &&
startHash != nil && stopHash.IsEqual(p.prevGetBlockInvsStop) &&
startHash.IsEqual(p.prevGetBlockInvsStart)
isDuplicate := p.prevGetBlockInvsHigh != nil && p.prevGetBlockInvsLow != nil &&
lowHash != nil && highHash.IsEqual(p.prevGetBlockInvsHigh) &&
lowHash.IsEqual(p.prevGetBlockInvsLow)
p.prevGetBlockInvsMtx.Unlock()
if isDuplicate {
log.Tracef("Filtering duplicate [getblockinvs] with start "+
"hash %s, stop hash %s", startHash, stopHash)
log.Tracef("Filtering duplicate [getblockinvs] with low "+
"hash %s, high hash %s", lowHash, highHash)
return nil
}
// Construct the getblockinvs request and queue it to be sent.
msg := wire.NewMsgGetBlockInvs(startHash, stopHash)
msg := wire.NewMsgGetBlockInvs(lowHash, highHash)
p.QueueMessage(msg, nil)
// Update the previous getblockinvs request information for filtering
// duplicates.
p.prevGetBlockInvsMtx.Lock()
p.prevGetBlockInvsStart = startHash
p.prevGetBlockInvsStop = stopHash
p.prevGetBlockInvsLow = lowHash
p.prevGetBlockInvsHigh = highHash
p.prevGetBlockInvsMtx.Unlock()
return nil
}

View File

@ -464,18 +464,18 @@ func (r FutureGetHeadersResult) Receive() ([]wire.BlockHeader, error) {
// of the RPC at some future time by invoking the Receive function on the returned instance.
//
// See GetTopHeaders for the blocking version and more details.
func (c *Client) GetTopHeadersAsync(startHash *daghash.Hash) FutureGetHeadersResult {
func (c *Client) GetTopHeadersAsync(highHash *daghash.Hash) FutureGetHeadersResult {
var hash *string
if startHash != nil {
hash = rpcmodel.String(startHash.String())
if highHash != nil {
hash = rpcmodel.String(highHash.String())
}
cmd := rpcmodel.NewGetTopHeadersCmd(hash)
return c.sendCmd(cmd)
}
// GetTopHeaders sends a getTopHeaders rpc command to the server.
func (c *Client) GetTopHeaders(startHash *daghash.Hash) ([]wire.BlockHeader, error) {
return c.GetTopHeadersAsync(startHash).Receive()
func (c *Client) GetTopHeaders(highHash *daghash.Hash) ([]wire.BlockHeader, error) {
return c.GetTopHeadersAsync(highHash).Receive()
}
// GetHeadersAsync returns an instance of a type that can be used to get the result

View File

@ -667,29 +667,29 @@ func NewGetCurrentNetCmd() *GetCurrentNetCmd {
// GetTopHeadersCmd defined the getTopHeaders JSON-RPC command.
type GetTopHeadersCmd struct {
StartHash *string `json:"startHash"`
HighHash *string `json:"highHash"`
}
// NewGetTopHeadersCmd returns a new instance which can be used to issue a
// getTopHeaders JSON-RPC command.
func NewGetTopHeadersCmd(startHash *string) *GetTopHeadersCmd {
func NewGetTopHeadersCmd(highHash *string) *GetTopHeadersCmd {
return &GetTopHeadersCmd{
StartHash: startHash,
HighHash: highHash,
}
}
// GetHeadersCmd defines the getHeaders JSON-RPC command.
type GetHeadersCmd struct {
StartHash string `json:"startHash"`
StopHash string `json:"stopHash"`
LowHash string `json:"lowHash"`
HighHash string `json:"highHash"`
}
// NewGetHeadersCmd returns a new instance which can be used to issue a
// getHeaders JSON-RPC command.
func NewGetHeadersCmd(startHash, stopHash string) *GetHeadersCmd {
func NewGetHeadersCmd(lowHash, highHash string) *GetHeadersCmd {
return &GetHeadersCmd{
StartHash: startHash,
StopHash: stopHash,
LowHash: lowHash,
HighHash: highHash,
}
}

View File

@ -952,8 +952,8 @@ func TestRPCServerCommands(t *testing.T) {
},
marshalled: `{"jsonrpc":"1.0","method":"getHeaders","params":["",""],"id":1}`,
unmarshalled: &rpcmodel.GetHeadersCmd{
StartHash: "",
StopHash: "",
LowHash: "",
HighHash: "",
},
},
{
@ -969,8 +969,8 @@ func TestRPCServerCommands(t *testing.T) {
},
marshalled: `{"jsonrpc":"1.0","method":"getHeaders","params":["000000000000000001f1739002418e2f9a84c47a4fd2a0eb7a787a6b7dc12f16","000000000000000000ba33b33e1fad70b69e234fc24414dd47113bff38f523f7"],"id":1}`,
unmarshalled: &rpcmodel.GetHeadersCmd{
StartHash: "000000000000000001f1739002418e2f9a84c47a4fd2a0eb7a787a6b7dc12f16",
StopHash: "000000000000000000ba33b33e1fad70b69e234fc24414dd47113bff38f523f7",
LowHash: "000000000000000001f1739002418e2f9a84c47a4fd2a0eb7a787a6b7dc12f16",
HighHash: "000000000000000000ba33b33e1fad70b69e234fc24414dd47113bff38f523f7",
},
},
{
@ -998,7 +998,7 @@ func TestRPCServerCommands(t *testing.T) {
},
marshalled: `{"jsonrpc":"1.0","method":"getTopHeaders","params":["000000000000000000ba33b33e1fad70b69e234fc24414dd47113bff38f523f7"],"id":1}`,
unmarshalled: &rpcmodel.GetTopHeadersCmd{
StartHash: rpcmodel.String("000000000000000000ba33b33e1fad70b69e234fc24414dd47113bff38f523f7"),
HighHash: rpcmodel.String("000000000000000000ba33b33e1fad70b69e234fc24414dd47113bff38f523f7"),
},
},
{

View File

@ -20,15 +20,20 @@ func (sp *Peer) OnBlockLocator(_ *peer.Peer, msg *wire.MsgBlockLocator) {
}
// If the first hash of the block locator is known, it means we found
// the highest shared block.
firstHash := msg.BlockLocatorHashes[0]
if dag.BlockExists(firstHash) {
if dag.IsKnownFinalizedBlock(firstHash) {
highHash := msg.BlockLocatorHashes[0]
if dag.BlockExists(highHash) {
if dag.IsKnownFinalizedBlock(highHash) {
peerLog.Debugf("Cannot sync with peer %s because the highest"+
" shared chain block (%s) is below the finality point", sp, firstHash)
" shared chain block (%s) is below the finality point", sp, highHash)
sp.server.SyncManager.RemoveFromSyncCandidates(sp.Peer)
return
}
err := sp.Peer.PushGetBlockInvsMsg(firstHash, sp.Peer.SelectedTip())
// We send the highHash as the GetBlockInvsMsg's lowHash here.
// This is not a mistake. The invs we desire start from the highest
// hash that we know of and end at the highest hash that the peer
// knows of.
err := sp.Peer.PushGetBlockInvsMsg(highHash, sp.Peer.SelectedTip())
if err != nil {
peerLog.Errorf("Failed pushing get blocks message for peer %s: %s",
sp, err)
@ -36,9 +41,9 @@ func (sp *Peer) OnBlockLocator(_ *peer.Peer, msg *wire.MsgBlockLocator) {
}
return
}
startHash, stopHash := dag.FindNextLocatorBoundaries(msg.BlockLocatorHashes)
if startHash == nil {
highHash, lowHash := dag.FindNextLocatorBoundaries(msg.BlockLocatorHashes)
if highHash == nil {
panic("Couldn't find any unknown hashes in the block locator.")
}
sp.PushGetBlockLocatorMsg(startHash, stopHash)
sp.PushGetBlockLocatorMsg(highHash, lowHash)
}

View File

@ -7,23 +7,23 @@ import (
// OnGetBlockInvs is invoked when a peer receives a getblockinvs kaspa
// message.
// It finds the blue future between msg.StartHash and msg.StopHash
// It finds the blue future between msg.LowHash and msg.HighHash
// and send the invs to the requesting peer.
func (sp *Peer) OnGetBlockInvs(_ *peer.Peer, msg *wire.MsgGetBlockInvs) {
dag := sp.server.DAG
// We want to prevent a situation where the syncing peer needs
// to call getblocks once again, but the block we sent him
// won't affect his selected chain, so next time it'll try
// to call getblocks once again, but the block we sent it
// won't affect its selected chain, so next time it'll try
// to find the highest shared chain block, it'll find the
// same one as before.
// To prevent that we use blockdag.FinalityInterval as maxHashes.
// This way, if one getblocks is not enough to get the peer
// synced, we can know for sure that its selected chain will
// change, so we'll have higher shared chain block.
hashList, err := dag.GetBlueBlocksHashesBetween(msg.StartHash, msg.StopHash,
hashList, err := dag.BlueBlockHashesBetween(msg.LowHash, msg.HighHash,
wire.MaxInvPerMsg)
if err != nil {
peerLog.Warnf("Error getting blue blocks between %s and %s: %s", msg.StartHash, msg.StopHash, err)
peerLog.Warnf("Error getting blue blocks between %s and %s: %s", msg.LowHash, msg.HighHash, err)
sp.Disconnect()
return
}

View File

@ -9,10 +9,10 @@ import (
// OnGetBlockLocator is invoked when a peer receives a getlocator kaspa
// message.
func (sp *Peer) OnGetBlockLocator(_ *peer.Peer, msg *wire.MsgGetBlockLocator) {
locator, err := sp.server.DAG.BlockLocatorFromHashes(msg.StartHash, msg.StopHash)
locator, err := sp.server.DAG.BlockLocatorFromHashes(msg.HighHash, msg.LowHash)
if err != nil || len(locator) == 0 {
warning := fmt.Sprintf("Couldn't build a block locator between blocks "+
"%s and %s that was requested from peer %s", msg.StartHash, msg.StopHash, sp)
"%s and %s that was requested from peer %s", msg.HighHash, msg.LowHash, sp)
if err != nil {
warning = fmt.Sprintf("%s: %s", warning, err)
}

View File

@ -11,21 +11,21 @@ import (
func handleGetHeaders(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*rpcmodel.GetHeadersCmd)
startHash := &daghash.ZeroHash
if c.StartHash != "" {
err := daghash.Decode(startHash, c.StartHash)
lowHash := &daghash.ZeroHash
if c.LowHash != "" {
err := daghash.Decode(lowHash, c.LowHash)
if err != nil {
return nil, rpcDecodeHexError(c.StopHash)
return nil, rpcDecodeHexError(c.HighHash)
}
}
stopHash := &daghash.ZeroHash
if c.StopHash != "" {
err := daghash.Decode(stopHash, c.StopHash)
highHash := &daghash.ZeroHash
if c.HighHash != "" {
err := daghash.Decode(highHash, c.HighHash)
if err != nil {
return nil, rpcDecodeHexError(c.StopHash)
return nil, rpcDecodeHexError(c.HighHash)
}
}
headers, err := s.cfg.SyncMgr.GetBlueBlocksHeadersBetween(startHash, stopHash)
headers, err := s.cfg.SyncMgr.BlueBlockHeadersBetween(lowHash, highHash)
if err != nil {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCMisc,

View File

@ -11,15 +11,15 @@ import (
func handleGetTopHeaders(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*rpcmodel.GetTopHeadersCmd)
var startHash *daghash.Hash
if c.StartHash != nil {
startHash = &daghash.Hash{}
err := daghash.Decode(startHash, *c.StartHash)
var highHash *daghash.Hash
if c.HighHash != nil {
highHash = &daghash.Hash{}
err := daghash.Decode(highHash, *c.HighHash)
if err != nil {
return nil, rpcDecodeHexError(*c.StartHash)
return nil, rpcDecodeHexError(*c.HighHash)
}
}
headers, err := s.cfg.DAG.GetTopHeaders(startHash)
headers, err := s.cfg.DAG.GetTopHeaders(highHash)
if err != nil {
return nil, internalRPCError(err.Error(),
"Failed to get top headers")

View File

@ -269,12 +269,12 @@ func (b *rpcSyncMgr) SyncPeerID() int32 {
return b.syncMgr.SyncPeerID()
}
// GetBlueBlocksHeadersBetween returns the headers of the blocks after the provided
// start hash until the provided stop hash is reached, or up to the
// BlueBlockHeadersBetween returns the headers of the blocks after the provided
// low hash until the provided high hash is reached, or up to the
// provided max number of block headers.
//
// This function is safe for concurrent access and is part of the
// rpcserverSyncManager interface implementation.
func (b *rpcSyncMgr) GetBlueBlocksHeadersBetween(startHash, stopHash *daghash.Hash) ([]*wire.BlockHeader, error) {
return b.server.DAG.GetBlueBlocksHeadersBetween(startHash, stopHash)
func (b *rpcSyncMgr) BlueBlockHeadersBetween(lowHash, highHash *daghash.Hash) ([]*wire.BlockHeader, error) {
return b.server.DAG.BlueBlockHeadersBetween(lowHash, highHash)
}

View File

@ -735,11 +735,11 @@ type rpcserverSyncManager interface {
// used to sync from or 0 if there is none.
SyncPeerID() int32
// GetBlueBlocksHeadersBetween returns the headers of the blocks after the first known
// block in the provided locators until the provided stop hash or the
// BlueBlockHeadersBetween returns the headers of the blocks after the first known
// block in the provided locators until the provided high hash or the
// current tip is reached, up to a max of wire.MaxBlockHeadersPerMsg
// hashes.
GetBlueBlocksHeadersBetween(startHash, stopHash *daghash.Hash) ([]*wire.BlockHeader, error)
BlueBlockHeadersBetween(lowHash, highHash *daghash.Hash) ([]*wire.BlockHeader, error)
}
// rpcserverConfig is a descriptor containing the RPC server configuration.

View File

@ -394,13 +394,13 @@ var helpDescsEnUS = map[string]string{
// GetTopHeadersCmd help.
"getTopHeaders--synopsis": "Returns the top block headers starting with the provided start hash (not inclusive)",
"getTopHeaders-startHash": "Block hash to start including block headers from; if not found, it'll start from the virtual.",
"getTopHeaders-highHash": "Block hash to start including block headers from; if not found, it'll start from the virtual.",
"getTopHeaders--result0": "Serialized block headers of all located blocks, limited to some arbitrary maximum number of hashes (currently 2000, which matches the wire protocol headers message, but this is not guaranteed)",
// GetHeadersCmd help.
"getHeaders--synopsis": "Returns block headers starting with the first known block hash from the request",
"getHeaders-startHash": "Block hash to start including headers from; if not found, it'll start from the genesis block.",
"getHeaders-stopHash": "Block hash to stop including block headers for; if not found, all headers to the latest known block are returned.",
"getHeaders-lowHash": "Block hash to start including headers from; if not found, it'll start from the genesis block.",
"getHeaders-highHash": "Block hash to stop including block headers for; if not found, all headers to the latest known block are returned.",
"getHeaders--result0": "Serialized block headers of all located blocks, limited to some arbitrary maximum number of hashes (currently 2000, which matches the wire protocol headers message, but this is not guaranteed)",
// GetInfoCmd help.

View File

@ -442,8 +442,8 @@ func BenchmarkDecodeHeaders(b *testing.B) {
func BenchmarkDecodeGetBlockInvs(b *testing.B) {
pver := ProtocolVersion
var m MsgGetBlockInvs
m.StartHash = &daghash.Hash{1}
m.StopHash = &daghash.Hash{1}
m.LowHash = &daghash.Hash{1}
m.HighHash = &daghash.Hash{1}
// Serialize it so the bytes are available to test the decode below.
var bb bytes.Buffer

View File

@ -14,32 +14,32 @@ import (
// getblockinvs message. It is used to request a list of blocks starting after the
// start hash and until the stop hash.
type MsgGetBlockInvs struct {
StartHash *daghash.Hash
StopHash *daghash.Hash
LowHash *daghash.Hash
HighHash *daghash.Hash
}
// KaspaDecode decodes r using the kaspa protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgGetBlockInvs) KaspaDecode(r io.Reader, pver uint32) error {
msg.StartHash = &daghash.Hash{}
err := ReadElement(r, msg.StartHash)
msg.LowHash = &daghash.Hash{}
err := ReadElement(r, msg.LowHash)
if err != nil {
return err
}
msg.StopHash = &daghash.Hash{}
return ReadElement(r, msg.StopHash)
msg.HighHash = &daghash.Hash{}
return ReadElement(r, msg.HighHash)
}
// KaspaEncode encodes the receiver to w using the kaspa protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgGetBlockInvs) KaspaEncode(w io.Writer, pver uint32) error {
err := WriteElement(w, msg.StartHash)
err := WriteElement(w, msg.LowHash)
if err != nil {
return err
}
return WriteElement(w, msg.StopHash)
return WriteElement(w, msg.HighHash)
}
// Command returns the protocol command string for the message. This is part
@ -58,9 +58,9 @@ func (msg *MsgGetBlockInvs) MaxPayloadLength(pver uint32) uint32 {
// NewMsgGetBlockInvs returns a new kaspa getblockinvs message that conforms to the
// Message interface using the passed parameters and defaults for the remaining
// fields.
func NewMsgGetBlockInvs(startHash, stopHash *daghash.Hash) *MsgGetBlockInvs {
func NewMsgGetBlockInvs(lowHash, highHash *daghash.Hash) *MsgGetBlockInvs {
return &MsgGetBlockInvs{
StartHash: startHash,
StopHash: stopHash,
LowHash: lowHash,
HighHash: highHash,
}
}

View File

@ -32,9 +32,9 @@ func TestGetBlockInvs(t *testing.T) {
// Ensure we get the same data back out.
msg := NewMsgGetBlockInvs(startHash, stopHash)
if !msg.StopHash.IsEqual(stopHash) {
if !msg.HighHash.IsEqual(stopHash) {
t.Errorf("NewMsgGetBlockInvs: wrong stop hash - got %v, want %v",
msg.StopHash, stopHash)
msg.HighHash, stopHash)
}
// Ensure the command is expected value.

View File

@ -12,21 +12,21 @@ import (
//
// This message has no payload.
type MsgGetBlockLocator struct {
StartHash *daghash.Hash
StopHash *daghash.Hash
HighHash *daghash.Hash
LowHash *daghash.Hash
}
// KaspaDecode decodes r using the kaspa protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgGetBlockLocator) KaspaDecode(r io.Reader, pver uint32) error {
msg.StartHash = &daghash.Hash{}
err := ReadElement(r, msg.StartHash)
msg.HighHash = &daghash.Hash{}
err := ReadElement(r, msg.HighHash)
if err != nil {
return err
}
msg.StopHash = &daghash.Hash{}
err = ReadElement(r, msg.StopHash)
msg.LowHash = &daghash.Hash{}
err = ReadElement(r, msg.LowHash)
if err != nil {
return err
}
@ -36,12 +36,12 @@ func (msg *MsgGetBlockLocator) KaspaDecode(r io.Reader, pver uint32) error {
// KaspaEncode encodes the receiver to w using the kaspa protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgGetBlockLocator) KaspaEncode(w io.Writer, pver uint32) error {
err := WriteElement(w, msg.StartHash)
err := WriteElement(w, msg.HighHash)
if err != nil {
return err
}
err = WriteElement(w, msg.StopHash)
err = WriteElement(w, msg.LowHash)
if err != nil {
return err
}
@ -63,9 +63,9 @@ func (msg *MsgGetBlockLocator) MaxPayloadLength(pver uint32) uint32 {
// NewMsgGetBlockLocator returns a new getlocator message that conforms to the
// Message interface using the passed parameters and defaults for the remaining
// fields.
func NewMsgGetBlockLocator(startHash, stopHash *daghash.Hash) *MsgGetBlockLocator {
func NewMsgGetBlockLocator(highHash, lowHash *daghash.Hash) *MsgGetBlockLocator {
return &MsgGetBlockLocator{
StartHash: startHash,
StopHash: stopHash,
HighHash: highHash,
LowHash: lowHash,
}
}

View File

@ -17,7 +17,7 @@ import (
// limited by a specific hash to stop at or the maximum number of block headers
// per message, which is currently 2000.
//
// Set the StopHash field to the hash at which to stop and use
// Set the HighHash field to the hash at which to stop and use
// AddBlockLocatorHash to build up the list of block locator hashes.
//
// The algorithm for building the block locator hashes should be to add the