Skip to content

Commit

Permalink
Use common.JSONNumber instead of json.Number
Browse files Browse the repository at this point in the history
  • Loading branch information
martinboehm committed May 9, 2020
1 parent c3d58f0 commit ee3217a
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 122 deletions.
26 changes: 13 additions & 13 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,19 @@ type Blocks struct {

// BlockInfo contains extended block header data and a list of block txids
type BlockInfo struct {
Hash string `json:"hash"`
Prev string `json:"previousBlockHash,omitempty"`
Next string `json:"nextBlockHash,omitempty"`
Height uint32 `json:"height"`
Confirmations int `json:"confirmations"`
Size int `json:"size"`
Time int64 `json:"time,omitempty"`
Version json.Number `json:"version"`
MerkleRoot string `json:"merkleRoot"`
Nonce string `json:"nonce"`
Bits string `json:"bits"`
Difficulty string `json:"difficulty"`
Txids []string `json:"tx,omitempty"`
Hash string `json:"hash"`
Prev string `json:"previousBlockHash,omitempty"`
Next string `json:"nextBlockHash,omitempty"`
Height uint32 `json:"height"`
Confirmations int `json:"confirmations"`
Size int `json:"size"`
Time int64 `json:"time,omitempty"`
Version common.JSONNumber `json:"version"`
MerkleRoot string `json:"merkleRoot"`
Nonce string `json:"nonce"`
Bits string `json:"bits"`
Difficulty string `json:"difficulty"`
Txids []string `json:"tx,omitempty"`
}

// Block contains information about block
Expand Down
5 changes: 3 additions & 2 deletions bchain/baseparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/golang/glog"
"github.com/juju/errors"
"github.com/trezor/blockbook/common"
)

// BaseParser implements data parsing/handling functionality base for all other parsers
Expand Down Expand Up @@ -39,9 +40,9 @@ func (p *BaseParser) GetAddrDescForUnknownInput(tx *Tx, input int) AddressDescri

const zeros = "0000000000000000000000000000000000000000"

// AmountToBigInt converts amount in json.Number (string) to big.Int
// AmountToBigInt converts amount in common.JSONNumber (string) to big.Int
// it uses string operations to avoid problems with rounding
func (p *BaseParser) AmountToBigInt(n json.Number) (big.Int, error) {
func (p *BaseParser) AmountToBigInt(n common.JSONNumber) (big.Int, error) {
var r big.Int
s := string(n)
i := strings.IndexByte(s, '.')
Expand Down
7 changes: 5 additions & 2 deletions bchain/baseparser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// +build unittest

package bchain

import (
"encoding/json"
"math/big"
"testing"

"github.com/trezor/blockbook/common"
)

func NewBaseParser(adp int) *BaseParser {
Expand Down Expand Up @@ -44,7 +47,7 @@ func TestBaseParser_AmountToDecimalString(t *testing.T) {
func TestBaseParser_AmountToBigInt(t *testing.T) {
for _, tt := range amounts {
t.Run(tt.s, func(t *testing.T) {
got, err := NewBaseParser(tt.adp).AmountToBigInt(json.Number(tt.s))
got, err := NewBaseParser(tt.adp).AmountToBigInt(common.JSONNumber(tt.s))
if err != nil {
t.Errorf("BaseParser.AmountToBigInt() error = %v", err)
return
Expand Down
33 changes: 17 additions & 16 deletions bchain/coins/btc/bitcoinrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/juju/errors"
"github.com/martinboehm/btcd/wire"
"github.com/trezor/blockbook/bchain"
"github.com/trezor/blockbook/common"
)

// BitcoinRPC is an interface to JSON-RPC bitcoind service.
Expand Down Expand Up @@ -237,13 +238,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 json.Number `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 common.JSONNumber `json:"difficulty"`
SizeOnDisk int64 `json:"size_on_disk"`
Warnings string `json:"warnings"`
} `json:"result"`
}

Expand All @@ -256,11 +257,11 @@ type CmdGetNetworkInfo struct {
type ResGetNetworkInfo struct {
Error *bchain.RPCError `json:"error"`
Result struct {
Version json.Number `json:"version"`
Subversion json.Number `json:"subversion"`
ProtocolVersion json.Number `json:"protocolversion"`
Timeoffset float64 `json:"timeoffset"`
Warnings string `json:"warnings"`
Version common.JSONNumber `json:"version"`
Subversion common.JSONNumber `json:"subversion"`
ProtocolVersion common.JSONNumber `json:"protocolversion"`
Timeoffset float64 `json:"timeoffset"`
Warnings string `json:"warnings"`
} `json:"result"`
}

Expand Down Expand Up @@ -358,8 +359,8 @@ type CmdEstimateSmartFee struct {
type ResEstimateSmartFee struct {
Error *bchain.RPCError `json:"error"`
Result struct {
Feerate json.Number `json:"feerate"`
Blocks int `json:"blocks"`
Feerate common.JSONNumber `json:"feerate"`
Blocks int `json:"blocks"`
} `json:"result"`
}

Expand All @@ -373,8 +374,8 @@ type CmdEstimateFee struct {
}

type ResEstimateFee struct {
Error *bchain.RPCError `json:"error"`
Result json.Number `json:"result"`
Error *bchain.RPCError `json:"error"`
Result common.JSONNumber `json:"result"`
}

// sendrawtransaction
Expand Down
2 changes: 1 addition & 1 deletion bchain/coins/dash/dashparser_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// build unittest
// +build unittest

package dash

Expand Down
105 changes: 53 additions & 52 deletions bchain/coins/dcr/decredrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/juju/errors"
"github.com/trezor/blockbook/bchain"
"github.com/trezor/blockbook/bchain/coins/btc"
"github.com/trezor/blockbook/common"
)

// voteBitYes defines the vote bit set when a given block validates the previous
Expand Down Expand Up @@ -167,61 +168,61 @@ type GetBlockHashResult struct {
type GetBlockResult struct {
Error Error `json:"error"`
Result struct {
Hash string `json:"hash"`
Confirmations int64 `json:"confirmations"`
Size int32 `json:"size"`
Height uint32 `json:"height"`
Version json.Number `json:"version"`
MerkleRoot string `json:"merkleroot"`
StakeRoot string `json:"stakeroot"`
RawTx []RawTx `json:"rawtx"`
Tx []string `json:"tx,omitempty"`
STx []string `json:"stx,omitempty"`
Time int64 `json:"time"`
Nonce json.Number `json:"nonce"`
VoteBits uint16 `json:"votebits"`
FinalState string `json:"finalstate"`
Voters uint16 `json:"voters"`
FreshStake uint8 `json:"freshstake"`
Revocations uint8 `json:"revocations"`
PoolSize uint32 `json:"poolsize"`
Bits string `json:"bits"`
SBits float64 `json:"sbits"`
ExtraData string `json:"extradata"`
StakeVersion uint32 `json:"stakeversion"`
Difficulty float64 `json:"difficulty"`
ChainWork string `json:"chainwork"`
PreviousHash string `json:"previousblockhash"`
NextHash string `json:"nextblockhash,omitempty"`
Hash string `json:"hash"`
Confirmations int64 `json:"confirmations"`
Size int32 `json:"size"`
Height uint32 `json:"height"`
Version common.JSONNumber `json:"version"`
MerkleRoot string `json:"merkleroot"`
StakeRoot string `json:"stakeroot"`
RawTx []RawTx `json:"rawtx"`
Tx []string `json:"tx,omitempty"`
STx []string `json:"stx,omitempty"`
Time int64 `json:"time"`
Nonce common.JSONNumber `json:"nonce"`
VoteBits uint16 `json:"votebits"`
FinalState string `json:"finalstate"`
Voters uint16 `json:"voters"`
FreshStake uint8 `json:"freshstake"`
Revocations uint8 `json:"revocations"`
PoolSize uint32 `json:"poolsize"`
Bits string `json:"bits"`
SBits float64 `json:"sbits"`
ExtraData string `json:"extradata"`
StakeVersion uint32 `json:"stakeversion"`
Difficulty float64 `json:"difficulty"`
ChainWork string `json:"chainwork"`
PreviousHash string `json:"previousblockhash"`
NextHash string `json:"nextblockhash,omitempty"`
} `json:"result"`
}

type GetBlockHeaderResult struct {
Error Error `json:"error"`
Result struct {
Hash string `json:"hash"`
Confirmations int64 `json:"confirmations"`
Version json.Number `json:"version"`
MerkleRoot string `json:"merkleroot"`
StakeRoot string `json:"stakeroot"`
VoteBits uint16 `json:"votebits"`
FinalState string `json:"finalstate"`
Voters uint16 `json:"voters"`
FreshStake uint8 `json:"freshstake"`
Revocations uint8 `json:"revocations"`
PoolSize uint32 `json:"poolsize"`
Bits string `json:"bits"`
SBits float64 `json:"sbits"`
Height uint32 `json:"height"`
Size uint32 `json:"size"`
Time int64 `json:"time"`
Nonce uint32 `json:"nonce"`
ExtraData string `json:"extradata"`
StakeVersion uint32 `json:"stakeversion"`
Difficulty float64 `json:"difficulty"`
ChainWork string `json:"chainwork"`
PreviousHash string `json:"previousblockhash,omitempty"`
NextHash string `json:"nextblockhash,omitempty"`
Hash string `json:"hash"`
Confirmations int64 `json:"confirmations"`
Version common.JSONNumber `json:"version"`
MerkleRoot string `json:"merkleroot"`
StakeRoot string `json:"stakeroot"`
VoteBits uint16 `json:"votebits"`
FinalState string `json:"finalstate"`
Voters uint16 `json:"voters"`
FreshStake uint8 `json:"freshstake"`
Revocations uint8 `json:"revocations"`
PoolSize uint32 `json:"poolsize"`
Bits string `json:"bits"`
SBits float64 `json:"sbits"`
Height uint32 `json:"height"`
Size uint32 `json:"size"`
Time int64 `json:"time"`
Nonce uint32 `json:"nonce"`
ExtraData string `json:"extradata"`
StakeVersion uint32 `json:"stakeversion"`
Difficulty float64 `json:"difficulty"`
ChainWork string `json:"chainwork"`
PreviousHash string `json:"previousblockhash,omitempty"`
NextHash string `json:"nextblockhash,omitempty"`
} `json:"result"`
}

Expand Down Expand Up @@ -296,8 +297,8 @@ type EstimateSmartFeeResult struct {
}

type EstimateFeeResult struct {
Error Error `json:"error"`
Result json.Number `json:"result"`
Error Error `json:"error"`
Result common.JSONNumber `json:"result"`
}

type SendRawTransactionResult struct {
Expand Down Expand Up @@ -636,7 +637,7 @@ func (d *DecredRPC) GetBlockInfo(hash string) (*bchain.BlockInfo, error) {
Version: block.Result.Version,
Nonce: block.Result.Nonce,
Bits: block.Result.Bits,
Difficulty: json.Number(strconv.FormatFloat(block.Result.Difficulty, 'e', -1, 64)),
Difficulty: common.JSONNumber(strconv.FormatFloat(block.Result.Difficulty, 'e', -1, 64)),
Txids: block.Result.Tx,
}

Expand Down
5 changes: 3 additions & 2 deletions bchain/coins/eth/ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/golang/glog"
"github.com/juju/errors"
"github.com/trezor/blockbook/bchain"
"github.com/trezor/blockbook/common"
)

// EthereumNet type specifies the type of ethereum network
Expand Down Expand Up @@ -569,8 +570,8 @@ func (b *EthereumRPC) GetBlockInfo(hash string) (*bchain.BlockInfo, error) {
}
return &bchain.BlockInfo{
BlockHeader: *bch,
Difficulty: json.Number(head.Difficulty),
Nonce: json.Number(head.Nonce),
Difficulty: common.JSONNumber(head.Difficulty),
Nonce: common.JSONNumber(head.Nonce),
Txids: txs.Transactions,
}, nil
}
Expand Down
10 changes: 6 additions & 4 deletions bchain/coins/nuls/nulsparser_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// +build unittest

package nuls

import (
"encoding/hex"
"encoding/json"
"math/big"
"reflect"
"testing"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/martinboehm/btcutil/hdkeychain"
"github.com/trezor/blockbook/bchain"
"github.com/trezor/blockbook/bchain/coins/btc"
"github.com/trezor/blockbook/common"
)

var (
Expand Down Expand Up @@ -41,7 +43,7 @@ func init() {
{
ValueSat: *big.NewInt(399999000000),
N: 0,
JsonValue: json.Number("0"),
JsonValue: common.JSONNumber("0"),
ScriptPubKey: bchain.ScriptPubKey{
Hex: "Nse4zpZHsUuU7h5ymv28pcGbwHju3joV",
Addresses: []string{
Expand Down Expand Up @@ -73,7 +75,7 @@ func init() {
{
ValueSat: *big.NewInt(400000000000),
N: 0,
JsonValue: json.Number("0"),
JsonValue: common.JSONNumber("0"),
ScriptPubKey: bchain.ScriptPubKey{
Hex: "Nse4ikjE88g2BgsNwsswTdkSwiSrKjjS",
Addresses: []string{
Expand All @@ -84,7 +86,7 @@ func init() {
{
ValueSat: *big.NewInt(7286565570000),
N: 1,
JsonValue: json.Number("0"),
JsonValue: common.JSONNumber("0"),
ScriptPubKey: bchain.ScriptPubKey{
Hex: "Nse119z2oSDJYkFkxmwYDiYtPfBeNkqi",
Addresses: []string{
Expand Down
Loading

0 comments on commit ee3217a

Please sign in to comment.