peerstore: remove gogo-specific Protobuf magic
This commit is contained in:
parent
6e2f434578
commit
8baf05b71d
|
@ -1,6 +1,7 @@
|
||||||
package pstoreds
|
package pstoreds
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -46,7 +47,7 @@ type addrsRecord struct {
|
||||||
// flush writes the record to the datastore by calling ds.Put, unless the record is
|
// flush writes the record to the datastore by calling ds.Put, unless the record is
|
||||||
// marked for deletion, in which case we call ds.Delete. To be called within a lock.
|
// marked for deletion, in which case we call ds.Delete. To be called within a lock.
|
||||||
func (r *addrsRecord) flush(write ds.Write) (err error) {
|
func (r *addrsRecord) flush(write ds.Write) (err error) {
|
||||||
key := addrBookBase.ChildString(b32.RawStdEncoding.EncodeToString([]byte(r.Id.ID)))
|
key := addrBookBase.ChildString(b32.RawStdEncoding.EncodeToString(r.Id))
|
||||||
|
|
||||||
if len(r.Addrs) == 0 {
|
if len(r.Addrs) == 0 {
|
||||||
if err = write.Delete(context.TODO(), key); err == nil {
|
if err = write.Delete(context.TODO(), key); err == nil {
|
||||||
|
@ -245,7 +246,7 @@ func (ab *dsAddrBook) loadRecord(id peer.ID, cache bool, update bool) (pr *addrs
|
||||||
switch err {
|
switch err {
|
||||||
case ds.ErrNotFound:
|
case ds.ErrNotFound:
|
||||||
err = nil
|
err = nil
|
||||||
pr.Id = &pb.ProtoPeerID{ID: id}
|
pr.Id = []byte(id)
|
||||||
case nil:
|
case nil:
|
||||||
if err = pr.Unmarshal(data); err != nil {
|
if err = pr.Unmarshal(data); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -430,7 +431,12 @@ func (ab *dsAddrBook) Addrs(p peer.ID) []ma.Multiaddr {
|
||||||
|
|
||||||
addrs := make([]ma.Multiaddr, len(pr.Addrs))
|
addrs := make([]ma.Multiaddr, len(pr.Addrs))
|
||||||
for i, a := range pr.Addrs {
|
for i, a := range pr.Addrs {
|
||||||
addrs[i] = a.Addr
|
var err error
|
||||||
|
addrs[i], err = ma.NewMultiaddrBytes(a.Addr)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("failed to parse peerstore entry for peer %v while querying addrs, err: %v", p, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return addrs
|
return addrs
|
||||||
}
|
}
|
||||||
|
@ -484,7 +490,7 @@ func (ab *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duratio
|
||||||
newExp := ab.clock.Now().Add(ttl).Unix()
|
newExp := ab.clock.Now().Add(ttl).Unix()
|
||||||
addrsMap := make(map[string]*pb.AddrBookRecord_AddrEntry, len(pr.Addrs))
|
addrsMap := make(map[string]*pb.AddrBookRecord_AddrEntry, len(pr.Addrs))
|
||||||
for _, addr := range pr.Addrs {
|
for _, addr := range pr.Addrs {
|
||||||
addrsMap[string(addr.Addr.Bytes())] = addr
|
addrsMap[string(addr.Addr)] = addr
|
||||||
}
|
}
|
||||||
|
|
||||||
updateExisting := func(incoming ma.Multiaddr) *pb.AddrBookRecord_AddrEntry {
|
updateExisting := func(incoming ma.Multiaddr) *pb.AddrBookRecord_AddrEntry {
|
||||||
|
@ -521,7 +527,7 @@ func (ab *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duratio
|
||||||
// } else {
|
// } else {
|
||||||
// new addr, add & broadcast
|
// new addr, add & broadcast
|
||||||
entry := &pb.AddrBookRecord_AddrEntry{
|
entry := &pb.AddrBookRecord_AddrEntry{
|
||||||
Addr: &pb.ProtoAddr{Multiaddr: incoming},
|
Addr: incoming.Bytes(),
|
||||||
Ttl: int64(ttl),
|
Ttl: int64(ttl),
|
||||||
Expiry: newExp,
|
Expiry: newExp,
|
||||||
}
|
}
|
||||||
|
@ -555,7 +561,7 @@ func deleteInPlace(s []*pb.AddrBookRecord_AddrEntry, addrs []ma.Multiaddr) []*pb
|
||||||
Outer:
|
Outer:
|
||||||
for i, addr := range s {
|
for i, addr := range s {
|
||||||
for _, del := range addrs {
|
for _, del := range addrs {
|
||||||
if !addr.Addr.Equal(del) {
|
if !bytes.Equal(del.Bytes(), addr.Addr) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
survived--
|
survived--
|
||||||
|
|
|
@ -281,7 +281,7 @@ func (gc *dsAddrBookGc) purgeStore() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
id := record.Id.ID
|
id := record.Id
|
||||||
if !record.clean(gc.ab.clock.Now()) {
|
if !record.clean(gc.ab.clock.Now()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -289,7 +289,7 @@ func (gc *dsAddrBookGc) purgeStore() {
|
||||||
if err := record.flush(batch); err != nil {
|
if err := record.flush(batch); err != nil {
|
||||||
log.Warnf("failed to flush entry modified by GC for peer: &v, err: %v", id, err)
|
log.Warnf("failed to flush entry modified by GC for peer: &v, err: %v", id, err)
|
||||||
}
|
}
|
||||||
gc.ab.cache.Remove(id)
|
gc.ab.cache.Remove(peer.ID(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = batch.Commit(context.TODO()); err != nil {
|
if err = batch.Commit(context.TODO()); err != nil {
|
||||||
|
|
|
@ -1,111 +0,0 @@
|
||||||
package pstore_pb
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p/core/peer"
|
|
||||||
pt "github.com/libp2p/go-libp2p/core/test"
|
|
||||||
|
|
||||||
"github.com/gogo/protobuf/proto"
|
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
|
||||||
)
|
|
||||||
|
|
||||||
// customGogoType aggregates the interfaces that custom Gogo types need to implement.
|
|
||||||
// it is only used for type assertions.
|
|
||||||
type customGogoType interface {
|
|
||||||
proto.Marshaler
|
|
||||||
proto.Unmarshaler
|
|
||||||
json.Marshaler
|
|
||||||
json.Unmarshaler
|
|
||||||
proto.Sizer
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProtoAddr is a custom type used by gogo to serde raw peer IDs into the peer.ID type, and back.
|
|
||||||
type ProtoPeerID struct {
|
|
||||||
peer.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ customGogoType = (*ProtoPeerID)(nil)
|
|
||||||
|
|
||||||
func (id ProtoPeerID) Marshal() ([]byte, error) {
|
|
||||||
return []byte(id.ID), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (id ProtoPeerID) MarshalTo(data []byte) (n int, err error) {
|
|
||||||
return copy(data, []byte(id.ID)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (id ProtoPeerID) MarshalJSON() ([]byte, error) {
|
|
||||||
m, _ := id.Marshal()
|
|
||||||
return json.Marshal(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (id *ProtoPeerID) Unmarshal(data []byte) (err error) {
|
|
||||||
id.ID = peer.ID(string(data))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (id *ProtoPeerID) UnmarshalJSON(data []byte) error {
|
|
||||||
var v []byte
|
|
||||||
err := json.Unmarshal(data, &v)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return id.Unmarshal(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (id ProtoPeerID) Size() int {
|
|
||||||
return len([]byte(id.ID))
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProtoAddr is a custom type used by gogo to serde raw multiaddresses into the ma.Multiaddr type, and back.
|
|
||||||
type ProtoAddr struct {
|
|
||||||
ma.Multiaddr
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ customGogoType = (*ProtoAddr)(nil)
|
|
||||||
|
|
||||||
func (a ProtoAddr) Marshal() ([]byte, error) {
|
|
||||||
return a.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a ProtoAddr) MarshalTo(data []byte) (n int, err error) {
|
|
||||||
return copy(data, a.Bytes()), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a ProtoAddr) MarshalJSON() ([]byte, error) {
|
|
||||||
m, _ := a.Marshal()
|
|
||||||
return json.Marshal(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ProtoAddr) Unmarshal(data []byte) (err error) {
|
|
||||||
a.Multiaddr, err = ma.NewMultiaddrBytes(data)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ProtoAddr) UnmarshalJSON(data []byte) error {
|
|
||||||
v := new([]byte)
|
|
||||||
err := json.Unmarshal(data, v)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return a.Unmarshal(*v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a ProtoAddr) Size() int {
|
|
||||||
return len(a.Bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewPopulatedProtoAddr generates a populated instance of the custom gogo type ProtoAddr.
|
|
||||||
// It is required by gogo-generated tests.
|
|
||||||
func NewPopulatedProtoAddr(r randyPstore) *ProtoAddr {
|
|
||||||
a, _ := ma.NewMultiaddr("/ip4/123.123.123.123/tcp/7001")
|
|
||||||
return &ProtoAddr{Multiaddr: a}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewPopulatedProtoPeerID generates a populated instance of the custom gogo type ProtoPeerID.
|
|
||||||
// It is required by gogo-generated tests.
|
|
||||||
func NewPopulatedProtoPeerID(r randyPstore) *ProtoPeerID {
|
|
||||||
id, _ := pt.RandPeerID()
|
|
||||||
return &ProtoPeerID{ID: id}
|
|
||||||
}
|
|
|
@ -5,7 +5,6 @@ package pstore_pb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
fmt "fmt"
|
fmt "fmt"
|
||||||
_ "github.com/gogo/protobuf/gogoproto"
|
|
||||||
proto "github.com/gogo/protobuf/proto"
|
proto "github.com/gogo/protobuf/proto"
|
||||||
io "io"
|
io "io"
|
||||||
math "math"
|
math "math"
|
||||||
|
@ -26,7 +25,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
|
||||||
// AddrBookRecord represents a record for a peer in the address book.
|
// AddrBookRecord represents a record for a peer in the address book.
|
||||||
type AddrBookRecord struct {
|
type AddrBookRecord struct {
|
||||||
// The peer ID.
|
// The peer ID.
|
||||||
Id *ProtoPeerID `protobuf:"bytes,1,opt,name=id,proto3,customtype=ProtoPeerID" json:"id,omitempty"`
|
Id []byte `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||||
// The multiaddresses. This is a sorted list where element 0 expires the soonest.
|
// The multiaddresses. This is a sorted list where element 0 expires the soonest.
|
||||||
Addrs []*AddrBookRecord_AddrEntry `protobuf:"bytes,2,rep,name=addrs,proto3" json:"addrs,omitempty"`
|
Addrs []*AddrBookRecord_AddrEntry `protobuf:"bytes,2,rep,name=addrs,proto3" json:"addrs,omitempty"`
|
||||||
// The most recently received signed PeerRecord.
|
// The most recently received signed PeerRecord.
|
||||||
|
@ -66,6 +65,13 @@ func (m *AddrBookRecord) XXX_DiscardUnknown() {
|
||||||
|
|
||||||
var xxx_messageInfo_AddrBookRecord proto.InternalMessageInfo
|
var xxx_messageInfo_AddrBookRecord proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *AddrBookRecord) GetId() []byte {
|
||||||
|
if m != nil {
|
||||||
|
return m.Id
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *AddrBookRecord) GetAddrs() []*AddrBookRecord_AddrEntry {
|
func (m *AddrBookRecord) GetAddrs() []*AddrBookRecord_AddrEntry {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
return m.Addrs
|
return m.Addrs
|
||||||
|
@ -82,7 +88,7 @@ func (m *AddrBookRecord) GetCertifiedRecord() *AddrBookRecord_CertifiedRecord {
|
||||||
|
|
||||||
// AddrEntry represents a single multiaddress.
|
// AddrEntry represents a single multiaddress.
|
||||||
type AddrBookRecord_AddrEntry struct {
|
type AddrBookRecord_AddrEntry struct {
|
||||||
Addr *ProtoAddr `protobuf:"bytes,1,opt,name=addr,proto3,customtype=ProtoAddr" json:"addr,omitempty"`
|
Addr []byte `protobuf:"bytes,1,opt,name=addr,proto3" json:"addr,omitempty"`
|
||||||
// The point in time when this address expires.
|
// The point in time when this address expires.
|
||||||
Expiry int64 `protobuf:"varint,2,opt,name=expiry,proto3" json:"expiry,omitempty"`
|
Expiry int64 `protobuf:"varint,2,opt,name=expiry,proto3" json:"expiry,omitempty"`
|
||||||
// The original TTL of this address.
|
// The original TTL of this address.
|
||||||
|
@ -122,6 +128,13 @@ func (m *AddrBookRecord_AddrEntry) XXX_DiscardUnknown() {
|
||||||
|
|
||||||
var xxx_messageInfo_AddrBookRecord_AddrEntry proto.InternalMessageInfo
|
var xxx_messageInfo_AddrBookRecord_AddrEntry proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *AddrBookRecord_AddrEntry) GetAddr() []byte {
|
||||||
|
if m != nil {
|
||||||
|
return m.Addr
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *AddrBookRecord_AddrEntry) GetExpiry() int64 {
|
func (m *AddrBookRecord_AddrEntry) GetExpiry() int64 {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
return m.Expiry
|
return m.Expiry
|
||||||
|
@ -201,28 +214,24 @@ func init() {
|
||||||
func init() { proto.RegisterFile("pstore.proto", fileDescriptor_f96873690e08a98f) }
|
func init() { proto.RegisterFile("pstore.proto", fileDescriptor_f96873690e08a98f) }
|
||||||
|
|
||||||
var fileDescriptor_f96873690e08a98f = []byte{
|
var fileDescriptor_f96873690e08a98f = []byte{
|
||||||
// 322 bytes of a gzipped FileDescriptorProto
|
// 257 bytes of a gzipped FileDescriptorProto
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x29, 0x28, 0x2e, 0xc9,
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x29, 0x28, 0x2e, 0xc9,
|
||||||
0x2f, 0x4a, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x84, 0xf1, 0x92, 0xa4, 0x74, 0xd3,
|
0x2f, 0x4a, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x84, 0xf1, 0x92, 0x94, 0x36, 0x33,
|
||||||
0x33, 0x4b, 0x32, 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xf5, 0xd3, 0xf3, 0xd3, 0xf3, 0xf5, 0xc1,
|
0x71, 0xf1, 0x39, 0xa6, 0xa4, 0x14, 0x39, 0xe5, 0xe7, 0x67, 0x07, 0xa5, 0x26, 0xe7, 0x17, 0xa5,
|
||||||
0x2a, 0x92, 0x4a, 0xd3, 0xc0, 0x3c, 0x30, 0x07, 0xcc, 0x82, 0xe8, 0x54, 0xba, 0xcc, 0xc4, 0xc5,
|
0x08, 0xf1, 0x71, 0x31, 0x65, 0xa6, 0x48, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x04, 0x31, 0x65, 0xa6,
|
||||||
0xe7, 0x98, 0x92, 0x52, 0xe4, 0x94, 0x9f, 0x9f, 0x1d, 0x94, 0x9a, 0x9c, 0x5f, 0x94, 0x22, 0x24,
|
0x08, 0x59, 0x72, 0xb1, 0x26, 0xa6, 0xa4, 0x14, 0x15, 0x4b, 0x30, 0x29, 0x30, 0x6b, 0x70, 0x1b,
|
||||||
0xcf, 0xc5, 0x94, 0x99, 0x22, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0xe3, 0xc4, 0x7f, 0xeb, 0x9e, 0x3c,
|
0x29, 0xeb, 0xc1, 0x75, 0xeb, 0xa1, 0xea, 0x04, 0x73, 0x5d, 0xf3, 0x4a, 0x8a, 0x2a, 0x83, 0x20,
|
||||||
0x77, 0x00, 0x48, 0x65, 0x40, 0x6a, 0x6a, 0x91, 0xa7, 0x4b, 0x10, 0x53, 0x66, 0x8a, 0x90, 0x25,
|
0x3a, 0x84, 0x42, 0xb8, 0x04, 0x92, 0x53, 0x8b, 0x4a, 0x32, 0xd3, 0x32, 0x53, 0x53, 0xe2, 0x8b,
|
||||||
0x17, 0x6b, 0x62, 0x4a, 0x4a, 0x51, 0xb1, 0x04, 0x93, 0x02, 0xb3, 0x06, 0xb7, 0x91, 0xb2, 0x1e,
|
0xc0, 0x8a, 0x24, 0x98, 0x15, 0x18, 0x35, 0xb8, 0x8d, 0x34, 0x71, 0x9b, 0xe2, 0x0c, 0xd3, 0x01,
|
||||||
0xdc, 0x76, 0x3d, 0x54, 0xa3, 0xc0, 0x5c, 0xd7, 0xbc, 0x92, 0xa2, 0xca, 0x20, 0x88, 0x0e, 0xa1,
|
0xe1, 0x07, 0xf1, 0x27, 0xa3, 0x0a, 0x48, 0x79, 0x72, 0x71, 0xc2, 0x6d, 0x12, 0x12, 0xe2, 0x62,
|
||||||
0x10, 0x2e, 0x81, 0xe4, 0xd4, 0xa2, 0x92, 0xcc, 0xb4, 0xcc, 0xd4, 0x94, 0xf8, 0x22, 0xb0, 0x22,
|
0x01, 0xd9, 0x05, 0x75, 0x2f, 0x98, 0x2d, 0x24, 0xc6, 0xc5, 0x96, 0x5a, 0x51, 0x90, 0x59, 0x54,
|
||||||
0x09, 0x66, 0x05, 0x46, 0x0d, 0x6e, 0x23, 0x4d, 0xdc, 0xa6, 0x38, 0xc3, 0x74, 0x40, 0xf8, 0x41,
|
0x29, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0x1c, 0x04, 0xe5, 0x09, 0x09, 0x70, 0x31, 0x97, 0x94, 0xe4,
|
||||||
0xfc, 0xc9, 0xa8, 0x02, 0x52, 0x11, 0x5c, 0x9c, 0x70, 0x9b, 0x84, 0x14, 0xb9, 0x58, 0x40, 0x76,
|
0x80, 0x5d, 0xc0, 0x1c, 0x04, 0x62, 0x4a, 0x99, 0x72, 0xf1, 0xa3, 0x59, 0x07, 0x52, 0x54, 0x9c,
|
||||||
0x41, 0x3d, 0xc0, 0x7b, 0xeb, 0x9e, 0x3c, 0x27, 0xd8, 0x03, 0x20, 0x15, 0x41, 0x60, 0x29, 0x21,
|
0x5a, 0x08, 0x36, 0x8f, 0x25, 0x08, 0xc4, 0x04, 0x89, 0x14, 0x25, 0x96, 0x83, 0xcd, 0xe2, 0x09,
|
||||||
0x31, 0x2e, 0xb6, 0xd4, 0x8a, 0x82, 0xcc, 0xa2, 0x4a, 0x09, 0x26, 0x05, 0x46, 0x0d, 0xe6, 0x20,
|
0x02, 0x31, 0x9d, 0x24, 0x4e, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1, 0x23, 0x39,
|
||||||
0x28, 0x4f, 0x48, 0x80, 0x8b, 0xb9, 0xa4, 0x24, 0x07, 0xec, 0x20, 0xe6, 0x20, 0x10, 0x53, 0xca,
|
0xc6, 0x09, 0x8f, 0xe5, 0x18, 0x2e, 0x3c, 0x96, 0x63, 0xb8, 0xf1, 0x58, 0x8e, 0x21, 0x89, 0x0d,
|
||||||
0x94, 0x8b, 0x1f, 0xcd, 0x76, 0x90, 0xa2, 0xe2, 0xd4, 0x42, 0xb0, 0xf1, 0x2c, 0x41, 0x20, 0x26,
|
0x1c, 0xc2, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x41, 0x6b, 0xe5, 0x93, 0x71, 0x01, 0x00,
|
||||||
0x48, 0xa4, 0x28, 0xb1, 0x1c, 0x6c, 0x16, 0x4f, 0x10, 0x88, 0xe9, 0xa4, 0xf0, 0xe3, 0xa1, 0x1c,
|
0x00,
|
||||||
0xe3, 0x81, 0x47, 0x72, 0x8c, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, 0x91,
|
|
||||||
0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x90, 0xc4,
|
|
||||||
0x06, 0x0e, 0x7e, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x29, 0xcd, 0xe8, 0xd4, 0xc8, 0x01,
|
|
||||||
0x00, 0x00,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *AddrBookRecord) Marshal() (dAtA []byte, err error) {
|
func (m *AddrBookRecord) Marshal() (dAtA []byte, err error) {
|
||||||
|
@ -271,15 +280,10 @@ func (m *AddrBookRecord) MarshalToSizedBuffer(dAtA []byte) (int, error) {
|
||||||
dAtA[i] = 0x12
|
dAtA[i] = 0x12
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if m.Id != nil {
|
if len(m.Id) > 0 {
|
||||||
{
|
i -= len(m.Id)
|
||||||
size := m.Id.Size()
|
copy(dAtA[i:], m.Id)
|
||||||
i -= size
|
i = encodeVarintPstore(dAtA, i, uint64(len(m.Id)))
|
||||||
if _, err := m.Id.MarshalTo(dAtA[i:]); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
i = encodeVarintPstore(dAtA, i, uint64(size))
|
|
||||||
}
|
|
||||||
i--
|
i--
|
||||||
dAtA[i] = 0xa
|
dAtA[i] = 0xa
|
||||||
}
|
}
|
||||||
|
@ -316,15 +320,10 @@ func (m *AddrBookRecord_AddrEntry) MarshalToSizedBuffer(dAtA []byte) (int, error
|
||||||
i--
|
i--
|
||||||
dAtA[i] = 0x10
|
dAtA[i] = 0x10
|
||||||
}
|
}
|
||||||
if m.Addr != nil {
|
if len(m.Addr) > 0 {
|
||||||
{
|
i -= len(m.Addr)
|
||||||
size := m.Addr.Size()
|
copy(dAtA[i:], m.Addr)
|
||||||
i -= size
|
i = encodeVarintPstore(dAtA, i, uint64(len(m.Addr)))
|
||||||
if _, err := m.Addr.MarshalTo(dAtA[i:]); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
i = encodeVarintPstore(dAtA, i, uint64(size))
|
|
||||||
}
|
|
||||||
i--
|
i--
|
||||||
dAtA[i] = 0xa
|
dAtA[i] = 0xa
|
||||||
}
|
}
|
||||||
|
@ -377,133 +376,14 @@ func encodeVarintPstore(dAtA []byte, offset int, v uint64) int {
|
||||||
dAtA[offset] = uint8(v)
|
dAtA[offset] = uint8(v)
|
||||||
return base
|
return base
|
||||||
}
|
}
|
||||||
func NewPopulatedAddrBookRecord(r randyPstore, easy bool) *AddrBookRecord {
|
|
||||||
this := &AddrBookRecord{}
|
|
||||||
this.Id = NewPopulatedProtoPeerID(r)
|
|
||||||
if r.Intn(5) != 0 {
|
|
||||||
v1 := r.Intn(5)
|
|
||||||
this.Addrs = make([]*AddrBookRecord_AddrEntry, v1)
|
|
||||||
for i := 0; i < v1; i++ {
|
|
||||||
this.Addrs[i] = NewPopulatedAddrBookRecord_AddrEntry(r, easy)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if r.Intn(5) != 0 {
|
|
||||||
this.CertifiedRecord = NewPopulatedAddrBookRecord_CertifiedRecord(r, easy)
|
|
||||||
}
|
|
||||||
if !easy && r.Intn(10) != 0 {
|
|
||||||
}
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPopulatedAddrBookRecord_AddrEntry(r randyPstore, easy bool) *AddrBookRecord_AddrEntry {
|
|
||||||
this := &AddrBookRecord_AddrEntry{}
|
|
||||||
this.Addr = NewPopulatedProtoAddr(r)
|
|
||||||
this.Expiry = int64(r.Int63())
|
|
||||||
if r.Intn(2) == 0 {
|
|
||||||
this.Expiry *= -1
|
|
||||||
}
|
|
||||||
this.Ttl = int64(r.Int63())
|
|
||||||
if r.Intn(2) == 0 {
|
|
||||||
this.Ttl *= -1
|
|
||||||
}
|
|
||||||
if !easy && r.Intn(10) != 0 {
|
|
||||||
}
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPopulatedAddrBookRecord_CertifiedRecord(r randyPstore, easy bool) *AddrBookRecord_CertifiedRecord {
|
|
||||||
this := &AddrBookRecord_CertifiedRecord{}
|
|
||||||
this.Seq = uint64(uint64(r.Uint32()))
|
|
||||||
v2 := r.Intn(100)
|
|
||||||
this.Raw = make([]byte, v2)
|
|
||||||
for i := 0; i < v2; i++ {
|
|
||||||
this.Raw[i] = byte(r.Intn(256))
|
|
||||||
}
|
|
||||||
if !easy && r.Intn(10) != 0 {
|
|
||||||
}
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
type randyPstore interface {
|
|
||||||
Float32() float32
|
|
||||||
Float64() float64
|
|
||||||
Int63() int64
|
|
||||||
Int31() int32
|
|
||||||
Uint32() uint32
|
|
||||||
Intn(n int) int
|
|
||||||
}
|
|
||||||
|
|
||||||
func randUTF8RunePstore(r randyPstore) rune {
|
|
||||||
ru := r.Intn(62)
|
|
||||||
if ru < 10 {
|
|
||||||
return rune(ru + 48)
|
|
||||||
} else if ru < 36 {
|
|
||||||
return rune(ru + 55)
|
|
||||||
}
|
|
||||||
return rune(ru + 61)
|
|
||||||
}
|
|
||||||
func randStringPstore(r randyPstore) string {
|
|
||||||
v3 := r.Intn(100)
|
|
||||||
tmps := make([]rune, v3)
|
|
||||||
for i := 0; i < v3; i++ {
|
|
||||||
tmps[i] = randUTF8RunePstore(r)
|
|
||||||
}
|
|
||||||
return string(tmps)
|
|
||||||
}
|
|
||||||
func randUnrecognizedPstore(r randyPstore, maxFieldNumber int) (dAtA []byte) {
|
|
||||||
l := r.Intn(5)
|
|
||||||
for i := 0; i < l; i++ {
|
|
||||||
wire := r.Intn(4)
|
|
||||||
if wire == 3 {
|
|
||||||
wire = 5
|
|
||||||
}
|
|
||||||
fieldNumber := maxFieldNumber + r.Intn(100)
|
|
||||||
dAtA = randFieldPstore(dAtA, r, fieldNumber, wire)
|
|
||||||
}
|
|
||||||
return dAtA
|
|
||||||
}
|
|
||||||
func randFieldPstore(dAtA []byte, r randyPstore, fieldNumber int, wire int) []byte {
|
|
||||||
key := uint32(fieldNumber)<<3 | uint32(wire)
|
|
||||||
switch wire {
|
|
||||||
case 0:
|
|
||||||
dAtA = encodeVarintPopulatePstore(dAtA, uint64(key))
|
|
||||||
v4 := r.Int63()
|
|
||||||
if r.Intn(2) == 0 {
|
|
||||||
v4 *= -1
|
|
||||||
}
|
|
||||||
dAtA = encodeVarintPopulatePstore(dAtA, uint64(v4))
|
|
||||||
case 1:
|
|
||||||
dAtA = encodeVarintPopulatePstore(dAtA, uint64(key))
|
|
||||||
dAtA = append(dAtA, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))
|
|
||||||
case 2:
|
|
||||||
dAtA = encodeVarintPopulatePstore(dAtA, uint64(key))
|
|
||||||
ll := r.Intn(100)
|
|
||||||
dAtA = encodeVarintPopulatePstore(dAtA, uint64(ll))
|
|
||||||
for j := 0; j < ll; j++ {
|
|
||||||
dAtA = append(dAtA, byte(r.Intn(256)))
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
dAtA = encodeVarintPopulatePstore(dAtA, uint64(key))
|
|
||||||
dAtA = append(dAtA, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))
|
|
||||||
}
|
|
||||||
return dAtA
|
|
||||||
}
|
|
||||||
func encodeVarintPopulatePstore(dAtA []byte, v uint64) []byte {
|
|
||||||
for v >= 1<<7 {
|
|
||||||
dAtA = append(dAtA, uint8(uint64(v)&0x7f|0x80))
|
|
||||||
v >>= 7
|
|
||||||
}
|
|
||||||
dAtA = append(dAtA, uint8(v))
|
|
||||||
return dAtA
|
|
||||||
}
|
|
||||||
func (m *AddrBookRecord) Size() (n int) {
|
func (m *AddrBookRecord) Size() (n int) {
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
var l int
|
var l int
|
||||||
_ = l
|
_ = l
|
||||||
if m.Id != nil {
|
l = len(m.Id)
|
||||||
l = m.Id.Size()
|
if l > 0 {
|
||||||
n += 1 + l + sovPstore(uint64(l))
|
n += 1 + l + sovPstore(uint64(l))
|
||||||
}
|
}
|
||||||
if len(m.Addrs) > 0 {
|
if len(m.Addrs) > 0 {
|
||||||
|
@ -525,8 +405,8 @@ func (m *AddrBookRecord_AddrEntry) Size() (n int) {
|
||||||
}
|
}
|
||||||
var l int
|
var l int
|
||||||
_ = l
|
_ = l
|
||||||
if m.Addr != nil {
|
l = len(m.Addr)
|
||||||
l = m.Addr.Size()
|
if l > 0 {
|
||||||
n += 1 + l + sovPstore(uint64(l))
|
n += 1 + l + sovPstore(uint64(l))
|
||||||
}
|
}
|
||||||
if m.Expiry != 0 {
|
if m.Expiry != 0 {
|
||||||
|
@ -618,10 +498,9 @@ func (m *AddrBookRecord) Unmarshal(dAtA []byte) error {
|
||||||
if postIndex > l {
|
if postIndex > l {
|
||||||
return io.ErrUnexpectedEOF
|
return io.ErrUnexpectedEOF
|
||||||
}
|
}
|
||||||
var v ProtoPeerID
|
m.Id = append(m.Id[:0], dAtA[iNdEx:postIndex]...)
|
||||||
m.Id = &v
|
if m.Id == nil {
|
||||||
if err := m.Id.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
|
m.Id = []byte{}
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
iNdEx = postIndex
|
iNdEx = postIndex
|
||||||
case 2:
|
case 2:
|
||||||
|
@ -773,10 +652,9 @@ func (m *AddrBookRecord_AddrEntry) Unmarshal(dAtA []byte) error {
|
||||||
if postIndex > l {
|
if postIndex > l {
|
||||||
return io.ErrUnexpectedEOF
|
return io.ErrUnexpectedEOF
|
||||||
}
|
}
|
||||||
var v ProtoAddr
|
m.Addr = append(m.Addr[:0], dAtA[iNdEx:postIndex]...)
|
||||||
m.Addr = &v
|
if m.Addr == nil {
|
||||||
if err := m.Addr.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
|
m.Addr = []byte{}
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
iNdEx = postIndex
|
iNdEx = postIndex
|
||||||
case 2:
|
case 2:
|
||||||
|
|
|
@ -1,15 +1,10 @@
|
||||||
syntax = "proto3";
|
syntax = "proto3";
|
||||||
package pstore.pb;
|
package pstore.pb;
|
||||||
|
|
||||||
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
|
|
||||||
|
|
||||||
option (gogoproto.benchgen_all) = true;
|
|
||||||
option (gogoproto.populate_all) = true;
|
|
||||||
|
|
||||||
// AddrBookRecord represents a record for a peer in the address book.
|
// AddrBookRecord represents a record for a peer in the address book.
|
||||||
message AddrBookRecord {
|
message AddrBookRecord {
|
||||||
// The peer ID.
|
// The peer ID.
|
||||||
bytes id = 1 [(gogoproto.customtype) = "ProtoPeerID"];
|
bytes id = 1;
|
||||||
|
|
||||||
// The multiaddresses. This is a sorted list where element 0 expires the soonest.
|
// The multiaddresses. This is a sorted list where element 0 expires the soonest.
|
||||||
repeated AddrEntry addrs = 2;
|
repeated AddrEntry addrs = 2;
|
||||||
|
@ -19,7 +14,7 @@ message AddrBookRecord {
|
||||||
|
|
||||||
// AddrEntry represents a single multiaddress.
|
// AddrEntry represents a single multiaddress.
|
||||||
message AddrEntry {
|
message AddrEntry {
|
||||||
bytes addr = 1 [(gogoproto.customtype) = "ProtoAddr"];
|
bytes addr = 1;
|
||||||
|
|
||||||
// The point in time when this address expires.
|
// The point in time when this address expires.
|
||||||
int64 expiry = 2;
|
int64 expiry = 2;
|
||||||
|
|
|
@ -1,183 +0,0 @@
|
||||||
// Code generated by protoc-gen-gogo. DO NOT EDIT.
|
|
||||||
// source: pstore.proto
|
|
||||||
|
|
||||||
package pstore_pb
|
|
||||||
|
|
||||||
import (
|
|
||||||
fmt "fmt"
|
|
||||||
_ "github.com/gogo/protobuf/gogoproto"
|
|
||||||
github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto"
|
|
||||||
proto "github.com/gogo/protobuf/proto"
|
|
||||||
math "math"
|
|
||||||
math_rand "math/rand"
|
|
||||||
testing "testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var _ = proto.Marshal
|
|
||||||
var _ = fmt.Errorf
|
|
||||||
var _ = math.Inf
|
|
||||||
|
|
||||||
func BenchmarkAddrBookRecordProtoMarshal(b *testing.B) {
|
|
||||||
popr := math_rand.New(math_rand.NewSource(616))
|
|
||||||
total := 0
|
|
||||||
pops := make([]*AddrBookRecord, 10000)
|
|
||||||
for i := 0; i < 10000; i++ {
|
|
||||||
pops[i] = NewPopulatedAddrBookRecord(popr, false)
|
|
||||||
}
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
dAtA, err := github_com_gogo_protobuf_proto.Marshal(pops[i%10000])
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
total += len(dAtA)
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(total / b.N))
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAddrBookRecordProtoUnmarshal(b *testing.B) {
|
|
||||||
popr := math_rand.New(math_rand.NewSource(616))
|
|
||||||
total := 0
|
|
||||||
datas := make([][]byte, 10000)
|
|
||||||
for i := 0; i < 10000; i++ {
|
|
||||||
dAtA, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedAddrBookRecord(popr, false))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
datas[i] = dAtA
|
|
||||||
}
|
|
||||||
msg := &AddrBookRecord{}
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
total += len(datas[i%10000])
|
|
||||||
if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(total / b.N))
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAddrBookRecord_AddrEntryProtoMarshal(b *testing.B) {
|
|
||||||
popr := math_rand.New(math_rand.NewSource(616))
|
|
||||||
total := 0
|
|
||||||
pops := make([]*AddrBookRecord_AddrEntry, 10000)
|
|
||||||
for i := 0; i < 10000; i++ {
|
|
||||||
pops[i] = NewPopulatedAddrBookRecord_AddrEntry(popr, false)
|
|
||||||
}
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
dAtA, err := github_com_gogo_protobuf_proto.Marshal(pops[i%10000])
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
total += len(dAtA)
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(total / b.N))
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAddrBookRecord_AddrEntryProtoUnmarshal(b *testing.B) {
|
|
||||||
popr := math_rand.New(math_rand.NewSource(616))
|
|
||||||
total := 0
|
|
||||||
datas := make([][]byte, 10000)
|
|
||||||
for i := 0; i < 10000; i++ {
|
|
||||||
dAtA, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedAddrBookRecord_AddrEntry(popr, false))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
datas[i] = dAtA
|
|
||||||
}
|
|
||||||
msg := &AddrBookRecord_AddrEntry{}
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
total += len(datas[i%10000])
|
|
||||||
if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(total / b.N))
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAddrBookRecord_CertifiedRecordProtoMarshal(b *testing.B) {
|
|
||||||
popr := math_rand.New(math_rand.NewSource(616))
|
|
||||||
total := 0
|
|
||||||
pops := make([]*AddrBookRecord_CertifiedRecord, 10000)
|
|
||||||
for i := 0; i < 10000; i++ {
|
|
||||||
pops[i] = NewPopulatedAddrBookRecord_CertifiedRecord(popr, false)
|
|
||||||
}
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
dAtA, err := github_com_gogo_protobuf_proto.Marshal(pops[i%10000])
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
total += len(dAtA)
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(total / b.N))
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAddrBookRecord_CertifiedRecordProtoUnmarshal(b *testing.B) {
|
|
||||||
popr := math_rand.New(math_rand.NewSource(616))
|
|
||||||
total := 0
|
|
||||||
datas := make([][]byte, 10000)
|
|
||||||
for i := 0; i < 10000; i++ {
|
|
||||||
dAtA, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedAddrBookRecord_CertifiedRecord(popr, false))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
datas[i] = dAtA
|
|
||||||
}
|
|
||||||
msg := &AddrBookRecord_CertifiedRecord{}
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
total += len(datas[i%10000])
|
|
||||||
if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(total / b.N))
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAddrBookRecordSize(b *testing.B) {
|
|
||||||
popr := math_rand.New(math_rand.NewSource(616))
|
|
||||||
total := 0
|
|
||||||
pops := make([]*AddrBookRecord, 1000)
|
|
||||||
for i := 0; i < 1000; i++ {
|
|
||||||
pops[i] = NewPopulatedAddrBookRecord(popr, false)
|
|
||||||
}
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
total += pops[i%1000].Size()
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(total / b.N))
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAddrBookRecord_AddrEntrySize(b *testing.B) {
|
|
||||||
popr := math_rand.New(math_rand.NewSource(616))
|
|
||||||
total := 0
|
|
||||||
pops := make([]*AddrBookRecord_AddrEntry, 1000)
|
|
||||||
for i := 0; i < 1000; i++ {
|
|
||||||
pops[i] = NewPopulatedAddrBookRecord_AddrEntry(popr, false)
|
|
||||||
}
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
total += pops[i%1000].Size()
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(total / b.N))
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAddrBookRecord_CertifiedRecordSize(b *testing.B) {
|
|
||||||
popr := math_rand.New(math_rand.NewSource(616))
|
|
||||||
total := 0
|
|
||||||
pops := make([]*AddrBookRecord_CertifiedRecord, 1000)
|
|
||||||
for i := 0; i < 1000; i++ {
|
|
||||||
pops[i] = NewPopulatedAddrBookRecord_CertifiedRecord(popr, false)
|
|
||||||
}
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
total += pops[i%1000].Size()
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(total / b.N))
|
|
||||||
}
|
|
||||||
|
|
||||||
//These tests are generated by github.com/gogo/protobuf/plugin/testgen
|
|
Loading…
Reference in New Issue