mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-14 01:56:44 +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.
85 lines
2.8 KiB
Go
85 lines
2.8 KiB
Go
package rpc
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"github.com/kaspanet/kaspad/rpcmodel"
|
|
"github.com/kaspanet/kaspad/util/daghash"
|
|
"strconv"
|
|
)
|
|
|
|
// handleGetBlockHeader implements the getBlockHeader command.
|
|
func handleGetBlockHeader(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
|
c := cmd.(*rpcmodel.GetBlockHeaderCmd)
|
|
|
|
// Fetch the header from chain.
|
|
hash, err := daghash.NewHashFromStr(c.Hash)
|
|
if err != nil {
|
|
return nil, rpcDecodeHexError(c.Hash)
|
|
}
|
|
blockHeader, err := s.cfg.DAG.HeaderByHash(hash)
|
|
if err != nil {
|
|
return nil, &rpcmodel.RPCError{
|
|
Code: rpcmodel.ErrRPCBlockNotFound,
|
|
Message: "Block not found",
|
|
}
|
|
}
|
|
|
|
// When the verbose flag isn't set, simply return the serialized block
|
|
// header as a hex-encoded string.
|
|
if c.Verbose != nil && !*c.Verbose {
|
|
var headerBuf bytes.Buffer
|
|
err := blockHeader.Serialize(&headerBuf)
|
|
if err != nil {
|
|
context := "Failed to serialize block header"
|
|
return nil, internalRPCError(err.Error(), context)
|
|
}
|
|
return hex.EncodeToString(headerBuf.Bytes()), nil
|
|
}
|
|
|
|
// The verbose flag is set, so generate the JSON object and return it.
|
|
|
|
// Get the block chain height from chain.
|
|
blockChainHeight, err := s.cfg.DAG.BlockChainHeightByHash(hash)
|
|
if err != nil {
|
|
context := "Failed to obtain block height"
|
|
return nil, internalRPCError(err.Error(), context)
|
|
}
|
|
|
|
// Get the hashes for the next blocks unless there are none.
|
|
var nextHashStrings []string
|
|
if blockChainHeight < s.cfg.DAG.ChainHeight() { //TODO: (Ori) This is probably wrong. Done only for compilation
|
|
childHashes, err := s.cfg.DAG.ChildHashesByHash(hash)
|
|
if err != nil {
|
|
context := "No next block"
|
|
return nil, internalRPCError(err.Error(), context)
|
|
}
|
|
nextHashStrings = daghash.Strings(childHashes)
|
|
}
|
|
|
|
blockConfirmations, err := s.cfg.DAG.BlockConfirmationsByHash(hash)
|
|
if err != nil {
|
|
context := "Could not get block confirmations"
|
|
return nil, internalRPCError(err.Error(), context)
|
|
}
|
|
|
|
params := s.cfg.DAGParams
|
|
blockHeaderReply := rpcmodel.GetBlockHeaderVerboseResult{
|
|
Hash: c.Hash,
|
|
Confirmations: blockConfirmations,
|
|
Height: blockChainHeight,
|
|
Version: blockHeader.Version,
|
|
VersionHex: fmt.Sprintf("%08x", blockHeader.Version),
|
|
HashMerkleRoot: blockHeader.HashMerkleRoot.String(),
|
|
AcceptedIDMerkleRoot: blockHeader.AcceptedIDMerkleRoot.String(),
|
|
NextHashes: nextHashStrings,
|
|
ParentHashes: daghash.Strings(blockHeader.ParentHashes),
|
|
Nonce: uint64(blockHeader.Nonce),
|
|
Time: blockHeader.Timestamp.Unix(),
|
|
Bits: strconv.FormatInt(int64(blockHeader.Bits), 16),
|
|
Difficulty: getDifficultyRatio(blockHeader.Bits, params),
|
|
}
|
|
return blockHeaderReply, nil
|
|
}
|