github.com/bchainhub/blockbook@v0.3.2/bchain/coins/flo/florpc.go (about) 1 package flo 2 3 import ( 4 "blockbook/bchain" 5 "blockbook/bchain/coins/btc" 6 "encoding/json" 7 8 "github.com/juju/errors" 9 10 "github.com/golang/glog" 11 ) 12 13 // FloRPC is an interface to JSON-RPC bitcoind service. 14 type FloRPC struct { 15 *btc.BitcoinRPC 16 } 17 18 // NewFloRPC returns new FloRPC instance. 19 func NewFloRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) { 20 b, err := btc.NewBitcoinRPC(config, pushHandler) 21 if err != nil { 22 return nil, err 23 } 24 25 s := &FloRPC{ 26 b.(*btc.BitcoinRPC), 27 } 28 s.RPCMarshaler = btc.JSONMarshalerV2{} 29 s.ChainConfig.SupportsEstimateFee = false 30 31 return s, nil 32 } 33 34 // Initialize initializes FloRPC instance. 35 func (f *FloRPC) Initialize() error { 36 ci, err := f.GetChainInfo() 37 if err != nil { 38 return err 39 } 40 chainName := ci.Chain 41 42 glog.Info("Chain name ", chainName) 43 params := GetChainParams(chainName) 44 45 // always create parser 46 f.Parser = NewFloParser(params, f.ChainConfig) 47 48 // parameters for getInfo request 49 if params.Net == MainnetMagic { 50 f.Testnet = false 51 f.Network = "livenet" 52 } else { 53 f.Testnet = true 54 f.Network = "testnet" 55 } 56 57 glog.Info("rpc: block chain ", params.Name) 58 59 return nil 60 } 61 62 // GetBlock returns block with given hash. 63 func (f *FloRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { 64 var err error 65 if hash == "" { 66 hash, err = f.GetBlockHash(height) 67 if err != nil { 68 return nil, err 69 } 70 } 71 if !f.ParseBlocks { 72 return f.GetBlockFull(hash) 73 } 74 // optimization 75 if height > 0 { 76 return f.GetBlockWithoutHeader(hash, height) 77 } 78 header, err := f.GetBlockHeader(hash) 79 if err != nil { 80 return nil, err 81 } 82 data, err := f.GetBlockRaw(hash) 83 if err != nil { 84 return nil, err 85 } 86 block, err := f.Parser.ParseBlock(data) 87 if err != nil { 88 return nil, errors.Annotatef(err, "hash %v", hash) 89 } 90 block.BlockHeader = *header 91 return block, nil 92 } 93 94 // GetBlockFull returns block with given hash 95 func (f *FloRPC) GetBlockFull(hash string) (*bchain.Block, error) { 96 glog.V(1).Info("rpc: getblock (verbosity=2) ", hash) 97 98 res := btc.ResGetBlockFull{} 99 req := btc.CmdGetBlock{Method: "getblock"} 100 req.Params.BlockHash = hash 101 req.Params.Verbosity = 2 102 err := f.Call(&req, &res) 103 104 if err != nil { 105 return nil, errors.Annotatef(err, "hash %v", hash) 106 } 107 if res.Error != nil { 108 if btc.IsErrBlockNotFound(res.Error) { 109 return nil, bchain.ErrBlockNotFound 110 } 111 return nil, errors.Annotatef(res.Error, "hash %v", hash) 112 } 113 114 for i := range res.Result.Txs { 115 tx := &res.Result.Txs[i] 116 for j := range tx.Vout { 117 vout := &tx.Vout[j] 118 // convert vout.JsonValue to big.Int and clear it, it is only temporary value used for unmarshal 119 vout.ValueSat, err = f.Parser.AmountToBigInt(vout.JsonValue) 120 if err != nil { 121 return nil, err 122 } 123 vout.JsonValue = "" 124 } 125 } 126 127 return &res.Result, nil 128 } 129 130 // GetTransactionForMempool returns a transaction by the transaction ID. 131 // It could be optimized for mempool, i.e. without block time and confirmations 132 func (f *FloRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) { 133 return f.GetTransaction(txid) 134 }