From 60b6c345a864fc0697fb710cceed697d1f274d7e Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Wed, 14 Sep 2022 15:51:21 -0500 Subject: [PATCH] Adjust EffectiveAmountValue to include ActivatedSupportSum. Make use of this in GetEffectiveAmount() tests. --- db/db_get.go | 18 ++++++++-------- db/db_test.go | 40 +++++++++++++++++++++++++++--------- db/prefixes/prefixes.go | 17 +++++++++------ db/prefixes/prefixes_test.go | 2 +- testdata/Si_resolve.csv | 18 ++++++++++++++++ testdata/i.csv | 40 ++++++++++++++++++------------------ 6 files changed, 88 insertions(+), 47 deletions(-) create mode 100644 testdata/Si_resolve.csv diff --git a/db/db_get.go b/db/db_get.go index 13771f0..54c3fe8 100644 --- a/db/db_get.go +++ b/db/db_get.go @@ -446,7 +446,7 @@ func (db *ReadOnlyDBColumnFamily) GetActiveAmount(claimHash []byte, txoType uint } startKey := prefixes.NewActiveAmountKey(claimHash, txoType, 0) - endKey := prefixes.NewActiveAmountKey(claimHash, txoType, height) + endKey := prefixes.NewActiveAmountKey(claimHash, txoType, height+1) startKeyRaw := startKey.PartialPack(3) endKeyRaw := endKey.PartialPack(3) @@ -467,14 +467,6 @@ func (db *ReadOnlyDBColumnFamily) GetActiveAmount(claimHash []byte, txoType uint } func (db *ReadOnlyDBColumnFamily) GetEffectiveAmount(claimHash []byte, supportOnly bool) (uint64, error) { - if supportOnly { - supportAmount, err := db.GetActiveAmount(claimHash, prefixes.ActivatedSupportTXOType, db.Height+1) - if err != nil { - return 0, err - } - return supportAmount, nil - } - handle, err := db.EnsureHandle(prefixes.EffectiveAmount) if err != nil { return 0, err @@ -492,7 +484,13 @@ func (db *ReadOnlyDBColumnFamily) GetEffectiveAmount(claimHash []byte, supportOn value := prefixes.EffectiveAmountValue{} value.UnpackValue(slice.Data()) - return value.EffectiveAmount, nil + var amount uint64 + if supportOnly { + amount += value.ActivatedSupportSum + } else { + amount += value.ActivatedSum + } + return amount, nil } func (db *ReadOnlyDBColumnFamily) GetSupportAmount(claimHash []byte) (uint64, error) { diff --git a/db/db_test.go b/db/db_test.go index c25bd10..85ed7ec 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -571,47 +571,67 @@ func TestGetClaimToChannel(t *testing.T) { } func TestGetEffectiveAmountSupportOnly(t *testing.T) { - filePath := "../testdata/S_resolve.csv" - want := uint64(78999149300) - claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd" + filePath := "../testdata/Si_resolve.csv" + want := uint64(20000006) + claimHashStr := "00000324e40fcb63a0b517a3660645e9bd99244a" claimHash, _ := hex.DecodeString(claimHashStr) db, _, toDefer, err := OpenAndFillTmpDBColumnFamlies(filePath) if err != nil { t.Error(err) } defer toDefer() - db.Height = 1116054 + db.Height = 999999999 amount, err := db.GetEffectiveAmount(claimHash, true) if err != nil { t.Error(err) } - if amount != want { t.Errorf("Expected %d, got %d", want, amount) } + + // Cross-check against iterator-based implementation. + iteratorAmount, err := db.GetActiveAmount(claimHash, prefixes.ActivatedSupportTXOType, db.Height) + if err != nil { + t.Error(err) + } + if iteratorAmount != want { + t.Errorf("Expected %d, got %d", want, iteratorAmount) + } } func TestGetEffectiveAmount(t *testing.T) { - filePath := "../testdata/i_resolve.csv" - want := uint64(507171810600) - claimHashStr := "2556ed1cab9d17f2a9392030a9ad7f5d138f11bd" + filePath := "../testdata/Si_resolve.csv" + want := uint64(21000006) + claimHashStr := "00000324e40fcb63a0b517a3660645e9bd99244a" claimHash, _ := hex.DecodeString(claimHashStr) db, _, toDefer, err := OpenAndFillTmpDBColumnFamlies(filePath) if err != nil { t.Error(err) } defer toDefer() - db.Height = 1116054 + db.Height = 999999999 amount, err := db.GetEffectiveAmount(claimHash, false) if err != nil { t.Error(err) } - if amount != want { t.Errorf("Expected %d, got %d", want, amount) } + + // Cross-check against iterator-based implementation. + iteratorAmount1, err := db.GetActiveAmount(claimHash, prefixes.ActivatedSupportTXOType, db.Height) + if err != nil { + t.Error(err) + } + iteratorAmount2, err := db.GetActiveAmount(claimHash, prefixes.ActivateClaimTXOType, db.Height) + if err != nil { + t.Error(err) + } + if iteratorAmount1+iteratorAmount2 != want { + t.Errorf("Expected %d, got %d (%d + %d)", want, iteratorAmount1+iteratorAmount2, iteratorAmount1, iteratorAmount2) + } } func TestGetSupportAmount(t *testing.T) { diff --git a/db/prefixes/prefixes.go b/db/prefixes/prefixes.go index e4b628a..76ac64d 100644 --- a/db/prefixes/prefixes.go +++ b/db/prefixes/prefixes.go @@ -3530,7 +3530,8 @@ type EffectiveAmountKey struct { } type EffectiveAmountValue struct { - EffectiveAmount uint64 `json:"effective_amount"` + ActivatedSum uint64 `json:"activated_sum"` + ActivatedSupportSum uint64 `json:"activated_support_sum"` } func (kv *EffectiveAmountKey) NumFields() int { @@ -3564,19 +3565,23 @@ func (kv *EffectiveAmountKey) UnpackKey(buf []byte) { } func (kv *EffectiveAmountValue) PackValue() []byte { - // b'>Q' - n := 8 + // b'>QQ' + n := 8 + 8 buf := make([]byte, n) offset := 0 - binary.BigEndian.PutUint64(buf[offset:], kv.EffectiveAmount) + binary.BigEndian.PutUint64(buf[offset:], kv.ActivatedSum) + offset += 8 + binary.BigEndian.PutUint64(buf[offset:], kv.ActivatedSupportSum) offset += 8 return buf[:offset] } func (kv *EffectiveAmountValue) UnpackValue(buf []byte) { - // b'>Q' + // b'>QQ' offset := 0 - kv.EffectiveAmount = binary.BigEndian.Uint64(buf[offset:]) + kv.ActivatedSum = binary.BigEndian.Uint64(buf[offset:]) + offset += 8 + kv.ActivatedSupportSum = binary.BigEndian.Uint64(buf[offset:]) offset += 8 } diff --git a/db/prefixes/prefixes_test.go b/db/prefixes/prefixes_test.go index 2a8a94a..125c5ef 100644 --- a/db/prefixes/prefixes_test.go +++ b/db/prefixes/prefixes_test.go @@ -429,7 +429,7 @@ func TestHashXMempoolStatus(t *testing.T) { func TestEffectiveAmount(t *testing.T) { prefix := byte(prefixes.EffectiveAmount) filePath := fmt.Sprintf("../../testdata/%c.csv", prefix) - //synthesizeTestData([]byte{prefix}, filePath, []int{20}, []int{8}, [][3]int{}) + //synthesizeTestData([]byte{prefix}, filePath, []int{20}, []int{8, 8}, [][3]int{}) key := &prefixes.EffectiveAmountKey{} testGeneric(filePath, prefix, key.NumFields())(t) } diff --git a/testdata/Si_resolve.csv b/testdata/Si_resolve.csv new file mode 100644 index 0000000..578ccb2 --- /dev/null +++ b/testdata/Si_resolve.csv @@ -0,0 +1,18 @@ +Si,, +S,53000000a420c44374f4f399ab4807fa1901eefc8701000e94ad0297ec210000,00000000000f4240 +S,53000000c27eef5ea69e0d73f118826c7e326bb46901000773de00371d660000,000000001dcd6500 +S,5300000110e40894573f528c393fbcec7a472ec85301000d069c01516b320000,0000000000989680 +S,5300000324e40fcb63a0b517a3660645e9bd99244a01000f2fd8030bb6ba0000,00000000000f4240 +S,5300000324e40fcb63a0b517a3660645e9bd99244a02000f2ff4030bc8a50000,0000000001312d00 +S,5300000324e40fcb63a0b517a3660645e9bd99244a02000f2ff6030bc8b00000,0000000000000003 +S,5300000324e40fcb63a0b517a3660645e9bd99244a02000f2ff7030bc8b10000,0000000000000002 +S,5300000324e40fcb63a0b517a3660645e9bd99244a02000f2ff9030bc8cf0000,0000000000000001 +S,53000003d1538a0f19f5cd4bc1a62cc294f5c8993401000c816a011c7c990000,00000000000f4240 +S,53000008d47beeff8325e795a8604226145b01702b01000ef1ed02dbb2a20000,00000000000186a0 +S,5300000906499e073e94370ceff37cb21c2821244401000fa7c40369842d0000,00000000000186a0 +S,5300000906499e073e94370ceff37cb21c2821244402000fa7c403698fff0000,00000000000000a1 +S,5300000906499e073e94370ceff37cb21c2821244402000fa7c80369f0010000,000000000000000f +S,53000009c3172e034a255f3c03566dca84bb9f046a01000e07020225c69c0000,000000000007a120 +S,53000009ca6e0caaaef16872b4bd4f6f1b8c2363e201000eb5af02b169560000,00000000000f4240 +i,6900000324e40fcb63a0b517a3660645e9bd99244a,0000000001406f460000000001312d06 +i,6900000906499e073e94370ceff37cb21c28212444,000000000001875000000000000000b0 diff --git a/testdata/i.csv b/testdata/i.csv index d4b61f4..63a28ad 100644 --- a/testdata/i.csv +++ b/testdata/i.csv @@ -1,21 +1,21 @@ i, -6903dc9970183b8a05d118c84a37c355fe34d95d01,0e74dd23295a4610 -6916bd29750d8d92b32677eda07e10af313c0019d9,ff0579207ec6e594 -6926bdfcb4a1ad81f460ad561283584695cd6cea59,b834b13a8918262f -6930776827481ec15fa07e0dc266e376846467237d,4bf0a5127a1216dc -6955a1eaf08f9468a6c3565fe16b2ae4b726045538,e32029de8b58dd6e -69614fa6bc0cea1366377456bc88dda9ec7b6d4c3c,55bf2d8e0e262697 -6971e7b039dde5797ae167b9783f87c4a67711799d,9197b827b560fc34 -697765a71d8a4082d056baaae83f8b4af1e124f5e9,62c0d5dfde7ef884 -6993e121642c01e2edca50d565ff3df1a656e83ebd,1796c74886d45045 -69af0684076bc64adcbcd621de9c09fd15dade3e17,f9240ab9a9650d9f -69b70fdcc95d3b51ec28872a143c7a6fc947e6a58e,a8950968d95759a9 -69bdb90916f78badec506a895b0edceb47533297f9,331c0ca597601ed7 -69c276b7070ba79e75c82e4d97a70e4428dd3be058,85c61c842e5bfe7f -69cb8215b0c9440227a9e7e76bce468bdb4fa0f714,9c42e1ba41275362 -69d2556fe7b8fce36a71c78e0001452298903ef81b,f61cf52e7e645bf8 -69d2677078f1448c0e4295711e61a85a9fb6a280d1,28d57b45b1700cb3 -69dd979490444cab3efc1e517f83369df6e2e279a3,dad1b5133cc15dd4 -69e6e6834cf0da132c77af275cbab9885cbbc3f022,df55f9fd0278ca71 -69ea25444b8d0ab22ac355f4b692b0d7006b672f4a,0703f38a1428ff8e -69fcb79c5e463ac4a0e078f9cd24e2c718b66c40d6,5be7b71b6dca5a20 +691d3476414324a257c62079b055446cdfdb58fcb7,3fc1f36ad9acdae3160db55befe1fdcf +692514be0d49b250058c2c59c62b07383705d71c54,055bf209d8b7132f1743aee761d9c64d +6932de7e3a7bae762b9533e955fd2b2444f6656aa7,9371781e0655df3b914d18da42605b3d +6938e5c7d134233b0758e54f5eacb9dcee412f51f9,12079ef8dffde085385b2aafe7e8be53 +693a56c48c3ec6bc90fdd02e0f0e2f01472c3f13f5,8c422f4f35e4170079a13f3e9f25a9db +693fe5c0979c7c4892486d478c8c57b75f0fa9bba3,8eeaafae4e906ccc36ec29bc3d4f1676 +694abea2af1c27003a1c777e84891a0f81b3b5a382,fe24b3d28f8cf49fad2d4548726ac8bd +694c245cf621a28925da7d84e77137a1d54085d1b8,c04cf11c401c4fbc8976825e9b6db9ca +6951010e69f84df87d09cdae7706e25ecdc10a8a6f,a93d6f9c06d1e807c1d674828167cd7c +695661d8955be621b5f44c7965f517c17d2d1d48c6,8b945701a1d2920c3e6283ab7fda14ee +696fac500a5674eaa078abc71ceb48006c14a3f6aa,1f8000aec89b229349aa154e72fd4de3 +697506379203bd2f8980d13c766966e400509e28f9,5ce938e06a98aa8b8bb0cfea5dce7e33 +6975c5f2cdc6e8fdb64682557b1bcbb92f52c6113f,2817aa0f0806bb276e1af4af42504720 +6984b87daaba891147b4c0f25c15703b2640df9833,169009ea3ff014d352a13152c8d39999 +699f3d1a3f634bb035c626cdfa927caa59e2617bc4,8f3e2352ed874155a3aa3dd90a61430e +69b9dfcdaced1d6d696fab02be31cbee50cbffcdf9,281d9817a5360c1be0ac7710132adebe +69ca1fa3e939d061d74163b2da17c3d2b926484c5e,40ecc3bd5dc6b871ce3095456e043427 +69cea59483161df9420027c4328f85382871798ee4,3919862cc4f0f910a954ffc4d08a6195 +69d8ff3b5f44f5585e7d7e1349d1d62ba3fbe6831c,d48bd4f6c44ef8b6aabeb6b6e1a61894 +69f22b1918f28b1e10d2946f84f6f3c8fa25865ba3,b49011d36a56e0dbe5a5cbce94159f66