github.com/bchainhub/blockbook@v0.3.2/bchain/coins/monetaryunit/monetaryunitrpc.go (about)

     1  package monetaryunit
     2  
     3  import (
     4  	"blockbook/bchain"
     5  	"blockbook/bchain/coins/btc"
     6  	"encoding/json"
     7  
     8  	"github.com/golang/glog"
     9  )
    10  
    11  // MonetaryUnitRPC is an interface to JSON-RPC bitcoind service.
    12  type MonetaryUnitRPC struct {
    13  	*btc.BitcoinRPC
    14  }
    15  
    16  // NewMonetaryUnitRPC returns new MonetaryUnitRPC instance.
    17  func NewMonetaryUnitRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
    18  	b, err := btc.NewBitcoinRPC(config, pushHandler)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	s := &MonetaryUnitRPC{
    24  		b.(*btc.BitcoinRPC),
    25  	}
    26  	s.RPCMarshaler = btc.JSONMarshalerV1{}
    27  	s.ChainConfig.SupportsEstimateFee = true
    28  	s.ChainConfig.SupportsEstimateSmartFee = false
    29  
    30  	return s, nil
    31  }
    32  
    33  // Initialize initializes MonetaryUnitRPC instance.
    34  func (b *MonetaryUnitRPC) 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 = NewMonetaryUnitParser(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 gets block from backend
    62  func (b *MonetaryUnitRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
    63  
    64  	if height == 0 {
    65  		var err error
    66  		if hash == "" {
    67  			hash, err = b.GetBlockHash(height)
    68  			if err != nil {
    69  				return nil, err
    70  			}
    71  		}
    72  		if !b.ParseBlocks {
    73  			return b.GetBlockFull(hash)
    74  		}
    75  		return b.GetBlockWithoutHeader(hash, height)
    76  
    77  	}
    78  
    79  	var err error
    80  	if hash == "" && height > 0 {
    81  		hash, err = b.GetBlockHash(height)
    82  		if err != nil {
    83  			return nil, err
    84  		}
    85  	}
    86  
    87  	glog.V(1).Info("rpc: getblock (verbosity=1) ", hash)
    88  
    89  	res := btc.ResGetBlockThin{}
    90  	req := btc.CmdGetBlock{Method: "getblock"}
    91  	req.Params.BlockHash = hash
    92  	req.Params.Verbosity = 1
    93  	err = b.Call(&req, &res)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	txs := make([]bchain.Tx, 0, len(res.Result.Txids))
    99  	for _, txid := range res.Result.Txids {
   100  		tx, err := b.GetTransaction(txid)
   101  		if err != nil {
   102  			if err == bchain.ErrTxNotFound {
   103  				glog.Errorf("rpc: getblock: skipping transanction in block %s due error: %s", hash, err)
   104  				continue
   105  			}
   106  			return nil, err
   107  		}
   108  		txs = append(txs, *tx)
   109  	}
   110  	block := &bchain.Block{
   111  		BlockHeader: res.Result.BlockHeader,
   112  		Txs:         txs,
   113  	}
   114  	return block, nil
   115  }