peer: Add PeerRecordFromProtobuf.

This commit is contained in:
Vibhav Pant 2020-03-27 11:24:40 +05:30
parent 075ac7484c
commit 6690d78eeb
No known key found for this signature in database
GPG Key ID: BA939833A10FCBAA

View File

@ -108,6 +108,23 @@ func PeerRecordFromAddrInfo(info AddrInfo) *PeerRecord {
return rec
}
// PeerRecordFromProtobuf creates a PeerRecord from a protobuf PeerRecord
// struct.
func PeerRecordFromProtobuf(msg *pb.PeerRecord) (*PeerRecord, error) {
record := &PeerRecord{}
var id ID
if err := id.UnmarshalBinary(msg.PeerId); err != nil {
return nil, err
}
record.PeerID = id
record.Addrs = addrsFromProtobuf(msg.Addresses)
record.Seq = msg.Seq
return record, nil
}
// TimestampSeq is a helper to generate a timestamp-based sequence number for a PeerRecord.
func TimestampSeq() uint64 {
return uint64(time.Now().UnixNano())
@ -138,14 +155,13 @@ func (r *PeerRecord) UnmarshalRecord(bytes []byte) error {
if err != nil {
return err
}
var id ID
err = id.UnmarshalBinary(msg.PeerId)
rPtr, err := PeerRecordFromProtobuf(&msg)
if err != nil {
return err
}
r.PeerID = id
r.Addrs = addrsFromProtobuf(msg.Addresses)
r.Seq = msg.Seq
*r = *rPtr
return nil
}