blockbook/bchain/coins/dash/dashrpc.go

115 lines
2.6 KiB
Go
Raw Normal View History

2018-06-08 07:23:28 -06:00
package dash
import (
"encoding/json"
"github.com/golang/glog"
"github.com/juju/errors"
2020-02-26 09:17:43 -07:00
"github.com/trezor/blockbook/bchain"
"github.com/trezor/blockbook/bchain/coins/btc"
2018-06-08 07:23:28 -06:00
)
const firstBlockWithSpecialTransactions = 1028160
2019-03-06 09:50:20 -07:00
// DashRPC is an interface to JSON-RPC bitcoind service
2018-06-08 07:23:28 -06:00
type DashRPC struct {
*btc.BitcoinRPC
}
2019-03-06 09:50:20 -07:00
// NewDashRPC returns new DashRPC instance
2018-06-08 07:23:28 -06:00
func NewDashRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
b, err := btc.NewBitcoinRPC(config, pushHandler)
if err != nil {
return nil, err
}
s := &DashRPC{
b.(*btc.BitcoinRPC),
}
s.RPCMarshaler = btc.JSONMarshalerV1{}
2018-07-24 07:58:37 -06:00
s.ChainConfig.SupportsEstimateSmartFee = false
2018-06-08 07:23:28 -06:00
return s, nil
}
// Initialize initializes DashRPC instance.
func (b *DashRPC) Initialize() error {
ci, err := b.GetChainInfo()
2018-06-08 07:23:28 -06:00
if err != nil {
return err
}
chainName := ci.Chain
2018-06-08 07:23:28 -06:00
params := GetChainParams(chainName)
// always create parser
b.Parser = NewDashParser(params, b.ChainConfig)
// parameters for getInfo request
if params.Net == MainnetMagic {
b.Testnet = false
b.Network = "livenet"
} else {
b.Testnet = true
b.Network = "testnet"
}
glog.Info("rpc: block chain ", params.Name)
return nil
}
2019-03-06 09:50:20 -07:00
// GetBlock returns block with given hash
func (b *DashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
if hash == "" && height < firstBlockWithSpecialTransactions {
return b.BitcoinRPC.GetBlock(hash, height)
}
var err error
if hash == "" && height > 0 {
hash, err = b.GetBlockHash(height)
if err != nil {
return nil, err
}
}
glog.V(1).Info("rpc: getblock (verbosity=1) ", hash)
res := btc.ResGetBlockThin{}
req := btc.CmdGetBlock{Method: "getblock"}
req.Params.BlockHash = hash
req.Params.Verbosity = 1
err = b.Call(&req, &res)
if err != nil {
return nil, errors.Annotatef(err, "hash %v", hash)
}
if res.Error != nil {
return nil, errors.Annotatef(res.Error, "hash %v", hash)
}
txs := make([]bchain.Tx, 0, len(res.Result.Txids))
for _, txid := range res.Result.Txids {
tx, err := b.GetTransaction(txid)
if err != nil {
if err == bchain.ErrTxNotFound {
glog.Errorf("rpc: getblock: skipping transanction in block %s due error: %s", hash, err)
continue
}
return nil, err
}
txs = append(txs, *tx)
}
block := &bchain.Block{
BlockHeader: res.Result.BlockHeader,
Txs: txs,
}
return block, nil
}
// GetTransactionForMempool returns a transaction by the transaction ID.
// It could be optimized for mempool, i.e. without block time and confirmations
2019-03-06 09:50:20 -07:00
func (b *DashRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) {
return b.GetTransaction(txid)
}