mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-10 16:16:47 +00:00

* [NOD-1098] Change timestamps to be millisecond precision * [NOD-1098] Change lock times to use milliseconds * [NOD-1098] Use milliseconds precision everywhere * [NOD-1098] Implement type mstime.Time * [NOD-1098] Fix block 100000 timestamp * [NOD-1098] Change orphan child to be one millisecond delay after its parent * [NOD-1098] Remove test that checks if header timestamps have the right precision, and instead add tests for mstime, and fix genesis for testnet and devnet * [NOD-1098] Fix comment * [NOD-1098] Fix comment * [NOD-1098] Fix testnet genesis * [NOD-1098] Rename UnixMilli->UnixMilliseconds
81 lines
2.6 KiB
Go
81 lines
2.6 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 DAG.
|
|
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 hashes for the next blocks unless there are none.
|
|
childHashes, err := s.cfg.DAG.ChildHashesByHash(hash)
|
|
if err != nil {
|
|
context := "No next block"
|
|
return nil, internalRPCError(err.Error(), context)
|
|
}
|
|
childHashStrings := 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)
|
|
}
|
|
|
|
selectedParentHash, err := s.cfg.DAG.SelectedParentHash(hash)
|
|
if err != nil {
|
|
context := "Could not get block selected parent"
|
|
return nil, internalRPCError(err.Error(), context)
|
|
}
|
|
|
|
params := s.cfg.DAGParams
|
|
blockHeaderReply := rpcmodel.GetBlockHeaderVerboseResult{
|
|
Hash: c.Hash,
|
|
Confirmations: blockConfirmations,
|
|
Version: blockHeader.Version,
|
|
VersionHex: fmt.Sprintf("%08x", blockHeader.Version),
|
|
HashMerkleRoot: blockHeader.HashMerkleRoot.String(),
|
|
AcceptedIDMerkleRoot: blockHeader.AcceptedIDMerkleRoot.String(),
|
|
ChildHashes: childHashStrings,
|
|
ParentHashes: daghash.Strings(blockHeader.ParentHashes),
|
|
SelectedParentHash: selectedParentHash.String(),
|
|
Nonce: blockHeader.Nonce,
|
|
Time: blockHeader.Timestamp.UnixMilliseconds(),
|
|
Bits: strconv.FormatInt(int64(blockHeader.Bits), 16),
|
|
Difficulty: getDifficultyRatio(blockHeader.Bits, params),
|
|
}
|
|
return blockHeaderReply, nil
|
|
}
|