From 9356e41730f2a3c64a2b3f2f9d645e49303965b3 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Tue, 30 Jan 2018 09:45:47 +0100 Subject: [PATCH] Add getblockcount bitcoind rpc call --- bitcoin/bitcoinrpc.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/bitcoin/bitcoinrpc.go b/bitcoin/bitcoinrpc.go index d0a2f3ef..402664c4 100644 --- a/bitcoin/bitcoinrpc.go +++ b/bitcoin/bitcoinrpc.go @@ -44,6 +44,17 @@ type resGetBestBlockHash struct { Result string `json:"result"` } +// getblockcount + +type cmdGetBlockCount struct { + Method string `json:"method"` +} + +type resGetBlockCount struct { + Error *RPCError `json:"error"` + Result uint32 `json:"result"` +} + // getblockheader type cmdGetBlockHeader struct { @@ -149,6 +160,23 @@ func (b *BitcoinRPC) GetBestBlockHash() (string, error) { return res.Result, nil } +// GetBestBlockHeight returns height of the tip of the best-block-chain. +func (b *BitcoinRPC) GetBestBlockHeight() (uint32, error) { + log.Printf("rpc: getblockcount") + + res := resGetBlockCount{} + req := cmdGetBlockCount{Method: "getblockcount"} + err := b.call(&req, &res) + + if err != nil { + return 0, err + } + if res.Error != nil { + return 0, res.Error + } + return res.Result, nil +} + // GetBlockHash returns hash of block in best-block-chain at given height. func (b *BitcoinRPC) GetBlockHash(height uint32) (string, error) { log.Printf("rpc: getblockhash %v", height)