From f282294e079e280f9b7986a35ed23d9138c29486 Mon Sep 17 00:00:00 2001 From: Jeffrey Picard Date: Tue, 22 Feb 2022 13:12:34 -0500 Subject: [PATCH] more tests --- db/db.go | 24 +++++++++--------------- db/db_test.go | 30 +++++++++++++++++++++++++----- db/prefixes/prefixes.go | 9 +++++++++ main.go | 11 ++++++----- testdata/S_resolve.csv | 11 +++++++++++ 5 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 testdata/S_resolve.csv diff --git a/db/db.go b/db/db.go index 60042f3..f86709e 100644 --- a/db/db.go +++ b/db/db.go @@ -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 diff --git a/db/db_test.go b/db/db_test.go index f391e29..e565cc3 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -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) diff --git a/db/prefixes/prefixes.go b/db/prefixes/prefixes.go index 16a3093..95f282a 100644 --- a/db/prefixes/prefixes.go +++ b/db/prefixes/prefixes.go @@ -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' diff --git a/main.go b/main.go index 177904a..202b37f 100644 --- a/main.go +++ b/main.go @@ -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 } diff --git a/testdata/S_resolve.csv b/testdata/S_resolve.csv new file mode 100644 index 0000000..2b9eb8d --- /dev/null +++ b/testdata/S_resolve.csv @@ -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