kaspad/server/rpc/handle_get_headers.go
stasatdaglabs 9d434de4a5 [NOD-640] Revamp blueBlocksBetween to return up to maxEntries from lowNode's antiPast to highNode's antiFuture (#593)
* [NOD-640] Revamp blueBlocksBetween to return up to maxEntries from highNode's antiFuture.

* [NOD-640] Fix bad traversal.

* [NOD-640] Use more accurate len.

* [NOD-640] Use more appropriate len in another place.

* [NOD-640] Remove the whole business with highNode's anticone.

* [NOD-640] Rename highNodeAntiFuture to candidateNodes.

* [NOD-640] Explain the highNode.blueScore-lowNode.blueScore+1 approximation.

* [NOD-640] UpHeap -> upHeap.

* [NOD-640] Fix off-by-one error.

* [NOD-640] Rename blueBlocksBetween to antiPastBetween,

* [NOD-640] upHeap -> up-heap.

* [NOD-640] Use a classic for to populate nodes.

* [NOD-640] Reworded a comment.

* [NOD-640] Clarify a comment.

* [NOD-640] Fix nodes declaration.
2020-01-22 12:13:55 +02:00

50 lines
1.2 KiB
Go

package rpc
import (
"bytes"
"encoding/hex"
"github.com/kaspanet/kaspad/rpcmodel"
"github.com/kaspanet/kaspad/util/daghash"
)
// handleGetHeaders implements the getHeaders command.
func handleGetHeaders(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*rpcmodel.GetHeadersCmd)
lowHash := &daghash.ZeroHash
if c.LowHash != "" {
err := daghash.Decode(lowHash, c.LowHash)
if err != nil {
return nil, rpcDecodeHexError(c.HighHash)
}
}
highHash := &daghash.ZeroHash
if c.HighHash != "" {
err := daghash.Decode(highHash, c.HighHash)
if err != nil {
return nil, rpcDecodeHexError(c.HighHash)
}
}
headers, err := s.cfg.SyncMgr.AntiPastHeadersBetween(lowHash, highHash)
if err != nil {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCMisc,
Message: err.Error(),
}
}
// Return the serialized block headers as hex-encoded strings.
hexBlockHeaders := make([]string, len(headers))
var buf bytes.Buffer
for i, h := range headers {
err := h.Serialize(&buf)
if err != nil {
return nil, internalRPCError(err.Error(),
"Failed to serialize block header")
}
hexBlockHeaders[i] = hex.EncodeToString(buf.Bytes())
buf.Reset()
}
return hexBlockHeaders, nil
}