mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-07 05:46:32 +00:00
p2p: proto leftover (#4995)
## Description removing codec.go from p2p pkg and some leftover amino encoding Closes: #XXX
This commit is contained in:
13
p2p/codec.go
13
p2p/codec.go
@@ -1,13 +0,0 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
amino "github.com/tendermint/go-amino"
|
||||
|
||||
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
|
||||
)
|
||||
|
||||
var cdc = amino.NewCodec()
|
||||
|
||||
func init() {
|
||||
cryptoamino.RegisterAmino(cdc)
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/tendermint/tendermint/libs/bytes"
|
||||
tmstrings "github.com/tendermint/tendermint/libs/strings"
|
||||
tmp2p "github.com/tendermint/tendermint/proto/p2p"
|
||||
"github.com/tendermint/tendermint/version"
|
||||
)
|
||||
|
||||
@@ -220,30 +222,50 @@ func (info DefaultNodeInfo) NetAddress() (*NetAddress, error) {
|
||||
return NewNetAddressString(idAddr)
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------
|
||||
// These methods are for Protobuf Compatibility
|
||||
func (info DefaultNodeInfo) ToProto() *tmp2p.DefaultNodeInfo {
|
||||
|
||||
// Size returns the size of the amino encoding, in bytes.
|
||||
func (info *DefaultNodeInfo) Size() int {
|
||||
bs, _ := info.Marshal()
|
||||
return len(bs)
|
||||
}
|
||||
|
||||
// Marshal returns the amino encoding.
|
||||
func (info *DefaultNodeInfo) Marshal() ([]byte, error) {
|
||||
return cdc.MarshalBinaryBare(info)
|
||||
}
|
||||
|
||||
// MarshalTo calls Marshal and copies to the given buffer.
|
||||
func (info *DefaultNodeInfo) MarshalTo(data []byte) (int, error) {
|
||||
bs, err := info.Marshal()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
dni := new(tmp2p.DefaultNodeInfo)
|
||||
dni.ProtocolVersion = tmp2p.ProtocolVersion{
|
||||
P2P: info.ProtocolVersion.P2P,
|
||||
Block: info.ProtocolVersion.Block,
|
||||
App: info.ProtocolVersion.App,
|
||||
}
|
||||
return copy(data, bs), nil
|
||||
|
||||
dni.DefaultNodeID = string(info.DefaultNodeID)
|
||||
dni.ListenAddr = info.ListenAddr
|
||||
dni.Network = info.Network
|
||||
dni.Version = info.Version
|
||||
dni.Channels = info.Channels
|
||||
dni.Moniker = info.Moniker
|
||||
dni.Other = tmp2p.DefaultNodeInfoOther{
|
||||
TxIndex: info.Other.TxIndex,
|
||||
RPCAddress: info.Other.RPCAddress,
|
||||
}
|
||||
|
||||
return dni
|
||||
}
|
||||
|
||||
// Unmarshal deserializes from amino encoded form.
|
||||
func (info *DefaultNodeInfo) Unmarshal(bs []byte) error {
|
||||
return cdc.UnmarshalBinaryBare(bs, info)
|
||||
func DefaultNodeInfoFromToProto(pb *tmp2p.DefaultNodeInfo) (DefaultNodeInfo, error) {
|
||||
if pb == nil {
|
||||
return DefaultNodeInfo{}, errors.New("nil node info")
|
||||
}
|
||||
dni := DefaultNodeInfo{
|
||||
ProtocolVersion: ProtocolVersion{
|
||||
P2P: pb.ProtocolVersion.P2P,
|
||||
Block: pb.ProtocolVersion.Block,
|
||||
App: pb.ProtocolVersion.App,
|
||||
},
|
||||
DefaultNodeID: ID(pb.DefaultNodeID),
|
||||
ListenAddr: pb.ListenAddr,
|
||||
Network: pb.Network,
|
||||
Version: pb.Version,
|
||||
Channels: pb.Channels,
|
||||
Moniker: pb.Moniker,
|
||||
Other: DefaultNodeInfoOther{
|
||||
TxIndex: pb.Other.TxIndex,
|
||||
RPCAddress: pb.Other.RPCAddress,
|
||||
},
|
||||
}
|
||||
|
||||
return dni, nil
|
||||
}
|
||||
|
||||
@@ -9,7 +9,9 @@ import (
|
||||
"golang.org/x/net/netutil"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
"github.com/tendermint/tendermint/libs/protoio"
|
||||
"github.com/tendermint/tendermint/p2p/conn"
|
||||
tmp2p "github.com/tendermint/tendermint/proto/p2p"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -524,20 +526,18 @@ func handshake(
|
||||
var (
|
||||
errc = make(chan error, 2)
|
||||
|
||||
peerNodeInfo DefaultNodeInfo
|
||||
ourNodeInfo = nodeInfo.(DefaultNodeInfo)
|
||||
pbpeerNodeInfo tmp2p.DefaultNodeInfo
|
||||
peerNodeInfo DefaultNodeInfo
|
||||
ourNodeInfo = nodeInfo.(DefaultNodeInfo)
|
||||
)
|
||||
|
||||
go func(errc chan<- error, c net.Conn) {
|
||||
_, err := cdc.MarshalBinaryLengthPrefixedWriter(c, ourNodeInfo)
|
||||
_, err := protoio.NewDelimitedWriter(c).WriteMsg(ourNodeInfo.ToProto())
|
||||
errc <- err
|
||||
}(errc, c)
|
||||
go func(errc chan<- error, c net.Conn) {
|
||||
_, err := cdc.UnmarshalBinaryLengthPrefixedReader(
|
||||
c,
|
||||
&peerNodeInfo,
|
||||
int64(MaxNodeInfoSize()),
|
||||
)
|
||||
protoReader := protoio.NewDelimitedReader(c, MaxNodeInfoSize())
|
||||
err := protoReader.ReadMsg(&pbpeerNodeInfo)
|
||||
errc <- err
|
||||
}(errc, c)
|
||||
|
||||
@@ -548,6 +548,11 @@ func handshake(
|
||||
}
|
||||
}
|
||||
|
||||
peerNodeInfo, err := DefaultNodeInfoFromToProto(&pbpeerNodeInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return peerNodeInfo, c.SetDeadline(time.Time{})
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
"github.com/tendermint/tendermint/libs/protoio"
|
||||
"github.com/tendermint/tendermint/p2p/conn"
|
||||
tmp2p "github.com/tendermint/tendermint/proto/p2p"
|
||||
)
|
||||
|
||||
var defaultNodeName = "host_peer"
|
||||
@@ -575,19 +577,24 @@ func TestTransportHandshake(t *testing.T) {
|
||||
}
|
||||
|
||||
go func(c net.Conn) {
|
||||
_, err := cdc.MarshalBinaryLengthPrefixedWriter(c, peerNodeInfo.(DefaultNodeInfo))
|
||||
_, err := protoio.NewDelimitedWriter(c).WriteMsg(peerNodeInfo.(DefaultNodeInfo).ToProto())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}(c)
|
||||
go func(c net.Conn) {
|
||||
var ni DefaultNodeInfo
|
||||
|
||||
_, err := cdc.UnmarshalBinaryLengthPrefixedReader(
|
||||
c,
|
||||
&ni,
|
||||
int64(MaxNodeInfoSize()),
|
||||
var (
|
||||
// ni DefaultNodeInfo
|
||||
pbni tmp2p.DefaultNodeInfo
|
||||
)
|
||||
|
||||
protoReader := protoio.NewDelimitedReader(c, MaxNodeInfoSize())
|
||||
err := protoReader.ReadMsg(&pbni)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = DefaultNodeInfoFromToProto(&pbni)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user