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