Check if unindexed confirmed tx height exists

This commit is contained in:
migwi 2019-08-07 23:16:59 +03:00 committed by Martin
parent 0f39657006
commit d4a7fcabd9
2 changed files with 20 additions and 12 deletions

View File

@ -76,12 +76,13 @@ type Vout struct {
// Tx is blockchain transaction // Tx is blockchain transaction
// unnecessary fields are commented out to avoid overhead // unnecessary fields are commented out to avoid overhead
type Tx struct { type Tx struct {
Hex string `json:"hex"` Hex string `json:"hex"`
Txid string `json:"txid"` Txid string `json:"txid"`
Version int32 `json:"version"` Version int32 `json:"version"`
LockTime uint32 `json:"locktime"` LockTime uint32 `json:"locktime"`
Vin []Vin `json:"vin"` Vin []Vin `json:"vin"`
Vout []Vout `json:"vout"` Vout []Vout `json:"vout"`
BlockHeight uint32 `json:"blockHeight,omitempty"`
// BlockHash string `json:"blockhash,omitempty"` // BlockHash string `json:"blockhash,omitempty"`
Confirmations uint32 `json:"confirmations,omitempty"` Confirmations uint32 `json:"confirmations,omitempty"`
Time int64 `json:"time,omitempty"` Time int64 `json:"time,omitempty"`

View File

@ -65,13 +65,20 @@ func (c *TxCache) GetTransaction(txid string) (*bchain.Tx, uint32, error) {
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
// the transaction may me not yet indexed, in that case get the height from the backend switch {
if ta == nil { case ta == nil:
h, err = c.chain.GetBestBlockHeight() // the transaction may not yet be indexed, in that case:
if err != nil { if tx.BlockHeight > 0 {
return nil, 0, err // Check if the tx height value is set.
h = tx.BlockHeight
} else {
// Get the height from the backend's bestblock.
h, err = c.chain.GetBestBlockHeight()
if err != nil {
return nil, 0, err
}
} }
} else { default:
h = ta.Height h = ta.Height
} }
} else if c.chainType == bchain.ChainEthereumType { } else if c.chainType == bchain.ChainEthereumType {