herald.go/db/prefixes/prefixes_test.go

333 lines
8.8 KiB
Go
Raw Normal View History

package prefixes_test
import (
"bytes"
"encoding/csv"
"encoding/hex"
"fmt"
"log"
"os"
"testing"
dbpkg "github.com/lbryio/hub/db"
2022-01-19 18:30:05 +01:00
prefixes "github.com/lbryio/hub/db/prefixes"
"github.com/linxGnu/grocksdb"
)
2022-02-03 20:18:00 +01:00
func testInit(filePath string) (*grocksdb.DB, [][]string, func(), *grocksdb.ColumnFamilyHandle) {
log.Println(filePath)
file, err := os.Open(filePath)
if err != nil {
log.Println(err)
}
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
log.Println(err)
}
2022-02-03 20:18:00 +01:00
columnFamily := records[0][0]
records = records[1:]
2022-01-10 07:05:41 +01:00
// wOpts := grocksdb.NewDefaultWriteOptions()
opts := grocksdb.NewDefaultOptions()
opts.SetCreateIfMissing(true)
db, err := grocksdb.OpenDb(opts, "tmp")
if err != nil {
log.Println(err)
}
2022-02-03 20:18:00 +01:00
handle, err := db.CreateColumnFamily(opts, columnFamily)
if err != nil {
log.Println(err)
}
2022-01-10 07:05:41 +01:00
toDefer := func() {
db.Close()
err = os.RemoveAll("./tmp")
if err != nil {
log.Println(err)
}
2022-01-10 07:05:41 +01:00
}
2022-02-03 20:18:00 +01:00
return db, records, toDefer, handle
2022-01-10 07:05:41 +01:00
}
2022-01-15 13:01:43 +01:00
func testGeneric(filePath string, prefix byte, numPartials int) func(*testing.T) {
return func(t *testing.T) {
wOpts := grocksdb.NewDefaultWriteOptions()
2022-02-03 20:18:00 +01:00
db, records, toDefer, handle := testInit(filePath)
2022-01-15 13:01:43 +01:00
defer toDefer()
for _, record := range records {
key, err := hex.DecodeString(record[0])
if err != nil {
log.Println(err)
}
val, err := hex.DecodeString(record[1])
if err != nil {
log.Println(err)
}
2022-02-03 20:18:00 +01:00
// db.Put(wOpts, key, val)
db.PutCF(wOpts, handle, key, val)
2022-01-15 13:01:43 +01:00
}
// test prefix
options := dbpkg.NewIterateOptions().WithPrefix([]byte{prefix}).WithIncludeValue(true)
2022-02-03 20:18:00 +01:00
options = options.WithCfHandle(handle)
ch := dbpkg.IterCF(db, options)
2022-01-15 13:01:43 +01:00
var i = 0
for kv := range ch {
// log.Println(kv.Key)
2022-01-19 18:30:05 +01:00
gotKey, err := prefixes.PackGenericKey(prefix, kv.Key)
2022-01-15 13:01:43 +01:00
if err != nil {
log.Println(err)
}
for j := 1; j <= numPartials; j++ {
2022-01-19 18:30:05 +01:00
keyPartial, _ := prefixes.PackPartialGenericKey(prefix, kv.Key, j)
2022-01-15 13:01:43 +01:00
// Check pack partial for sanity
if !bytes.HasPrefix(gotKey, keyPartial) {
t.Errorf("%+v should be prefix of %+v\n", keyPartial, gotKey)
}
}
2022-01-19 18:30:05 +01:00
got, err := prefixes.PackGenericValue(prefix, kv.Value)
2022-01-15 13:01:43 +01:00
if err != nil {
log.Println(err)
}
wantKey, err := hex.DecodeString(records[i][0])
if err != nil {
log.Println(err)
}
want, err := hex.DecodeString(records[i][1])
if err != nil {
log.Println(err)
}
if !bytes.Equal(gotKey, wantKey) {
t.Errorf("gotKey: %+v, wantKey: %+v\n", got, want)
}
if !bytes.Equal(got, want) {
t.Errorf("got: %+v, want: %+v\n", got, want)
}
i++
}
// Test start / stop
start, err := hex.DecodeString(records[0][0])
if err != nil {
log.Println(err)
}
2022-01-15 22:07:19 +01:00
numRecords := i
// var numRecords = 9
// if prefix == prefixes.Undo || prefix == prefixes.DBState {
// numRecords = 1
// }
stop, err := hex.DecodeString(records[numRecords-1][0])
2022-01-15 13:01:43 +01:00
if err != nil {
log.Println(err)
}
options2 := dbpkg.NewIterateOptions().WithStart(start).WithStop(stop).WithIncludeValue(true)
2022-02-03 20:18:00 +01:00
options2 = options2.WithCfHandle(handle)
ch2 := dbpkg.IterCF(db, options2)
2022-01-15 13:01:43 +01:00
i = 0
for kv := range ch2 {
2022-01-19 18:30:05 +01:00
got, err := prefixes.PackGenericValue(prefix, kv.Value)
2022-01-15 13:01:43 +01:00
if err != nil {
log.Println(err)
}
want, err := hex.DecodeString(records[i][1])
if err != nil {
log.Println(err)
}
if !bytes.Equal(got, want) {
t.Errorf("got: %+v, want: %+v\n", got, want)
}
i++
2022-01-15 07:14:30 +01:00
}
}
2022-01-15 13:01:43 +01:00
}
2022-01-15 07:14:30 +01:00
2022-01-15 22:07:19 +01:00
func TestSupportAmount(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.SupportAmount)
testGeneric(filePath, prefixes.SupportAmount, 1)(t)
2022-01-15 22:07:19 +01:00
}
func TestChannelCount(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ChannelCount)
testGeneric(filePath, prefixes.ChannelCount, 1)(t)
2022-01-15 22:07:19 +01:00
}
func TestDBState(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.DBState)
testGeneric(filePath, prefixes.DBState, 0)(t)
2022-01-15 22:07:19 +01:00
}
func TestBlockTxs(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.BlockTXs)
testGeneric(filePath, prefixes.BlockTXs, 1)(t)
2022-01-15 22:07:19 +01:00
}
func TestTxCount(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.TxCount)
testGeneric(filePath, prefixes.TxCount, 1)(t)
2022-01-15 22:07:19 +01:00
}
func TestTxHash(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.TxHash)
testGeneric(filePath, prefixes.TxHash, 1)(t)
2022-01-15 22:07:19 +01:00
}
func TestTxNum(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.TxNum)
testGeneric(filePath, prefixes.TxNum, 1)(t)
2022-01-15 22:07:19 +01:00
}
2022-01-15 13:01:43 +01:00
func TestTx(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.Tx)
testGeneric(filePath, prefixes.Tx, 1)(t)
2022-01-15 13:01:43 +01:00
}
2022-01-15 07:14:30 +01:00
2022-01-15 13:01:43 +01:00
func TestHashXHistory(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.HashXHistory)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.HashXHistory, 2)(t)
2022-01-15 07:14:30 +01:00
}
2022-01-15 02:42:57 +01:00
func TestUndo(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.Undo)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.Undo, 1)(t)
2022-01-15 02:42:57 +01:00
}
2022-01-15 02:18:51 +01:00
func TestBlockHash(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.BlockHash)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.BlockHash, 1)(t)
2022-01-15 02:18:51 +01:00
}
2022-01-15 00:41:50 +01:00
func TestBlockHeader(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.Header)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.Header, 1)(t)
2022-01-15 00:41:50 +01:00
}
2022-01-14 23:40:08 +01:00
func TestClaimToTXO(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ClaimToTXO)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ClaimToTXO, 1)(t)
}
2022-01-14 23:40:08 +01:00
2022-01-15 13:01:43 +01:00
func TestTXOToClaim(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.TXOToClaim)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.TXOToClaim, 2)(t)
}
2022-01-14 23:40:08 +01:00
2022-01-15 13:01:43 +01:00
func TestClaimShortID(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ClaimShortIdPrefix)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ClaimShortIdPrefix, 3)(t)
}
2022-01-14 23:40:08 +01:00
2022-01-15 13:01:43 +01:00
func TestClaimToChannel(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ClaimToChannel)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ClaimToChannel, 3)(t)
}
2022-01-14 23:40:08 +01:00
2022-01-15 13:01:43 +01:00
func TestChannelToClaim(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ChannelToClaim)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ChannelToClaim, 4)(t)
2022-01-14 23:40:08 +01:00
}
2022-01-15 13:01:43 +01:00
func TestClaimToSupport(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ClaimToSupport)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ClaimToSupport, 3)(t)
}
2022-01-14 22:19:34 +01:00
2022-01-15 13:01:43 +01:00
func TestSupportToClaim(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.SupportToClaim)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.SupportToClaim, 2)(t)
}
2022-01-14 22:19:34 +01:00
2022-01-15 13:01:43 +01:00
func TestClaimExpiration(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ClaimExpiration)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ClaimExpiration, 3)(t)
}
2022-01-14 22:19:34 +01:00
2022-01-15 13:01:43 +01:00
func TestClaimTakeover(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ClaimTakeover)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ClaimTakeover, 1)(t)
}
2022-01-14 22:19:34 +01:00
2022-01-15 13:01:43 +01:00
func TestPendingActivation(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.PendingActivation)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.PendingActivation, 4)(t)
}
2022-01-14 22:19:34 +01:00
2022-01-15 13:01:43 +01:00
func TestActivated(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ActivatedClaimAndSupport)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ActivatedClaimAndSupport, 3)(t)
}
2022-01-14 22:19:34 +01:00
2022-01-15 13:01:43 +01:00
func TestActiveAmount(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ActiveAmount)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ActiveAmount, 5)(t)
2022-01-14 22:19:34 +01:00
}
2022-01-15 13:01:43 +01:00
func TestEffectiveAmount(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.EffectiveAmount)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.EffectiveAmount, 4)(t)
}
2022-01-14 20:31:07 +01:00
2022-01-15 13:01:43 +01:00
func TestRepost(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.Repost)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.Repost, 1)(t)
}
2022-01-14 20:31:07 +01:00
2022-01-15 13:01:43 +01:00
func TestRepostedClaim(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.RepostedClaim)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.RepostedClaim, 3)(t)
}
2022-01-14 20:31:07 +01:00
2022-01-15 13:01:43 +01:00
func TestClaimDiff(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.ClaimDiff)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.ClaimDiff, 1)(t)
}
func TestUTXO(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.UTXO)
2022-01-15 13:01:43 +01:00
testGeneric(filePath, prefixes.UTXO, 1)(t)
}
func TestHashXUTXO(t *testing.T) {
2022-02-08 18:50:37 +01:00
filePath := fmt.Sprintf("../../testdata/%c.csv", prefixes.HashXUTXO)
2022-01-15 22:07:19 +01:00
testGeneric(filePath, prefixes.HashXUTXO, 3)(t)
}
func TestUTXOKey_String(t *testing.T) {
tests := []struct {
name string
prefix []byte
hashx []byte
txnum uint32
nout uint16
want string
}{
{
name: "Converts to string",
prefix: []byte("u"),
hashx: []byte("AAAAAAAAAA"),
txnum: 0,
nout: 0,
want: "*prefixes.UTXOKey(hashX=41414141414141414141, tx_num=0, nout=0)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key := &prefixes.UTXOKey{
Prefix: tt.prefix,
HashX: tt.hashx,
TxNum: tt.txnum,
Nout: tt.nout,
}
got := fmt.Sprint(key)
log.Println(got)
if got != tt.want {
t.Errorf("got: %s, want: %s\n", got, tt.want)
}
})
}
}