PendingActivation
This commit is contained in:
parent
3402eceb8e
commit
6d4615ce31
4 changed files with 204 additions and 8 deletions
|
@ -594,10 +594,10 @@ class PendingActivationValue(typing.NamedTuple):
|
|||
|
||||
type PendingActivationKey struct {
|
||||
Prefix []byte `json:"prefix"`
|
||||
Height int32 `json:"height"`
|
||||
TxoType int32 `json:"txo_height"`
|
||||
TxNum int32 `json:"tx_num"`
|
||||
Position int32 `json:"position"`
|
||||
Height uint32 `json:"height"`
|
||||
TxoType uint8 `json:"txo_type"`
|
||||
TxNum uint32 `json:"tx_num"`
|
||||
Position uint16 `json:"position"`
|
||||
}
|
||||
|
||||
func (k *PendingActivationKey) IsSupport() bool {
|
||||
|
@ -613,6 +613,102 @@ type PendingActivationValue struct {
|
|||
NormalizedName string `json:"normalized_name"`
|
||||
}
|
||||
|
||||
func (k *PendingActivationKey) PackKey() []byte {
|
||||
prefixLen := 1
|
||||
// b'>LBLH'
|
||||
n := prefixLen + 4 + 1 + 4 + 2
|
||||
key := make([]byte, n)
|
||||
copy(key, k.Prefix)
|
||||
binary.BigEndian.PutUint32(key[prefixLen:], k.Height)
|
||||
key[prefixLen+4] = k.TxoType
|
||||
binary.BigEndian.PutUint32(key[prefixLen+5:], k.TxNum)
|
||||
binary.BigEndian.PutUint16(key[prefixLen+9:], k.Position)
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
func (v *PendingActivationValue) PackValue() []byte {
|
||||
nameLen := len(v.NormalizedName)
|
||||
n := 20 + 2 + nameLen
|
||||
value := make([]byte, n)
|
||||
copy(value, v.ClaimHash[:20])
|
||||
binary.BigEndian.PutUint16(value[20:], uint16(nameLen))
|
||||
copy(value[22:], []byte(v.NormalizedName))
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func PendingActivationKeyPackPartialNFields(nFields int) func(*PendingActivationKey) []byte {
|
||||
return func(u *PendingActivationKey) []byte {
|
||||
return PendingActivationKeyPackPartial(u, nFields)
|
||||
}
|
||||
}
|
||||
|
||||
func PendingActivationKeyPackPartial(k *PendingActivationKey, 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 > 4 {
|
||||
nFields = 4
|
||||
}
|
||||
if nFields < 0 {
|
||||
nFields = 0
|
||||
}
|
||||
|
||||
// b'>4sLH'
|
||||
prefixLen := 1
|
||||
var n = prefixLen
|
||||
for i := 0; i <= nFields; i++ {
|
||||
switch i {
|
||||
case 1:
|
||||
n += 4
|
||||
case 2:
|
||||
n += 1
|
||||
case 3:
|
||||
n += 4
|
||||
case 4:
|
||||
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.Height)
|
||||
case 2:
|
||||
key[prefixLen+4] = k.TxoType
|
||||
case 3:
|
||||
binary.BigEndian.PutUint32(key[prefixLen+5:], k.TxNum)
|
||||
case 4:
|
||||
binary.BigEndian.PutUint16(key[prefixLen+9:], k.Position)
|
||||
}
|
||||
}
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
func PendingActivationKeyUnpack(key []byte) *PendingActivationKey {
|
||||
prefixLen := 1
|
||||
return &PendingActivationKey{
|
||||
Prefix: key[:prefixLen],
|
||||
Height: binary.BigEndian.Uint32(key[prefixLen:]),
|
||||
TxoType: key[prefixLen+4],
|
||||
TxNum: binary.BigEndian.Uint32(key[prefixLen+5:]),
|
||||
Position: binary.BigEndian.Uint16(key[prefixLen+9:]),
|
||||
}
|
||||
}
|
||||
|
||||
func PendingActivationValueUnpack(value []byte) *PendingActivationValue {
|
||||
nameLen := binary.BigEndian.Uint16(value[20:])
|
||||
return &PendingActivationValue{
|
||||
ClaimHash: value[:20],
|
||||
NormalizedName: string(value[22 : 22+nameLen]),
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
class ActivationKey(typing.NamedTuple):
|
||||
txo_type: int
|
||||
|
@ -1612,8 +1708,9 @@ func UnpackGenericKey(key []byte) (byte, interface{}, error) {
|
|||
case ClaimExpiration:
|
||||
|
||||
case ClaimTakeover:
|
||||
case PendingActivation:
|
||||
return 0x0, nil, errors.Base("key unpack function for %v not implemented", firstByte)
|
||||
case PendingActivation:
|
||||
return PendingActivation, PendingActivationKeyUnpack(key), nil
|
||||
case ActivatedClaimAndSupport:
|
||||
return ActivatedClaimAndSupport, ActivationKeyUnpack(key), nil
|
||||
case ActiveAmount:
|
||||
|
@ -1675,8 +1772,9 @@ func UnpackGenericValue(key, value []byte) (byte, interface{}, error) {
|
|||
case ClaimExpiration:
|
||||
|
||||
case ClaimTakeover:
|
||||
case PendingActivation:
|
||||
return 0x0, nil, errors.Base("value unpack not implemented for key %v", key)
|
||||
case PendingActivation:
|
||||
return PendingActivation, PendingActivationValueUnpack(value), nil
|
||||
case ActivatedClaimAndSupport:
|
||||
return ActivatedClaimAndSupport, ActivationValueUnpack(value), nil
|
||||
case ActiveAmount:
|
||||
|
|
|
@ -44,6 +44,94 @@ func testInit(filePath string) (*grocksdb.DB, [][]string, func()) {
|
|||
return db, records, toDefer
|
||||
}
|
||||
|
||||
func TestPendingActivation(t *testing.T) {
|
||||
|
||||
filePath := "../../resources/pending_activation.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.PendingActivation}).WithIncludeValue(true)
|
||||
ch := dbpkg.Iter(db, options)
|
||||
var i = 0
|
||||
for kv := range ch {
|
||||
// log.Println(kv.Key)
|
||||
gotKey := kv.Key.(*prefixes.PendingActivationKey).PackKey()
|
||||
|
||||
keyPartial1 := prefixes.PendingActivationKeyPackPartial(kv.Key.(*prefixes.PendingActivationKey), 1)
|
||||
keyPartial2 := prefixes.PendingActivationKeyPackPartial(kv.Key.(*prefixes.PendingActivationKey), 2)
|
||||
keyPartial3 := prefixes.PendingActivationKeyPackPartial(kv.Key.(*prefixes.PendingActivationKey), 3)
|
||||
keyPartial4 := prefixes.PendingActivationKeyPackPartial(kv.Key.(*prefixes.PendingActivationKey), 4)
|
||||
|
||||
// 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)
|
||||
}
|
||||
if !bytes.HasPrefix(gotKey, keyPartial4) {
|
||||
t.Errorf("%+v should be prefix of %+v\n", keyPartial4, gotKey)
|
||||
}
|
||||
|
||||
got := kv.Value.(*prefixes.PendingActivationValue).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.PendingActivationValue).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 TestActivated(t *testing.T) {
|
||||
|
||||
filePath := "../../resources/activated_claim_and_support.csv"
|
||||
|
|
4
main.go
4
main.go
|
@ -38,7 +38,7 @@ func main() {
|
|||
|
||||
options := &db.IterOptions{
|
||||
FillCache: false,
|
||||
Prefix: []byte{prefixes.ActivatedClaimAndSupport},
|
||||
Prefix: []byte{prefixes.PendingActivation},
|
||||
Start: nil,
|
||||
Stop: nil,
|
||||
IncludeStart: true,
|
||||
|
@ -49,7 +49,7 @@ func main() {
|
|||
RawValue: true,
|
||||
}
|
||||
|
||||
db.ReadWriteRawN(dbVal, options, "./resources/activated_claim_and_support.csv", 10)
|
||||
db.ReadWriteRawN(dbVal, options, "./resources/pending_activation.csv", 10)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
10
resources/pending_activation.csv
Normal file
10
resources/pending_activation.csv
Normal file
|
@ -0,0 +1,10 @@
|
|||
5100002e5002000059610000,04c7d5e2360f10ab8e28d5d831abb29b72cea3a8000c697473616469736173746572
|
||||
5100002e52020000596d0000,04c7d5e2360f10ab8e28d5d831abb29b72cea3a8000c697473616469736173746572
|
||||
5100005d570200025e360000,32d4fc78396c239f5c1a0a041242eebb2636750900036f6e65
|
||||
5100005d590200025e570001,c923ae766c269535048c06674261e546004375ab000374776f
|
||||
5100005d5a0200025e6a0001,c923ae766c269535048c06674261e546004375ab000374776f
|
||||
51000061e50200029e100000,a467b70b0a9ddba924d0a115206fe7c1cb25a3460003707567
|
||||
51000080a002000449380000,c6ddef5e005606bd816177c7f0cba2404c7191310006757465737432
|
||||
510000c3c102000864bb0000,32d4fc78396c239f5c1a0a041242eebb2636750900036f6e65
|
||||
510000fd8002000c380b0000,c2ba0ad053f45d77ae569a1b5c407bc213365fda00037a6564
|
||||
510001556c02001030b40000,467513d3a6eed0114964d751cd85ed49c8e3af4e000b776172616e647065616365
|
|
Loading…
Reference in a new issue