more tests

This commit is contained in:
Jeffrey Picard 2022-02-22 13:12:34 -05:00
parent 20a426a6cd
commit f282294e07
5 changed files with 60 additions and 25 deletions

View file

@ -220,6 +220,7 @@ func (ps *PathSegment) String() string {
}
// BisectRight returns the index of the first element in the list that is greater than or equal to the value.
// https://stackoverflow.com/questions/29959506/is-there-a-go-analog-of-pythons-bisect-module
func BisectRight(arr []uint32, val uint32) uint32 {
i := sort.Search(len(arr), func(i int) bool { return arr[i] >= val })
return uint32(i)
@ -354,18 +355,9 @@ func GetChannelForClaim(db *ReadOnlyDBColumnFamily, claimHash []byte, txNum uint
func GetActiveAmount(db *ReadOnlyDBColumnFamily, claimHash []byte, txoType uint8, height uint32) (uint64, error) {
cfName := string(prefixes.ActiveAmount)
handle := db.Handles[cfName]
startKey := &prefixes.ActiveAmountKey{
Prefix: []byte{prefixes.ActiveAmount},
ClaimHash: claimHash,
TxoType: txoType,
ActivationHeight: 0,
}
endKey := &prefixes.ActiveAmountKey{
Prefix: []byte{prefixes.ActiveAmount},
ClaimHash: claimHash,
TxoType: txoType,
ActivationHeight: height,
}
startKey := prefixes.NewActiveAmountKey(claimHash, txoType, 0)
endKey := prefixes.NewActiveAmountKey(claimHash, txoType, height)
startKeyRaw := prefixes.ActiveAmountKeyPackPartial(startKey, 3)
endKeyRaw := prefixes.ActiveAmountKeyPackPartial(endKey, 3)
// Prefix and handle
@ -1099,12 +1091,14 @@ func (o *IterOptions) StopIteration(key []byte) bool {
return false
}
maxLen := int(math.Min(float64(len(key)), float64(len(o.Stop))))
// TODO: Look at not doing floating point conversions for this
maxLenStop := int(math.Min(float64(len(key)), float64(len(o.Stop))))
maxLenStart := int(math.Min(float64(len(key)), float64(len(o.Start))))
if o.Stop != nil &&
(bytes.HasPrefix(key, o.Stop) || bytes.Compare(o.Stop, key[:maxLen]) < 0) {
(bytes.HasPrefix(key, o.Stop) || bytes.Compare(o.Stop, key[:maxLenStop]) < 0) {
return true
} else if o.Start != nil &&
bytes.Compare(o.Start, key[:len(o.Start)]) > 0 {
bytes.Compare(o.Start, key[:maxLenStart]) > 0 {
return true
} else if o.Prefix != nil && !bytes.HasPrefix(key, o.Prefix) {
return true

View file

@ -413,26 +413,46 @@ func TestGetTXOToClaim(t *testing.T) {
}
}
// TestGetClaimToTXO Tests getting a ClaimToTXO value from the db.
func TestGetSupportAmount(t *testing.T) {
func TestGetEffectiveAmount(t *testing.T) {
filePath := "../testdata/S_resolve.csv"
want := uint64(586370959900)
claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd"
claimHash, _ := hex.DecodeString(claimHashStr)
db, _, toDefer, err := OpenAndFillTmpDBColumnFamlies(filePath)
if err != nil {
t.Error(err)
}
defer toDefer()
db.Height = 1116054
amount, err := dbpkg.GetEffectiveAmount(db, claimHash, true)
if err != nil {
t.Error(err)
}
if amount != want {
t.Errorf("Expected %d, got %d", want, amount)
}
log.Println(amount)
}
func TestGetSupportAmount(t *testing.T) {
want := uint64(8654754160700)
claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd"
claimHash, err := hex.DecodeString(claimHashStr)
if err != nil {
t.Error(err)
return
}
filePath := "../testdata/a_resolve.csv"
db, _, toDefer, err := OpenAndFillTmpDBColumnFamlies(filePath)
if err != nil {
t.Error(err)
return
}
defer toDefer()
res, err := dbpkg.GetSupportAmount(db, claimHash)
if err != nil {
t.Error(err)
return
}
if res != want {
t.Errorf("Expected %d, got %d", want, res)

View file

@ -2984,6 +2984,15 @@ type ActiveAmountValue struct {
Amount uint64 `json:"amount"`
}
func NewActiveAmountKey(claimHash []byte, txoType uint8, activationHeight uint32) *ActiveAmountKey {
return &ActiveAmountKey{
Prefix: []byte{ActiveAmount},
ClaimHash: claimHash,
TxoType: txoType,
ActivationHeight: activationHeight,
}
}
func (k *ActiveAmountKey) PackKey() []byte {
prefixLen := 1
// b'>20sBLLH'

11
main.go
View file

@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"log"
"os"
@ -110,17 +111,17 @@ func main() {
return
} else if args.CmdType == server.DBCmd3 {
// channelHash, _ := hex.DecodeString("2556ed1cab9d17f2a9392030a9ad7f5d138f11bd")
channelHash, _ := hex.DecodeString("2556ed1cab9d17f2a9392030a9ad7f5d138f11bd")
// name := util.NormalizeName("@Styxhexenhammer666")
txNum := uint32(0x6284e3)
// txNum := uint32(0x6284e3)
// position := uint16(0x0)
// typ := uint8(prefixes.ACTIVATED_CLAIM_TXO_TYPE)
var rawPrefix byte = prefixes.TxHash
var rawPrefix byte = prefixes.ActiveAmount
var startRaw []byte = nil
prefix := []byte{rawPrefix}
columnFamily := string(prefix)
// start := prefixes.NewClaimTakeoverKey(name)
start := prefixes.NewTxHashKey(txNum)
start := prefixes.NewActiveAmountKey(channelHash, prefixes.ACTIVATED_SUPPORT_TXO_TYPE, 0)
startRaw = start.PackKey()
// start := &prefixes.ChannelCountKey{
// Prefix: prefix,
@ -148,7 +149,7 @@ func main() {
options.CfHandle = handles[1]
db.ReadWriteRawNColumnFamilies(dbVal, options, fmt.Sprintf("./testdata/%s_resolve.csv", columnFamily), 1)
db.ReadWriteRawNColumnFamilies(dbVal, options, fmt.Sprintf("./testdata/%s_resolve.csv", columnFamily), 10)
return
}

11
testdata/S_resolve.csv vendored Normal file
View file

@ -0,0 +1,11 @@
S,,
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a6b67006286030000,0000007615cbad28
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a706a0063105c0000,000000000bebc200
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a73ea006367550000,0000000005f5e100
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7d63006469750000,0000000db0b7c894
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7ebf00648c480000,00000000b2d05e00
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a810e0064ccc00000,000000003b9aca00
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a825b006503cf0000,00000002bf52c92c
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a88930066814a0000,00000000dc887a34
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a88f900669d240000,0000000005f5e100
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a88f900669d260000,000000001dcd6500
1 S
2 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a6b67006286030000 0000007615cbad28
3 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a706a0063105c0000 000000000bebc200
4 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a73ea006367550000 0000000005f5e100
5 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7d63006469750000 0000000db0b7c894
6 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7ebf00648c480000 00000000b2d05e00
7 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a810e0064ccc00000 000000003b9aca00
8 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a825b006503cf0000 00000002bf52c92c
9 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a88930066814a0000 00000000dc887a34
10 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a88f900669d240000 0000000005f5e100
11 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a88f900669d260000 000000001dcd6500