change is manually serialized
This commit is contained in:
parent
85a7f74f83
commit
1a3f34c345
4 changed files with 46 additions and 42 deletions
|
@ -1,11 +1,14 @@
|
|||
package change
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/mojura/enkodo"
|
||||
)
|
||||
|
||||
type ChangeType int32
|
||||
type ChangeType uint32
|
||||
|
||||
const (
|
||||
AddClaim ChangeType = iota
|
||||
|
@ -54,46 +57,56 @@ func (c Change) SetAmount(amt int64) Change {
|
|||
return c
|
||||
}
|
||||
|
||||
func (c *Change) MarshalEnkodo(enc *enkodo.Encoder) error {
|
||||
enc.Bytes(c.ClaimID[:])
|
||||
enc.Bytes(c.OutPoint.Hash[:])
|
||||
enc.Uint32(c.OutPoint.Index)
|
||||
enc.Int32(int32(c.Type))
|
||||
enc.Int32(c.Height)
|
||||
enc.Int32(c.ActiveHeight)
|
||||
enc.Int32(c.VisibleHeight)
|
||||
enc.Int64(c.Amount)
|
||||
func (c *Change) MarshalTo(enc *bytes.Buffer) error {
|
||||
enc.Write(c.ClaimID[:])
|
||||
enc.Write(c.OutPoint.Hash[:])
|
||||
var temp [8]byte
|
||||
binary.BigEndian.PutUint32(temp[:4], c.OutPoint.Index)
|
||||
enc.Write(temp[:4])
|
||||
binary.BigEndian.PutUint32(temp[:4], uint32(c.Type))
|
||||
enc.Write(temp[:4])
|
||||
binary.BigEndian.PutUint32(temp[:4], uint32(c.Height))
|
||||
enc.Write(temp[:4])
|
||||
binary.BigEndian.PutUint32(temp[:4], uint32(c.ActiveHeight))
|
||||
enc.Write(temp[:4])
|
||||
binary.BigEndian.PutUint32(temp[:4], uint32(c.VisibleHeight))
|
||||
enc.Write(temp[:4])
|
||||
binary.BigEndian.PutUint64(temp[:], uint64(c.Amount))
|
||||
enc.Write(temp[:])
|
||||
|
||||
if c.SpentChildren != nil {
|
||||
enc.Int32(int32(len(c.SpentChildren)))
|
||||
binary.BigEndian.PutUint32(temp[:4], uint32(len(c.SpentChildren)))
|
||||
enc.Write(temp[:4])
|
||||
for key := range c.SpentChildren {
|
||||
enc.String(key)
|
||||
binary.BigEndian.PutUint16(temp[:2], uint16(len(key))) // technically limited to 255; not sure we trust it
|
||||
enc.Write(temp[:2])
|
||||
enc.WriteString(key)
|
||||
}
|
||||
} else {
|
||||
enc.Int32(0)
|
||||
binary.BigEndian.PutUint32(temp[:4], 0)
|
||||
enc.Write(temp[:4])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Change) UnmarshalEnkodo(dec *enkodo.Decoder) error {
|
||||
id := c.ClaimID[:]
|
||||
err := dec.Bytes(&id)
|
||||
op := c.OutPoint.Hash[:]
|
||||
err = dec.Bytes(&op)
|
||||
c.OutPoint.Index, err = dec.Uint32()
|
||||
t, err := dec.Int32()
|
||||
c.Type = ChangeType(t)
|
||||
c.Height, err = dec.Int32()
|
||||
c.ActiveHeight, err = dec.Int32()
|
||||
c.VisibleHeight, err = dec.Int32()
|
||||
c.Amount, err = dec.Int64()
|
||||
keys, err := dec.Int32()
|
||||
func (c *Change) UnmarshalFrom(dec *bytes.Buffer) error {
|
||||
copy(c.ClaimID[:], dec.Next(ClaimIDSize))
|
||||
copy(c.OutPoint.Hash[:], dec.Next(chainhash.HashSize))
|
||||
c.OutPoint.Index = binary.BigEndian.Uint32(dec.Next(4))
|
||||
c.Type = ChangeType(binary.BigEndian.Uint32(dec.Next(4)))
|
||||
c.Height = int32(binary.BigEndian.Uint32(dec.Next(4)))
|
||||
c.ActiveHeight = int32(binary.BigEndian.Uint32(dec.Next(4)))
|
||||
c.VisibleHeight = int32(binary.BigEndian.Uint32(dec.Next(4)))
|
||||
c.Amount = int64(binary.BigEndian.Uint64(dec.Next(8)))
|
||||
keys := binary.BigEndian.Uint32(dec.Next(4))
|
||||
if keys > 0 {
|
||||
c.SpentChildren = map[string]bool{}
|
||||
}
|
||||
for keys > 0 {
|
||||
keys--
|
||||
key, _ := dec.String()
|
||||
keySize := int(binary.BigEndian.Uint16(dec.Next(2)))
|
||||
key := string(dec.Next(keySize))
|
||||
c.SpentChildren[key] = true
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package noderepo
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/mojura/enkodo"
|
||||
"sort"
|
||||
|
||||
"github.com/btcsuite/btcd/claimtrie/change"
|
||||
|
@ -29,12 +28,10 @@ func (repo *Pebble) AppendChanges(changes []change.Change) error {
|
|||
defer batch.Close()
|
||||
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
encoder := enkodo.NewWriter(buffer)
|
||||
defer encoder.Close()
|
||||
|
||||
for _, chg := range changes {
|
||||
buffer.Reset()
|
||||
err := encoder.Encode(&chg)
|
||||
err := chg.MarshalTo(buffer)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "in marshaller")
|
||||
}
|
||||
|
@ -63,13 +60,10 @@ func (repo *Pebble) LoadChanges(name []byte) ([]change.Change, error) {
|
|||
func unmarshalChanges(name, data []byte) ([]change.Change, error) {
|
||||
var changes []change.Change
|
||||
|
||||
reader := bytes.NewReader(data)
|
||||
decoder := enkodo.NewReader(reader)
|
||||
defer decoder.Close()
|
||||
|
||||
for reader.Len() > 0 {
|
||||
buffer := bytes.NewBuffer(data)
|
||||
for buffer.Len() > 0 {
|
||||
var chg change.Change
|
||||
err := decoder.Decode(&chg)
|
||||
err := chg.UnmarshalFrom(buffer)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "in decode")
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -17,7 +17,6 @@ require (
|
|||
github.com/felixge/fgprof v0.9.1
|
||||
github.com/jessevdk/go-flags v1.4.0
|
||||
github.com/jrick/logrotate v1.0.0
|
||||
github.com/mojura/enkodo v0.5.5 // indirect
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/shirou/gopsutil/v3 v3.21.6
|
||||
github.com/spf13/cobra v1.1.3
|
||||
|
|
2
go.sum
2
go.sum
|
@ -268,8 +268,6 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F
|
|||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mojura/enkodo v0.5.5 h1:bkjksvk8XeO0SLjtkUzvqS9oJlAIO5WFibSCSn9o/58=
|
||||
github.com/mojura/enkodo v0.5.5/go.mod h1:9/1bBkNTRhwLPSFAo0uG1a+numnsZMxNIzLdWxlFl+g=
|
||||
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/nats-io/nats.go v1.8.1/go.mod h1:BrFz9vVn0fU3AcH9Vn4Kd7W0NpJ651tD5omQ3M8LwxM=
|
||||
|
|
Loading…
Reference in a new issue