|
| 1 | +//go:build chainrpc |
| 2 | +// +build chainrpc |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "fmt" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 12 | + "github.com/btcsuite/btcd/wire" |
| 13 | + "github.com/lightningnetwork/lnd/lnrpc/chainrpc" |
| 14 | + "github.com/urfave/cli" |
| 15 | +) |
| 16 | + |
| 17 | +// chainCommands will return the set of commands to enable for chainrpc builds. |
| 18 | +func chainCommands() []cli.Command { |
| 19 | + return []cli.Command{ |
| 20 | + { |
| 21 | + Name: "chain", |
| 22 | + Category: "On-chain", |
| 23 | + Usage: "Interact with the bitcoin blockchain.", |
| 24 | + Subcommands: []cli.Command{ |
| 25 | + getBlockCommand, |
| 26 | + getBestBlockCommand, |
| 27 | + getBlockHashCommand, |
| 28 | + }, |
| 29 | + }, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func getChainClient(ctx *cli.Context) (chainrpc.ChainKitClient, func()) { |
| 34 | + conn := getClientConn(ctx, false) |
| 35 | + |
| 36 | + cleanUp := func() { |
| 37 | + conn.Close() |
| 38 | + } |
| 39 | + |
| 40 | + return chainrpc.NewChainKitClient(conn), cleanUp |
| 41 | +} |
| 42 | + |
| 43 | +var getBlockCommand = cli.Command{ |
| 44 | + Name: "getblock", |
| 45 | + Category: "On-chain", |
| 46 | + Usage: "Get block by block hash.", |
| 47 | + Description: "Returns a block given the corresponding block hash.", |
| 48 | + Flags: []cli.Flag{ |
| 49 | + cli.StringFlag{ |
| 50 | + Name: "hash", |
| 51 | + Usage: "the target block hash", |
| 52 | + }, |
| 53 | + cli.BoolFlag{ |
| 54 | + Name: "verbose", |
| 55 | + Usage: "print entire block as JSON", |
| 56 | + }, |
| 57 | + }, |
| 58 | + Action: actionDecorator(getBlock), |
| 59 | +} |
| 60 | + |
| 61 | +func getBlock(ctx *cli.Context) error { |
| 62 | + ctxc := getContext() |
| 63 | + |
| 64 | + var ( |
| 65 | + args = ctx.Args() |
| 66 | + blockHashString string |
| 67 | + ) |
| 68 | + |
| 69 | + verbose := false |
| 70 | + if ctx.IsSet("verbose") { |
| 71 | + verbose = true |
| 72 | + } |
| 73 | + |
| 74 | + switch { |
| 75 | + case ctx.IsSet("hash"): |
| 76 | + blockHashString = ctx.String("hash") |
| 77 | + |
| 78 | + case args.Present(): |
| 79 | + blockHashString = args.First() |
| 80 | + |
| 81 | + default: |
| 82 | + return fmt.Errorf("hash argument missing") |
| 83 | + } |
| 84 | + |
| 85 | + blockHash, err := chainhash.NewHashFromStr(blockHashString) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + client, cleanUp := getChainClient(ctx) |
| 91 | + defer cleanUp() |
| 92 | + |
| 93 | + req := &chainrpc.GetBlockRequest{BlockHash: blockHash.CloneBytes()} |
| 94 | + resp, err := client.GetBlock(ctxc, req) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + // Convert raw block bytes into wire.MsgBlock. |
| 100 | + msgBlock := &wire.MsgBlock{} |
| 101 | + blockReader := bytes.NewReader(resp.RawBlock) |
| 102 | + err = msgBlock.Deserialize(blockReader) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + if verbose { |
| 108 | + printJSON(msgBlock) |
| 109 | + } else { |
| 110 | + printJSON(msgBlock.Header) |
| 111 | + } |
| 112 | + |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +var getBestBlockCommand = cli.Command{ |
| 117 | + Name: "getbestblock", |
| 118 | + Category: "On-chain", |
| 119 | + Usage: "Get best block.", |
| 120 | + Description: "Returns the latest block hash and height from the " + |
| 121 | + "valid most-work chain.", |
| 122 | + Action: actionDecorator(getBestBlock), |
| 123 | +} |
| 124 | + |
| 125 | +func getBestBlock(ctx *cli.Context) error { |
| 126 | + ctxc := getContext() |
| 127 | + |
| 128 | + client, cleanUp := getChainClient(ctx) |
| 129 | + defer cleanUp() |
| 130 | + |
| 131 | + resp, err := client.GetBestBlock(ctxc, &chainrpc.GetBestBlockRequest{}) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + // Cast gRPC block hash bytes as chain hash type. |
| 137 | + var blockHash chainhash.Hash |
| 138 | + copy(blockHash[:], resp.BlockHash) |
| 139 | + |
| 140 | + printJSON(struct { |
| 141 | + BlockHash chainhash.Hash `json:"block_hash"` |
| 142 | + BlockHeight int32 `json:"block_height"` |
| 143 | + }{ |
| 144 | + BlockHash: blockHash, |
| 145 | + BlockHeight: resp.BlockHeight, |
| 146 | + }) |
| 147 | + |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +var getBlockHashCommand = cli.Command{ |
| 152 | + Name: "getblockhash", |
| 153 | + Category: "On-chain", |
| 154 | + Usage: "Get block hash by block height.", |
| 155 | + Description: "Returns the block hash from the best chain at a given " + |
| 156 | + "height.", |
| 157 | + Flags: []cli.Flag{ |
| 158 | + cli.Int64Flag{ |
| 159 | + Name: "height", |
| 160 | + Usage: "target block height", |
| 161 | + }, |
| 162 | + }, |
| 163 | + Action: actionDecorator(getBlockHash), |
| 164 | +} |
| 165 | + |
| 166 | +func getBlockHash(ctx *cli.Context) error { |
| 167 | + ctxc := getContext() |
| 168 | + |
| 169 | + // Display the command's help message if we do not have the expected |
| 170 | + // number of arguments/flags. |
| 171 | + if ctx.NArg()+ctx.NumFlags() != 1 { |
| 172 | + return cli.ShowCommandHelp(ctx, "getblockhash") |
| 173 | + } |
| 174 | + |
| 175 | + var ( |
| 176 | + args = ctx.Args() |
| 177 | + blockHeight int64 |
| 178 | + ) |
| 179 | + |
| 180 | + switch { |
| 181 | + case ctx.IsSet("height"): |
| 182 | + blockHeight = ctx.Int64("height") |
| 183 | + |
| 184 | + case args.Present(): |
| 185 | + blockHeightString := args.First() |
| 186 | + |
| 187 | + // Convert block height positional argument from string to |
| 188 | + // int64. |
| 189 | + var err error |
| 190 | + blockHeight, err = strconv.ParseInt(blockHeightString, 10, 64) |
| 191 | + if err != nil { |
| 192 | + return err |
| 193 | + } |
| 194 | + |
| 195 | + default: |
| 196 | + return fmt.Errorf("block height argument missing") |
| 197 | + } |
| 198 | + |
| 199 | + client, cleanUp := getChainClient(ctx) |
| 200 | + defer cleanUp() |
| 201 | + |
| 202 | + req := &chainrpc.GetBlockHashRequest{BlockHeight: blockHeight} |
| 203 | + resp, err := client.GetBlockHash(ctxc, req) |
| 204 | + if err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + |
| 208 | + // Cast gRPC block hash bytes as chain hash type. |
| 209 | + var blockHash chainhash.Hash |
| 210 | + copy(blockHash[:], resp.BlockHash) |
| 211 | + |
| 212 | + printJSON(struct { |
| 213 | + BlockHash chainhash.Hash `json:"block_hash"` |
| 214 | + }{ |
| 215 | + BlockHash: blockHash, |
| 216 | + }) |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
0 commit comments