From 341393b1ebb9cafad9afdd50fc364f586e77d166 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 26 Mar 2018 15:17:44 +0200 Subject: [PATCH] Define error ErrBlockNotFound and implement it in btc --- bchain/coins/btc/bitcoinrpc.go | 20 ++++++++++++++++++++ bchain/types.go | 13 ++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/bchain/coins/btc/bitcoinrpc.go b/bchain/coins/btc/bitcoinrpc.go index a85f24ca..b9607472 100644 --- a/bchain/coins/btc/bitcoinrpc.go +++ b/bchain/coins/btc/bitcoinrpc.go @@ -331,6 +331,11 @@ func (b *BitcoinRPC) GetBlockChainInfo() (string, error) { return res.Result.Chain, nil } +func isErrBlockNotFound(err error) bool { + return err.Error() == "Block not found" || + err.Error() == "Block height out of range" +} + // GetBlockHash returns hash of block in best-block-chain at given height. func (b *BitcoinRPC) GetBlockHash(height uint32) (string, error) { glog.V(1).Info("rpc: getblockhash ", height) @@ -344,6 +349,9 @@ func (b *BitcoinRPC) GetBlockHash(height uint32) (string, error) { return "", errors.Annotatef(err, "height %v", height) } if res.Error != nil { + if isErrBlockNotFound(res.Error) { + return "", bchain.ErrBlockNotFound + } return "", errors.Annotatef(res.Error, "height %v", height) } return res.Result, nil @@ -363,6 +371,9 @@ func (b *BitcoinRPC) GetBlockHeader(hash string) (*bchain.BlockHeader, error) { return nil, errors.Annotatef(err, "hash %v", hash) } if res.Error != nil { + if isErrBlockNotFound(res.Error) { + return nil, bchain.ErrBlockNotFound + } return nil, errors.Annotatef(res.Error, "hash %v", hash) } return &res.Result, nil @@ -423,6 +434,9 @@ func (b *BitcoinRPC) GetBlockRaw(hash string) ([]byte, error) { return nil, errors.Annotatef(err, "hash %v", hash) } if res.Error != nil { + if isErrBlockNotFound(res.Error) { + return nil, bchain.ErrBlockNotFound + } return nil, errors.Annotatef(res.Error, "hash %v", hash) } return hex.DecodeString(res.Result) @@ -443,6 +457,9 @@ func (b *BitcoinRPC) GetBlockList(hash string) (*bchain.Block, error) { return nil, errors.Annotatef(err, "hash %v", hash) } if res.Error != nil { + if isErrBlockNotFound(res.Error) { + return nil, bchain.ErrBlockNotFound + } return nil, errors.Annotatef(res.Error, "hash %v", hash) } @@ -475,6 +492,9 @@ func (b *BitcoinRPC) GetBlockFull(hash string) (*bchain.Block, error) { return nil, errors.Annotatef(err, "hash %v", hash) } if res.Error != nil { + if isErrBlockNotFound(res.Error) { + return nil, bchain.ErrBlockNotFound + } return nil, errors.Annotatef(res.Error, "hash %v", hash) } return &res.Result, nil diff --git a/bchain/types.go b/bchain/types.go index 64a72a2c..e482ee39 100644 --- a/bchain/types.go +++ b/bchain/types.go @@ -1,6 +1,17 @@ package bchain -import "fmt" +import ( + "errors" + "fmt" +) + +// errors with specific meaning returned by blockchain rpc +var ( + // ErrBlockNotFound is returned when block is not found + // either unknown hash or too high height + // can be returned from GetBlockHash, GetBlockHeader, GetBlock + ErrBlockNotFound = errors.New("Block not found") +) type ScriptSig struct { // Asm string `json:"asm"`