Fix some DB logic and add tests (blockchain_test.go).

This commit is contained in:
Jonathan Moody 2022-09-01 14:21:53 -05:00
parent f55a5ed777
commit 293a3f685e
3 changed files with 192 additions and 4 deletions

View file

@ -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
}
}

View file

@ -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,
}

182
server/blockchain_test.go Normal file
View file

@ -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))
}
}