kaspad/server/rpc/handle_get_selected_tip.go
Dan Aharoni 47214121a7 [NOD-423] Implement get selected tip RPC command (#469)
* [NOD-423] Rename BestBlock to SelectedTip

* [NOD-423] Implement GetSelectedTip RPC command

* [NOD-423] Add help to getSelectedTip command

* [NOD-423] Fix getSelectedTip test

* [NOD-423] Fix tests so they would compile. These tests will need to be rewriten at some point.

* [NOD-423] Make integration test compile. Test need to be revisited

* [NOD-423] Rename variables

* [NOD-423] Change comment s about best block to selected tip.

* [NOD-423] Update comment

* [NOD-423] Change height to bluescore
2019-11-20 12:04:22 +02:00

47 lines
1.3 KiB
Go

package rpc
import (
"encoding/hex"
"github.com/daglabs/btcd/btcjson"
"github.com/daglabs/btcd/database"
"github.com/daglabs/btcd/util"
)
// handleGetSelectedTip implements the getSelectedTip command.
func handleGetSelectedTip(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
getSelectedTipCmd := cmd.(*btcjson.GetSelectedTipCmd)
selectedTipHash := s.cfg.DAG.SelectedTipHash()
var blockBytes []byte
err := s.cfg.DB.View(func(dbTx database.Tx) error {
var err error
blockBytes, err = dbTx.FetchBlock(selectedTipHash)
return err
})
if err != nil {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCBlockNotFound,
Message: "Block not found",
}
}
// 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 getSelectedTipCmd.Verbose != nil && !*getSelectedTipCmd.Verbose {
return hex.EncodeToString(blockBytes), nil
}
// Deserialize the block.
blk, err := util.NewBlockFromBytes(blockBytes)
if err != nil {
context := "Failed to deserialize block"
return nil, internalRPCError(err.Error(), context)
}
blockVerboseResult, err := buildGetBlockVerboseResult(s, blk, getSelectedTipCmd.VerboseTx == nil || !*getSelectedTipCmd.VerboseTx)
if err != nil {
return nil, err
}
return blockVerboseResult, nil
}