kaspad/server/rpc/handle_get_chain_from_block.go
stasatdaglabs f46dec449d [NOD-510] Change all references to Bitcoin to Kaspa (#531)
* [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.
2019-12-12 15:21:41 +02:00

91 lines
2.5 KiB
Go

package rpc
import (
"fmt"
"github.com/kaspanet/kaspad/rpcmodel"
"github.com/kaspanet/kaspad/util/daghash"
)
const (
// maxBlocksInGetChainFromBlockResult is the max amount of blocks that
// are allowed in a GetChainFromBlockResult.
maxBlocksInGetChainFromBlockResult = 1000
)
// handleGetChainFromBlock implements the getChainFromBlock command.
func handleGetChainFromBlock(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
if s.cfg.AcceptanceIndex == nil {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCNoAcceptanceIndex,
Message: "The acceptance index must be " +
"enabled to get the selected parent chain " +
"(specify --acceptanceindex)",
}
}
c := cmd.(*rpcmodel.GetChainFromBlockCmd)
var startHash *daghash.Hash
if c.StartHash != nil {
startHash = &daghash.Hash{}
err := daghash.Decode(startHash, *c.StartHash)
if err != nil {
return nil, rpcDecodeHexError(*c.StartHash)
}
}
s.cfg.DAG.RLock()
defer s.cfg.DAG.RUnlock()
// If startHash is not in the selected parent chain, there's nothing
// to do; return an error.
if startHash != nil && !s.cfg.DAG.BlockExists(startHash) {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCBlockNotFound,
Message: "Block not found in the DAG",
}
}
// Retrieve the selected parent chain.
removedChainHashes, addedChainHashes, err := s.cfg.DAG.SelectedParentChain(startHash)
if err != nil {
return nil, err
}
// Limit the amount of blocks in the response
if len(addedChainHashes) > maxBlocksInGetChainFromBlockResult {
addedChainHashes = addedChainHashes[:maxBlocksInGetChainFromBlockResult]
}
// Collect addedChainBlocks.
addedChainBlocks, err := collectChainBlocks(s, addedChainHashes)
if err != nil {
return nil, &rpcmodel.RPCError{
Code: rpcmodel.ErrRPCInternal.Code,
Message: fmt.Sprintf("could not collect chain blocks: %s", err),
}
}
// Collect removedHashes.
removedHashes := make([]string, len(removedChainHashes))
for i, hash := range removedChainHashes {
removedHashes[i] = hash.String()
}
result := &rpcmodel.GetChainFromBlockResult{
RemovedChainBlockHashes: removedHashes,
AddedChainBlocks: addedChainBlocks,
Blocks: nil,
}
// If the user specified to include the blocks, collect them as well.
if c.IncludeBlocks {
getBlockVerboseResults, err := hashesToGetBlockVerboseResults(s, addedChainHashes)
if err != nil {
return nil, err
}
result.Blocks = getBlockVerboseResults
}
return result, nil
}