HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus().

This commit is contained in:
Jonathan Moody 2022-09-29 15:13:30 -05:00
parent a91e9f82ff
commit 1eb645a0b9
2 changed files with 51 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package db
// db_get.go contains the basic access functions to the database. // db_get.go contains the basic access functions to the database.
import ( import (
"crypto/sha256"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log" "log"
@ -267,11 +268,17 @@ func (db *ReadOnlyDBColumnFamily) GetHistory(hashX []byte) ([]TxInfo, error) {
} }
func (db *ReadOnlyDBColumnFamily) GetStatus(hashX []byte) ([]byte, error) { func (db *ReadOnlyDBColumnFamily) GetStatus(hashX []byte) ([]byte, error) {
// Lookup in HashXMempoolStatus first.
status, err := db.getMempoolStatus(hashX)
if err == nil && status != nil {
return status, err
}
// No indexed mempool status. Lookup in HashXStatus second.
handle, err := db.EnsureHandle(prefixes.HashXStatus) handle, err := db.EnsureHandle(prefixes.HashXStatus)
if err != nil { if err != nil {
return nil, err return nil, err
} }
key := &prefixes.HashXStatusKey{ key := &prefixes.HashXStatusKey{
Prefix: []byte{prefixes.HashXStatus}, Prefix: []byte{prefixes.HashXStatus},
HashX: hashX, HashX: hashX,
@ -279,15 +286,51 @@ func (db *ReadOnlyDBColumnFamily) GetStatus(hashX []byte) ([]byte, error) {
rawKey := key.PackKey() rawKey := key.PackKey()
slice, err := db.DB.GetCF(db.Opts, handle, rawKey) slice, err := db.DB.GetCF(db.Opts, handle, rawKey)
defer slice.Free() defer slice.Free()
if err == nil && slice.Size() > 0 {
rawValue := make([]byte, len(slice.Data()))
copy(rawValue, slice.Data())
value := prefixes.HashXStatusValue{}
value.UnpackValue(rawValue)
return value.Status, nil
}
// No indexed status. Fall back to enumerating HashXHistory.
txs, err := db.GetHistory(hashX)
if err != nil {
return nil, err
}
hash := sha256.New()
for _, tx := range txs {
hash.Write([]byte(fmt.Sprintf("%s:%d:", tx.TxHash.String(), tx.Height)))
}
// TODO: Mempool history
return hash.Sum(nil), err
}
func (db *ReadOnlyDBColumnFamily) getMempoolStatus(hashX []byte) ([]byte, error) {
handle, err := db.EnsureHandle(prefixes.HashXMempoolStatus)
if err != nil {
return nil, err
}
key := &prefixes.HashXMempoolStatusKey{
Prefix: []byte{prefixes.HashXMempoolStatus},
HashX: hashX,
}
rawKey := key.PackKey()
slice, err := db.DB.GetCF(db.Opts, handle, rawKey)
defer slice.Free()
if err != nil { if err != nil {
return nil, err return nil, err
} else if slice.Size() == 0 { } else if slice.Size() == 0 {
return nil, err return nil, nil
} }
rawValue := make([]byte, len(slice.Data())) rawValue := make([]byte, len(slice.Data()))
copy(rawValue, slice.Data()) copy(rawValue, slice.Data())
return rawValue, nil value := prefixes.HashXMempoolStatusValue{}
value.UnpackValue(rawValue)
return value.Status, nil
} }
// GetStreamsAndChannelRepostedByChannelHashes returns a map of streams and channel hashes that are reposted by the given channel hashes. // GetStreamsAndChannelRepostedByChannelHashes returns a map of streams and channel hashes that are reposted by the given channel hashes.

View file

@ -443,7 +443,11 @@ func TestAddressSubscribe(t *testing.T) {
script, _ := txscript.PayToAddrScript(address) script, _ := txscript.PayToAddrScript(address)
note := hashXNotification{} note := hashXNotification{}
copy(note.hashX[:], hashXScript(script, sm.chain)) copy(note.hashX[:], hashXScript(script, sm.chain))
note.status = append(note.status, []byte("fake status bytes for addr2")...) status, err := hex.DecodeString((*resp1)[1])
if err != nil {
t.Errorf("decode err: %v", err)
}
note.status = append(note.status, []byte(status)...)
t.Logf("sending notification") t.Logf("sending notification")
sm.doNotify(note) sm.doNotify(note)