github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/dash/dashrpc.go (about)

     1  package dash
     2  
     3  import (
     4  	"blockbook/bchain"
     5  	"blockbook/bchain/coins/btc"
     6  	"encoding/json"
     7  
     8  	"github.com/golang/glog"
     9  	"github.com/juju/errors"
    10  )
    11  
    12  const firstBlockWithSpecialTransactions = 1028160
    13  
    14  // DashRPC is an interface to JSON-RPC bitcoind service
    15  type DashRPC struct {
    16  	*btc.BitcoinRPC
    17  }
    18  
    19  // NewDashRPC returns new DashRPC instance
    20  func NewDashRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
    21  	b, err := btc.NewBitcoinRPC(config, pushHandler)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	s := &DashRPC{
    27  		b.(*btc.BitcoinRPC),
    28  	}
    29  	s.RPCMarshaler = btc.JSONMarshalerV1{}
    30  	s.ChainConfig.SupportsEstimateSmartFee = false
    31  
    32  	return s, nil
    33  }
    34  
    35  // Initialize initializes DashRPC instance.
    36  func (b *DashRPC) Initialize() error {
    37  	ci, err := b.GetChainInfo()
    38  	if err != nil {
    39  		return err
    40  	}
    41  	chainName := ci.Chain
    42  
    43  	params := GetChainParams(chainName)
    44  
    45  	// always create parser
    46  	b.Parser = NewDashParser(params, b.ChainConfig)
    47  
    48  	// parameters for getInfo request
    49  	if params.Net == MainnetMagic {
    50  		b.Testnet = false
    51  		b.Network = "livenet"
    52  	} else {
    53  		b.Testnet = true
    54  		b.Network = "testnet"
    55  	}
    56  
    57  	glog.Info("rpc: block chain ", params.Name)
    58  
    59  	return nil
    60  }
    61  
    62  // GetBlock returns block with given hash
    63  func (b *DashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
    64  	if hash == "" && height < firstBlockWithSpecialTransactions {
    65  		return b.BitcoinRPC.GetBlock(hash, height)
    66  	}
    67  
    68  	var err error
    69  	if hash == "" && height > 0 {
    70  		hash, err = b.GetBlockHash(height)
    71  		if err != nil {
    72  			return nil, err
    73  		}
    74  	}
    75  
    76  	glog.V(1).Info("rpc: getblock (verbosity=1) ", hash)
    77  
    78  	res := btc.ResGetBlockThin{}
    79  	req := btc.CmdGetBlock{Method: "getblock"}
    80  	req.Params.BlockHash = hash
    81  	req.Params.Verbosity = 1
    82  	err = b.Call(&req, &res)
    83  
    84  	if err != nil {
    85  		return nil, errors.Annotatef(err, "hash %v", hash)
    86  	}
    87  	if res.Error != nil {
    88  		return nil, errors.Annotatef(res.Error, "hash %v", hash)
    89  	}
    90  
    91  	txs := make([]bchain.Tx, 0, len(res.Result.Txids))
    92  	for _, txid := range res.Result.Txids {
    93  		tx, err := b.GetTransaction(txid)
    94  		if err != nil {
    95  			if err == bchain.ErrTxNotFound {
    96  				glog.Errorf("rpc: getblock: skipping transanction in block %s due error: %s", hash, err)
    97  				continue
    98  			}
    99  			return nil, err
   100  		}
   101  		txs = append(txs, *tx)
   102  	}
   103  	block := &bchain.Block{
   104  		BlockHeader: res.Result.BlockHeader,
   105  		Txs:         txs,
   106  	}
   107  	return block, nil
   108  }
   109  
   110  // GetTransactionForMempool returns a transaction by the transaction ID.
   111  // It could be optimized for mempool, i.e. without block time and confirmations
   112  func (b *DashRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) {
   113  	return b.GetTransaction(txid)
   114  }