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

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