Refactor and starting to add resolve
This commit is contained in:
parent
6d126e82df
commit
9c4a3aa690
7 changed files with 510 additions and 421 deletions
330
db/db.go
330
db/db.go
|
@ -3,8 +3,6 @@ package db
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
|
@ -13,6 +11,60 @@ import (
|
|||
"github.com/linxGnu/grocksdb"
|
||||
)
|
||||
|
||||
type ResolveResult struct {
|
||||
Name string
|
||||
NormalizedName string
|
||||
ClaimHash []byte
|
||||
TxNum int
|
||||
Position int
|
||||
TxHash []byte
|
||||
Height int
|
||||
Amount int
|
||||
ShortUrl string
|
||||
IsControlling bool
|
||||
CanonicalUrl string
|
||||
CreationHeight int
|
||||
ActivationHeight int
|
||||
ExpirationHeight int
|
||||
EffectiveAmount int
|
||||
SupportAmount int
|
||||
Reposted int
|
||||
LastTakeoverHeight int
|
||||
ClaimsInChannel int
|
||||
ChannelHash []byte
|
||||
RepostedClaimHash []byte
|
||||
SignatureValid bool
|
||||
}
|
||||
|
||||
type ResolveError struct {
|
||||
Error error
|
||||
}
|
||||
|
||||
type OptionalResolveResultOrError interface {
|
||||
GetResult() *ResolveResult
|
||||
GetError() *ResolveError
|
||||
}
|
||||
|
||||
type optionalResolveResultOrError struct {
|
||||
res *ResolveResult
|
||||
err *ResolveError
|
||||
}
|
||||
|
||||
func (x *optionalResolveResultOrError) GetResult() *ResolveResult {
|
||||
return x.res
|
||||
}
|
||||
|
||||
func (x *optionalResolveResultOrError) GetError() *ResolveError {
|
||||
return x.err
|
||||
}
|
||||
|
||||
type ExpandedResolveResult struct {
|
||||
Stream OptionalResolveResultOrError
|
||||
Channel OptionalResolveResultOrError
|
||||
Repost OptionalResolveResultOrError
|
||||
RepostedChannel OptionalResolveResultOrError
|
||||
}
|
||||
|
||||
type IterOptions struct {
|
||||
FillCache bool
|
||||
Prefix []byte
|
||||
|
@ -24,6 +76,7 @@ type IterOptions struct {
|
|||
IncludeValue bool
|
||||
RawKey bool
|
||||
RawValue bool
|
||||
It *grocksdb.Iterator
|
||||
}
|
||||
|
||||
// NewIterateOptions creates a defualt options structure for a db iterator.
|
||||
|
@ -39,6 +92,7 @@ func NewIterateOptions() *IterOptions {
|
|||
IncludeValue: false,
|
||||
RawKey: false,
|
||||
RawValue: false,
|
||||
It: nil,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,108 +146,134 @@ func (o *IterOptions) WithRawValue(rawValue bool) *IterOptions {
|
|||
return o
|
||||
}
|
||||
|
||||
func (o *IterOptions) StopIteration(key []byte) bool {
|
||||
if key == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
maxLen := int(math.Min(float64(len(key)), float64(len(o.Stop))))
|
||||
if o.Stop != nil &&
|
||||
(bytes.HasPrefix(key, o.Stop) || bytes.Compare(o.Stop, key[:maxLen]) < 0) {
|
||||
return true
|
||||
} else if o.Start != nil &&
|
||||
bytes.Compare(o.Start, key[:len(o.Start)]) > 0 {
|
||||
return true
|
||||
} else if o.Prefix != nil && !bytes.HasPrefix(key, o.Prefix) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func ParseURL(url string) (stream string, channel string, err error) {
|
||||
return "", "", nil
|
||||
}
|
||||
|
||||
func Resolve(db *grocksdb.DB, url string) *ExpandedResolveResult {
|
||||
var res = &ExpandedResolveResult{
|
||||
Stream: nil,
|
||||
Channel: nil,
|
||||
Repost: nil,
|
||||
RepostedChannel: nil,
|
||||
}
|
||||
|
||||
stream, channel, err := ParseURL(url)
|
||||
if err != nil {
|
||||
res.Stream = &optionalResolveResultOrError{
|
||||
err: &ResolveError{err},
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
log.Println(stream, channel)
|
||||
return res
|
||||
}
|
||||
|
||||
func (opts *IterOptions) ReadRow(ch chan *prefixes.PrefixRowKV, prevKey *[]byte) bool {
|
||||
it := opts.It
|
||||
key := it.Key()
|
||||
keyData := key.Data()
|
||||
keyLen := len(keyData)
|
||||
value := it.Value()
|
||||
valueData := value.Data()
|
||||
valueLen := len(valueData)
|
||||
|
||||
var outKey interface{} = nil
|
||||
var outValue interface{} = nil
|
||||
var err error = nil
|
||||
|
||||
// We need to check the current key is we're not including the stop
|
||||
// key.
|
||||
if !opts.IncludeStop && opts.StopIteration(keyData) {
|
||||
return false
|
||||
}
|
||||
|
||||
// We have to copy the key no matter what because we need to check
|
||||
// it on the next iterations to see if we're going to stop.
|
||||
newKeyData := make([]byte, keyLen)
|
||||
copy(newKeyData, keyData)
|
||||
if opts.IncludeKey && !opts.RawKey {
|
||||
outKey, err = prefixes.UnpackGenericKey(newKeyData)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else if opts.IncludeKey {
|
||||
outKey = newKeyData
|
||||
}
|
||||
|
||||
// Value could be quite large, so this setting could be important
|
||||
// for performance in some cases.
|
||||
if opts.IncludeValue {
|
||||
newValueData := make([]byte, valueLen)
|
||||
copy(newValueData, valueData)
|
||||
if !opts.RawValue {
|
||||
outValue, err = prefixes.UnpackGenericValue(newKeyData, newValueData)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else {
|
||||
outValue = newValueData
|
||||
}
|
||||
}
|
||||
|
||||
key.Free()
|
||||
value.Free()
|
||||
|
||||
ch <- &prefixes.PrefixRowKV{
|
||||
Key: outKey,
|
||||
Value: outValue,
|
||||
}
|
||||
*prevKey = newKeyData
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func Iter(db *grocksdb.DB, opts *IterOptions) <-chan *prefixes.PrefixRowKV {
|
||||
ch := make(chan *prefixes.PrefixRowKV)
|
||||
|
||||
ro := grocksdb.NewDefaultReadOptions()
|
||||
ro.SetFillCache(opts.FillCache)
|
||||
it := db.NewIterator(ro)
|
||||
opts.It = it
|
||||
|
||||
it.Seek(opts.Prefix)
|
||||
if opts.Start != nil {
|
||||
it.Seek(opts.Start)
|
||||
}
|
||||
|
||||
stopIteration := func(key []byte) bool {
|
||||
if key == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
maxLen := int(math.Min(float64(len(key)), float64(len(opts.Stop))))
|
||||
if opts.Stop != nil &&
|
||||
(bytes.HasPrefix(key, opts.Stop) || bytes.Compare(opts.Stop, key[:maxLen]) < 0) {
|
||||
return true
|
||||
} else if opts.Start != nil &&
|
||||
bytes.Compare(opts.Start, key[:len(opts.Start)]) > 0 {
|
||||
return true
|
||||
} else if opts.Prefix != nil && !bytes.HasPrefix(key, opts.Prefix) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer it.Close()
|
||||
defer close(ch)
|
||||
|
||||
var prevKey []byte = nil
|
||||
if !opts.IncludeStart {
|
||||
it.Next()
|
||||
}
|
||||
var prevKey []byte = nil
|
||||
for ; !stopIteration(prevKey) && it.Valid(); it.Next() {
|
||||
key := it.Key()
|
||||
keyData := key.Data()
|
||||
keyLen := len(keyData)
|
||||
value := it.Value()
|
||||
valueData := value.Data()
|
||||
valueLen := len(valueData)
|
||||
|
||||
var outKey interface{} = nil
|
||||
var outValue interface{} = nil
|
||||
var err error = nil
|
||||
|
||||
// We need to check the current key is we're not including the stop
|
||||
// key.
|
||||
if !opts.IncludeStop && stopIteration(keyData) {
|
||||
return
|
||||
}
|
||||
|
||||
// We have to copy the key no matter what because we need to check
|
||||
// it on the next iterations to see if we're going to stop.
|
||||
newKeyData := make([]byte, keyLen)
|
||||
copy(newKeyData, keyData)
|
||||
if opts.IncludeKey && !opts.RawKey {
|
||||
//unpackKeyFnValue := reflect.ValueOf(KeyUnpackFunc)
|
||||
//keyArgs := []reflect.Value{reflect.ValueOf(newKeyData)}
|
||||
//unpackKeyFnResult := unpackKeyFnValue.Call(keyArgs)
|
||||
//outKey = unpackKeyFnResult[0].Interface()
|
||||
_, outKey, err = prefixes.UnpackGenericKey(newKeyData)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else if opts.IncludeKey {
|
||||
outKey = newKeyData
|
||||
}
|
||||
|
||||
// Value could be quite large, so this setting could be important
|
||||
// for performance in some cases.
|
||||
if opts.IncludeValue {
|
||||
newValueData := make([]byte, valueLen)
|
||||
copy(newValueData, valueData)
|
||||
//unpackValueFnValue := reflect.ValueOf(ValueUnpackFunc)
|
||||
//valueArgs := []reflect.Value{reflect.ValueOf(newValueData)}
|
||||
//unpackValueFnResult := unpackValueFnValue.Call(valueArgs)
|
||||
//outValue = unpackValueFnResult[0].Interface()
|
||||
if !opts.RawValue {
|
||||
_, outValue, err = prefixes.UnpackGenericValue(newKeyData, newValueData)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else {
|
||||
outValue = newValueData
|
||||
}
|
||||
}
|
||||
|
||||
key.Free()
|
||||
value.Free()
|
||||
|
||||
ch <- &prefixes.PrefixRowKV{
|
||||
Key: outKey,
|
||||
Value: outValue,
|
||||
}
|
||||
prevKey = newKeyData
|
||||
|
||||
if !it.Valid() && opts.IncludeStop {
|
||||
opts.ReadRow(ch, &prevKey)
|
||||
}
|
||||
for ; !opts.StopIteration(prevKey) && it.Valid(); it.Next() {
|
||||
opts.ReadRow(ch, &prevKey)
|
||||
}
|
||||
}()
|
||||
|
||||
|
@ -202,7 +282,6 @@ func Iter(db *grocksdb.DB, opts *IterOptions) <-chan *prefixes.PrefixRowKV {
|
|||
|
||||
func GetDB(name string) (*grocksdb.DB, error) {
|
||||
opts := grocksdb.NewDefaultOptions()
|
||||
// db, err := grocksdb.OpenDb(opts, name)
|
||||
db, err := grocksdb.OpenDbAsSecondary(opts, name, "asdf")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -242,65 +321,6 @@ func ReadPrefixN(db *grocksdb.DB, prefix []byte, n int) []*prefixes.PrefixRowKV
|
|||
return res
|
||||
}
|
||||
|
||||
func OpenDB(name string, start string) int {
|
||||
// Read db
|
||||
opts := grocksdb.NewDefaultOptions()
|
||||
db, err := grocksdb.OpenDb(opts, name)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
defer db.Close()
|
||||
ro := grocksdb.NewDefaultReadOptions()
|
||||
ro.SetFillCache(false)
|
||||
|
||||
log.Println(db.Name())
|
||||
|
||||
it := db.NewIterator(ro)
|
||||
defer it.Close()
|
||||
|
||||
var i = 0
|
||||
it.Seek([]byte(start))
|
||||
for ; it.Valid(); it.Next() {
|
||||
key := it.Key()
|
||||
value := it.Value()
|
||||
|
||||
fmt.Printf("Key: %v Value: %v\n", hex.EncodeToString(key.Data()), hex.EncodeToString(value.Data()))
|
||||
|
||||
key.Free()
|
||||
value.Free()
|
||||
i++
|
||||
}
|
||||
if err := it.Err(); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func OpenAndWriteDB(db *grocksdb.DB, options *IterOptions, out string) {
|
||||
|
||||
ch := Iter(db, options)
|
||||
|
||||
var i = 0
|
||||
for kv := range ch {
|
||||
key := kv.Key.(*prefixes.UTXOKey)
|
||||
value := kv.Value.(*prefixes.UTXOValue)
|
||||
|
||||
keyMarshal, err := json.Marshal(key)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
valMarshal, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
log.Println(string(keyMarshal), string(valMarshal))
|
||||
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
func ReadWriteRawN(db *grocksdb.DB, options *IterOptions, out string, n int) {
|
||||
|
||||
options.RawKey = true
|
||||
|
@ -335,27 +355,15 @@ func ReadWriteRawN(db *grocksdb.DB, options *IterOptions, out string, n int) {
|
|||
}
|
||||
}
|
||||
|
||||
func GenerateTestData() {
|
||||
func GenerateTestData(prefix byte, fileName string) {
|
||||
dbVal, err := GetDB("/mnt/d/data/wallet/lbry-rocksdb/")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// options := &IterOptions{
|
||||
// FillCache: false,
|
||||
// Prefix: []byte{prefixes.HashXUTXO},
|
||||
// Start: nil,
|
||||
// Stop: nil,
|
||||
// IncludeStart: true,
|
||||
// IncludeStop: false,
|
||||
// IncludeKey: true,
|
||||
// IncludeValue: true,
|
||||
// RawKey: true,
|
||||
// RawValue: true,
|
||||
// }
|
||||
options := NewIterateOptions()
|
||||
options.WithRawKey(true).WithRawValue(true)
|
||||
options.WithPrefix([]byte{prefixes.HashXUTXO})
|
||||
options.WithRawKey(true).WithRawValue(true).WithIncludeValue(true)
|
||||
options.WithPrefix([]byte{prefix})
|
||||
|
||||
ReadWriteRawN(dbVal, options, "./resources/hashx_utxo.csv", 10)
|
||||
ReadWriteRawN(dbVal, options, fileName, 10)
|
||||
}
|
||||
|
|
|
@ -3616,363 +3616,363 @@ func UTXOValueUnpack(value []byte) *UTXOValue {
|
|||
}
|
||||
}
|
||||
|
||||
func generic(voidstar interface{}, firstByte byte, function byte, functionName string) (byte, interface{}, error) {
|
||||
func generic(voidstar interface{}, firstByte byte, function byte, functionName string) (interface{}, error) {
|
||||
var data []byte
|
||||
if function < 2 {
|
||||
data = voidstar.([]byte)
|
||||
}
|
||||
switch uint16(firstByte) | uint16(function)<<8 {
|
||||
case ClaimToSupport:
|
||||
return ClaimToSupport, ClaimToSupportKeyUnpack(data), nil
|
||||
return ClaimToSupportKeyUnpack(data), nil
|
||||
case ClaimToSupport | 1<<8:
|
||||
return ClaimToSupport, ClaimToSupportValueUnpack(data), nil
|
||||
return ClaimToSupportValueUnpack(data), nil
|
||||
case ClaimToSupport | 2<<8:
|
||||
return ClaimToSupport, voidstar.(*ClaimToSupportKey).PackKey(), nil
|
||||
return voidstar.(*ClaimToSupportKey).PackKey(), nil
|
||||
case ClaimToSupport | 3<<8:
|
||||
return ClaimToSupport, voidstar.(*ClaimToSupportValue).PackValue(), nil
|
||||
return voidstar.(*ClaimToSupportValue).PackValue(), nil
|
||||
case ClaimToSupport | 4<<8:
|
||||
return ClaimToSupport, ClaimToSupportKeyPackPartialKey(voidstar.(*ClaimToSupportKey)), nil
|
||||
return ClaimToSupportKeyPackPartialKey(voidstar.(*ClaimToSupportKey)), nil
|
||||
case SupportToClaim:
|
||||
return SupportToClaim, SupportToClaimKeyUnpack(data), nil
|
||||
return SupportToClaimKeyUnpack(data), nil
|
||||
case SupportToClaim | 1<<8:
|
||||
return SupportToClaim, SupportToClaimValueUnpack(data), nil
|
||||
return SupportToClaimValueUnpack(data), nil
|
||||
case SupportToClaim | 2<<8:
|
||||
return SupportToClaim, voidstar.(*SupportToClaimKey).PackKey(), nil
|
||||
return voidstar.(*SupportToClaimKey).PackKey(), nil
|
||||
case SupportToClaim | 3<<8:
|
||||
return SupportToClaim, voidstar.(*SupportToClaimValue).PackValue(), nil
|
||||
return voidstar.(*SupportToClaimValue).PackValue(), nil
|
||||
case SupportToClaim | 4<<8:
|
||||
return SupportToClaim, SupportToClaimKeyPackPartialKey(voidstar.(*SupportToClaimKey)), nil
|
||||
return SupportToClaimKeyPackPartialKey(voidstar.(*SupportToClaimKey)), nil
|
||||
case ClaimToTXO:
|
||||
return ClaimToTXO, ClaimToTXOKeyUnpack(data), nil
|
||||
return ClaimToTXOKeyUnpack(data), nil
|
||||
case ClaimToTXO | 1<<8:
|
||||
return ClaimToTXO, ClaimToTXOValueUnpack(data), nil
|
||||
return ClaimToTXOValueUnpack(data), nil
|
||||
case ClaimToTXO | 2<<8:
|
||||
return ClaimToTXO, voidstar.(*ClaimToTXOKey).PackKey(), nil
|
||||
return voidstar.(*ClaimToTXOKey).PackKey(), nil
|
||||
case ClaimToTXO | 3<<8:
|
||||
return ClaimToTXO, voidstar.(*ClaimToTXOValue).PackValue(), nil
|
||||
return voidstar.(*ClaimToTXOValue).PackValue(), nil
|
||||
case ClaimToTXO | 4<<8:
|
||||
return ClaimToTXO, ClaimToTXOKeyPackPartialKey(voidstar.(*ClaimToTXOKey)), nil
|
||||
return ClaimToTXOKeyPackPartialKey(voidstar.(*ClaimToTXOKey)), nil
|
||||
case TXOToClaim:
|
||||
return TXOToClaim, TXOToClaimKeyUnpack(data), nil
|
||||
return TXOToClaimKeyUnpack(data), nil
|
||||
case TXOToClaim | 1<<8:
|
||||
return TXOToClaim, TXOToClaimValueUnpack(data), nil
|
||||
return TXOToClaimValueUnpack(data), nil
|
||||
case TXOToClaim | 2<<8:
|
||||
return TXOToClaim, voidstar.(*TXOToClaimKey).PackKey(), nil
|
||||
return voidstar.(*TXOToClaimKey).PackKey(), nil
|
||||
case TXOToClaim | 3<<8:
|
||||
return TXOToClaim, voidstar.(*TXOToClaimValue).PackValue(), nil
|
||||
return voidstar.(*TXOToClaimValue).PackValue(), nil
|
||||
case TXOToClaim | 4<<8:
|
||||
return TXOToClaim, TXOToClaimKeyPackPartialKey(voidstar.(*TXOToClaimKey)), nil
|
||||
return TXOToClaimKeyPackPartialKey(voidstar.(*TXOToClaimKey)), nil
|
||||
|
||||
case ClaimToChannel:
|
||||
return ClaimToChannel, ClaimToChannelKeyUnpack(data), nil
|
||||
return ClaimToChannelKeyUnpack(data), nil
|
||||
case ClaimToChannel | 1<<8:
|
||||
return ClaimToChannel, ClaimToChannelValueUnpack(data), nil
|
||||
return ClaimToChannelValueUnpack(data), nil
|
||||
case ClaimToChannel | 2<<8:
|
||||
return ClaimToChannel, voidstar.(*ClaimToChannelKey).PackKey(), nil
|
||||
return voidstar.(*ClaimToChannelKey).PackKey(), nil
|
||||
case ClaimToChannel | 3<<8:
|
||||
return ClaimToChannel, voidstar.(*ClaimToChannelValue).PackValue(), nil
|
||||
return voidstar.(*ClaimToChannelValue).PackValue(), nil
|
||||
case ClaimToChannel | 4<<8:
|
||||
return ClaimToChannel, ClaimToChannelKeyPackPartialKey(voidstar.(*ClaimToChannelKey)), nil
|
||||
return ClaimToChannelKeyPackPartialKey(voidstar.(*ClaimToChannelKey)), nil
|
||||
case ChannelToClaim:
|
||||
return ChannelToClaim, ChannelToClaimKeyUnpack(data), nil
|
||||
return ChannelToClaimKeyUnpack(data), nil
|
||||
case ChannelToClaim | 1<<8:
|
||||
return ChannelToClaim, ChannelToClaimValueUnpack(data), nil
|
||||
return ChannelToClaimValueUnpack(data), nil
|
||||
case ChannelToClaim | 2<<8:
|
||||
return ChannelToClaim, voidstar.(*ChannelToClaimKey).PackKey(), nil
|
||||
return voidstar.(*ChannelToClaimKey).PackKey(), nil
|
||||
case ChannelToClaim | 3<<8:
|
||||
return ChannelToClaim, voidstar.(*ChannelToClaimValue).PackValue(), nil
|
||||
return voidstar.(*ChannelToClaimValue).PackValue(), nil
|
||||
case ChannelToClaim | 4<<8:
|
||||
return ChannelToClaim, ChannelToClaimKeyPackPartialKey(voidstar.(*ChannelToClaimKey)), nil
|
||||
return ChannelToClaimKeyPackPartialKey(voidstar.(*ChannelToClaimKey)), nil
|
||||
|
||||
case ClaimShortIdPrefix:
|
||||
return ClaimShortIdPrefix, ClaimShortIDKeyUnpack(data), nil
|
||||
return ClaimShortIDKeyUnpack(data), nil
|
||||
case ClaimShortIdPrefix | 1<<8:
|
||||
return ClaimShortIdPrefix, ClaimShortIDValueUnpack(data), nil
|
||||
return ClaimShortIDValueUnpack(data), nil
|
||||
case ClaimShortIdPrefix | 2<<8:
|
||||
return ClaimShortIdPrefix, voidstar.(*ClaimShortIDKey).PackKey(), nil
|
||||
return voidstar.(*ClaimShortIDKey).PackKey(), nil
|
||||
case ClaimShortIdPrefix | 3<<8:
|
||||
return ClaimShortIdPrefix, voidstar.(*ClaimShortIDValue).PackValue(), nil
|
||||
return voidstar.(*ClaimShortIDValue).PackValue(), nil
|
||||
case ClaimShortIdPrefix | 4<<8:
|
||||
return ClaimShortIdPrefix, ClaimShortIDKeyPackPartialKey(voidstar.(*ClaimShortIDKey)), nil
|
||||
return ClaimShortIDKeyPackPartialKey(voidstar.(*ClaimShortIDKey)), nil
|
||||
case EffectiveAmount:
|
||||
return EffectiveAmount, EffectiveAmountKeyUnpack(data), nil
|
||||
return EffectiveAmountKeyUnpack(data), nil
|
||||
case EffectiveAmount | 1<<8:
|
||||
return EffectiveAmount, EffectiveAmountValueUnpack(data), nil
|
||||
return EffectiveAmountValueUnpack(data), nil
|
||||
case EffectiveAmount | 2<<8:
|
||||
return EffectiveAmount, voidstar.(*EffectiveAmountKey).PackKey(), nil
|
||||
return voidstar.(*EffectiveAmountKey).PackKey(), nil
|
||||
case EffectiveAmount | 3<<8:
|
||||
return EffectiveAmount, voidstar.(*EffectiveAmountValue).PackValue(), nil
|
||||
return voidstar.(*EffectiveAmountValue).PackValue(), nil
|
||||
case EffectiveAmount | 4<<8:
|
||||
return EffectiveAmount, EffectiveAmountKeyPackPartialKey(voidstar.(*EffectiveAmountKey)), nil
|
||||
return EffectiveAmountKeyPackPartialKey(voidstar.(*EffectiveAmountKey)), nil
|
||||
case ClaimExpiration:
|
||||
return ClaimExpiration, ClaimExpirationKeyUnpack(data), nil
|
||||
return ClaimExpirationKeyUnpack(data), nil
|
||||
case ClaimExpiration | 1<<8:
|
||||
return ClaimExpiration, ClaimExpirationValueUnpack(data), nil
|
||||
return ClaimExpirationValueUnpack(data), nil
|
||||
case ClaimExpiration | 2<<8:
|
||||
return ClaimExpiration, voidstar.(*ClaimExpirationKey).PackKey(), nil
|
||||
return voidstar.(*ClaimExpirationKey).PackKey(), nil
|
||||
case ClaimExpiration | 3<<8:
|
||||
return ClaimExpiration, voidstar.(*ClaimExpirationValue).PackValue(), nil
|
||||
return voidstar.(*ClaimExpirationValue).PackValue(), nil
|
||||
case ClaimExpiration | 4<<8:
|
||||
return ClaimExpiration, ClaimExpirationKeyPackPartialKey(voidstar.(*ClaimExpirationKey)), nil
|
||||
return ClaimExpirationKeyPackPartialKey(voidstar.(*ClaimExpirationKey)), nil
|
||||
|
||||
case ClaimTakeover:
|
||||
return ClaimTakeover, ClaimTakeoverKeyUnpack(data), nil
|
||||
return ClaimTakeoverKeyUnpack(data), nil
|
||||
case ClaimTakeover | 1<<8:
|
||||
return ClaimTakeover, ClaimTakeoverValueUnpack(data), nil
|
||||
return ClaimTakeoverValueUnpack(data), nil
|
||||
case ClaimTakeover | 2<<8:
|
||||
return ClaimTakeover, voidstar.(*ClaimTakeoverKey).PackKey(), nil
|
||||
return voidstar.(*ClaimTakeoverKey).PackKey(), nil
|
||||
case ClaimTakeover | 3<<8:
|
||||
return ClaimTakeover, voidstar.(*ClaimTakeoverValue).PackValue(), nil
|
||||
return voidstar.(*ClaimTakeoverValue).PackValue(), nil
|
||||
case ClaimTakeover | 4<<8:
|
||||
return ClaimTakeover, ClaimTakeoverKeyPackPartialKey(voidstar.(*ClaimTakeoverKey)), nil
|
||||
return ClaimTakeoverKeyPackPartialKey(voidstar.(*ClaimTakeoverKey)), nil
|
||||
case PendingActivation:
|
||||
return PendingActivation, PendingActivationKeyUnpack(data), nil
|
||||
return PendingActivationKeyUnpack(data), nil
|
||||
case PendingActivation | 1<<8:
|
||||
return PendingActivation, PendingActivationValueUnpack(data), nil
|
||||
return PendingActivationValueUnpack(data), nil
|
||||
case PendingActivation | 2<<8:
|
||||
return PendingActivation, voidstar.(*PendingActivationKey).PackKey(), nil
|
||||
return voidstar.(*PendingActivationKey).PackKey(), nil
|
||||
case PendingActivation | 3<<8:
|
||||
return PendingActivation, voidstar.(*PendingActivationValue).PackValue(), nil
|
||||
return voidstar.(*PendingActivationValue).PackValue(), nil
|
||||
case PendingActivation | 4<<8:
|
||||
return PendingActivation, PendingActivationKeyPackPartialKey(voidstar.(*PendingActivationKey)), nil
|
||||
return PendingActivationKeyPackPartialKey(voidstar.(*PendingActivationKey)), nil
|
||||
case ActivatedClaimAndSupport:
|
||||
return ActivatedClaimAndSupport, ActivationKeyUnpack(data), nil
|
||||
return ActivationKeyUnpack(data), nil
|
||||
case ActivatedClaimAndSupport | 1<<8:
|
||||
return ActivatedClaimAndSupport, ActivationValueUnpack(data), nil
|
||||
return ActivationValueUnpack(data), nil
|
||||
case ActivatedClaimAndSupport | 2<<8:
|
||||
return ActivatedClaimAndSupport, voidstar.(*ActivationKey).PackKey(), nil
|
||||
return voidstar.(*ActivationKey).PackKey(), nil
|
||||
case ActivatedClaimAndSupport | 3<<8:
|
||||
return ActivatedClaimAndSupport, voidstar.(*ActivationValue).PackValue(), nil
|
||||
return voidstar.(*ActivationValue).PackValue(), nil
|
||||
case ActivatedClaimAndSupport | 4<<8:
|
||||
return ActivatedClaimAndSupport, ActivationKeyPackPartialKey(voidstar.(*ActivationKey)), nil
|
||||
return ActivationKeyPackPartialKey(voidstar.(*ActivationKey)), nil
|
||||
case ActiveAmount:
|
||||
return ActiveAmount, ActiveAmountKeyUnpack(data), nil
|
||||
return ActiveAmountKeyUnpack(data), nil
|
||||
case ActiveAmount | 1<<8:
|
||||
return ActiveAmount, ActiveAmountValueUnpack(data), nil
|
||||
return ActiveAmountValueUnpack(data), nil
|
||||
case ActiveAmount | 2<<8:
|
||||
return ActiveAmount, voidstar.(*ActiveAmountKey).PackKey(), nil
|
||||
return voidstar.(*ActiveAmountKey).PackKey(), nil
|
||||
case ActiveAmount | 3<<8:
|
||||
return ActiveAmount, voidstar.(*ActiveAmountValue).PackValue(), nil
|
||||
return voidstar.(*ActiveAmountValue).PackValue(), nil
|
||||
case ActiveAmount | 4<<8:
|
||||
return ActiveAmount, ActiveAmountKeyPackPartialKey(voidstar.(*ActiveAmountKey)), nil
|
||||
return ActiveAmountKeyPackPartialKey(voidstar.(*ActiveAmountKey)), nil
|
||||
|
||||
case Repost:
|
||||
return Repost, RepostKeyUnpack(data), nil
|
||||
return RepostKeyUnpack(data), nil
|
||||
case Repost | 1<<8:
|
||||
return Repost, RepostValueUnpack(data), nil
|
||||
return RepostValueUnpack(data), nil
|
||||
case Repost | 2<<8:
|
||||
return Repost, voidstar.(*RepostKey).PackKey(), nil
|
||||
return voidstar.(*RepostKey).PackKey(), nil
|
||||
case Repost | 3<<8:
|
||||
return Repost, voidstar.(*RepostValue).PackValue(), nil
|
||||
return voidstar.(*RepostValue).PackValue(), nil
|
||||
case Repost | 4<<8:
|
||||
return Repost, RepostKeyPackPartialKey(voidstar.(*RepostKey)), nil
|
||||
return RepostKeyPackPartialKey(voidstar.(*RepostKey)), nil
|
||||
case RepostedClaim:
|
||||
return RepostedClaim, RepostedKeyUnpack(data), nil
|
||||
return RepostedKeyUnpack(data), nil
|
||||
case RepostedClaim | 1<<8:
|
||||
return RepostedClaim, RepostedValueUnpack(data), nil
|
||||
return RepostedValueUnpack(data), nil
|
||||
case RepostedClaim | 2<<8:
|
||||
return RepostedClaim, voidstar.(*RepostedKey).PackKey(), nil
|
||||
return voidstar.(*RepostedKey).PackKey(), nil
|
||||
case RepostedClaim | 3<<8:
|
||||
return RepostedClaim, voidstar.(*RepostedValue).PackValue(), nil
|
||||
return voidstar.(*RepostedValue).PackValue(), nil
|
||||
case RepostedClaim | 4<<8:
|
||||
return RepostedClaim, RepostedKeyPackPartialKey(voidstar.(*RepostedKey)), nil
|
||||
return RepostedKeyPackPartialKey(voidstar.(*RepostedKey)), nil
|
||||
|
||||
case Undo:
|
||||
return Undo, UndoKeyUnpack(data), nil
|
||||
return UndoKeyUnpack(data), nil
|
||||
case Undo | 1<<8:
|
||||
return Undo, UndoValueUnpack(data), nil
|
||||
return UndoValueUnpack(data), nil
|
||||
case Undo | 2<<8:
|
||||
return Undo, voidstar.(*UndoKey).PackKey(), nil
|
||||
return voidstar.(*UndoKey).PackKey(), nil
|
||||
case Undo | 3<<8:
|
||||
return Undo, voidstar.(*UndoValue).PackValue(), nil
|
||||
return voidstar.(*UndoValue).PackValue(), nil
|
||||
case Undo | 4<<8:
|
||||
return Undo, UndoKeyPackPartialKey(voidstar.(*UndoKey)), nil
|
||||
return UndoKeyPackPartialKey(voidstar.(*UndoKey)), nil
|
||||
case ClaimDiff:
|
||||
return ClaimDiff, TouchedOrDeletedClaimKeyUnpack(data), nil
|
||||
return TouchedOrDeletedClaimKeyUnpack(data), nil
|
||||
case ClaimDiff | 1<<8:
|
||||
return ClaimDiff, TouchedOrDeletedClaimValueUnpack(data), nil
|
||||
return TouchedOrDeletedClaimValueUnpack(data), nil
|
||||
case ClaimDiff | 2<<8:
|
||||
return ClaimDiff, voidstar.(*TouchedOrDeletedClaimKey).PackKey(), nil
|
||||
return voidstar.(*TouchedOrDeletedClaimKey).PackKey(), nil
|
||||
case ClaimDiff | 3<<8:
|
||||
return ClaimDiff, voidstar.(*TouchedOrDeletedClaimValue).PackValue(), nil
|
||||
return voidstar.(*TouchedOrDeletedClaimValue).PackValue(), nil
|
||||
case ClaimDiff | 4<<8:
|
||||
return ClaimDiff, TouchedOrDeletedClaimKeyPackPartialKey(voidstar.(*TouchedOrDeletedClaimKey)), nil
|
||||
return TouchedOrDeletedClaimKeyPackPartialKey(voidstar.(*TouchedOrDeletedClaimKey)), nil
|
||||
|
||||
case Tx:
|
||||
return Tx, TxKeyUnpack(data), nil
|
||||
return TxKeyUnpack(data), nil
|
||||
case Tx | 1<<8:
|
||||
return Tx, TxValueUnpack(data), nil
|
||||
return TxValueUnpack(data), nil
|
||||
case Tx | 2<<8:
|
||||
return Tx, voidstar.(*TxKey).PackKey(), nil
|
||||
return voidstar.(*TxKey).PackKey(), nil
|
||||
case Tx | 3<<8:
|
||||
return Tx, voidstar.(*TxValue).PackValue(), nil
|
||||
return voidstar.(*TxValue).PackValue(), nil
|
||||
case Tx | 4<<8:
|
||||
return Tx, TxKeyPackPartialKey(voidstar.(*TxKey)), nil
|
||||
return TxKeyPackPartialKey(voidstar.(*TxKey)), nil
|
||||
case BlockHash:
|
||||
return BlockHash, BlockHashKeyUnpack(data), nil
|
||||
return BlockHashKeyUnpack(data), nil
|
||||
case BlockHash | 1<<8:
|
||||
return BlockHash, BlockHashValueUnpack(data), nil
|
||||
return BlockHashValueUnpack(data), nil
|
||||
case BlockHash | 2<<8:
|
||||
return BlockHash, voidstar.(*BlockHashKey).PackKey(), nil
|
||||
return voidstar.(*BlockHashKey).PackKey(), nil
|
||||
case BlockHash | 3<<8:
|
||||
return BlockHash, voidstar.(*BlockHashValue).PackValue(), nil
|
||||
return voidstar.(*BlockHashValue).PackValue(), nil
|
||||
case BlockHash | 4<<8:
|
||||
return BlockHash, BlockHashKeyPackPartialKey(voidstar.(*BlockHashKey)), nil
|
||||
return BlockHashKeyPackPartialKey(voidstar.(*BlockHashKey)), nil
|
||||
case Header:
|
||||
return Header, BlockHeaderKeyUnpack(data), nil
|
||||
return BlockHeaderKeyUnpack(data), nil
|
||||
case Header | 1<<8:
|
||||
return Header, BlockHeaderValueUnpack(data), nil
|
||||
return BlockHeaderValueUnpack(data), nil
|
||||
case Header | 2<<8:
|
||||
return Header, voidstar.(*BlockHeaderKey).PackKey(), nil
|
||||
return voidstar.(*BlockHeaderKey).PackKey(), nil
|
||||
case Header | 3<<8:
|
||||
return Header, voidstar.(*BlockHeaderValue).PackValue(), nil
|
||||
return voidstar.(*BlockHeaderValue).PackValue(), nil
|
||||
case Header | 4<<8:
|
||||
return Header, BlockHeaderKeyPackPartialKey(voidstar.(*BlockHeaderKey)), nil
|
||||
return BlockHeaderKeyPackPartialKey(voidstar.(*BlockHeaderKey)), nil
|
||||
case TxNum:
|
||||
return TxNum, TxNumKeyUnpack(data), nil
|
||||
return TxNumKeyUnpack(data), nil
|
||||
case TxNum | 1<<8:
|
||||
return TxNum, TxNumValueUnpack(data), nil
|
||||
return TxNumValueUnpack(data), nil
|
||||
case TxNum | 2<<8:
|
||||
return TxNum, voidstar.(*TxNumKey).PackKey(), nil
|
||||
return voidstar.(*TxNumKey).PackKey(), nil
|
||||
case TxNum | 3<<8:
|
||||
return TxNum, voidstar.(*TxNumValue).PackValue(), nil
|
||||
return voidstar.(*TxNumValue).PackValue(), nil
|
||||
case TxNum | 4<<8:
|
||||
return TxNum, TxNumKeyPackPartialKey(voidstar.(*TxNumKey)), nil
|
||||
return TxNumKeyPackPartialKey(voidstar.(*TxNumKey)), nil
|
||||
|
||||
case TxCount:
|
||||
return TxCount, TxCountKeyUnpack(data), nil
|
||||
return TxCountKeyUnpack(data), nil
|
||||
case TxCount | 1<<8:
|
||||
return TxCount, TxCountValueUnpack(data), nil
|
||||
return TxCountValueUnpack(data), nil
|
||||
case TxCount | 2<<8:
|
||||
return TxCount, voidstar.(*TxCountKey).PackKey(), nil
|
||||
return voidstar.(*TxCountKey).PackKey(), nil
|
||||
case TxCount | 3<<8:
|
||||
return TxCount, voidstar.(*TxCountValue).PackValue(), nil
|
||||
return voidstar.(*TxCountValue).PackValue(), nil
|
||||
case TxCount | 4<<8:
|
||||
return TxCount, TxCountKeyPackPartialKey(voidstar.(*TxCountKey)), nil
|
||||
return TxCountKeyPackPartialKey(voidstar.(*TxCountKey)), nil
|
||||
case TxHash:
|
||||
return TxHash, TxHashKeyUnpack(data), nil
|
||||
return TxHashKeyUnpack(data), nil
|
||||
case TxHash | 1<<8:
|
||||
return TxHash, TxHashValueUnpack(data), nil
|
||||
return TxHashValueUnpack(data), nil
|
||||
case TxHash | 2<<8:
|
||||
return TxHash, voidstar.(*TxHashKey).PackKey(), nil
|
||||
return voidstar.(*TxHashKey).PackKey(), nil
|
||||
case TxHash | 3<<8:
|
||||
return TxHash, voidstar.(*TxHashValue).PackValue(), nil
|
||||
return voidstar.(*TxHashValue).PackValue(), nil
|
||||
case TxHash | 4<<8:
|
||||
return TxHash, TxHashKeyPackPartialKey(voidstar.(*TxHashKey)), nil
|
||||
return TxHashKeyPackPartialKey(voidstar.(*TxHashKey)), nil
|
||||
case UTXO:
|
||||
return UTXO, UTXOKeyUnpack(data), nil
|
||||
return UTXOKeyUnpack(data), nil
|
||||
case UTXO | 1<<8:
|
||||
return UTXO, UTXOValueUnpack(data), nil
|
||||
return UTXOValueUnpack(data), nil
|
||||
case UTXO | 2<<8:
|
||||
return UTXO, voidstar.(*UTXOKey).PackKey(), nil
|
||||
return voidstar.(*UTXOKey).PackKey(), nil
|
||||
case UTXO | 3<<8:
|
||||
return UTXO, voidstar.(*UTXOValue).PackValue(), nil
|
||||
return voidstar.(*UTXOValue).PackValue(), nil
|
||||
case UTXO | 4<<8:
|
||||
return UTXO, UTXOKeyPackPartialKey(voidstar.(*UTXOKey)), nil
|
||||
return UTXOKeyPackPartialKey(voidstar.(*UTXOKey)), nil
|
||||
case HashXUTXO:
|
||||
return UTXO, HashXUTXOKeyUnpack(data), nil
|
||||
return HashXUTXOKeyUnpack(data), nil
|
||||
case HashXUTXO | 1<<8:
|
||||
return UTXO, HashXUTXOValueUnpack(data), nil
|
||||
return HashXUTXOValueUnpack(data), nil
|
||||
case HashXUTXO | 2<<8:
|
||||
return UTXO, voidstar.(*HashXUTXOKey).PackKey(), nil
|
||||
return voidstar.(*HashXUTXOKey).PackKey(), nil
|
||||
case HashXUTXO | 3<<8:
|
||||
return UTXO, voidstar.(*HashXUTXOValue).PackValue(), nil
|
||||
return voidstar.(*HashXUTXOValue).PackValue(), nil
|
||||
case HashXUTXO | 4<<8:
|
||||
return UTXO, HashXUTXOKeyPackPartialKey(voidstar.(*HashXUTXOKey)), nil
|
||||
return HashXUTXOKeyPackPartialKey(voidstar.(*HashXUTXOKey)), nil
|
||||
case HashXHistory:
|
||||
return HashXHistory, HashXHistoryKeyUnpack(data), nil
|
||||
return HashXHistoryKeyUnpack(data), nil
|
||||
case HashXHistory | 1<<8:
|
||||
return HashXHistory, HashXHistoryValueUnpack(data), nil
|
||||
return HashXHistoryValueUnpack(data), nil
|
||||
case HashXHistory | 2<<8:
|
||||
return HashXHistory, voidstar.(*HashXHistoryKey).PackKey(), nil
|
||||
return voidstar.(*HashXHistoryKey).PackKey(), nil
|
||||
case HashXHistory | 3<<8:
|
||||
return HashXHistory, voidstar.(*HashXHistoryValue).PackValue(), nil
|
||||
return voidstar.(*HashXHistoryValue).PackValue(), nil
|
||||
case HashXHistory | 4<<8:
|
||||
return HashXHistory, HashXHistoryKeyPackPartialKey(voidstar.(*HashXHistoryKey)), nil
|
||||
return HashXHistoryKeyPackPartialKey(voidstar.(*HashXHistoryKey)), nil
|
||||
case DBState:
|
||||
return DBState, DBStateKeyUnpack(data), nil
|
||||
return DBStateKeyUnpack(data), nil
|
||||
case DBState | 1<<8:
|
||||
return DBState, DBStateValueUnpack(data), nil
|
||||
return DBStateValueUnpack(data), nil
|
||||
case DBState | 2<<8:
|
||||
return DBState, voidstar.(*DBStateKey).PackKey(), nil
|
||||
return voidstar.(*DBStateKey).PackKey(), nil
|
||||
case DBState | 3<<8:
|
||||
return DBState, voidstar.(*DBStateValue).PackValue(), nil
|
||||
return voidstar.(*DBStateValue).PackValue(), nil
|
||||
case DBState | 4<<8:
|
||||
return DBState, DBStateKeyPackPartialKey(voidstar.(*DBStateKey)), nil
|
||||
return DBStateKeyPackPartialKey(voidstar.(*DBStateKey)), nil
|
||||
|
||||
case ChannelCount:
|
||||
return ChannelCount, ChannelCountKeyUnpack(data), nil
|
||||
return ChannelCountKeyUnpack(data), nil
|
||||
case ChannelCount | 1<<8:
|
||||
return ChannelCount, ChannelCountValueUnpack(data), nil
|
||||
return ChannelCountValueUnpack(data), nil
|
||||
case ChannelCount | 2<<8:
|
||||
return ChannelCount, voidstar.(*ChannelCountKey).PackKey(), nil
|
||||
return voidstar.(*ChannelCountKey).PackKey(), nil
|
||||
case ChannelCount | 3<<8:
|
||||
return ChannelCount, voidstar.(*ChannelCountValue).PackValue(), nil
|
||||
return voidstar.(*ChannelCountValue).PackValue(), nil
|
||||
case ChannelCount | 4<<8:
|
||||
return ChannelCount, ChannelCountKeyPackPartialKey(voidstar.(*ChannelCountKey)), nil
|
||||
return ChannelCountKeyPackPartialKey(voidstar.(*ChannelCountKey)), nil
|
||||
case SupportAmount:
|
||||
return SupportAmount, SupportAmountKeyUnpack(data), nil
|
||||
return SupportAmountKeyUnpack(data), nil
|
||||
case SupportAmount | 1<<8:
|
||||
return SupportAmount, SupportAmountValueUnpack(data), nil
|
||||
return SupportAmountValueUnpack(data), nil
|
||||
case SupportAmount | 2<<8:
|
||||
return SupportAmount, voidstar.(*SupportAmountKey).PackKey(), nil
|
||||
return voidstar.(*SupportAmountKey).PackKey(), nil
|
||||
case SupportAmount | 3<<8:
|
||||
return SupportAmount, voidstar.(*SupportAmountValue).PackValue(), nil
|
||||
return voidstar.(*SupportAmountValue).PackValue(), nil
|
||||
case SupportAmount | 4<<8:
|
||||
return SupportAmount, SupportAmountKeyPackPartialKey(voidstar.(*SupportAmountKey)), nil
|
||||
return SupportAmountKeyPackPartialKey(voidstar.(*SupportAmountKey)), nil
|
||||
case BlockTXs:
|
||||
return BlockTXs, BlockTxsKeyUnpack(data), nil
|
||||
return BlockTxsKeyUnpack(data), nil
|
||||
case BlockTXs | 1<<8:
|
||||
return BlockTXs, BlockTxsValueUnpack(data), nil
|
||||
return BlockTxsValueUnpack(data), nil
|
||||
case BlockTXs | 2<<8:
|
||||
return BlockTXs, voidstar.(*BlockTxsKey).PackKey(), nil
|
||||
return voidstar.(*BlockTxsKey).PackKey(), nil
|
||||
case BlockTXs | 3<<8:
|
||||
return BlockTXs, voidstar.(*BlockTxsValue).PackValue(), nil
|
||||
return voidstar.(*BlockTxsValue).PackValue(), nil
|
||||
case BlockTXs | 4<<8:
|
||||
return BlockTXs, BlockTxsKeyPackPartialKey(voidstar.(*BlockTxsKey)), nil
|
||||
return BlockTxsKeyPackPartialKey(voidstar.(*BlockTxsKey)), nil
|
||||
|
||||
}
|
||||
return 0x0, nil, errors.Base("%s function for %v not implemented", functionName, firstByte)
|
||||
return nil, errors.Base("%s function for %v not implemented", functionName, firstByte)
|
||||
}
|
||||
|
||||
func UnpackGenericKey(key []byte) (byte, interface{}, error) {
|
||||
func UnpackGenericKey(key []byte) (interface{}, error) {
|
||||
if len(key) == 0 {
|
||||
return 0x0, nil, errors.Base("key length zero")
|
||||
return nil, errors.Base("key length zero")
|
||||
}
|
||||
return generic(key, key[0], 0, "unpack key")
|
||||
}
|
||||
|
||||
func UnpackGenericValue(key, value []byte) (byte, interface{}, error) {
|
||||
func UnpackGenericValue(key, value []byte) (interface{}, error) {
|
||||
if len(key) == 0 {
|
||||
return 0x0, nil, errors.Base("key length zero")
|
||||
return nil, errors.Base("key length zero")
|
||||
}
|
||||
if len(value) == 0 {
|
||||
return 0x0, nil, errors.Base("value length zero")
|
||||
return nil, errors.Base("value length zero")
|
||||
}
|
||||
return generic(value, key[0], 1, "unpack value")
|
||||
}
|
||||
|
||||
func PackPartialGenericKey(prefix byte, key interface{}, nFields int) (byte, []byte, error) {
|
||||
func PackPartialGenericKey(prefix byte, key interface{}, nFields int) ([]byte, error) {
|
||||
if key == nil {
|
||||
return 0x0, nil, errors.Base("key length zero")
|
||||
return nil, errors.Base("key length zero")
|
||||
}
|
||||
x, y, z := generic(key, prefix, 4, "pack partial key")
|
||||
res := y.(func(int) []byte)(nFields)
|
||||
return x, res, z
|
||||
genericRes, err := generic(key, prefix, 4, "pack partial key")
|
||||
res := genericRes.(func(int) []byte)(nFields)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func PackGenericKey(prefix byte, key interface{}) (byte, []byte, error) {
|
||||
func PackGenericKey(prefix byte, key interface{}) ([]byte, error) {
|
||||
if key == nil {
|
||||
return 0x0, nil, errors.Base("key length zero")
|
||||
return nil, errors.Base("key length zero")
|
||||
}
|
||||
x, y, z := generic(key, prefix, 2, "pack key")
|
||||
return x, y.([]byte), z
|
||||
genericRes, err := generic(key, prefix, 2, "pack key")
|
||||
return genericRes.([]byte), err
|
||||
}
|
||||
|
||||
func PackGenericValue(prefix byte, value interface{}) (byte, []byte, error) {
|
||||
func PackGenericValue(prefix byte, value interface{}) ([]byte, error) {
|
||||
if value == nil {
|
||||
return 0x0, nil, errors.Base("value length zero")
|
||||
return nil, errors.Base("value length zero")
|
||||
}
|
||||
x, y, z := generic(value, prefix, 3, "pack value")
|
||||
return x, y.([]byte), z
|
||||
genericRes, err := generic(value, prefix, 3, "pack value")
|
||||
return genericRes.([]byte), err
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"testing"
|
||||
|
||||
dbpkg "github.com/lbryio/hub/db"
|
||||
"github.com/lbryio/hub/db/prefixes"
|
||||
prefixes "github.com/lbryio/hub/db/prefixes"
|
||||
"github.com/linxGnu/grocksdb"
|
||||
)
|
||||
|
||||
|
@ -67,20 +67,20 @@ func testGeneric(filePath string, prefix byte, numPartials int) func(*testing.T)
|
|||
var i = 0
|
||||
for kv := range ch {
|
||||
// log.Println(kv.Key)
|
||||
_, gotKey, err := prefixes.PackGenericKey(prefix, 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)
|
||||
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)
|
||||
got, err := prefixes.PackGenericValue(prefix, kv.Value)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ func testGeneric(filePath string, prefix byte, numPartials int) func(*testing.T)
|
|||
ch2 := dbpkg.Iter(db, options2)
|
||||
i = 0
|
||||
for kv := range ch2 {
|
||||
_, got, err := prefixes.PackGenericValue(prefix, kv.Value)
|
||||
got, err := prefixes.PackGenericValue(prefix, kv.Value)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package server
|
||||
package server_test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"github.com/lbryio/hub/internal/metrics"
|
||||
pb "github.com/lbryio/hub/protobuf/go"
|
||||
server "github.com/lbryio/hub/server"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
@ -43,19 +44,19 @@ func removeFile(fileName string) {
|
|||
}
|
||||
}
|
||||
|
||||
func makeDefaultArgs() *Args {
|
||||
args := &Args{
|
||||
CmdType: ServeCmd,
|
||||
Host: DefaultHost,
|
||||
Port: DefaultPort,
|
||||
EsHost: DefaultEsHost,
|
||||
EsPort: DefaultEsPort,
|
||||
PrometheusPort: DefaultPrometheusPort,
|
||||
EsIndex: DefaultEsIndex,
|
||||
RefreshDelta: DefaultRefreshDelta,
|
||||
CacheTTL: DefaultCacheTTL,
|
||||
PeerFile: DefaultPeerFile,
|
||||
Country: DefaultCountry,
|
||||
func makeDefaultArgs() *server.Args {
|
||||
args := &server.Args{
|
||||
CmdType: server.ServeCmd,
|
||||
Host: server.DefaultHost,
|
||||
Port: server.DefaultPort,
|
||||
EsHost: server.DefaultEsHost,
|
||||
EsPort: server.DefaultEsPort,
|
||||
PrometheusPort: server.DefaultPrometheusPort,
|
||||
EsIndex: server.DefaultEsIndex,
|
||||
RefreshDelta: server.DefaultRefreshDelta,
|
||||
CacheTTL: server.DefaultCacheTTL,
|
||||
PeerFile: server.DefaultPeerFile,
|
||||
Country: server.DefaultCountry,
|
||||
DisableEs: true,
|
||||
Debug: true,
|
||||
DisableLoadPeers: true,
|
||||
|
@ -88,26 +89,26 @@ func TestAddPeer(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := MakeHubServer(ctx, args)
|
||||
server.ExternalIP = net.IPv4(0, 0, 0, 0)
|
||||
hubServer := server.MakeHubServer(ctx, args)
|
||||
hubServer.ExternalIP = net.IPv4(0, 0, 0, 0)
|
||||
metrics.PeersKnown.Set(0)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
var peer *Peer
|
||||
var peer *server.Peer
|
||||
if strings.Contains(tt.name, "1 unique") {
|
||||
peer = &Peer{
|
||||
peer = &server.Peer{
|
||||
Address: "1.1.1.1",
|
||||
Port: "50051",
|
||||
}
|
||||
} else {
|
||||
x := i + 1
|
||||
peer = &Peer{
|
||||
peer = &server.Peer{
|
||||
Address: fmt.Sprintf("%d.%d.%d.%d", x, x, x, x),
|
||||
Port: "50051",
|
||||
}
|
||||
}
|
||||
//log.Printf("Adding peer %+v\n", msg)
|
||||
err := server.addPeer(peer, false, false)
|
||||
err := hubServer.AddPeerExported()(peer, false, false)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
@ -147,31 +148,31 @@ func TestPeerWriter(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := MakeHubServer(ctx, args)
|
||||
server.ExternalIP = net.IPv4(0, 0, 0, 0)
|
||||
hubServer := server.MakeHubServer(ctx, args)
|
||||
hubServer.ExternalIP = net.IPv4(0, 0, 0, 0)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
var peer *Peer
|
||||
var peer *server.Peer
|
||||
if strings.Contains(tt.name, "1 unique") {
|
||||
peer = &Peer{
|
||||
peer = &server.Peer{
|
||||
Address: "1.1.1.1",
|
||||
Port: "50051",
|
||||
}
|
||||
} else {
|
||||
x := i + 1
|
||||
peer = &Peer{
|
||||
peer = &server.Peer{
|
||||
Address: fmt.Sprintf("%d.%d.%d.%d", x, x, x, x),
|
||||
Port: "50051",
|
||||
}
|
||||
}
|
||||
//log.Printf("Adding peer %+v\n", peer)
|
||||
err := server.addPeer(peer, false, false)
|
||||
err := hubServer.AddPeerExported()(peer, false, false)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
//log.Println("Counting lines...")
|
||||
got := lineCountFile(server.Args.PeerFile)
|
||||
got := lineCountFile(hubServer.Args.PeerFile)
|
||||
if got != tt.want {
|
||||
t.Errorf("lineCountFile(peers.txt) = %d, want %d", got, tt.want)
|
||||
}
|
||||
|
@ -211,12 +212,12 @@ func TestAddPeerEndpoint(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := MakeHubServer(ctx, args)
|
||||
server2 := MakeHubServer(ctx, args2)
|
||||
hubServer := server.MakeHubServer(ctx, args)
|
||||
hubServer2 := server.MakeHubServer(ctx, args2)
|
||||
metrics.PeersKnown.Set(0)
|
||||
go server.Run()
|
||||
go server2.Run()
|
||||
//go server.Run()
|
||||
go hubServer.Run()
|
||||
go hubServer2.Run()
|
||||
//go hubServer.Run()
|
||||
conn, err := grpc.Dial("localhost:"+args.Port,
|
||||
grpc.WithInsecure(),
|
||||
grpc.WithBlock(),
|
||||
|
@ -237,15 +238,15 @@ func TestAddPeerEndpoint(t *testing.T) {
|
|||
log.Println(err)
|
||||
}
|
||||
|
||||
server.GrpcServer.GracefulStop()
|
||||
server2.GrpcServer.GracefulStop()
|
||||
got1 := server.getNumPeers()
|
||||
got2 := server2.getNumPeers()
|
||||
hubServer.GrpcServer.GracefulStop()
|
||||
hubServer2.GrpcServer.GracefulStop()
|
||||
got1 := hubServer.GetNumPeersExported()()
|
||||
got2 := hubServer2.GetNumPeersExported()()
|
||||
if got1 != tt.wantServerOne {
|
||||
t.Errorf("len(server.PeerServers) = %d, want %d\n", got1, tt.wantServerOne)
|
||||
t.Errorf("len(hubServer.PeerServers) = %d, want %d\n", got1, tt.wantServerOne)
|
||||
}
|
||||
if got2 != tt.wantServerTwo {
|
||||
t.Errorf("len(server2.PeerServers) = %d, want %d\n", got2, tt.wantServerTwo)
|
||||
t.Errorf("len(hubServer2.PeerServers) = %d, want %d\n", got2, tt.wantServerTwo)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -277,13 +278,13 @@ func TestAddPeerEndpoint2(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := MakeHubServer(ctx, args)
|
||||
server2 := MakeHubServer(ctx, args2)
|
||||
server3 := MakeHubServer(ctx, args3)
|
||||
hubServer := server.MakeHubServer(ctx, args)
|
||||
hubServer2 := server.MakeHubServer(ctx, args2)
|
||||
hubServer3 := server.MakeHubServer(ctx, args3)
|
||||
metrics.PeersKnown.Set(0)
|
||||
go server.Run()
|
||||
go server2.Run()
|
||||
go server3.Run()
|
||||
go hubServer.Run()
|
||||
go hubServer2.Run()
|
||||
go hubServer3.Run()
|
||||
conn, err := grpc.Dial("localhost:"+args.Port,
|
||||
grpc.WithInsecure(),
|
||||
grpc.WithBlock(),
|
||||
|
@ -313,20 +314,20 @@ func TestAddPeerEndpoint2(t *testing.T) {
|
|||
log.Println(err)
|
||||
}
|
||||
|
||||
server.GrpcServer.GracefulStop()
|
||||
server2.GrpcServer.GracefulStop()
|
||||
server3.GrpcServer.GracefulStop()
|
||||
got1 := server.getNumPeers()
|
||||
got2 := server2.getNumPeers()
|
||||
got3 := server3.getNumPeers()
|
||||
hubServer.GrpcServer.GracefulStop()
|
||||
hubServer2.GrpcServer.GracefulStop()
|
||||
hubServer3.GrpcServer.GracefulStop()
|
||||
got1 := hubServer.GetNumPeersExported()()
|
||||
got2 := hubServer2.GetNumPeersExported()()
|
||||
got3 := hubServer3.GetNumPeersExported()()
|
||||
if got1 != tt.wantServerOne {
|
||||
t.Errorf("len(server.PeerServers) = %d, want %d\n", got1, tt.wantServerOne)
|
||||
t.Errorf("len(hubServer.PeerServers) = %d, want %d\n", got1, tt.wantServerOne)
|
||||
}
|
||||
if got2 != tt.wantServerTwo {
|
||||
t.Errorf("len(server2.PeerServers) = %d, want %d\n", got2, tt.wantServerTwo)
|
||||
t.Errorf("len(hubServer2.PeerServers) = %d, want %d\n", got2, tt.wantServerTwo)
|
||||
}
|
||||
if got3 != tt.wantServerThree {
|
||||
t.Errorf("len(server3.PeerServers) = %d, want %d\n", got3, tt.wantServerThree)
|
||||
t.Errorf("len(hubServer3.PeerServers) = %d, want %d\n", got3, tt.wantServerThree)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -358,13 +359,13 @@ func TestAddPeerEndpoint3(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := MakeHubServer(ctx, args)
|
||||
server2 := MakeHubServer(ctx, args2)
|
||||
server3 := MakeHubServer(ctx, args3)
|
||||
hubServer := server.MakeHubServer(ctx, args)
|
||||
hubServer2 := server.MakeHubServer(ctx, args2)
|
||||
hubServer3 := server.MakeHubServer(ctx, args3)
|
||||
metrics.PeersKnown.Set(0)
|
||||
go server.Run()
|
||||
go server2.Run()
|
||||
go server3.Run()
|
||||
go hubServer.Run()
|
||||
go hubServer2.Run()
|
||||
go hubServer3.Run()
|
||||
conn, err := grpc.Dial("localhost:"+args.Port,
|
||||
grpc.WithInsecure(),
|
||||
grpc.WithBlock(),
|
||||
|
@ -402,20 +403,20 @@ func TestAddPeerEndpoint3(t *testing.T) {
|
|||
log.Println(err)
|
||||
}
|
||||
|
||||
server.GrpcServer.GracefulStop()
|
||||
server2.GrpcServer.GracefulStop()
|
||||
server3.GrpcServer.GracefulStop()
|
||||
got1 := server.getNumPeers()
|
||||
got2 := server2.getNumPeers()
|
||||
got3 := server3.getNumPeers()
|
||||
hubServer.GrpcServer.GracefulStop()
|
||||
hubServer2.GrpcServer.GracefulStop()
|
||||
hubServer3.GrpcServer.GracefulStop()
|
||||
got1 := hubServer.GetNumPeersExported()()
|
||||
got2 := hubServer2.GetNumPeersExported()()
|
||||
got3 := hubServer3.GetNumPeersExported()()
|
||||
if got1 != tt.wantServerOne {
|
||||
t.Errorf("len(server.PeerServers) = %d, want %d\n", got1, tt.wantServerOne)
|
||||
t.Errorf("len(hubServer.PeerServers) = %d, want %d\n", got1, tt.wantServerOne)
|
||||
}
|
||||
if got2 != tt.wantServerTwo {
|
||||
t.Errorf("len(server2.PeerServers) = %d, want %d\n", got2, tt.wantServerTwo)
|
||||
t.Errorf("len(hubServer2.PeerServers) = %d, want %d\n", got2, tt.wantServerTwo)
|
||||
}
|
||||
if got3 != tt.wantServerThree {
|
||||
t.Errorf("len(server3.PeerServers) = %d, want %d\n", got3, tt.wantServerThree)
|
||||
t.Errorf("len(hubServer3.PeerServers) = %d, want %d\n", got3, tt.wantServerThree)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -436,41 +437,41 @@ func TestUDPServer(t *testing.T) {
|
|||
want string
|
||||
}{
|
||||
{
|
||||
name: "hubs server external ip",
|
||||
name: "hubs hubServer external ip",
|
||||
want: "127.0.0.1",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := MakeHubServer(ctx, args)
|
||||
server2 := MakeHubServer(ctx, args2)
|
||||
go server.Run()
|
||||
go server2.Run()
|
||||
hubServer := server.MakeHubServer(ctx, args)
|
||||
hubServer2 := server.MakeHubServer(ctx, args2)
|
||||
go hubServer.Run()
|
||||
go hubServer2.Run()
|
||||
metrics.PeersKnown.Set(0)
|
||||
|
||||
peer := &Peer{
|
||||
peer := &server.Peer{
|
||||
Address: "0.0.0.0",
|
||||
Port: "50052",
|
||||
}
|
||||
|
||||
err := server.addPeer(peer, true, true)
|
||||
err := hubServer.AddPeerExported()(peer, true, true)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
server.GrpcServer.GracefulStop()
|
||||
server2.GrpcServer.GracefulStop()
|
||||
hubServer.GrpcServer.GracefulStop()
|
||||
hubServer2.GrpcServer.GracefulStop()
|
||||
|
||||
got1 := server.ExternalIP.String()
|
||||
got1 := hubServer.ExternalIP.String()
|
||||
if got1 != tt.want {
|
||||
t.Errorf("server.ExternalIP = %s, want %s\n", got1, tt.want)
|
||||
t.Errorf("server.Args.Port = %s\n", server.Args.Port)
|
||||
t.Errorf("hubServer.ExternalIP = %s, want %s\n", got1, tt.want)
|
||||
t.Errorf("hubServer.Args.Port = %s\n", hubServer.Args.Port)
|
||||
}
|
||||
got2 := server2.ExternalIP.String()
|
||||
got2 := hubServer2.ExternalIP.String()
|
||||
if got2 != tt.want {
|
||||
t.Errorf("server2.ExternalIP = %s, want %s\n", got2, tt.want)
|
||||
t.Errorf("server2.Args.Port = %s\n", server2.Args.Port)
|
||||
t.Errorf("hubServer2.ExternalIP = %s, want %s\n", got2, tt.want)
|
||||
t.Errorf("hubServer2.Args.Port = %s\n", hubServer2.Args.Port)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
69
server/search_test.go
Normal file
69
server/search_test.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package server_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
pb "github.com/lbryio/hub/protobuf/go"
|
||||
server "github.com/lbryio/hub/server"
|
||||
"github.com/olivere/elastic/v7"
|
||||
)
|
||||
|
||||
func TestInt32ArrToInterface(t *testing.T) {
|
||||
want := []int32{0, 10, 100}
|
||||
got := server.Int32ArrToInterface(want)
|
||||
for i, x := range got {
|
||||
if x.(int32) != want[i] {
|
||||
t.Errorf("flags: got: %v, want: %v\n", x, want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrArrToInterface(t *testing.T) {
|
||||
want := []string{"asdf", "qwer", "xczv"}
|
||||
got := server.StrArrToInterface(want)
|
||||
for i, x := range got {
|
||||
if strings.Compare(x.(string), want[i]) != 0 {
|
||||
t.Errorf("flags: got: %v, want: %v\n", x, want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddTermsField(t *testing.T) {
|
||||
name := "qwer"
|
||||
arr := []string{"a", "b", "c"}
|
||||
var query *elastic.BoolQuery = elastic.NewBoolQuery()
|
||||
query = server.AddTermsField(query, arr, name)
|
||||
fmt.Printf("query: %v\n", query)
|
||||
}
|
||||
|
||||
func TestSearch(t *testing.T) {
|
||||
handler := http.NotFound
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler(w, r)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
handler = func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := `{}`
|
||||
|
||||
w.Write([]byte(resp))
|
||||
}
|
||||
|
||||
context := context.Background()
|
||||
args := makeDefaultArgs()
|
||||
hubServer := server.MakeHubServer(context, args)
|
||||
req := &pb.SearchRequest{
|
||||
Text: "asdf",
|
||||
}
|
||||
out, err := hubServer.Search(context, req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
log.Println(out)
|
||||
}
|
9
server/server_test_pkg.go
Normal file
9
server/server_test_pkg.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package server
|
||||
|
||||
func (s *Server) AddPeerExported() func(*Peer, bool, bool) error {
|
||||
return s.addPeer
|
||||
}
|
||||
|
||||
func (s *Server) GetNumPeersExported() func() int64 {
|
||||
return s.getNumPeers
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package server
|
||||
package server_test
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
server "github.com/lbryio/hub/server"
|
||||
)
|
||||
|
||||
// TestUDPPing tests UDPPing correctness against prod server.
|
||||
|
@ -36,7 +38,7 @@ func TestUDPPing(t *testing.T) {
|
|||
toAddr := "spv16.lbry.com"
|
||||
toPort := "50001"
|
||||
|
||||
pong, err := UDPPing(toAddr, toPort)
|
||||
pong, err := server.UDPPing(toAddr, toPort)
|
||||
gotCountry := pong.DecodeCountry()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
|
Loading…
Reference in a new issue