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)