diff --git a/p2p/codec.go b/p2p/codec.go deleted file mode 100644 index 463276318..000000000 --- a/p2p/codec.go +++ /dev/null @@ -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) -} diff --git a/p2p/node_info.go b/p2p/node_info.go index ea7fc961c..5e6e46e06 100644 --- a/p2p/node_info.go +++ b/p2p/node_info.go @@ -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) -} + dni := new(tmp2p.DefaultNodeInfo) + dni.ProtocolVersion = tmp2p.ProtocolVersion{ + P2P: info.ProtocolVersion.P2P, + Block: info.ProtocolVersion.Block, + App: info.ProtocolVersion.App, + } + + 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, + } -// Marshal returns the amino encoding. -func (info *DefaultNodeInfo) Marshal() ([]byte, error) { - return cdc.MarshalBinaryBare(info) + return dni } -// 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 +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 copy(data, bs), nil -} -// Unmarshal deserializes from amino encoded form. -func (info *DefaultNodeInfo) Unmarshal(bs []byte) error { - return cdc.UnmarshalBinaryBare(bs, info) + return dni, nil } diff --git a/p2p/transport.go b/p2p/transport.go index 7a962bb16..3d1821744 100644 --- a/p2p/transport.go +++ b/p2p/transport.go @@ -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{}) } diff --git a/p2p/transport_test.go b/p2p/transport_test.go index e461c3645..30149b615 100644 --- a/p2p/transport_test.go +++ b/p2p/transport_test.go @@ -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) } diff --git a/proto/p2p/types.pb.go b/proto/p2p/types.pb.go index 817a81e0b..6533681fa 100644 --- a/proto/p2p/types.pb.go +++ b/proto/p2p/types.pb.go @@ -252,8 +252,8 @@ func (m *DefaultNodeInfo) GetOther() DefaultNodeInfoOther { } type DefaultNodeInfoOther struct { - TxIndex string `protobuf:"bytes,1,opt,name=tx_index,json=txIndex,proto3" json:"tx_index,omitempty"` - RPCAdddress string `protobuf:"bytes,2,opt,name=rpc_address,json=rpcAddress,proto3" json:"rpc_address,omitempty"` + TxIndex string `protobuf:"bytes,1,opt,name=tx_index,json=txIndex,proto3" json:"tx_index,omitempty"` + RPCAddress string `protobuf:"bytes,2,opt,name=rpc_address,json=rpcAddress,proto3" json:"rpc_address,omitempty"` } func (m *DefaultNodeInfoOther) Reset() { *m = DefaultNodeInfoOther{} } @@ -296,9 +296,9 @@ func (m *DefaultNodeInfoOther) GetTxIndex() string { return "" } -func (m *DefaultNodeInfoOther) GetRPCAdddress() string { +func (m *DefaultNodeInfoOther) GetRPCAddress() string { if m != nil { - return m.RPCAdddress + return m.RPCAddress } return "" } @@ -313,39 +313,38 @@ func init() { func init() { proto.RegisterFile("proto/p2p/types.proto", fileDescriptor_5c4320c1810ca85c) } var fileDescriptor_5c4320c1810ca85c = []byte{ - // 498 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x53, 0xcf, 0x6e, 0x1a, 0x3f, - 0x18, 0x64, 0x97, 0xe5, 0x4f, 0x3e, 0x7e, 0x88, 0xfc, 0x2c, 0x5a, 0x6d, 0x72, 0xd8, 0x45, 0x48, - 0xad, 0x50, 0x0e, 0x50, 0xd1, 0x53, 0x8f, 0xa1, 0xa8, 0x12, 0x97, 0x14, 0x59, 0x55, 0x0e, 0xbd, - 0xac, 0x60, 0xed, 0x80, 0x05, 0xd8, 0x96, 0xd7, 0x69, 0xc9, 0x5b, 0xf4, 0xb1, 0x72, 0xcc, 0xb1, - 0x27, 0x54, 0x2d, 0x0f, 0xd1, 0x6b, 0x65, 0x7b, 0x93, 0x20, 0xc4, 0x6d, 0x66, 0xec, 0xd9, 0xf9, - 0xbe, 0x91, 0x17, 0xde, 0x48, 0x25, 0xb4, 0x18, 0xc8, 0xa1, 0x1c, 0xe8, 0x07, 0x49, 0xb3, 0xbe, - 0xe5, 0xa8, 0xad, 0x29, 0x27, 0x54, 0x6d, 0x18, 0xd7, 0x4e, 0xe9, 0xcb, 0xa1, 0xbc, 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, 0x21, 0x7c, 0xf9, 0x51, - 0xe6, 0x55, 0x0b, 0x3f, 0xfe, 0x0b, 0x00, 0x00, 0xff, 0xff, 0x48, 0x04, 0xbe, 0xde, 0x3c, 0x03, - 0x00, 0x00, + // 496 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x93, 0x4f, 0x6f, 0xda, 0x30, + 0x18, 0xc6, 0x49, 0x08, 0x7f, 0xfa, 0x32, 0x46, 0x67, 0xb1, 0x29, 0xed, 0x21, 0x41, 0x48, 0x9b, + 0x50, 0x0f, 0x20, 0xb1, 0xd3, 0x8e, 0x63, 0x68, 0x12, 0x97, 0x0e, 0x59, 0x53, 0x0f, 0xbb, 0x44, + 0x10, 0xbb, 0x60, 0x01, 0xb6, 0xe5, 0xb8, 0x1b, 0xfd, 0x16, 0xfb, 0x58, 0x3d, 0xf6, 0xb8, 0x13, + 0x9a, 0xc2, 0x87, 0xd8, 0x75, 0xb2, 0x9d, 0xb6, 0x08, 0x71, 0x7b, 0x9e, 0xd7, 0x7e, 0xf3, 0xbc, + 0xef, 0x4f, 0x0e, 0xbc, 0x95, 0x4a, 0x68, 0x31, 0x90, 0x43, 0x39, 0xd0, 0xf7, 0x92, 0x66, 0x7d, + 0xeb, 0x51, 0x5b, 0x53, 0x4e, 0xa8, 0xda, 0x30, 0xae, 0x5d, 0xa5, 0x2f, 0x87, 0xf2, 0xf2, 0x83, + 0x5e, 0x32, 0x45, 0x12, 0x39, 0x53, 0xfa, 0x7e, 0xe0, 0x1a, 0x17, 0x62, 0x21, 0x5e, 0x94, 0xbb, + 0xdb, 0x9d, 0x03, 0x5c, 0x53, 0xfd, 0x99, 0x10, 0x45, 0xb3, 0x0c, 0xbd, 0x03, 0x9f, 0x91, 0xd0, + 0xeb, 0x78, 0xbd, 0xb3, 0x51, 0x35, 0xdf, 0xc5, 0xfe, 0x64, 0x8c, 0x7d, 0x46, 0x6c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0xc5, 0x9d, 0x14, 0x16, 0x5d, 0x42, 0x3d, 0x5d, 0xce, 0x38, 0xa7, 0xeb, + 0x2c, 0xac, 0x76, 0xbc, 0xde, 0x2b, 0xfc, 0xec, 0x4d, 0xd7, 0x46, 0x70, 0xb6, 0xa2, 0x2a, 0xac, + 0xb9, 0xae, 0xc2, 0xa2, 0xaf, 0x50, 0x11, 0x7a, 0x49, 0x55, 0x58, 0xb7, 0x48, 0xae, 0x4e, 0x23, + 0x39, 0x62, 0xfa, 0xcd, 0x74, 0x14, 0x5c, 0x5c, 0x7b, 0x77, 0x0e, 0xed, 0x53, 0x97, 0xd0, 0x05, + 0xd4, 0xf5, 0x36, 0x61, 0x9c, 0xd0, 0xad, 0x7b, 0x43, 0xb8, 0xa6, 0xb7, 0x13, 0x63, 0xd1, 0x00, + 0x1a, 0x4a, 0xa6, 0x16, 0x01, 0xcd, 0xb2, 0x02, 0xde, 0xeb, 0x7c, 0x17, 0x03, 0x9e, 0x7e, 0x29, + 0x5e, 0x1f, 0x06, 0x25, 0xd3, 0x42, 0x8f, 0xc6, 0x0f, 0x79, 0xe4, 0x3d, 0xe6, 0x91, 0xf7, 0x37, + 0x8f, 0xbc, 0xdf, 0xfb, 0xa8, 0xf4, 0xb8, 0x8f, 0x4a, 0x7f, 0xf6, 0x51, 0xe9, 0xc7, 0xd5, 0x82, + 0xe9, 0xe5, 0xdd, 0xbc, 0x9f, 0x8a, 0xcd, 0xe0, 0x65, 0x81, 0x43, 0xf9, 0xfc, 0x9f, 0xcc, 0xab, + 0x56, 0x7e, 0xfc, 0x1f, 0x00, 0x00, 0xff, 0xff, 0x57, 0xdb, 0x7b, 0x33, 0x3b, 0x03, 0x00, 0x00, } func (m *NetAddress) Marshal() (dAtA []byte, err error) { @@ -540,10 +539,10 @@ func (m *DefaultNodeInfoOther) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if len(m.RPCAdddress) > 0 { - i -= len(m.RPCAdddress) - copy(dAtA[i:], m.RPCAdddress) - i = encodeVarintTypes(dAtA, i, uint64(len(m.RPCAdddress))) + if len(m.RPCAddress) > 0 { + i -= len(m.RPCAddress) + copy(dAtA[i:], m.RPCAddress) + i = encodeVarintTypes(dAtA, i, uint64(len(m.RPCAddress))) i-- dAtA[i] = 0x12 } @@ -657,7 +656,7 @@ func (m *DefaultNodeInfoOther) Size() (n int) { if l > 0 { n += 1 + l + sovTypes(uint64(l)) } - l = len(m.RPCAdddress) + l = len(m.RPCAddress) if l > 0 { n += 1 + l + sovTypes(uint64(l)) } @@ -1324,7 +1323,7 @@ func (m *DefaultNodeInfoOther) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field RPCAdddress", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field RPCAddress", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -1352,7 +1351,7 @@ func (m *DefaultNodeInfoOther) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.RPCAdddress = string(dAtA[iNdEx:postIndex]) + m.RPCAddress = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex diff --git a/proto/p2p/types.proto b/proto/p2p/types.proto index 542fc4f19..648686571 100644 --- a/proto/p2p/types.proto +++ b/proto/p2p/types.proto @@ -31,5 +31,5 @@ message DefaultNodeInfo { message DefaultNodeInfoOther { string tx_index = 1; - string rpc_address = 2 [(gogoproto.customname) = "RPCAdddress"]; + string rpc_address = 2 [(gogoproto.customname) = "RPCAddress"]; } diff --git a/state/codec.go b/state/codec.go deleted file mode 100644 index df2c15545..000000000 --- a/state/codec.go +++ /dev/null @@ -1,13 +0,0 @@ -package state - -import ( - amino "github.com/tendermint/go-amino" - - cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino" -) - -var cdc = amino.NewCodec() - -func init() { - cryptoamino.RegisterAmino(cdc) -}