From a22bf37788ed578722e4cfa95e9996433a81caa2 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Sat, 21 May 2022 13:09:31 +0200 Subject: [PATCH] Litecoin: Adapt to MWEB related changes in backend 0.21.2, #770 --- bchain/coins/litecoin/litecoinrpc.go | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/bchain/coins/litecoin/litecoinrpc.go b/bchain/coins/litecoin/litecoinrpc.go index ad0ec462..ca62445b 100644 --- a/bchain/coins/litecoin/litecoinrpc.go +++ b/bchain/coins/litecoin/litecoinrpc.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/golang/glog" + "github.com/juju/errors" "github.com/trezor/blockbook/bchain" "github.com/trezor/blockbook/bchain/coins/btc" ) @@ -56,3 +57,56 @@ func (b *LitecoinRPC) Initialize() error { return nil } + +// GetBlock returns block with given hash. +// Litecoin cannot use optimized BitcoinRPC.GetBlock since v 0.21.2, +// which introduced MWEB fields to the transaction data and made the serialized block incompatible with Bitcoin wire protocol +func (b *LitecoinRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { + var err error + if hash == "" && height > 0 { + hash, err = b.GetBlockHash(height) + if err != nil { + return nil, err + } + } + + glog.V(1).Info("rpc: getblock (verbosity=1) ", hash) + + res := btc.ResGetBlockThin{} + req := btc.CmdGetBlock{Method: "getblock"} + req.Params.BlockHash = hash + req.Params.Verbosity = 1 + err = b.Call(&req, &res) + + if err != nil { + return nil, errors.Annotatef(err, "hash %v", hash) + } + if res.Error != nil { + return nil, errors.Annotatef(res.Error, "hash %v", hash) + } + + txs := make([]bchain.Tx, 0, len(res.Result.Txids)) + for _, txid := range res.Result.Txids { + tx, err := b.GetTransaction(txid) + if err != nil { + if err == bchain.ErrTxNotFound { + glog.Errorf("rpc: getblock: skipping transaction in block %s due error: %s", hash, err) + continue + } + return nil, err + } + txs = append(txs, *tx) + } + block := &bchain.Block{ + BlockHeader: res.Result.BlockHeader, + Txs: txs, + } + return block, nil +} + +// GetTransactionForMempool returns a transaction by the transaction ID +// Litecoin cannot use optimized BitcoinRPC.GetTransactionForMempool since v 0.21.2, +// which introduced MWEB fields to the transaction data and made the serialized transaction incompatible with Bitcoin wire protocol +func (b *LitecoinRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) { + return b.GetTransaction(txid) +}