Catchup to python-herald schema. Plus lots of refactoring. #49
2 changed files with 51 additions and 21 deletions
|
@ -7,12 +7,60 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-restruct/restruct"
|
"github.com/go-restruct/restruct"
|
||||||
|
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
restruct.EnableExprBeta()
|
restruct.EnableExprBeta()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Type OnesComplementEffectiveAmount (uint64) has to be encoded specially
|
||||||
|
// to get the desired sort ordering.
|
||||||
|
// Implement the Sizer, Packer, Unpacker interface to handle it manually.
|
||||||
|
|
||||||
|
func (amt *OnesComplementEffectiveAmount) SizeOf() int {
|
||||||
|
return 8
|
||||||
|
}
|
||||||
|
|
||||||
|
func (amt *OnesComplementEffectiveAmount) Pack(buf []byte, order binary.ByteOrder) ([]byte, error) {
|
||||||
|
binary.BigEndian.PutUint64(buf, OnesCompTwiddle64-uint64(*amt))
|
||||||
|
return buf[8:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (amt *OnesComplementEffectiveAmount) Unpack(buf []byte, order binary.ByteOrder) ([]byte, error) {
|
||||||
|
*amt = OnesComplementEffectiveAmount(OnesCompTwiddle64 - binary.BigEndian.Uint64(buf))
|
||||||
|
return buf[8:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Struct BlockTxsValue has a field TxHashes of type []*chainhash.Hash.
|
||||||
|
// I haven't been able to figure out the right annotations to make
|
||||||
|
// restruct.Pack,Unpack work automagically.
|
||||||
|
// Implement the Sizer, Packer, Unpacker interface to handle it manually.
|
||||||
|
|
||||||
|
func (kv *BlockTxsValue) SizeOf() int {
|
||||||
|
return 32 * len(kv.TxHashes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *BlockTxsValue) Pack(buf []byte, order binary.ByteOrder) ([]byte, error) {
|
||||||
|
offset := 0
|
||||||
|
for _, h := range kv.TxHashes {
|
||||||
|
offset += copy(buf[offset:], h[:])
|
||||||
|
}
|
||||||
|
return buf[offset:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *BlockTxsValue) Unpack(buf []byte, order binary.ByteOrder) ([]byte, error) {
|
||||||
|
offset := 0
|
||||||
|
kv.TxHashes = make([]*chainhash.Hash, len(buf)/32)
|
||||||
|
for i, _ := range kv.TxHashes {
|
||||||
|
kv.TxHashes[i] = (*chainhash.Hash)(buf[offset:32])
|
||||||
|
offset += 32
|
||||||
|
}
|
||||||
|
return buf[offset:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Metadata associated with each prefix/table
|
||||||
|
|
||||||
type tableMeta struct {
|
type tableMeta struct {
|
||||||
newKey func() interface{}
|
newKey func() interface{}
|
||||||
newValue func() interface{}
|
newValue func() interface{}
|
||||||
|
@ -595,12 +643,8 @@ var RegressionAPI_1 = &SerializationAPI{
|
||||||
PackValue: func(value BaseValue) ([]byte, error) {
|
PackValue: func(value BaseValue) ([]byte, error) {
|
||||||
return GenericPack(value, -1)
|
return GenericPack(value, -1)
|
||||||
},
|
},
|
||||||
UnpackKey: func(key []byte) (BaseKey, error) {
|
UnpackKey: UnpackGenericKey,
|
||||||
return UnpackGenericKey(key)
|
UnpackValue: UnpackGenericValue,
|
||||||
},
|
|
||||||
UnpackValue: func(prefix []byte, value []byte) (BaseValue, error) {
|
|
||||||
return UnpackGenericValue(prefix, value)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var RegressionAPI_2 = &SerializationAPI{
|
var RegressionAPI_2 = &SerializationAPI{
|
||||||
|
|
|
@ -1559,7 +1559,7 @@ type ClaimToChannelKey struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClaimToChannelValue struct {
|
type ClaimToChannelValue struct {
|
||||||
SigningHash []byte `json:"signing_hash"`
|
SigningHash []byte `struct:"[20]byte" json:"signing_hash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClaimToChannelKey(claimHash []byte, txNum uint32, position uint16) *ClaimToChannelKey {
|
func NewClaimToChannelKey(claimHash []byte, txNum uint32, position uint16) *ClaimToChannelKey {
|
||||||
|
@ -2665,20 +2665,6 @@ func ActiveAmountValueUnpack(value []byte) *ActiveAmountValue {
|
||||||
|
|
||||||
type OnesComplementEffectiveAmount uint64
|
type OnesComplementEffectiveAmount uint64
|
||||||
|
|
||||||
func (amt *OnesComplementEffectiveAmount) SizeOf() int {
|
|
||||||
return 8
|
|
||||||
}
|
|
||||||
|
|
||||||
func (amt *OnesComplementEffectiveAmount) Pack(buf []byte, order binary.ByteOrder) ([]byte, error) {
|
|
||||||
binary.BigEndian.PutUint64(buf, OnesCompTwiddle64-uint64(*amt))
|
|
||||||
return buf[8:], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (amt *OnesComplementEffectiveAmount) Unpack(buf []byte, order binary.ByteOrder) ([]byte, error) {
|
|
||||||
*amt = OnesComplementEffectiveAmount(OnesCompTwiddle64 - binary.BigEndian.Uint64(buf))
|
|
||||||
return buf[8:], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type EffectiveAmountKey struct {
|
type EffectiveAmountKey struct {
|
||||||
Prefix []byte `struct:"[1]byte" json:"prefix"`
|
Prefix []byte `struct:"[1]byte" json:"prefix"`
|
||||||
LengthEncodedNormalizedName // fields NormalizedNameLen, NormalizedName
|
LengthEncodedNormalizedName // fields NormalizedNameLen, NormalizedName
|
||||||
|
|
Loading…
Reference in a new issue