mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-09 23:56:42 +00:00

* [NOD-510] Change coinbase flags to kaspad. * [NOD-510] Removed superfluous spaces after periods in comments. * [NOD-510] Rename btcd -> kaspad in the root folder. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Continue renaming btcd -> kaspad. * [NOD-510] Rename btcjson -> kaspajson. * [NOD-510] Rename file names inside kaspajson. * [NOD-510] Rename kaspajson -> jsonrpc. * [NOD-510] Finish renaming in addrmgr. * [NOD-510] Rename package btcec to ecc. * [NOD-510] Finish renaming stuff in blockdag. * [NOD-510] Rename stuff in cmd. * [NOD-510] Rename stuff in config. * [NOD-510] Rename stuff in connmgr. * [NOD-510] Rename stuff in dagconfig. * [NOD-510] Rename stuff in database. * [NOD-510] Rename stuff in docker. * [NOD-510] Rename stuff in integration. * [NOD-510] Rename jsonrpc to rpcmodel. * [NOD-510] Rename stuff in limits. * [NOD-510] Rename stuff in logger. * [NOD-510] Rename stuff in mempool. * [NOD-510] Rename stuff in mining. * [NOD-510] Rename stuff in netsync. * [NOD-510] Rename stuff in peer. * [NOD-510] Rename stuff in release. * [NOD-510] Rename stuff in rpcclient. * [NOD-510] Rename stuff in server. * [NOD-510] Rename stuff in signal. * [NOD-510] Rename stuff in txscript. * [NOD-510] Rename stuff in util. * [NOD-510] Rename stuff in wire. * [NOD-510] Fix failing tests. * [NOD-510] Fix merge errors. * [NOD-510] Fix go vet errors. * [NOD-510] Remove merged file that's no longer relevant. * [NOD-510] Add a comment above Op0. * [NOD-510] Fix some comments referencing Bitcoin Core. * [NOD-510] Fix some more comments referencing Bitcoin Core. * [NOD-510] Fix bitcoin -> kaspa. * [NOD-510] Fix more bitcoin -> kaspa. * [NOD-510] Fix comments, remove DisconnectBlock in addrindex. * [NOD-510] Rename KSPD to KASD. * [NOD-510] Fix comments and user agent.
114 lines
3.1 KiB
Go
114 lines
3.1 KiB
Go
package rpc
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/hex"
|
|
"github.com/kaspanet/kaspad/config"
|
|
"github.com/kaspanet/kaspad/database"
|
|
"github.com/kaspanet/kaspad/rpcmodel"
|
|
"github.com/kaspanet/kaspad/util"
|
|
"github.com/kaspanet/kaspad/util/daghash"
|
|
"github.com/kaspanet/kaspad/util/subnetworkid"
|
|
"github.com/kaspanet/kaspad/wire"
|
|
)
|
|
|
|
// 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",
|
|
}
|
|
}
|
|
|
|
var blkBytes []byte
|
|
err = s.cfg.DB.View(func(dbTx database.Tx) error {
|
|
var err error
|
|
blkBytes, err = dbTx.FetchBlock(hash)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return nil, &rpcmodel.RPCError{
|
|
Code: rpcmodel.ErrRPCBlockNotFound,
|
|
Message: "Block not found",
|
|
}
|
|
}
|
|
|
|
// 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 := config.ActiveConfig().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.
|
|
var msgBlock wire.MsgBlock
|
|
err = msgBlock.Deserialize(bytes.NewReader(blkBytes))
|
|
if err != nil {
|
|
context := "Failed to deserialize block"
|
|
return nil, internalRPCError(err.Error(), context)
|
|
}
|
|
msgBlock.ConvertToPartial(requestSubnetworkID)
|
|
var b bytes.Buffer
|
|
msgBlock.Serialize(bufio.NewWriter(&b))
|
|
blkBytes = 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(blkBytes), nil
|
|
}
|
|
|
|
// The verbose flag is set, so generate the JSON object and return it.
|
|
|
|
// Deserialize the block.
|
|
blk, err := util.NewBlockFromBytes(blkBytes)
|
|
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, blk, c.VerboseTx == nil || !*c.VerboseTx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return blockReply, nil
|
|
}
|