github.com/bchainhub/blockbook@v0.3.2/bchain/coins/grs/grsrpc.go (about) 1 package grs 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 // GroestlcoinRPC is an interface to JSON-RPC service 13 type GroestlcoinRPC struct { 14 *btc.BitcoinRPC 15 } 16 17 // NewGroestlcoinRPC returns new GroestlcoinRPC instance 18 func NewGroestlcoinRPC(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 g := &GroestlcoinRPC{ 24 BitcoinRPC: b.(*btc.BitcoinRPC), 25 } 26 g.RPCMarshaler = btc.JSONMarshalerV1{} 27 return g, nil 28 } 29 30 // Initialize initializes GroestlcoinRPC instance. 31 func (g *GroestlcoinRPC) Initialize() error { 32 ci, err := g.GetChainInfo() 33 if err != nil { 34 return err 35 } 36 chainName := ci.Chain 37 38 params := GetChainParams(chainName) 39 40 g.Parser = NewGroestlcoinParser(params, g.ChainConfig) 41 42 // parameters for getInfo request 43 if params.Net == MainnetMagic { 44 g.Testnet = false 45 g.Network = "livenet" 46 } else { 47 g.Testnet = true 48 g.Network = "testnet" 49 } 50 51 glog.Info("rpc: block chain ", params.Name) 52 53 return nil 54 } 55 56 // GetBlock returns block with given hash. 57 func (g *GroestlcoinRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { 58 var err error 59 if hash == "" && height > 0 { 60 hash, err = g.GetBlockHash(height) 61 if err != nil { 62 return nil, err 63 } 64 } 65 66 glog.V(1).Info("rpc: getblock (verbosity=1) ", hash) 67 68 res := btc.ResGetBlockThin{} 69 req := btc.CmdGetBlock{Method: "getblock"} 70 req.Params.BlockHash = hash 71 req.Params.Verbosity = 1 72 err = g.Call(&req, &res) 73 74 if err != nil { 75 return nil, errors.Annotatef(err, "hash %v", hash) 76 } 77 if res.Error != nil { 78 return nil, errors.Annotatef(res.Error, "hash %v", hash) 79 } 80 81 txs := make([]bchain.Tx, 0, len(res.Result.Txids)) 82 for _, txid := range res.Result.Txids { 83 tx, err := g.GetTransaction(txid) 84 if err != nil { 85 if err == bchain.ErrTxNotFound { 86 glog.Errorf("rpc: getblock: skipping transanction in block %s due error: %s", hash, err) 87 continue 88 } 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 (g *GroestlcoinRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) { 103 return g.GetTransaction(txid) 104 }