ClaimToChannel
This commit is contained in:
parent
f48337e4b9
commit
96457abd10
4 changed files with 186 additions and 8 deletions
|
@ -376,14 +376,98 @@ class ClaimToChannelValue(typing.NamedTuple):
|
|||
type ClaimToChannelKey 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 ClaimToChannelValue struct {
|
||||
SigningHash []byte `json:"signing_hash"`
|
||||
}
|
||||
|
||||
func (k *ClaimToChannelKey) 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 *ClaimToChannelValue) PackValue() []byte {
|
||||
value := make([]byte, 20)
|
||||
copy(value, v.SigningHash[:20])
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func ClaimToChannelKeyPackPartialNFields(nFields int) func(*ClaimToChannelKey) []byte {
|
||||
return func(u *ClaimToChannelKey) []byte {
|
||||
return ClaimToChannelKeyPackPartial(u, nFields)
|
||||
}
|
||||
}
|
||||
|
||||
func ClaimToChannelKeyPackPartial(k *ClaimToChannelKey, 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[:20])
|
||||
case 2:
|
||||
binary.BigEndian.PutUint32(key[prefixLen+20:], k.TxNum)
|
||||
case 3:
|
||||
binary.BigEndian.PutUint16(key[prefixLen+24:], k.Position)
|
||||
}
|
||||
}
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
func ClaimToChannelKeyUnpack(key []byte) *ClaimToChannelKey {
|
||||
prefixLen := 1
|
||||
return &ClaimToChannelKey{
|
||||
Prefix: key[:prefixLen],
|
||||
ClaimHash: key[prefixLen : prefixLen+20],
|
||||
TxNum: binary.BigEndian.Uint32(key[prefixLen+20:]),
|
||||
Position: binary.BigEndian.Uint16(key[prefixLen+24:]),
|
||||
}
|
||||
}
|
||||
|
||||
func ClaimToChannelValueUnpack(value []byte) *ClaimToChannelValue {
|
||||
return &ClaimToChannelValue{
|
||||
SigningHash: value[:20],
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
class ChannelToClaimKey(typing.NamedTuple):
|
||||
signing_hash: bytes
|
||||
|
@ -2120,11 +2204,11 @@ func UnpackGenericKey(key []byte) (byte, interface{}, error) {
|
|||
return SupportToClaim, SupportToClaimKeyUnpack(key), nil
|
||||
|
||||
case ClaimToTXO:
|
||||
return 0x0, nil, errors.Base("key unpack function for %v not implemented", firstByte)
|
||||
case TXOToClaim:
|
||||
return 0x0, nil, errors.Base("key unpack function for %v not implemented", firstByte)
|
||||
|
||||
case ClaimToChannel:
|
||||
return 0x0, nil, errors.Base("key unpack function for %v not implemented", firstByte)
|
||||
return ClaimToChannel, ClaimToChannelKeyUnpack(key), nil
|
||||
case ChannelToClaim:
|
||||
return ChannelToClaim, ChannelToClaimKeyUnpack(key), nil
|
||||
|
||||
|
@ -2190,11 +2274,11 @@ func UnpackGenericValue(key, value []byte) (byte, interface{}, error) {
|
|||
return SupportToClaim, SupportToClaimValueUnpack(value), nil
|
||||
|
||||
case ClaimToTXO:
|
||||
return 0x0, nil, errors.Base("value unpack not implemented for key %v", key)
|
||||
case TXOToClaim:
|
||||
return 0x0, nil, errors.Base("value unpack not implemented for key %v", key)
|
||||
|
||||
case ClaimToChannel:
|
||||
return 0x0, nil, errors.Base("value unpack not implemented for key %v", key)
|
||||
return ClaimToChannel, ClaimToChannelValueUnpack(value), nil
|
||||
case ChannelToClaim:
|
||||
return ChannelToClaim, ChannelToClaimValueUnpack(value), nil
|
||||
|
||||
|
|
|
@ -44,6 +44,90 @@ func testInit(filePath string) (*grocksdb.DB, [][]string, func()) {
|
|||
return db, records, toDefer
|
||||
}
|
||||
|
||||
func TestClaimToChannel(t *testing.T) {
|
||||
|
||||
filePath := "../../resources/claim_to_channel.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.ClaimToChannel}).WithIncludeValue(true)
|
||||
ch := dbpkg.Iter(db, options)
|
||||
var i = 0
|
||||
for kv := range ch {
|
||||
// log.Println(kv.Key)
|
||||
gotKey := kv.Key.(*prefixes.ClaimToChannelKey).PackKey()
|
||||
|
||||
keyPartial1 := prefixes.ClaimToChannelKeyPackPartial(kv.Key.(*prefixes.ClaimToChannelKey), 1)
|
||||
keyPartial2 := prefixes.ClaimToChannelKeyPackPartial(kv.Key.(*prefixes.ClaimToChannelKey), 2)
|
||||
keyPartial3 := prefixes.ClaimToChannelKeyPackPartial(kv.Key.(*prefixes.ClaimToChannelKey), 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.ClaimToChannelValue).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.ClaimToChannelValue).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 TestChannelToClaim(t *testing.T) {
|
||||
|
||||
filePath := "../../resources/channel_to_claim.csv"
|
||||
|
|
4
main.go
4
main.go
|
@ -38,7 +38,7 @@ func main() {
|
|||
|
||||
options := &db.IterOptions{
|
||||
FillCache: false,
|
||||
Prefix: []byte{prefixes.ChannelToClaim},
|
||||
Prefix: []byte{prefixes.ClaimToChannel},
|
||||
Start: nil,
|
||||
Stop: nil,
|
||||
IncludeStart: true,
|
||||
|
@ -49,7 +49,7 @@ func main() {
|
|||
RawValue: true,
|
||||
}
|
||||
|
||||
db.ReadWriteRawN(dbVal, options, "./resources/channel_to_claim.csv", 10)
|
||||
db.ReadWriteRawN(dbVal, options, "./resources/claim_to_channel.csv", 10)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
10
resources/claim_to_channel.csv
Normal file
10
resources/claim_to_channel.csv
Normal file
|
@ -0,0 +1,10 @@
|
|||
49000000a420c44374f4f399ab4807fa1901eefc870297ec210000,a02a093c607b6772907f923cf15014397146874b
|
||||
49000000c27eef5ea69e0d73f118826c7e326bb46900371d660000,b237333ca0e44b0a91429a20420ed9bc6ea56a53
|
||||
4900000110e40894573f528c393fbcec7a472ec85301516b320000,f2a31cf5c4c3cd75a5c839d6b05947dfeb1970bb
|
||||
4900000324e40fcb63a0b517a3660645e9bd99244a030bb6ba0000,6b450166f56b44370d026ea2f70eb1be3454cd3f
|
||||
49000003d1538a0f19f5cd4bc1a62cc294f5c89934011c7c990000,ed6c15e48a78f5113fef78847525a5f664f76362
|
||||
49000008d47beeff8325e795a8604226145b01702b02dbb2a20000,f2cf43b86b9d70175dc22dbb9ff7806241d90780
|
||||
4900000906499e073e94370ceff37cb21c282124440369842d0000,f2cf43b86b9d70175dc22dbb9ff7806241d90780
|
||||
49000009c3172e034a255f3c03566dca84bb9f046a0225c69c0000,dd3944bc7ae2717606e8e3ae3561f83befe484ea
|
||||
49000009ca6e0caaaef16872b4bd4f6f1b8c2363e202b169560000,2fdbbf06aab844bb80ee6748244cadc5d698411e
|
||||
4900000ad9ded2e15d18987900d09e9b29ef33d03e02c972b30000,f6ac161e5f4100b6bb22544460b7dfc2af9b1ec3
|
|
Loading…
Reference in a new issue