Litecoin: Fix tx parsing related changes in backend 0.21.2, #770

This commit is contained in:
Martin Boehm 2022-05-22 12:05:39 +02:00
parent a22bf37788
commit b9e3cf8c9b
2 changed files with 58 additions and 1 deletions

View File

@ -728,10 +728,10 @@ func (b *BitcoinRPC) GetTransaction(txid string) (*bchain.Tx, error) {
return nil, err
}
tx, err := b.Parser.ParseTxFromJson(r)
tx.CoinSpecificData = r
if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid)
}
tx.CoinSpecificData = r
return tx, nil
}

View File

@ -1,8 +1,12 @@
package litecoin
import (
"encoding/json"
"github.com/golang/glog"
"github.com/martinboehm/btcd/wire"
"github.com/martinboehm/btcutil/chaincfg"
"github.com/trezor/blockbook/bchain"
"github.com/trezor/blockbook/bchain/coins/btc"
)
@ -68,3 +72,56 @@ func GetChainParams(chain string) *chaincfg.Params {
return &MainNetParams
}
}
// fallbackTx is used to handle situation when Litecoin mainnet returns
// for certain transactions Version 4294967295 instead of -1, which causes json unmarshal error
type fallbackTx struct {
Hex string `json:"hex"`
Txid string `json:"txid"`
Version uint32 `json:"version"`
LockTime uint32 `json:"locktime"`
Vin []bchain.Vin `json:"vin"`
Vout []bchain.Vout `json:"vout"`
BlockHeight uint32 `json:"blockHeight,omitempty"`
// BlockHash string `json:"blockhash,omitempty"`
Confirmations uint32 `json:"confirmations,omitempty"`
Time int64 `json:"time,omitempty"`
Blocktime int64 `json:"blocktime,omitempty"`
}
// ParseTxFromJson parses JSON message containing transaction and returns Tx struct
func (p *LitecoinParser) ParseTxFromJson(msg json.RawMessage) (*bchain.Tx, error) {
var tx bchain.Tx
err := json.Unmarshal(msg, &tx)
if err != nil {
var fTx fallbackTx
fErr := json.Unmarshal(msg, &fTx)
// log warning with Txid possibly parsed using fallbackTx
glog.Warningf("ParseTxFromJson txid %s to bchain.Tx error %v, using fallback method", fTx.Txid, err)
if fErr != nil {
return nil, fErr
}
tx.Hex = fTx.Hex
tx.Txid = fTx.Txid
tx.Version = int32(fTx.Version)
tx.LockTime = fTx.LockTime
tx.Vin = fTx.Vin
tx.Vout = fTx.Vout
tx.BlockHeight = fTx.BlockHeight
tx.Confirmations = fTx.Confirmations
tx.Time = fTx.Time
tx.Blocktime = fTx.Blocktime
}
for i := range tx.Vout {
vout := &tx.Vout[i]
// convert vout.JsonValue to big.Int and clear it, it is only temporary value used for unmarshal
vout.ValueSat, err = p.AmountToBigInt(vout.JsonValue)
if err != nil {
return nil, err
}
vout.JsonValue = ""
}
return &tx, nil
}