kaspad/rpc/handle_get_blocks.go
stasatdaglabs 3d45c8de50
[NOD-1130] Integrate RPC with the new architecture (#807)
* [NOD-1130] Delete rpcadapters.go.

* [NOD-1130] Delete p2p. Move rpc to top level.

* [NOD-1130] Remove DAGParams from rpcserverConfig.

* [NOD-1130] Remove rpcserverPeer, rpcserverConnManager, rpcserverSyncManager, and rpcserverConfig.

* [NOD-1130] Remove wallet RPC commands.

* [NOD-1130] Remove wallet RPC commands.

* [NOD-1130] Remove connmgr and peer.

* [NOD-1130] Move rpcmodel into rpc.

* [NOD-1130] Implement ConnectionCount.

* [NOD-1130] Remove ping and node RPC commands.

* [NOD-1130] Dummify handleGetNetTotals.

* [NOD-1130] Add NetConnection to Peer.

* [NOD-1130] Fix merge errors.

* [NOD-1130] Implement Peers.

* [NOD-1130] Fix HandleGetConnectedPeerInfo.

* [NOD-1130] Fix SendRawTransaction.

* [NOD-1130] Rename addManualNode to connect and removeManualNode to disconnect.

* [NOD-1130] Add a stub for AddBlock.

* [NOD-1130] Fix tests.

* [NOD-1130] Replace half-baked contents of RemoveConnection with a stub.

* [NOD-1130] Fix merge errors.

* [NOD-1130] Make golint happy.

* [NOD-1130] Get rid of something weird.

* [NOD-1130] Rename minerClient back to client.

* [NOD-1130] Add a few fields to GetConnectedPeerInfoResult.

* [NOD-1130] Rename oneTry to isPermanent.

* [NOD-1130] Implement ConnectionCount in NetAdapter.

* [NOD-1130] Move RawMempoolVerbose out of mempool.

* [NOD-1130] Move isSynced into the mining package.

* [NOD-1130] Fix a compilation error.

* [NOD-1130] Make golint happy.

* [NOD-1130] Fix merge errors.
2020-07-22 10:26:39 +03:00

116 lines
2.9 KiB
Go

package rpc
import (
"encoding/hex"
"github.com/kaspanet/kaspad/rpc/model"
"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.(*model.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.dag.RLock()
defer s.dag.RUnlock()
// If lowHash is not in the DAG, there's nothing to do; return an error.
if lowHash != nil && !s.dag.IsKnownBlock(lowHash) {
return nil, &model.RPCError{
Code: model.ErrRPCBlockNotFound,
Message: "Block not found",
}
}
// Retrieve the block hashes.
blockHashes, err := s.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 := &model.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))
for i, hash := range hashes {
block, err := s.dag.BlockByHash(hash)
if err != nil {
return nil, err
}
blockBytes, err := block.Bytes()
if err != nil {
return nil, err
}
blocks[i] = blockBytes
}
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) ([]model.GetBlockVerboseResult, error) {
verboseBlocks := make([]model.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
}