kaspad/server/rpc/handle_get_blocks.go
Dan Aharoni c0463a8a68 [NOD-675] Replace start-hash/stop-hash with meaningful names (#597)
* [NOD-675] Rename startHash/stopHash to lowHigh/stopHash

* [NOD-675] Fix typo

* [NOD-675] Undo go.mod go.sum conflicts

* [NOD-675] revert back to startHash for getChainFromBlock.

* [NOD-675] Revet back to startHash in getChainFromBlock leftovers.

* [NOD-675] Fix test name.
2020-01-22 18:14:42 +02:00

119 lines
3.0 KiB
Go

package rpc
import (
"encoding/hex"
"github.com/kaspanet/kaspad/database"
"github.com/kaspanet/kaspad/rpcmodel"
"github.com/kaspanet/kaspad/util"
"github.com/kaspanet/kaspad/util/daghash"
)
const (
// maxBlocksInGetBlocksResult is the max amount of blocks that are
// allowed in a GetBlocksResult.
maxBlocksInGetBlocksResult = 1000
)
func handleGetBlocks(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*rpcmodel.GetBlocksCmd)
var lowHash *daghash.Hash
if c.LowHash != nil {
lowHash = &daghash.Hash{}
err := daghash.Decode(lowHash, *c.LowHash)
if err != nil {
return nil, rpcDecodeHexError(*c.LowHash)
}
}
s.cfg.DAG.RLock()
defer s.cfg.DAG.RUnlock()
// If lowHash is not in the DAG, there's nothing to do; return an error.
if lowHash != nil && !s.cfg.DAG.HaveBlock(lowHash) {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCBlockNotFound,
Message: "Block not found",
}
}
// Retrieve the block hashes.
blockHashes, err := s.cfg.DAG.BlockHashesFrom(lowHash, maxBlocksInGetBlocksResult)
if err != nil {
return nil, err
}
// Convert the hashes to strings
hashes := make([]string, len(blockHashes))
for i, blockHash := range blockHashes {
hashes[i] = blockHash.String()
}
result := &rpcmodel.GetBlocksResult{
Hashes: hashes,
RawBlocks: nil,
VerboseBlocks: nil,
}
// Include more data if requested
if c.IncludeRawBlockData || c.IncludeVerboseBlockData {
blockBytesSlice, err := hashesToBlockBytes(s, blockHashes)
if err != nil {
return nil, err
}
if c.IncludeRawBlockData {
result.RawBlocks = blockBytesToStrings(blockBytesSlice)
}
if c.IncludeVerboseBlockData {
verboseBlocks, err := blockBytesToBlockVerboseResults(s, blockBytesSlice)
if err != nil {
return nil, err
}
result.VerboseBlocks = verboseBlocks
}
}
return result, nil
}
func hashesToBlockBytes(s *Server, hashes []*daghash.Hash) ([][]byte, error) {
blocks := make([][]byte, len(hashes))
err := s.cfg.DB.View(func(dbTx database.Tx) error {
for i, hash := range hashes {
blockBytes, err := dbTx.FetchBlock(hash)
if err != nil {
return err
}
blocks[i] = blockBytes
}
return nil
})
if err != nil {
return nil, err
}
return blocks, nil
}
func blockBytesToStrings(blockBytesSlice [][]byte) []string {
rawBlocks := make([]string, len(blockBytesSlice))
for i, blockBytes := range blockBytesSlice {
rawBlocks[i] = hex.EncodeToString(blockBytes)
}
return rawBlocks
}
func blockBytesToBlockVerboseResults(s *Server, blockBytesSlice [][]byte) ([]rpcmodel.GetBlockVerboseResult, error) {
verboseBlocks := make([]rpcmodel.GetBlockVerboseResult, len(blockBytesSlice))
for i, blockBytes := range blockBytesSlice {
block, err := util.NewBlockFromBytes(blockBytes)
if err != nil {
return nil, err
}
getBlockVerboseResult, err := buildGetBlockVerboseResult(s, block, false)
if err != nil {
return nil, err
}
verboseBlocks[i] = *getBlockVerboseResult
}
return verboseBlocks, nil
}