mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-13 17:46:39 +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.
119 lines
3.0 KiB
Go
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 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 DAG, there's nothing to do; return an error.
|
|
if startHash != nil && !s.cfg.DAG.HaveBlock(startHash) {
|
|
return nil, &rpcmodel.RPCError{
|
|
Code: rpcmodel.ErrRPCBlockNotFound,
|
|
Message: "Block not found",
|
|
}
|
|
}
|
|
|
|
// Retrieve the block hashes.
|
|
blockHashes, err := s.cfg.DAG.BlockHashesFrom(startHash, 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
|
|
}
|