diff --git a/db/prefixes/prefixes.go b/db/prefixes/prefixes.go index e0987d0..90a0a2a 100644 --- a/db/prefixes/prefixes.go +++ b/db/prefixes/prefixes.go @@ -632,17 +632,108 @@ class ActivationValue(typing.NamedTuple): type ActivationKey struct { Prefix []byte `json:"prefix"` - TxType int32 `json:"txo_type"` - TxNum int32 `json:"txo_num"` - Position int32 `json:"position"` + TxoType uint8 `json:"txo_type"` + TxNum uint32 `json:"tx_num"` + Position uint16 `json:"position"` } type ActivationValue struct { - Height int32 `json:"height"` + Height uint32 `json:"height"` ClaimHash []byte `json:"claim_hash"` NormalizedName string `json:"normalized_name"` } +func (k *ActivationKey) PackKey() []byte { + prefixLen := 1 + // b'>BLH' + n := prefixLen + 1 + 4 + 2 + key := make([]byte, n) + copy(key, k.Prefix) + copy(key[prefixLen:], []byte{k.TxoType}) + binary.BigEndian.PutUint32(key[prefixLen+1:], k.TxNum) + binary.BigEndian.PutUint16(key[prefixLen+5:], k.Position) + + return key +} + +func (v *ActivationValue) PackValue() []byte { + nameLen := len(v.NormalizedName) + n := 4 + 20 + 2 + nameLen + value := make([]byte, n) + binary.BigEndian.PutUint32(value, v.Height) + copy(value[4:], v.ClaimHash[:20]) + binary.BigEndian.PutUint16(value[24:], uint16(nameLen)) + copy(value[26:], []byte(v.NormalizedName)) + + return value +} + +func ActivationKeyPackPartialNFields(nFields int) func(*ActivationKey) []byte { + return func(u *ActivationKey) []byte { + return ActivationKeyPackPartial(u, nFields) + } +} + +func ActivationKeyPackPartial(k *ActivationKey, nFields int) []byte { + // Limit nFields between 0 and number of fields, we always at least need + // the prefix, and we never need to iterate past the number of fields. + if nFields > 3 { + nFields = 3 + } + if nFields < 0 { + nFields = 0 + } + + prefixLen := 1 + var n = prefixLen + for i := 0; i <= nFields; i++ { + switch i { + case 1: + n += 1 + case 2: + n += 4 + case 3: + n += 2 + } + } + + key := make([]byte, n) + + for i := 0; i <= nFields; i++ { + switch i { + case 0: + copy(key, k.Prefix) + case 1: + key[prefixLen] = k.TxoType + case 2: + binary.BigEndian.PutUint32(key[prefixLen+1:], k.TxNum) + case 3: + binary.BigEndian.PutUint16(key[prefixLen+5:], k.Position) + } + } + + return key +} + +func ActivationKeyUnpack(key []byte) *ActivationKey { + prefixLen := 1 + return &ActivationKey{ + Prefix: key[:prefixLen], + TxoType: key[prefixLen], + TxNum: binary.BigEndian.Uint32(key[prefixLen+1:]), + Position: binary.BigEndian.Uint16(key[prefixLen+5:]), + } +} + +func ActivationValueUnpack(value []byte) *ActivationValue { + nameLen := binary.BigEndian.Uint16(value[24:]) + return &ActivationValue{ + Height: binary.BigEndian.Uint32(value), + ClaimHash: value[4 : 20+4], + NormalizedName: string(value[26 : 26+nameLen]), + } +} + /* class ActiveAmountKey(typing.NamedTuple): @@ -1522,8 +1613,9 @@ func UnpackGenericKey(key []byte) (byte, interface{}, error) { case ClaimTakeover: case PendingActivation: - case ActivatedClaimAndSupport: return 0x0, nil, errors.Base("key unpack function for %v not implemented", firstByte) + case ActivatedClaimAndSupport: + return ActivatedClaimAndSupport, ActivationKeyUnpack(key), nil case ActiveAmount: return ActiveAmount, ActiveAmountKeyUnpack(key), nil @@ -1584,8 +1676,9 @@ func UnpackGenericValue(key, value []byte) (byte, interface{}, error) { case ClaimTakeover: case PendingActivation: - case ActivatedClaimAndSupport: return 0x0, nil, errors.Base("value unpack not implemented for key %v", key) + case ActivatedClaimAndSupport: + return ActivatedClaimAndSupport, ActivationValueUnpack(value), nil case ActiveAmount: return ActiveAmount, ActiveAmountValueUnpack(value), nil diff --git a/db/prefixes/prefixes_test.go b/db/prefixes/prefixes_test.go index 18778af..860e6c7 100644 --- a/db/prefixes/prefixes_test.go +++ b/db/prefixes/prefixes_test.go @@ -44,6 +44,90 @@ func testInit(filePath string) (*grocksdb.DB, [][]string, func()) { return db, records, toDefer } +func TestActivated(t *testing.T) { + + filePath := "../../resources/activated_claim_and_support.csv" + + wOpts := grocksdb.NewDefaultWriteOptions() + db, records, toDefer := testInit(filePath) + defer toDefer() + for _, record := range records { + key, err := hex.DecodeString(record[0]) + if err != nil { + log.Println(err) + } + val, err := hex.DecodeString(record[1]) + if err != nil { + log.Println(err) + } + db.Put(wOpts, key, val) + } + // test prefix + options := dbpkg.NewIterateOptions().WithPrefix([]byte{prefixes.ActivatedClaimAndSupport}).WithIncludeValue(true) + ch := dbpkg.Iter(db, options) + var i = 0 + for kv := range ch { + // log.Println(kv.Key) + gotKey := kv.Key.(*prefixes.ActivationKey).PackKey() + + keyPartial1 := prefixes.ActivationKeyPackPartial(kv.Key.(*prefixes.ActivationKey), 1) + keyPartial2 := prefixes.ActivationKeyPackPartial(kv.Key.(*prefixes.ActivationKey), 2) + keyPartial3 := prefixes.ActivationKeyPackPartial(kv.Key.(*prefixes.ActivationKey), 3) + + // Check pack partial for sanity + if !bytes.HasPrefix(gotKey, keyPartial1) { + t.Errorf("%+v should be prefix of %+v\n", keyPartial1, gotKey) + } + if !bytes.HasPrefix(gotKey, keyPartial2) { + t.Errorf("%+v should be prefix of %+v\n", keyPartial2, gotKey) + } + if !bytes.HasPrefix(gotKey, keyPartial3) { + t.Errorf("%+v should be prefix of %+v\n", keyPartial3, gotKey) + } + + got := kv.Value.(*prefixes.ActivationValue).PackValue() + wantKey, err := hex.DecodeString(records[i][0]) + if err != nil { + log.Println(err) + } + want, err := hex.DecodeString(records[i][1]) + if err != nil { + log.Println(err) + } + if !bytes.Equal(gotKey, wantKey) { + t.Errorf("gotKey: %+v, wantKey: %+v\n", got, want) + } + if !bytes.Equal(got, want) { + t.Errorf("got: %+v, want: %+v\n", got, want) + } + i++ + } + + // Test start / stop + start, err := hex.DecodeString(records[0][0]) + if err != nil { + log.Println(err) + } + stop, err := hex.DecodeString(records[9][0]) + if err != nil { + log.Println(err) + } + options2 := dbpkg.NewIterateOptions().WithStart(start).WithStop(stop).WithIncludeValue(true) + ch2 := dbpkg.Iter(db, options2) + i = 0 + for kv := range ch2 { + got := kv.Value.(*prefixes.ActivationValue).PackValue() + want, err := hex.DecodeString(records[i][1]) + if err != nil { + log.Println(err) + } + if !bytes.Equal(got, want) { + t.Errorf("got: %+v, want: %+v\n", got, want) + } + i++ + } +} + func TestActiveAmount(t *testing.T) { filePath := "../../resources/active_amount.csv" diff --git a/main.go b/main.go index 2831cdd..5ed3693 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { options := &db.IterOptions{ FillCache: false, - Prefix: []byte{prefixes.ActiveAmount}, + Prefix: []byte{prefixes.ActivatedClaimAndSupport}, Start: nil, Stop: nil, IncludeStart: true, @@ -49,7 +49,7 @@ func main() { RawValue: true, } - db.ReadWriteRawN(dbVal, options, "./resources/active_amount.csv", 10) + db.ReadWriteRawN(dbVal, options, "./resources/activated_claim_and_support.csv", 10) return } diff --git a/resources/activated_claim_and_support.csv b/resources/activated_claim_and_support.csv new file mode 100644 index 0000000..6e4f8f5 --- /dev/null +++ b/resources/activated_claim_and_support.csv @@ -0,0 +1,10 @@ +520100162aa70000,00021b3dc78ac4c326cd43cdc0c844b7cea13659449ab3e40015746573742d70686f746f2d7374726173626f757267 +520100162f600000,00021c27ebf95f7fdb89db5467bb1b88ea3b0f0f7ee5ce360003636e63 +5201001630960000,00021c63a6f91a86837ab84a4cf0d2dcbe94704a528cf820000f776f6e646572776f6d616e31393933 +5201001635e60000,00021d689673cc2a1aac64d7b2742705abfb09fca30d7e0500056d6d61736b +5201001638a80000,00021f4bc39342066646dc50f1a9954b41684d157b035dac00036f6e65 +5201001645ef0001,000220144689c1ccb4420309f93ab98799b28c49fa4d3809000a65617379737472656574 +52010016529a0000,0002225ff1628d66ae52295590b72b9a0b3a3527642a532600137465737470756230332d32312d323031372d32 +52010016529d0000,00022261a4c61ced261ab571bdb3410ae140bec6c31f14ce00117465737470756230332d32312d32303137 +5201001655960000,000222f42327bcb6d7578a2669e416b5aa185fe14ee8e03e00056569676874 +5201001664200000,00022584f69099600bdca9b062ba60432dba3c0ca2241167002c686973746f72792d6f662d6672696564726963682d69692d6f662d707275737369612d766f6c756d652d3137