Skip to content

Commit

Permalink
Update db_get.go helpers to use EffectiveAmount, RepostedCount
Browse files Browse the repository at this point in the history
tables. Update tests.
  • Loading branch information
moodyjon committed Aug 23, 2022
1 parent 5fb3b43 commit 6075f72
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 26 deletions.
52 changes: 30 additions & 22 deletions db/db_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

var i int = 0
ch := IterCF(db.DB, options)
key := prefixes.RepostedCountKey{Prefix: []byte{prefixes.RepostedCount}, ClaimHash: claimHash}
rawKey := key.PackKey()

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) {
Expand Down Expand Up @@ -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) {
Expand Down
60 changes: 58 additions & 2 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions testdata/S_resolve.csv
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions testdata/i_resolve.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
i,,
i,692556ed1cab9d17f2a9392030a9ad7f5d138f11bd,0000007615cbad28
i,692556ed1cab9d17f2a9392030a9ad7f5d138faf01,000000000bebc200
i,692556ed1cab9d17f2a9392030a9ad7f5d138fb074,0000000005f5e100
4 changes: 4 additions & 0 deletions testdata/j_resolve.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
j,,
j,6a2556ed1cab9d17f2a9392030a9ad7f5d138f11bd,00000005
j,6a255761310145baa958b5587d9b5571423e5a0d3c,00000005
j,6a255761310145baa958b5587d9b5571423f00c85b,0000000a

0 comments on commit 6075f72

Please sign in to comment.