github.com/trezor/blockbook@v0.4.1-0.20240328132726-e9a08582ee2c/bchain/coins/litecoin/litecoinrpc.go (about) 1 package litecoin 2 3 import ( 4 "encoding/json" 5 6 "github.com/golang/glog" 7 "github.com/juju/errors" 8 "github.com/trezor/blockbook/bchain" 9 "github.com/trezor/blockbook/bchain/coins/btc" 10 ) 11 12 // LitecoinRPC is an interface to JSON-RPC bitcoind service. 13 type LitecoinRPC struct { 14 *btc.BitcoinRPC 15 } 16 17 // NewLitecoinRPC returns new LitecoinRPC instance. 18 func NewLitecoinRPC(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 := &LitecoinRPC{ 25 b.(*btc.BitcoinRPC), 26 } 27 s.RPCMarshaler = btc.JSONMarshalerV2{} 28 s.ChainConfig.SupportsEstimateFee = false 29 30 return s, nil 31 } 32 33 // Initialize initializes LitecoinRPC instance. 34 func (b *LitecoinRPC) 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 = NewLitecoinParser(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 returns block with given hash. 62 // Litecoin cannot use optimized BitcoinRPC.GetBlock since v 0.21.2, 63 // which introduced MWEB fields to the transaction data and made the serialized block incompatible with Bitcoin wire protocol 64 func (b *LitecoinRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { 65 var err error 66 if hash == "" && height > 0 { 67 hash, err = b.GetBlockHash(height) 68 if err != nil { 69 return nil, err 70 } 71 } 72 73 glog.V(1).Info("rpc: getblock (verbosity=1) ", hash) 74 75 res := btc.ResGetBlockThin{} 76 req := btc.CmdGetBlock{Method: "getblock"} 77 req.Params.BlockHash = hash 78 req.Params.Verbosity = 1 79 err = b.Call(&req, &res) 80 81 if err != nil { 82 return nil, errors.Annotatef(err, "hash %v", hash) 83 } 84 if res.Error != nil { 85 return nil, errors.Annotatef(res.Error, "hash %v", hash) 86 } 87 88 txs := make([]bchain.Tx, 0, len(res.Result.Txids)) 89 for _, txid := range res.Result.Txids { 90 tx, err := b.GetTransaction(txid) 91 if err != nil { 92 if err == bchain.ErrTxNotFound { 93 glog.Errorf("rpc: getblock: skipping transaction in block %s due error: %s", hash, err) 94 continue 95 } 96 return nil, err 97 } 98 txs = append(txs, *tx) 99 } 100 block := &bchain.Block{ 101 BlockHeader: res.Result.BlockHeader, 102 Txs: txs, 103 } 104 return block, nil 105 } 106 107 // GetTransactionForMempool returns a transaction by the transaction ID 108 // Litecoin cannot use optimized BitcoinRPC.GetTransactionForMempool since v 0.21.2, 109 // which introduced MWEB fields to the transaction data and made the serialized transaction incompatible with Bitcoin wire protocol 110 func (b *LitecoinRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) { 111 return b.GetTransaction(txid) 112 }