mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 17:55:36 +01:00
chainrpc+lncli: expose GetBlockHeader + add to CLI
This commit is contained in:
parent
6edd1e1220
commit
c20e4871cd
3 changed files with 73 additions and 4 deletions
|
@ -25,6 +25,7 @@ func chainCommands() []cli.Command {
|
||||||
getBlockCommand,
|
getBlockCommand,
|
||||||
getBestBlockCommand,
|
getBestBlockCommand,
|
||||||
getBlockHashCommand,
|
getBlockHashCommand,
|
||||||
|
getBlockHeaderCommand,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -113,6 +114,45 @@ func getBlock(ctx *cli.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var getBlockHeaderCommand = cli.Command{
|
||||||
|
Name: "getblockheader",
|
||||||
|
Usage: "Get a block header.",
|
||||||
|
Category: "On-chain",
|
||||||
|
Description: "Returns a block header with a particular block hash.",
|
||||||
|
ArgsUsage: "hash",
|
||||||
|
Action: actionDecorator(getBlockHeader),
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBlockHeader(ctx *cli.Context) error {
|
||||||
|
ctxc := getContext()
|
||||||
|
args := ctx.Args()
|
||||||
|
|
||||||
|
// Display the command's help message if we do not have the expected
|
||||||
|
// number of arguments/flags.
|
||||||
|
if !args.Present() {
|
||||||
|
return cli.ShowCommandHelp(ctx, "getblockheader")
|
||||||
|
}
|
||||||
|
|
||||||
|
blockHash, err := chainhash.NewHashFromStr(args.First())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &chainrpc.GetBlockHeaderRequest{BlockHash: blockHash[:]}
|
||||||
|
|
||||||
|
client, cleanUp := getChainClient(ctx)
|
||||||
|
defer cleanUp()
|
||||||
|
|
||||||
|
resp, err := client.GetBlockHeader(ctxc, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
printRespJSON(resp)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var getBestBlockCommand = cli.Command{
|
var getBestBlockCommand = cli.Command{
|
||||||
Name: "getbestblock",
|
Name: "getbestblock",
|
||||||
Category: "On-chain",
|
Category: "On-chain",
|
||||||
|
|
|
@ -155,16 +155,16 @@ func isBanned(ctx *cli.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var getBlockHeaderCommand = cli.Command{
|
var getBlockHeaderNeutrinoCommand = cli.Command{
|
||||||
Name: "getblockheader",
|
Name: "getblockheader",
|
||||||
Usage: "Get a block header.",
|
Usage: "Get a block header.",
|
||||||
Category: "Neutrino",
|
Category: "Neutrino",
|
||||||
Description: "Returns a block header with a particular block hash.",
|
Description: "Returns a block header with a particular block hash.",
|
||||||
ArgsUsage: "hash",
|
ArgsUsage: "hash",
|
||||||
Action: actionDecorator(getBlockHeader),
|
Action: actionDecorator(getBlockHeaderNeutrino),
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBlockHeader(ctx *cli.Context) error {
|
func getBlockHeaderNeutrino(ctx *cli.Context) error {
|
||||||
ctxc := getContext()
|
ctxc := getContext()
|
||||||
args := ctx.Args()
|
args := ctx.Args()
|
||||||
|
|
||||||
|
@ -239,7 +239,7 @@ func neutrinoCommands() []cli.Command {
|
||||||
addPeerCommand,
|
addPeerCommand,
|
||||||
disconnectPeerCommand,
|
disconnectPeerCommand,
|
||||||
isBannedCommand,
|
isBannedCommand,
|
||||||
getBlockHeaderCommand,
|
getBlockHeaderNeutrinoCommand,
|
||||||
getCFilterCommand,
|
getCFilterCommand,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -46,6 +46,10 @@ var (
|
||||||
Entity: "onchain",
|
Entity: "onchain",
|
||||||
Action: "read",
|
Action: "read",
|
||||||
}},
|
}},
|
||||||
|
"/chainrpc.ChainKit/GetBlockHeader": {{
|
||||||
|
Entity: "onchain",
|
||||||
|
Action: "read",
|
||||||
|
}},
|
||||||
"/chainrpc.ChainKit/GetBestBlock": {{
|
"/chainrpc.ChainKit/GetBestBlock": {{
|
||||||
Entity: "onchain",
|
Entity: "onchain",
|
||||||
Action: "read",
|
Action: "read",
|
||||||
|
@ -293,6 +297,31 @@ func (s *Server) GetBlock(_ context.Context,
|
||||||
return &GetBlockResponse{RawBlock: rawBlock}, nil
|
return &GetBlockResponse{RawBlock: rawBlock}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetBlockHeader returns a block header given the corresponding block hash.
|
||||||
|
func (s *Server) GetBlockHeader(_ context.Context,
|
||||||
|
in *GetBlockHeaderRequest) (*GetBlockHeaderResponse, error) {
|
||||||
|
|
||||||
|
// We'll start by reconstructing the RPC request into what the
|
||||||
|
// underlying chain functionality expects.
|
||||||
|
var blockHash chainhash.Hash
|
||||||
|
copy(blockHash[:], in.BlockHash)
|
||||||
|
|
||||||
|
blockHeader, err := s.cfg.Chain.GetBlockHeader(&blockHash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize block header for RPC response.
|
||||||
|
var headerBuf bytes.Buffer
|
||||||
|
err = blockHeader.Serialize(&headerBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rawHeader := headerBuf.Bytes()
|
||||||
|
|
||||||
|
return &GetBlockHeaderResponse{RawBlockHeader: rawHeader}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetBestBlock returns the latest block hash and current height of the valid
|
// GetBestBlock returns the latest block hash and current height of the valid
|
||||||
// most-work chain.
|
// most-work chain.
|
||||||
func (s *Server) GetBestBlock(_ context.Context,
|
func (s *Server) GetBestBlock(_ context.Context,
|
||||||
|
|
Loading…
Add table
Reference in a new issue