From 1eb645a0b95dc0420e8de47e860748aed5729111 Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Thu, 29 Sep 2022 15:13:30 -0500 Subject: [PATCH] HashXStatus, HashXMempoolStatus not populated by default. Fix GetStatus(). --- db/db_get.go | 49 +++++++++++++++++++++++++++++-- server/jsonrpc_blockchain_test.go | 6 +++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/db/db_get.go b/db/db_get.go index e988724..d630c90 100644 --- a/db/db_get.go +++ b/db/db_get.go @@ -3,6 +3,7 @@ package db // db_get.go contains the basic access functions to the database. import ( + "crypto/sha256" "encoding/hex" "fmt" "log" @@ -267,11 +268,17 @@ func (db *ReadOnlyDBColumnFamily) GetHistory(hashX []byte) ([]TxInfo, 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) if err != nil { return nil, err } - key := &prefixes.HashXStatusKey{ Prefix: []byte{prefixes.HashXStatus}, HashX: hashX, @@ -279,15 +286,51 @@ func (db *ReadOnlyDBColumnFamily) GetStatus(hashX []byte) ([]byte, error) { rawKey := key.PackKey() slice, err := db.DB.GetCF(db.Opts, handle, rawKey) 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 { return nil, err } else if slice.Size() == 0 { - return nil, err + return nil, nil } rawValue := make([]byte, len(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. diff --git a/server/jsonrpc_blockchain_test.go b/server/jsonrpc_blockchain_test.go index e58965a..2c94ebb 100644 --- a/server/jsonrpc_blockchain_test.go +++ b/server/jsonrpc_blockchain_test.go @@ -443,7 +443,11 @@ func TestAddressSubscribe(t *testing.T) { script, _ := txscript.PayToAddrScript(address) note := hashXNotification{} 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") sm.doNotify(note)