github.com/bchainhub/blockbook@v0.3.2/bchain/coins/deeponion/deeponionrpc.go (about)

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