From 7290f8bbcd652700f12592de230c7d18d49d29d4 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Tue, 18 Sep 2018 10:58:47 +0200 Subject: [PATCH] Make GetBlockInfo and GetChainInfo more coin independent --- bchain/coins/bch/bcashrpc.go | 22 ++++++++++++++++++++ bchain/coins/btc/bitcoinrpc.go | 37 +++++++++++++++------------------- bchain/types.go | 14 ++++++------- 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/bchain/coins/bch/bcashrpc.go b/bchain/coins/bch/bcashrpc.go index cbf4b94f..89e7c74b 100644 --- a/bchain/coins/bch/bcashrpc.go +++ b/bchain/coins/bch/bcashrpc.go @@ -127,6 +127,28 @@ func (b *BCashRPC) GetBlockRaw(hash string) ([]byte, error) { return hex.DecodeString(res.Result) } +// GetBlockInfo returns extended header (more info than in bchain.BlockHeader) with a list of txids +func (b *BCashRPC) GetBlockInfo(hash string) (*bchain.BlockInfo, error) { + glog.V(1).Info("rpc: getblock (verbosity=1) ", hash) + + res := btc.ResGetBlockInfo{} + req := cmdGetBlock{Method: "getblock"} + req.Params.BlockHash = hash + req.Params.Verbose = true + err := b.Call(&req, &res) + + if err != nil { + 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 +} + // GetBlockFull returns block with given hash. func (b *BCashRPC) GetBlockFull(hash string) (*bchain.Block, error) { return nil, errors.New("Not implemented") diff --git a/bchain/coins/btc/bitcoinrpc.go b/bchain/coins/btc/bitcoinrpc.go index 576c35f9..27494586 100644 --- a/bchain/coins/btc/bitcoinrpc.go +++ b/bchain/coins/btc/bitcoinrpc.go @@ -11,7 +11,6 @@ import ( "math/big" "net" "net/http" - "strconv" "time" "github.com/btcsuite/btcd/wire" @@ -219,13 +218,13 @@ type CmdGetBlockChainInfo struct { type ResGetBlockChainInfo struct { Error *bchain.RPCError `json:"error"` Result struct { - Chain string `json:"chain"` - Blocks int `json:"blocks"` - Headers int `json:"headers"` - Bestblockhash string `json:"bestblockhash"` - Difficulty float64 `json:"difficulty"` - SizeOnDisk int64 `json:"size_on_disk"` - Warnings string `json:"warnings"` + Chain string `json:"chain"` + Blocks int `json:"blocks"` + Headers int `json:"headers"` + Bestblockhash string `json:"bestblockhash"` + Difficulty json.Number `json:"difficulty"` + SizeOnDisk int64 `json:"size_on_disk"` + Warnings string `json:"warnings"` } `json:"result"` } @@ -238,11 +237,11 @@ type CmdGetNetworkInfo struct { type ResGetNetworkInfo struct { Error *bchain.RPCError `json:"error"` Result struct { - Version int `json:"version"` - Subversion string `json:"subversion"` - ProtocolVersion int `json:"protocolversion"` - Timeoffset float64 `json:"timeoffset"` - Warnings string `json:"warnings"` + Version json.Number `json:"version"` + Subversion json.Number `json:"subversion"` + ProtocolVersion json.Number `json:"protocolversion"` + Timeoffset float64 `json:"timeoffset"` + Warnings string `json:"warnings"` } `json:"result"` } @@ -445,18 +444,14 @@ func (b *BitcoinRPC) GetChainInfo() (*bchain.ChainInfo, error) { Bestblockhash: resCi.Result.Bestblockhash, Blocks: resCi.Result.Blocks, Chain: resCi.Result.Chain, - Difficulty: resCi.Result.Difficulty, + Difficulty: string(resCi.Result.Difficulty), Headers: resCi.Result.Headers, SizeOnDisk: resCi.Result.SizeOnDisk, - Subversion: resNi.Result.Subversion, + Subversion: string(resNi.Result.Subversion), Timeoffset: resNi.Result.Timeoffset, } - if resNi.Result.Version > 0 { - rv.Version = strconv.Itoa(resNi.Result.Version) - } - if resNi.Result.ProtocolVersion > 0 { - rv.ProtocolVersion = strconv.Itoa(resNi.Result.ProtocolVersion) - } + rv.Version = string(resNi.Result.Version) + rv.ProtocolVersion = string(resNi.Result.ProtocolVersion) if len(resCi.Result.Warnings) > 0 { rv.Warnings = resCi.Result.Warnings + " " } diff --git a/bchain/types.go b/bchain/types.go index 84c87f99..4728a4bf 100644 --- a/bchain/types.go +++ b/bchain/types.go @@ -85,12 +85,12 @@ type BlockHeader struct { // BlockInfo contains extended block header data and a list of block txids type BlockInfo struct { BlockHeader - Version int64 `json:"version"` - MerkleRoot string `json:"merkleroot"` - Nonce uint64 `json:"nonce"` - Bits string `json:"bits"` - Difficulty float64 `json:"difficulty"` - Txids []string `json:"tx,omitempty"` + Version json.Number `json:"version"` + MerkleRoot string `json:"merkleroot"` + Nonce json.Number `json:"nonce"` + Bits string `json:"bits"` + Difficulty json.Number `json:"difficulty"` + Txids []string `json:"tx,omitempty"` } type MempoolEntry struct { @@ -115,7 +115,7 @@ type ChainInfo struct { Blocks int `json:"blocks"` Headers int `json:"headers"` Bestblockhash string `json:"bestblockhash"` - Difficulty float64 `json:"difficulty"` + Difficulty string `json:"difficulty"` SizeOnDisk int64 `json:"size_on_disk"` Version string `json:"version"` Subversion string `json:"subversion"`