From 293a3f685eb0211ff2bdacfb09ebfba6abf4e06b Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Thu, 1 Sep 2022 14:21:53 -0500 Subject: [PATCH] Fix some DB logic and add tests (blockchain_test.go). --- db/db.go | 10 ++- db/db_get.go | 4 +- server/blockchain_test.go | 182 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 server/blockchain_test.go diff --git a/db/db.go b/db/db.go index 76ae41f..164a9d0 100644 --- a/db/db.go +++ b/db/db.go @@ -407,7 +407,7 @@ func Iter(db *grocksdb.DB, opts *IterOptions) <-chan *prefixes.PrefixRowKV { } func (db *ReadOnlyDBColumnFamily) selectFrom(prefix []byte, startKey, stopKey prefixes.BaseKey) ([]*IterOptions, error) { - handle, err := db.EnsureHandle(prefixes.HashXHistory) + handle, err := db.EnsureHandle(prefix[0]) if err != nil { return nil, err } @@ -423,11 +423,14 @@ func (db *ReadOnlyDBColumnFamily) selectFrom(prefix []byte, startKey, stopKey pr func iterate(db *grocksdb.DB, opts []*IterOptions) <-chan []*prefixes.PrefixRowKV { out := make(chan []*prefixes.PrefixRowKV) routine := func() { - for _, o := range opts { + for i, o := range opts { + j := 0 for kv := range IterCF(db, o) { row := make([]*prefixes.PrefixRowKV, 0, 1) row = append(row, kv) + log.Debugf("iterate[%v][%v] %#v", i, j, kv) out <- row + j++ } } close(out) @@ -450,6 +453,9 @@ func innerJoin(db *grocksdb.DB, in <-chan []*prefixes.PrefixRowKV, selectFn func row := make([]*prefixes.PrefixRowKV, 0, len(kvs)+1) row = append(row, kvs...) row = append(row, kv...) + for i, kv := range row { + log.Debugf("row[%v] %#v", i, kv) + } out <- row } } diff --git a/db/db_get.go b/db/db_get.go index ac49ad9..13771f0 100644 --- a/db/db_get.go +++ b/db/db_get.go @@ -91,7 +91,7 @@ func (db *ReadOnlyDBColumnFamily) GetHeaders(height uint32, count uint32) ([][11 return nil, err } - startKeyRaw := prefixes.NewHeaderKey(0).PackKey() + startKeyRaw := prefixes.NewHeaderKey(height).PackKey() endKeyRaw := prefixes.NewHeaderKey(height + count).PackKey() options := NewIterateOptions().WithPrefix([]byte{prefixes.Header}).WithCfHandle(handle) options = options.WithIncludeKey(false).WithIncludeValue(true) //.WithIncludeStop(true) @@ -221,7 +221,7 @@ func (db *ReadOnlyDBColumnFamily) GetHistory(hashX []byte) ([]TxInfo, error) { Height: 0, } endKey := &prefixes.HashXHistoryKey{ - Prefix: []byte{prefixes.UTXO}, + Prefix: []byte{prefixes.HashXHistory}, HashX: hashX, Height: math.MaxUint32, } diff --git a/server/blockchain_test.go b/server/blockchain_test.go new file mode 100644 index 0000000..32cd9ac --- /dev/null +++ b/server/blockchain_test.go @@ -0,0 +1,182 @@ +package server + +import ( + "encoding/json" + "testing" + + "github.com/lbryio/herald.go/db" + "github.com/lbryio/lbcd/chaincfg" +) + +func TestGetChunk(t *testing.T) { + dbPath := "/Users/swdev1/hub/scribe_db.599529/lbry-rocksdb" + // dbPath := "/mnt/d/data/snapshot_1072108/lbry-rocksdb/" + secondaryPath := "asdf" + db, toDefer, err := db.GetProdDB(dbPath, secondaryPath) + defer toDefer() + if err != nil { + t.Skip("DB not found") + t.Error(err) + return + } + + s := &Server{ + DB: db, + Chain: &chaincfg.MainNetParams, + } + + for index := 0; index < 10; index++ { + req := blockGetChunkReq(index) + resp, err := (&req).Handle(s) + if err != nil { + t.Errorf("index: %v handler err: %v", index, err) + } + marshalled, err := json.MarshalIndent(resp, "", " ") + if err != nil { + t.Errorf("index: %v unmarshal err: %v", index, err) + } + t.Logf("index: %v resp: %v", index, string(marshalled)) + if len(*resp) != (CHUNK_SIZE * HEADER_SIZE * 2) { + t.Errorf("index: %v bad length: %v", index, len(*resp)) + } + } +} + +func TestGetHeader(t *testing.T) { + dbPath := "/Users/swdev1/hub/scribe_db.599529/lbry-rocksdb" + // dbPath := "/mnt/d/data/snapshot_1072108/lbry-rocksdb/" + secondaryPath := "asdf" + db, toDefer, err := db.GetProdDB(dbPath, secondaryPath) + defer toDefer() + if err != nil { + t.Skip("DB not found") + t.Error(err) + return + } + + s := &Server{ + DB: db, + Chain: &chaincfg.MainNetParams, + } + + for height := 1000; height < 1010; height++ { + req := blockGetHeaderReq(height) + resp, err := (&req).Handle(s) + if err != nil { + t.Errorf("height: %v handler err: %v", height, err) + } + marshalled, err := json.MarshalIndent(resp, "", " ") + if err != nil { + t.Errorf("height: %v unmarshal err: %v", height, err) + } + t.Logf("height: %v resp: %v", height, string(marshalled)) + } +} + +func TestGetBalance(t *testing.T) { + dbPath := "/Users/swdev1/hub/scribe_db.599529/lbry-rocksdb" + // dbPath := "/mnt/d/data/snapshot_1072108/lbry-rocksdb/" + secondaryPath := "asdf" + db, toDefer, err := db.GetProdDB(dbPath, secondaryPath) + defer toDefer() + if err != nil { + t.Skip("DB not found") + t.Error(err) + return + } + + s := &Server{ + DB: db, + Chain: &chaincfg.MainNetParams, + } + + addrs := []string{ + "bCoyqs8Pv4pss5EbNuyuokkdkCqEpDoHmG", + "bJr6cLth1UmR7wJ14BMc7ch73xBEEV77fV", + } + + for _, addr := range addrs { + req := addressGetBalanceReq{addr} + resp, err := (&req).Handle(s) + if err != nil { + t.Errorf("address: %v handler err: %v", addr, err) + } + marshalled, err := json.MarshalIndent(resp, "", " ") + if err != nil { + t.Errorf("address: %v unmarshal err: %v", addr, err) + } + t.Logf("address: %v resp: %v", addr, string(marshalled)) + } +} + +func TestGetHistory(t *testing.T) { + dbPath := "/Users/swdev1/hub/scribe_db.599529/lbry-rocksdb" + // dbPath := "/mnt/d/data/snapshot_1072108/lbry-rocksdb/" + secondaryPath := "asdf" + db, toDefer, err := db.GetProdDB(dbPath, secondaryPath) + defer toDefer() + if err != nil { + t.Skip("DB not found") + t.Error(err) + return + } + + s := &Server{ + DB: db, + Chain: &chaincfg.MainNetParams, + } + + addrs := []string{ + "bCoyqs8Pv4pss5EbNuyuokkdkCqEpDoHmG", + "bJr6cLth1UmR7wJ14BMc7ch73xBEEV77fV", + } + + for _, addr := range addrs { + req := addressGetHistoryReq{addr} + resp, err := (&req).Handle(s) + if err != nil { + t.Errorf("address: %v handler err: %v", addr, err) + } + marshalled, err := json.MarshalIndent(resp, "", " ") + if err != nil { + t.Errorf("address: %v unmarshal err: %v", addr, err) + } + t.Logf("address: %v resp: %v", addr, string(marshalled)) + } +} + +func TestListUnspent(t *testing.T) { + dbPath := "/Users/swdev1/hub/scribe_db.599529/lbry-rocksdb" + // dbPath := "/mnt/d/data/snapshot_1072108/lbry-rocksdb/" + secondaryPath := "asdf" + db, toDefer, err := db.GetProdDB(dbPath, secondaryPath) + defer toDefer() + if err != nil { + t.Skip("DB not found") + t.Error(err) + return + } + + s := &Server{ + DB: db, + Chain: &chaincfg.MainNetParams, + } + + addrs := []string{ + "bCoyqs8Pv4pss5EbNuyuokkdkCqEpDoHmG", + "bJr6cLth1UmR7wJ14BMc7ch73xBEEV77fV", + } + + for _, addr := range addrs { + req := addressListUnspentReq{addr} + resp, err := (&req).Handle(s) + if err != nil { + t.Errorf("address: %v handler err: %v", addr, err) + } + marshalled, err := json.MarshalIndent(resp, "", " ") + if err != nil { + t.Errorf("address: %v unmarshal err: %v", addr, err) + } + t.Logf("address: %v resp: %v", addr, string(marshalled)) + } +}