Add RepostedCount, EffectiveAmount prefix rows #51

Merged
moodyjon merged 3 commits from schema_catchup2 into master 2022-08-26 15:24:40 +02:00
5 changed files with 98 additions and 26 deletions
Showing only changes of commit 6075f7207c - Show all commits

View file

@ -212,28 +212,25 @@ func (db *ReadOnlyDBColumnFamily) GetRepost(claimHash []byte) ([]byte, error) {
} }
func (db *ReadOnlyDBColumnFamily) GetRepostedCount(claimHash []byte) (int, error) { func (db *ReadOnlyDBColumnFamily) GetRepostedCount(claimHash []byte) (int, error) {
handle, err := db.EnsureHandle(prefixes.RepostedClaim) handle, err := db.EnsureHandle(prefixes.RepostedCount)
if err != nil { if err != nil {
return 0, err return 0, err
} }
key := prefixes.NewRepostedKey(claimHash) key := prefixes.RepostedCountKey{Prefix: []byte{prefixes.RepostedCount}, ClaimHash: claimHash}
keyPrefix := key.PartialPack(1) rawKey := key.PackKey()
// 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 slice, err := db.DB.GetCF(db.Opts, handle, rawKey)
ch := IterCF(db.DB, options) defer slice.Free()
if err != nil {
for range ch { return 0, err
i++ } 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) { 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) { func (db *ReadOnlyDBColumnFamily) GetEffectiveAmount(claimHash []byte, supportOnly bool) (uint64, error) {
if supportOnly {
supportAmount, err := db.GetActiveAmount(claimHash, prefixes.ActivatedSupportTXOType, db.Height+1) supportAmount, err := db.GetActiveAmount(claimHash, prefixes.ActivatedSupportTXOType, db.Height+1)
if err != nil { if err != nil {
return 0, err return 0, err
} }
if supportOnly {
return supportAmount, nil return supportAmount, nil
} }
activationAmount, err := db.GetActiveAmount(claimHash, prefixes.ActivateClaimTXOType, db.Height+1) handle, err := db.EnsureHandle(prefixes.EffectiveAmount)
if err != nil { if err != nil {
return 0, err 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) { func (db *ReadOnlyDBColumnFamily) GetSupportAmount(claimHash []byte) (uint64, error) {

View file

@ -331,6 +331,7 @@ func TestGetDBState(t *testing.T) {
} }
func TestGetRepostedClaim(t *testing.T) { func TestGetRepostedClaim(t *testing.T) {
t.Skip("skipping obsolete? test of prefix W (Reposted)")
channelHash, _ := hex.DecodeString("2556ed1cab9d17f2a9392030a9ad7f5d138f11bd") channelHash, _ := hex.DecodeString("2556ed1cab9d17f2a9392030a9ad7f5d138f11bd")
want := 5 want := 5
// Should be non-existent // 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) { func TestPrintRepost(t *testing.T) {
filePath := "../testdata/V_resolve.csv" filePath := "../testdata/V_resolve.csv"
CatCSV(filePath) 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" filePath := "../testdata/S_resolve.csv"
want := uint64(586370959900) want := uint64(78999149300)
claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd" claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd"
claimHash, _ := hex.DecodeString(claimHashStr) claimHash, _ := hex.DecodeString(claimHashStr)
db, _, toDefer, err := OpenAndFillTmpDBColumnFamlies(filePath) 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) { func TestGetSupportAmount(t *testing.T) {
want := uint64(8654754160700) want := uint64(8654754160700)
claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd" claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd"

View file

@ -1,6 +1,6 @@
S,, S,,
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a6b67006286030000,0000007615cbad28 S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd01000a6b67006286030000,0000007615cbad28
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a706a0063105c0000,000000000bebc200 S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd01000a706a0063105c0000,000000000bebc200
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a73ea006367550000,0000000005f5e100 S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a73ea006367550000,0000000005f5e100
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7d63006469750000,0000000db0b7c894 S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7d63006469750000,0000000db0b7c894
moodyjon commented 2022-08-23 18:39:01 +02:00 (Migrated from github.com)
Review

I changed 2 of these rows to be ActivateClaimTXOType (1) instead of ActivatedSupportTXOType (2)..

I changed 2 of these rows to be ActivateClaimTXOType (1) instead of ActivatedSupportTXOType (2)..
S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7ebf00648c480000,00000000b2d05e00 S,532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7ebf00648c480000,00000000b2d05e00

1 S
2 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a6b67006286030000 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd01000a6b67006286030000 0000007615cbad28
3 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a706a0063105c0000 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd01000a706a0063105c0000 000000000bebc200
4 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a73ea006367550000 0000000005f5e100
5 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7d63006469750000 0000000db0b7c894
6 S 532556ed1cab9d17f2a9392030a9ad7f5d138f11bd02000a7ebf00648c480000 00000000b2d05e00

4
testdata/i_resolve.csv vendored Normal file
View file

@ -0,0 +1,4 @@
i,,
i,692556ed1cab9d17f2a9392030a9ad7f5d138f11bd,0000007615cbad28
i,692556ed1cab9d17f2a9392030a9ad7f5d138faf01,000000000bebc200
i,692556ed1cab9d17f2a9392030a9ad7f5d138fb074,0000000005f5e100
1 i
2 i 692556ed1cab9d17f2a9392030a9ad7f5d138f11bd 0000007615cbad28
3 i 692556ed1cab9d17f2a9392030a9ad7f5d138faf01 000000000bebc200
4 i 692556ed1cab9d17f2a9392030a9ad7f5d138fb074 0000000005f5e100

4
testdata/j_resolve.csv vendored Normal file
View file

@ -0,0 +1,4 @@
j,,
j,6a2556ed1cab9d17f2a9392030a9ad7f5d138f11bd,00000005
j,6a255761310145baa958b5587d9b5571423e5a0d3c,00000005
j,6a255761310145baa958b5587d9b5571423f00c85b,0000000a
1 j
2 j 6a2556ed1cab9d17f2a9392030a9ad7f5d138f11bd 00000005
3 j 6a255761310145baa958b5587d9b5571423e5a0d3c 00000005
4 j 6a255761310145baa958b5587d9b5571423f00c85b 0000000a