kaspad/server/rpc/handle_get_block.go
Svarog 2de3c1d0d4
[NOD-1160] Convert *config.Config from singleton to an object that is being passed around (#802)
* [NOD-1160] remove activeConfig from config package + update main

* [NOD-1160] Update main and addrmanager

* [NOD-1160] Update netAdapater

* [NOD-1160] Update connmanager

* [NOD-1160] Fix connmgr package

* [NOD-1160] Fixed DNSSeed functions

* [NOD-1160] Fixed protocol package and subpackages

* [NOD-1160] Fix p2p package

* [NOD-1160] Fix rpc package

* [NOD-1160] Fix kaspad a final time

* [NOD-1160] Make dnsseed.SeedFromDNS callable outside kaspad

* [NOD-1160] Fix tests

* [NOD-1160] Pass cfg to kaspad

* [NOD-1160] Add comment and remove redundant object

* [NOD-1160] Fix typo
2020-07-20 14:33:35 +03:00

109 lines
2.9 KiB
Go

package rpc
import (
"bufio"
"bytes"
"encoding/hex"
"github.com/kaspanet/kaspad/rpcmodel"
"github.com/kaspanet/kaspad/util"
"github.com/kaspanet/kaspad/util/daghash"
"github.com/kaspanet/kaspad/util/subnetworkid"
)
// handleGetBlock implements the getBlock command.
func handleGetBlock(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*rpcmodel.GetBlockCmd)
// Load the raw block bytes from the database.
hash, err := daghash.NewHashFromStr(c.Hash)
if err != nil {
return nil, rpcDecodeHexError(c.Hash)
}
// Return an appropriate error if the block is known to be invalid
if s.cfg.DAG.IsKnownInvalid(hash) {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCBlockInvalid,
Message: "Block is known to be invalid",
}
}
// Return an appropriate error if the block is an orphan
if s.cfg.DAG.IsKnownOrphan(hash) {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCOrphanBlock,
Message: "Block is an orphan",
}
}
block, err := s.cfg.DAG.BlockByHash(hash)
if err != nil {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCBlockNotFound,
Message: "Block not found",
}
}
blockBytes, err := block.Bytes()
if err != nil {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCBlockInvalid,
Message: "Cannot serialize block",
}
}
// Handle partial blocks
if c.Subnetwork != nil {
requestSubnetworkID, err := subnetworkid.NewFromStr(*c.Subnetwork)
if err != nil {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCInvalidRequest.Code,
Message: "invalid subnetwork string",
}
}
nodeSubnetworkID := s.appCfg.SubnetworkID
if requestSubnetworkID != nil {
if nodeSubnetworkID != nil {
if !nodeSubnetworkID.IsEqual(requestSubnetworkID) {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCInvalidRequest.Code,
Message: "subnetwork does not match this partial node",
}
}
// nothing to do - partial node stores partial blocks
} else {
// Deserialize the block.
msgBlock := block.MsgBlock()
msgBlock.ConvertToPartial(requestSubnetworkID)
var b bytes.Buffer
msgBlock.Serialize(bufio.NewWriter(&b))
blockBytes = b.Bytes()
}
}
}
// When the verbose flag is set to false, simply return the serialized block
// as a hex-encoded string (verbose flag is on by default).
if c.Verbose != nil && !*c.Verbose {
return hex.EncodeToString(blockBytes), nil
}
// The verbose flag is set, so generate the JSON object and return it.
// Deserialize the block.
block, err = util.NewBlockFromBytes(blockBytes)
if err != nil {
context := "Failed to deserialize block"
return nil, internalRPCError(err.Error(), context)
}
s.cfg.DAG.RLock()
defer s.cfg.DAG.RUnlock()
blockReply, err := buildGetBlockVerboseResult(s, block, c.VerboseTx == nil || !*c.VerboseTx)
if err != nil {
return nil, err
}
return blockReply, nil
}