2022-01-10 05:46:52 +01:00
|
|
|
package prefixes_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
dbpkg "github.com/lbryio/hub/db"
|
|
|
|
"github.com/lbryio/hub/db/prefixes"
|
|
|
|
"github.com/linxGnu/grocksdb"
|
|
|
|
)
|
|
|
|
|
2022-01-10 07:05:41 +01:00
|
|
|
func testInit(filePath string) (*grocksdb.DB, [][]string, func()) {
|
2022-01-10 05:46:52 +01:00
|
|
|
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-01-10 07:05:41 +01:00
|
|
|
// wOpts := grocksdb.NewDefaultWriteOptions()
|
2022-01-10 05:46:52 +01:00
|
|
|
opts := grocksdb.NewDefaultOptions()
|
|
|
|
opts.SetCreateIfMissing(true)
|
|
|
|
db, err := grocksdb.OpenDb(opts, "tmp")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-01-10 07:05:41 +01:00
|
|
|
toDefer := func() {
|
2022-01-10 05:46:52 +01:00
|
|
|
db.Close()
|
|
|
|
err = os.RemoveAll("./tmp")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-01-10 07:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return db, records, toDefer
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
db, records, toDefer := testInit(filePath)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
db.Put(wOpts, key, val)
|
|
|
|
}
|
|
|
|
// test prefix
|
|
|
|
options := dbpkg.NewIterateOptions().WithPrefix([]byte{prefix}).WithIncludeValue(true)
|
|
|
|
ch := dbpkg.Iter(db, options)
|
|
|
|
var i = 0
|
|
|
|
for kv := range ch {
|
|
|
|
// log.Println(kv.Key)
|
|
|
|
_, gotKey, err := prefixes.PackGenericKey(prefix, kv.Key)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 1; j <= numPartials; j++ {
|
|
|
|
_, keyPartial, _ := prefixes.PackPartialGenericKey(prefix, kv.Key, j)
|
|
|
|
// Check pack partial for sanity
|
|
|
|
if !bytes.HasPrefix(gotKey, keyPartial) {
|
|
|
|
t.Errorf("%+v should be prefix of %+v\n", keyPartial, gotKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, got, err := prefixes.PackGenericValue(prefix, kv.Value)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
var numRecords = 9
|
|
|
|
if prefix == prefixes.Undo {
|
|
|
|
numRecords = 1
|
|
|
|
}
|
|
|
|
stop, err := hex.DecodeString(records[numRecords][0])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
options2 := dbpkg.NewIterateOptions().WithStart(start).WithStop(stop).WithIncludeValue(true)
|
|
|
|
ch2 := dbpkg.Iter(db, options2)
|
|
|
|
i = 0
|
|
|
|
for kv := range ch2 {
|
|
|
|
_, got, err := prefixes.PackGenericValue(prefix, kv.Value)
|
|
|
|
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 13:01:43 +01:00
|
|
|
func TestTx(t *testing.T) {
|
|
|
|
testGeneric("../../resources/tx.csv", prefixes.Tx, 1)(t)
|
|
|
|
}
|
2022-01-15 07:14:30 +01:00
|
|
|
|
2022-01-15 13:01:43 +01:00
|
|
|
func TestHashXHistory(t *testing.T) {
|
|
|
|
filePath := "../../resources/hashx_history.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/undo.csv"
|
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) {
|
|
|
|
filePath := "../../resources/block_hash.csv"
|
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) {
|
|
|
|
filePath := "../../resources/header.csv"
|
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) {
|
|
|
|
filePath := "../../resources/claim_to_txo.csv"
|
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) {
|
|
|
|
filePath := "../../resources/txo_to_claim.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/claim_short_id_prefix.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/claim_to_channel.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/channel_to_claim.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/claim_to_support.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/support_to_claim.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/claim_expiration.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/claim_takeover.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/pending_activation.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/activated_claim_and_support.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/active_amount.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/effective_amount.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/repost.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/reposted_claim.csv"
|
|
|
|
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) {
|
|
|
|
filePath := "../../resources/claim_diff.csv"
|
|
|
|
testGeneric(filePath, prefixes.ClaimDiff, 1)(t)
|
|
|
|
}
|
2022-01-10 05:46:52 +01:00
|
|
|
|
|
|
|
func TestUTXO(t *testing.T) {
|
|
|
|
filePath := "../../resources/utxo.csv"
|
2022-01-15 13:01:43 +01:00
|
|
|
testGeneric(filePath, prefixes.UTXO, 1)(t)
|
2022-01-10 05:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashXUTXO(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
filePath string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Read HashX_UTXO correctly",
|
|
|
|
filePath: "../../resources/hashx_utxo.csv",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
log.Println(tt.filePath)
|
|
|
|
file, err := os.Open(tt.filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
reader := csv.NewReader(file)
|
|
|
|
records, err := reader.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wOpts := grocksdb.NewDefaultWriteOptions()
|
|
|
|
opts := grocksdb.NewDefaultOptions()
|
|
|
|
opts.SetCreateIfMissing(true)
|
|
|
|
db, err := grocksdb.OpenDb(opts, "tmp")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
err = os.RemoveAll("./tmp")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
db.Put(wOpts, key, val)
|
|
|
|
}
|
|
|
|
start, err := hex.DecodeString(records[0][0])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
options := dbpkg.NewIterateOptions().WithPrefix([]byte{prefixes.HashXUTXO}).WithStart(start).WithIncludeValue(true)
|
|
|
|
ch := dbpkg.Iter(db, options)
|
|
|
|
var i = 0
|
|
|
|
for kv := range ch {
|
|
|
|
log.Println(kv.Key)
|
|
|
|
got := kv.Value.(*prefixes.HashXUTXOValue).PackValue()
|
|
|
|
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++
|
|
|
|
if i > 9 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|