SupportToClaim

This commit is contained in:
Jeffrey Picard 2022-01-14 07:03:52 -05:00
parent 207ebd5608
commit 246a0445f7
4 changed files with 177 additions and 4 deletions

View file

@ -500,14 +500,91 @@ class SupportToClaimValue(typing.NamedTuple):
type SupportToClaimKey struct { type SupportToClaimKey struct {
Prefix []byte `json:"prefix"` Prefix []byte `json:"prefix"`
TxNum int32 `json:"tx_num"` TxNum uint32 `json:"tx_num"`
Position int32 `json:"position"` Position uint16 `json:"position"`
} }
type SupportToClaimValue struct { type SupportToClaimValue struct {
ClaimHash []byte `json:"claim_hash"` ClaimHash []byte `json:"claim_hash"`
} }
func (k *SupportToClaimKey) PackKey() []byte {
prefixLen := 1
// b'>LH'
n := prefixLen + 4 + 2
key := make([]byte, n)
copy(key, k.Prefix)
binary.BigEndian.PutUint32(key[prefixLen:], k.TxNum)
binary.BigEndian.PutUint16(key[prefixLen+4:], k.Position)
return key
}
func (v *SupportToClaimValue) PackValue() []byte {
value := make([]byte, 20)
copy(value, v.ClaimHash)
return value
}
func SupportToClaimKeyPackPartialNFields(nFields int) func(*SupportToClaimKey) []byte {
return func(u *SupportToClaimKey) []byte {
return SupportToClaimKeyPackPartial(u, nFields)
}
}
func SupportToClaimKeyPackPartial(k *SupportToClaimKey, 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 > 2 {
nFields = 2
}
if nFields < 0 {
nFields = 0
}
prefixLen := 1
var n = prefixLen
for i := 0; i <= nFields; i++ {
switch i {
case 1:
n += 4
case 2:
n += 2
}
}
key := make([]byte, n)
for i := 0; i <= nFields; i++ {
switch i {
case 0:
copy(key, k.Prefix)
case 1:
binary.BigEndian.PutUint32(key[prefixLen:], k.TxNum)
case 2:
binary.BigEndian.PutUint16(key[prefixLen+4:], k.Position)
}
}
return key
}
func SupportToClaimKeyUnpack(key []byte) *SupportToClaimKey {
prefixLen := 1
return &SupportToClaimKey{
Prefix: key[:prefixLen],
TxNum: binary.BigEndian.Uint32(key[prefixLen:]),
Position: binary.BigEndian.Uint16(key[prefixLen+4:]),
}
}
func SupportToClaimValueUnpack(value []byte) *SupportToClaimValue {
return &SupportToClaimValue{
ClaimHash: value[:20],
}
}
/* /*
class ClaimExpirationKey(typing.NamedTuple): class ClaimExpirationKey(typing.NamedTuple):
expiration: int expiration: int
@ -1861,9 +1938,12 @@ func UnpackGenericKey(key []byte) (byte, interface{}, error) {
firstByte := key[0] firstByte := key[0]
switch firstByte { switch firstByte {
case ClaimToSupport: case ClaimToSupport:
return 0x0, nil, errors.Base("key unpack function for %v not implemented", firstByte)
case SupportToClaim: case SupportToClaim:
return SupportToClaim, SupportToClaimKeyUnpack(key), nil
case ClaimToTXO: case ClaimToTXO:
return 0x0, nil, errors.Base("key unpack function for %v not implemented", firstByte)
case TXOToClaim: case TXOToClaim:
case ClaimToChannel: case ClaimToChannel:
@ -1926,9 +2006,12 @@ func UnpackGenericValue(key, value []byte) (byte, interface{}, error) {
firstByte := key[0] firstByte := key[0]
switch firstByte { switch firstByte {
case ClaimToSupport: case ClaimToSupport:
return 0x0, nil, errors.Base("value unpack not implemented for key %v", key)
case SupportToClaim: case SupportToClaim:
return SupportToClaim, SupportToClaimValueUnpack(value), nil
case ClaimToTXO: case ClaimToTXO:
return 0x0, nil, errors.Base("value unpack not implemented for key %v", key)
case TXOToClaim: case TXOToClaim:
case ClaimToChannel: case ClaimToChannel:

View file

@ -44,6 +44,86 @@ func testInit(filePath string) (*grocksdb.DB, [][]string, func()) {
return db, records, toDefer return db, records, toDefer
} }
func TestSupportToClaim(t *testing.T) {
filePath := "../../resources/support_to_claim.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.SupportToClaim}).WithIncludeValue(true)
ch := dbpkg.Iter(db, options)
var i = 0
for kv := range ch {
// log.Println(kv.Key)
gotKey := kv.Key.(*prefixes.SupportToClaimKey).PackKey()
keyPartial1 := prefixes.SupportToClaimKeyPackPartial(kv.Key.(*prefixes.SupportToClaimKey), 1)
keyPartial2 := prefixes.SupportToClaimKeyPackPartial(kv.Key.(*prefixes.SupportToClaimKey), 2)
// 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)
}
got := kv.Value.(*prefixes.SupportToClaimValue).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.SupportToClaimValue).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 TestClaimExpiration(t *testing.T) { func TestClaimExpiration(t *testing.T) {
filePath := "../../resources/claim_expiration.csv" filePath := "../../resources/claim_expiration.csv"

View file

@ -38,7 +38,7 @@ func main() {
options := &db.IterOptions{ options := &db.IterOptions{
FillCache: false, FillCache: false,
Prefix: []byte{prefixes.ClaimExpiration}, Prefix: []byte{prefixes.SupportToClaim},
Start: nil, Start: nil,
Stop: nil, Stop: nil,
IncludeStart: true, IncludeStart: true,
@ -49,7 +49,7 @@ func main() {
RawValue: true, RawValue: true,
} }
db.ReadWriteRawN(dbVal, options, "./resources/claim_expiration.csv", 10) db.ReadWriteRawN(dbVal, options, "./resources/support_to_claim.csv", 10)
return return
} }

View file

@ -0,0 +1,10 @@
4c000059610000,04c7d5e2360f10ab8e28d5d831abb29b72cea3a8
4c0000596d0000,04c7d5e2360f10ab8e28d5d831abb29b72cea3a8
4c00029e100000,a467b70b0a9ddba924d0a115206fe7c1cb25a346
4c000449380000,c6ddef5e005606bd816177c7f0cba2404c719131
4c000864bb0000,32d4fc78396c239f5c1a0a041242eebb26367509
4c000c380b0000,c2ba0ad053f45d77ae569a1b5c407bc213365fda
4c001030b40000,467513d3a6eed0114964d751cd85ed49c8e3af4e
4c0011116c0000,d565af1863ccf28e9d90f8730e40b5ee1d72258d
4c001111e90000,1e81ee06e8293438fbcbe9196494a69be0426c8b
4c0016c4180001,49cb931d20a96e17348aabbc28b5838e1a650d8c
1 4c000059610000 04c7d5e2360f10ab8e28d5d831abb29b72cea3a8
2 4c0000596d0000 04c7d5e2360f10ab8e28d5d831abb29b72cea3a8
3 4c00029e100000 a467b70b0a9ddba924d0a115206fe7c1cb25a346
4 4c000449380000 c6ddef5e005606bd816177c7f0cba2404c719131
5 4c000864bb0000 32d4fc78396c239f5c1a0a041242eebb26367509
6 4c000c380b0000 c2ba0ad053f45d77ae569a1b5c407bc213365fda
7 4c001030b40000 467513d3a6eed0114964d751cd85ed49c8e3af4e
8 4c0011116c0000 d565af1863ccf28e9d90f8730e40b5ee1d72258d
9 4c001111e90000 1e81ee06e8293438fbcbe9196494a69be0426c8b
10 4c0016c4180001 49cb931d20a96e17348aabbc28b5838e1a650d8c