Define error ErrBlockNotFound and implement it in btc

This commit is contained in:
Martin Boehm 2018-03-26 15:17:44 +02:00
parent 44fdb5528b
commit 341393b1eb
2 changed files with 32 additions and 1 deletions

View File

@ -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

View File

@ -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"`