diff --git a/db/db_get.go b/db/db_get.go index d9857b9..d561908 100644 --- a/db/db_get.go +++ b/db/db_get.go @@ -212,28 +212,25 @@ func (db *ReadOnlyDBColumnFamily) GetRepost(claimHash []byte) ([]byte, error) { } func (db *ReadOnlyDBColumnFamily) GetRepostedCount(claimHash []byte) (int, error) { - handle, err := db.EnsureHandle(prefixes.RepostedClaim) + handle, err := db.EnsureHandle(prefixes.RepostedCount) if err != nil { return 0, err } - key := prefixes.NewRepostedKey(claimHash) - keyPrefix := key.PartialPack(1) - // Prefix and handle - options := NewIterateOptions().WithPrefix(keyPrefix).WithCfHandle(handle) - // Start and stop bounds - // options = options.WithStart(keyPrefix) - // Don't include the key - options = options.WithIncludeValue(false) + key := prefixes.RepostedCountKey{Prefix: []byte{prefixes.RepostedCount}, ClaimHash: claimHash} + rawKey := key.PackKey() - var i int = 0 - ch := IterCF(db.DB, options) - - for range ch { - i++ + slice, err := db.DB.GetCF(db.Opts, handle, rawKey) + defer slice.Free() + if err != nil { + return 0, err + } else if slice.Size() == 0 { + return 0, nil } - return i, nil + value := prefixes.RepostedCountValue{} + value.UnpackValue(slice.Data()) + return int(value.RepostedCount), nil } func (db *ReadOnlyDBColumnFamily) GetChannelForClaim(claimHash []byte, txNum uint32, position uint16) ([]byte, error) { @@ -286,21 +283,32 @@ func (db *ReadOnlyDBColumnFamily) GetActiveAmount(claimHash []byte, txoType uint } func (db *ReadOnlyDBColumnFamily) GetEffectiveAmount(claimHash []byte, supportOnly bool) (uint64, error) { - supportAmount, err := db.GetActiveAmount(claimHash, prefixes.ActivatedSupportTXOType, db.Height+1) - if err != nil { - return 0, err - } - if supportOnly { + supportAmount, err := db.GetActiveAmount(claimHash, prefixes.ActivatedSupportTXOType, db.Height+1) + if err != nil { + return 0, err + } return supportAmount, nil } - activationAmount, err := db.GetActiveAmount(claimHash, prefixes.ActivateClaimTXOType, db.Height+1) + handle, err := db.EnsureHandle(prefixes.EffectiveAmount) if err != nil { return 0, err } - return activationAmount + supportAmount, nil + key := prefixes.EffectiveAmountKey{Prefix: []byte{prefixes.EffectiveAmount}, ClaimHash: claimHash} + rawKey := key.PackKey() + slice, err := db.DB.GetCF(db.Opts, handle, rawKey) + defer slice.Free() + if err != nil { + return 0, err + } else if slice.Size() == 0 { + return 0, nil + } + + value := prefixes.EffectiveAmountValue{} + value.UnpackValue(slice.Data()) + return value.EffectiveAmount, nil } func (db *ReadOnlyDBColumnFamily) GetSupportAmount(claimHash []byte) (uint64, error) { diff --git a/db/db_test.go b/db/db_test.go index e3c4081..c25bd10 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -331,6 +331,7 @@ func TestGetDBState(t *testing.T) { } func TestGetRepostedClaim(t *testing.T) { + t.Skip("skipping obsolete? test of prefix W (Reposted)") channelHash, _ := hex.DecodeString("2556ed1cab9d17f2a9392030a9ad7f5d138f11bd") want := 5 // Should be non-existent @@ -363,6 +364,39 @@ func TestGetRepostedClaim(t *testing.T) { } } +func TestGetRepostedCount(t *testing.T) { + channelHash, _ := hex.DecodeString("2556ed1cab9d17f2a9392030a9ad7f5d138f11bd") + want := 5 + // Should be non-existent + channelHash2, _ := hex.DecodeString("2556ed1cab9d17f2a9392030a9ad7f5d138f11bf") + filePath := "../testdata/j_resolve.csv" + db, _, toDefer, err := OpenAndFillTmpDBColumnFamlies(filePath) + if err != nil { + t.Error(err) + } + defer toDefer() + + count, err := db.GetRepostedCount(channelHash) + if err != nil { + t.Error(err) + } + + log.Println(count) + + if count != want { + t.Errorf("Expected %d, got %d", want, count) + } + + count2, err := db.GetRepostedCount(channelHash2) + if err != nil { + t.Error(err) + } + + if count2 != 0 { + t.Errorf("Expected 0, got %d", count2) + } +} + func TestPrintRepost(t *testing.T) { filePath := "../testdata/V_resolve.csv" CatCSV(filePath) @@ -536,9 +570,9 @@ func TestGetClaimToChannel(t *testing.T) { } } -func TestGetEffectiveAmount(t *testing.T) { +func TestGetEffectiveAmountSupportOnly(t *testing.T) { filePath := "../testdata/S_resolve.csv" - want := uint64(586370959900) + want := uint64(78999149300) claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd" claimHash, _ := hex.DecodeString(claimHashStr) db, _, toDefer, err := OpenAndFillTmpDBColumnFamlies(filePath) @@ -558,6 +592,28 @@ func TestGetEffectiveAmount(t *testing.T) { } } +func TestGetEffectiveAmount(t *testing.T) { + filePath := "../testdata/i_resolve.csv" + want := uint64(507171810600) + claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd" + claimHash, _ := hex.DecodeString(claimHashStr) + db, _, toDefer, err := OpenAndFillTmpDBColumnFamlies(filePath) + if err != nil { + t.Error(err) + } + defer toDefer() + db.Height = 1116054 + + amount, err := db.GetEffectiveAmount(claimHash, false) + if err != nil { + t.Error(err) + } + + if amount != want { + t.Errorf("Expected %d, got %d", want, amount) + } +} + func TestGetSupportAmount(t *testing.T) { want := uint64(8654754160700) claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd" diff --git a/testdata/S_resolve.csv b/testdata/S_resolve.csv index 2b9eb8d..1bf3965 100644 --- a/testdata/S_resolve.csv +++ b/testdata/S_resolve.csv @@ -1,6 +1,6 @@ S,, -S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a6b67006286030000,0000007615cbad28 -S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a706a0063105c0000,000000000bebc200 +S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd01000a6b67006286030000,0000007615cbad28 +S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd01000a706a0063105c0000,000000000bebc200 S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a73ea006367550000,0000000005f5e100 S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7d63006469750000,0000000db0b7c894 S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7ebf00648c480000,00000000b2d05e00 diff --git a/testdata/i_resolve.csv b/testdata/i_resolve.csv new file mode 100644 index 0000000..892e9cf --- /dev/null +++ b/testdata/i_resolve.csv @@ -0,0 +1,4 @@ +i,, +i,692556ed1cab9d17f2a9392030a9ad7f5d138f11bd,0000007615cbad28 +i,692556ed1cab9d17f2a9392030a9ad7f5d138faf01,000000000bebc200 +i,692556ed1cab9d17f2a9392030a9ad7f5d138fb074,0000000005f5e100 diff --git a/testdata/j_resolve.csv b/testdata/j_resolve.csv new file mode 100644 index 0000000..9245254 --- /dev/null +++ b/testdata/j_resolve.csv @@ -0,0 +1,4 @@ +j,, +j,6a2556ed1cab9d17f2a9392030a9ad7f5d138f11bd,00000005 +j,6a255761310145baa958b5587d9b5571423e5a0d3c,00000005 +j,6a255761310145baa958b5587d9b5571423f00c85b,0000000a