ClaimToSupport

This commit is contained in:
Jeffrey Picard 2022-01-14 07:58:51 -05:00
parent 246a0445f7
commit 9262facd17
4 changed files with 185 additions and 7 deletions

View file

@ -477,12 +477,96 @@ class ClaimToSupportValue(typing.NamedTuple):
type ClaimToSupportKey struct {
Prefix []byte `json:"prefix"`
ClaimHash []byte `json:"claim_hash"`
TxNum int32 `json:"tx_num"`
Position int32 `json:"position"`
TxNum uint32 `json:"tx_num"`
Position uint16 `json:"position"`
}
type ClaimToSupportValue struct {
Amount int32 `json:"amount"`
Amount uint64 `json:"amount"`
}
func (k *ClaimToSupportKey) PackKey() []byte {
prefixLen := 1
// b'>20sLH'
n := prefixLen + 20 + 4 + 2
key := make([]byte, n)
copy(key, k.Prefix)
copy(key[prefixLen:], k.ClaimHash[:20])
binary.BigEndian.PutUint32(key[prefixLen+20:], k.TxNum)
binary.BigEndian.PutUint16(key[prefixLen+24:], k.Position)
return key
}
func (v *ClaimToSupportValue) PackValue() []byte {
value := make([]byte, 8)
binary.BigEndian.PutUint64(value, v.Amount)
return value
}
func ClaimToSupportKeyPackPartialNFields(nFields int) func(*ClaimToSupportKey) []byte {
return func(u *ClaimToSupportKey) []byte {
return ClaimToSupportKeyPackPartial(u, nFields)
}
}
func ClaimToSupportKeyPackPartial(k *ClaimToSupportKey, 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
}
// b'>4sLH'
prefixLen := 1
var n = prefixLen
for i := 0; i <= nFields; i++ {
switch i {
case 1:
n += 20
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:
copy(key[prefixLen:], k.ClaimHash)
case 2:
binary.BigEndian.PutUint32(key[prefixLen+20:], k.TxNum)
case 3:
binary.BigEndian.PutUint16(key[prefixLen+24:], k.Position)
}
}
return key
}
func ClaimToSupportKeyUnpack(key []byte) *ClaimToSupportKey {
prefixLen := 1
return &ClaimToSupportKey{
Prefix: key[:prefixLen],
ClaimHash: key[prefixLen : prefixLen+20],
TxNum: binary.BigEndian.Uint32(key[prefixLen+20:]),
Position: binary.BigEndian.Uint16(key[prefixLen+24:]),
}
}
func ClaimToSupportValueUnpack(value []byte) *ClaimToSupportValue {
return &ClaimToSupportValue{
Amount: binary.BigEndian.Uint64(value),
}
}
/*
@ -1938,7 +2022,7 @@ func UnpackGenericKey(key []byte) (byte, interface{}, error) {
firstByte := key[0]
switch firstByte {
case ClaimToSupport:
return 0x0, nil, errors.Base("key unpack function for %v not implemented", firstByte)
return ClaimToSupport, ClaimToSupportKeyUnpack(key), nil
case SupportToClaim:
return SupportToClaim, SupportToClaimKeyUnpack(key), nil
@ -2006,7 +2090,7 @@ func UnpackGenericValue(key, value []byte) (byte, interface{}, error) {
firstByte := key[0]
switch firstByte {
case ClaimToSupport:
return 0x0, nil, errors.Base("value unpack not implemented for key %v", key)
return ClaimToSupport, ClaimToSupportValueUnpack(value), nil
case SupportToClaim:
return SupportToClaim, SupportToClaimValueUnpack(value), nil

View file

@ -44,6 +44,90 @@ func testInit(filePath string) (*grocksdb.DB, [][]string, func()) {
return db, records, toDefer
}
func TestClaimToSupport(t *testing.T) {
filePath := "../../resources/claim_to_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.ClaimToSupport}).WithIncludeValue(true)
ch := dbpkg.Iter(db, options)
var i = 0
for kv := range ch {
// log.Println(kv.Key)
gotKey := kv.Key.(*prefixes.ClaimToSupportKey).PackKey()
keyPartial1 := prefixes.ClaimToSupportKeyPackPartial(kv.Key.(*prefixes.ClaimToSupportKey), 1)
keyPartial2 := prefixes.ClaimToSupportKeyPackPartial(kv.Key.(*prefixes.ClaimToSupportKey), 2)
keyPartial3 := prefixes.ClaimToSupportKeyPackPartial(kv.Key.(*prefixes.ClaimToSupportKey), 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.ClaimToSupportValue).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.ClaimToSupportValue).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 TestSupportToClaim(t *testing.T) {
filePath := "../../resources/support_to_claim.csv"

View file

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

View file

@ -0,0 +1,10 @@
4b00000324e40fcb63a0b517a3660645e9bd99244a030bc8a50000,0000000001312d00
4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6e2aa0126,0000000001ea252a
4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6e7d20151,0000000005be6f7e
4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6eeb6014d,0000000001eac106
4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6eed00037,0000000001eac106
4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f4f00133,0000000001eac106
4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f4f001e5,0000000001eac106
4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f9b6013e,0000000001eac106
4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f9b7012f,0000000003d5820c
4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f9b9001c,0000000003d5820c
1 4b00000324e40fcb63a0b517a3660645e9bd99244a030bc8a50000 0000000001312d00
2 4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6e2aa0126 0000000001ea252a
3 4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6e7d20151 0000000005be6f7e
4 4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6eeb6014d 0000000001eac106
5 4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6eed00037 0000000001eac106
6 4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f4f00133 0000000001eac106
7 4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f4f001e5 0000000001eac106
8 4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f9b6013e 0000000001eac106
9 4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f9b7012f 0000000003d5820c
10 4b000023415fc7ba8a470f0cdf4a66bffacd5ba97902c6f9b9001c 0000000003d5820c