From a3b6e518b2eb9f2c55968910081a6ff78268fd87 Mon Sep 17 00:00:00 2001 From: Zhijie Shen <35021395+zjshen14@users.noreply.github.com> Date: Thu, 14 Mar 2019 10:25:12 -0700 Subject: [PATCH] Move rpc message type definitions into proto file (#741) --- dispatcher/dispatcher.go | 36 ++- p2p/agent.go | 22 +- p2p/pb/message.pb.go | 199 -------------- p2p/pb/message.proto | 30 --- proto/rpc/rpc.proto | 26 ++ protogen/iotexrpc/rpc.pb.go | 244 ++++++++++++++++-- protogen/utils.go | 56 ++-- .../internal/client/client_test.go | 3 + 8 files changed, 297 insertions(+), 319 deletions(-) delete mode 100644 p2p/pb/message.pb.go delete mode 100644 p2p/pb/message.proto diff --git a/dispatcher/dispatcher.go b/dispatcher/dispatcher.go index de8d917aef..c7c55782bd 100644 --- a/dispatcher/dispatcher.go +++ b/dispatcher/dispatcher.go @@ -66,7 +66,6 @@ type blockMsg struct { ctx context.Context chainID uint32 block *iotextypes.Block - blkType uint32 } func (m blockMsg) ChainID() uint32 { @@ -101,7 +100,7 @@ type IotxDispatcher struct { started int32 shutdown int32 eventChan chan interface{} - eventAudit map[uint32]int + eventAudit map[iotexrpc.MessageType]int eventAuditLock sync.RWMutex wg sync.WaitGroup quit chan struct{} @@ -114,7 +113,7 @@ type IotxDispatcher struct { func NewDispatcher(cfg config.Config) (Dispatcher, error) { d := &IotxDispatcher{ eventChan: make(chan interface{}, cfg.Dispatcher.EventChanSize), - eventAudit: make(map[uint32]int), + eventAudit: make(map[iotexrpc.MessageType]int), quit: make(chan struct{}), subscribers: make(map[uint32]Subscriber), } @@ -160,10 +159,10 @@ func (d *IotxDispatcher) EventChan() *chan interface{} { } // EventAudit returns the event audit map -func (d *IotxDispatcher) EventAudit() map[uint32]int { +func (d *IotxDispatcher) EventAudit() map[iotexrpc.MessageType]int { d.eventAuditLock.RLock() defer d.eventAuditLock.RUnlock() - snapshot := make(map[uint32]int) + snapshot := make(map[iotexrpc.MessageType]int) for k, v := range d.eventAudit { snapshot[k] = v } @@ -199,7 +198,7 @@ loop: // handleActionMsg handles actionMsg from all peers. func (d *IotxDispatcher) handleActionMsg(m *actionMsg) { - d.updateEventAudit(protogen.MsgActionType) + d.updateEventAudit(iotexrpc.MessageType_ACTION) if subscriber, ok := d.subscribers[m.ChainID()]; ok { if err := subscriber.HandleAction(m.ctx, m.action); err != nil { requestMtc.WithLabelValues("AddAction", "false").Inc() @@ -215,7 +214,7 @@ func (d *IotxDispatcher) handleBlockMsg(m *blockMsg) { d.subscribersMU.RLock() defer d.subscribersMU.RUnlock() if subscriber, ok := d.subscribers[m.ChainID()]; ok { - d.updateEventAudit(protogen.MsgBlockProtoMsgType) + d.updateEventAudit(iotexrpc.MessageType_BLOCK) if err := subscriber.HandleBlock(m.ctx, m.block); err != nil { log.L().Error("Fail to handle the block.", zap.Error(err)) } @@ -231,7 +230,7 @@ func (d *IotxDispatcher) handleBlockSyncMsg(m *blockSyncMsg) { zap.Uint64("start", m.sync.Start), zap.Uint64("end", m.sync.End)) - d.updateEventAudit(protogen.MsgBlockSyncReqType) + d.updateEventAudit(iotexrpc.MessageType_BLOCK_REQUEST) if subscriber, ok := d.subscribers[m.ChainID()]; ok { // dispatch to block sync if err := subscriber.HandleSyncRequest(m.ctx, m.peer, m.sync); err != nil { @@ -263,7 +262,6 @@ func (d *IotxDispatcher) dispatchBlockCommit(ctx context.Context, chainID uint32 ctx: ctx, chainID: chainID, block: (msg).(*iotextypes.Block), - blkType: protogen.MsgBlockProtoMsgType, }) } @@ -282,7 +280,7 @@ func (d *IotxDispatcher) dispatchBlockSyncReq(ctx context.Context, chainID uint3 // HandleBroadcast handles incoming broadcast message func (d *IotxDispatcher) HandleBroadcast(ctx context.Context, chainID uint32, message proto.Message) { - msgType, err := protogen.GetTypeFromProtoMsg(message) + msgType, err := protogen.GetTypeFromRPCMsg(message) if err != nil { log.L().Warn("Unexpected message handled by HandleBroadcast.", zap.Error(err)) } @@ -296,33 +294,33 @@ func (d *IotxDispatcher) HandleBroadcast(ctx context.Context, chainID uint32, me d.subscribersMU.RUnlock() switch msgType { - case protogen.MsgConsensusType: + case iotexrpc.MessageType_CONSENSUS: err := subscriber.HandleConsensusMsg(message.(*iotexrpc.Consensus)) if err != nil { log.L().Error("Failed to handle block propose.", zap.Error(err)) } - case protogen.MsgActionType: + case iotexrpc.MessageType_ACTION: d.dispatchAction(ctx, chainID, message) - case protogen.MsgBlockProtoMsgType: + case iotexrpc.MessageType_BLOCK: d.dispatchBlockCommit(ctx, chainID, message) default: - log.L().Warn("Unexpected msgType handled by HandleBroadcast.", zap.Uint32("msgType", msgType)) + log.L().Warn("Unexpected msgType handled by HandleBroadcast.", zap.Any("msgType", msgType)) } } // HandleTell handles incoming unicast message func (d *IotxDispatcher) HandleTell(ctx context.Context, chainID uint32, peer peerstore.PeerInfo, message proto.Message) { - msgType, err := protogen.GetTypeFromProtoMsg(message) + msgType, err := protogen.GetTypeFromRPCMsg(message) if err != nil { log.L().Warn("Unexpected message handled by HandleTell.", zap.Error(err)) } switch msgType { - case protogen.MsgBlockSyncReqType: + case iotexrpc.MessageType_BLOCK_REQUEST: d.dispatchBlockSyncReq(ctx, chainID, peer, message) - case protogen.MsgBlockProtoMsgType: + case iotexrpc.MessageType_BLOCK: d.dispatchBlockCommit(ctx, chainID, message) default: - log.L().Warn("Unexpected msgType handled by HandleTell.", zap.Uint32("msgType", msgType)) + log.L().Warn("Unexpected msgType handled by HandleTell.", zap.Any("msgType", msgType)) } } @@ -336,7 +334,7 @@ func (d *IotxDispatcher) enqueueEvent(event interface{}) { }() } -func (d *IotxDispatcher) updateEventAudit(t uint32) { +func (d *IotxDispatcher) updateEventAudit(t iotexrpc.MessageType) { d.eventAuditLock.Lock() defer d.eventAuditLock.Unlock() d.eventAudit[t]++ diff --git a/p2p/agent.go b/p2p/agent.go index 9fc69da945..cb046d9318 100644 --- a/p2p/agent.go +++ b/p2p/agent.go @@ -24,9 +24,9 @@ import ( "go.uber.org/zap" "github.com/iotexproject/iotex-core/config" - p2ppb "github.com/iotexproject/iotex-core/p2p/pb" "github.com/iotexproject/iotex-core/pkg/log" "github.com/iotexproject/iotex-core/protogen" + "github.com/iotexproject/iotex-core/protogen/iotexrpc" ) const ( @@ -115,7 +115,7 @@ func (p *Agent) Start(ctx context.Context) error { <-ready var ( peerID string - broadcast p2ppb.BroadcastMsg + broadcast iotexrpc.BroadcastMsg latency int64 ) skip := false @@ -150,7 +150,7 @@ func (p *Agent) Start(ctx context.Context) error { t, _ := ptypes.Timestamp(broadcast.GetTimestamp()) latency = time.Since(t).Nanoseconds() / time.Millisecond.Nanoseconds() - msg, err := protogen.TypifyProtoMsg(broadcast.MsgType, broadcast.MsgBody) + msg, err := protogen.TypifyRPCMsg(broadcast.MsgType, broadcast.MsgBody) if err != nil { err = errors.Wrap(err, "error when typifying broadcast message") return @@ -165,7 +165,7 @@ func (p *Agent) Start(ctx context.Context) error { // Blocking handling the unicast message until the agent is started <-ready var ( - unicast p2ppb.UnicastMsg + unicast iotexrpc.UnicastMsg peerID string latency int64 ) @@ -181,7 +181,7 @@ func (p *Agent) Start(ctx context.Context) error { err = errors.Wrap(err, "error when marshaling unicast message") return } - msg, err := protogen.TypifyProtoMsg(unicast.MsgType, unicast.MsgBody) + msg, err := protogen.TypifyRPCMsg(unicast.MsgType, unicast.MsgBody) if err != nil { err = errors.Wrap(err, "error when typifying unicast message") return @@ -277,7 +277,7 @@ func (p *Agent) Stop(ctx context.Context) error { // BroadcastOutbound sends a broadcast message to the whole network func (p *Agent) BroadcastOutbound(ctx context.Context, msg proto.Message) (err error) { - var msgType uint32 + var msgType iotexrpc.MessageType var msgBody []byte defer func() { status := successStr @@ -301,7 +301,7 @@ func (p *Agent) BroadcastOutbound(ctx context.Context, msg proto.Message) (err e err = errors.New("P2P context doesn't exist") return } - broadcast := p2ppb.BroadcastMsg{ + broadcast := iotexrpc.BroadcastMsg{ ChainId: p2pCtx.ChainID, PeerId: p.host.HostIdentity(), MsgType: msgType, @@ -322,7 +322,7 @@ func (p *Agent) BroadcastOutbound(ctx context.Context, msg proto.Message) (err e // UnicastOutbound sends a unicast message to the given address func (p *Agent) UnicastOutbound(ctx context.Context, peer peerstore.PeerInfo, msg proto.Message) (err error) { - var msgType uint32 + var msgType iotexrpc.MessageType var msgBody []byte defer func() { status := successStr @@ -340,7 +340,7 @@ func (p *Agent) UnicastOutbound(ctx context.Context, peer peerstore.PeerInfo, ms err = errors.New("P2P context doesn't exist") return } - unicast := p2ppb.UnicastMsg{ + unicast := iotexrpc.UnicastMsg{ ChainId: p2pCtx.ChainID, PeerId: p.host.HostIdentity(), MsgType: msgType, @@ -370,8 +370,8 @@ func (p *Agent) Neighbors(ctx context.Context) ([]peerstore.PeerInfo, error) { return p.host.Neighbors(ctx) } -func convertAppMsg(msg proto.Message) (uint32, []byte, error) { - msgType, err := protogen.GetTypeFromProtoMsg(msg) +func convertAppMsg(msg proto.Message) (iotexrpc.MessageType, []byte, error) { + msgType, err := protogen.GetTypeFromRPCMsg(msg) if err != nil { return 0, nil, errors.Wrap(err, "error when converting application message to proto") } diff --git a/p2p/pb/message.pb.go b/p2p/pb/message.pb.go deleted file mode 100644 index 8c23e04f5d..0000000000 --- a/p2p/pb/message.pb.go +++ /dev/null @@ -1,199 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// source: message.proto - -package p2ppb - -import ( - fmt "fmt" - proto "github.com/golang/protobuf/proto" - timestamp "github.com/golang/protobuf/ptypes/timestamp" - math "math" -) - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package - -type BroadcastMsg struct { - ChainId uint32 `protobuf:"varint,1,opt,name=chain_id,json=chainId,proto3" json:"chain_id,omitempty"` - MsgType uint32 `protobuf:"varint,2,opt,name=msg_type,json=msgType,proto3" json:"msg_type,omitempty"` - MsgBody []byte `protobuf:"bytes,3,opt,name=msg_body,json=msgBody,proto3" json:"msg_body,omitempty"` - PeerId string `protobuf:"bytes,4,opt,name=peer_id,json=peerId,proto3" json:"peer_id,omitempty"` - Timestamp *timestamp.Timestamp `protobuf:"bytes,5,opt,name=timestamp,proto3" json:"timestamp,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *BroadcastMsg) Reset() { *m = BroadcastMsg{} } -func (m *BroadcastMsg) String() string { return proto.CompactTextString(m) } -func (*BroadcastMsg) ProtoMessage() {} -func (*BroadcastMsg) Descriptor() ([]byte, []int) { - return fileDescriptor_33c57e4bae7b9afd, []int{0} -} - -func (m *BroadcastMsg) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_BroadcastMsg.Unmarshal(m, b) -} -func (m *BroadcastMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_BroadcastMsg.Marshal(b, m, deterministic) -} -func (m *BroadcastMsg) XXX_Merge(src proto.Message) { - xxx_messageInfo_BroadcastMsg.Merge(m, src) -} -func (m *BroadcastMsg) XXX_Size() int { - return xxx_messageInfo_BroadcastMsg.Size(m) -} -func (m *BroadcastMsg) XXX_DiscardUnknown() { - xxx_messageInfo_BroadcastMsg.DiscardUnknown(m) -} - -var xxx_messageInfo_BroadcastMsg proto.InternalMessageInfo - -func (m *BroadcastMsg) GetChainId() uint32 { - if m != nil { - return m.ChainId - } - return 0 -} - -func (m *BroadcastMsg) GetMsgType() uint32 { - if m != nil { - return m.MsgType - } - return 0 -} - -func (m *BroadcastMsg) GetMsgBody() []byte { - if m != nil { - return m.MsgBody - } - return nil -} - -func (m *BroadcastMsg) GetPeerId() string { - if m != nil { - return m.PeerId - } - return "" -} - -func (m *BroadcastMsg) GetTimestamp() *timestamp.Timestamp { - if m != nil { - return m.Timestamp - } - return nil -} - -type UnicastMsg struct { - ChainId uint32 `protobuf:"varint,1,opt,name=chain_id,json=chainId,proto3" json:"chain_id,omitempty"` - Addr string `protobuf:"bytes,2,opt,name=addr,proto3" json:"addr,omitempty"` - MsgType uint32 `protobuf:"varint,3,opt,name=msg_type,json=msgType,proto3" json:"msg_type,omitempty"` - MsgBody []byte `protobuf:"bytes,4,opt,name=msg_body,json=msgBody,proto3" json:"msg_body,omitempty"` - PeerId string `protobuf:"bytes,5,opt,name=peer_id,json=peerId,proto3" json:"peer_id,omitempty"` - Timestamp *timestamp.Timestamp `protobuf:"bytes,6,opt,name=timestamp,proto3" json:"timestamp,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *UnicastMsg) Reset() { *m = UnicastMsg{} } -func (m *UnicastMsg) String() string { return proto.CompactTextString(m) } -func (*UnicastMsg) ProtoMessage() {} -func (*UnicastMsg) Descriptor() ([]byte, []int) { - return fileDescriptor_33c57e4bae7b9afd, []int{1} -} - -func (m *UnicastMsg) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_UnicastMsg.Unmarshal(m, b) -} -func (m *UnicastMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_UnicastMsg.Marshal(b, m, deterministic) -} -func (m *UnicastMsg) XXX_Merge(src proto.Message) { - xxx_messageInfo_UnicastMsg.Merge(m, src) -} -func (m *UnicastMsg) XXX_Size() int { - return xxx_messageInfo_UnicastMsg.Size(m) -} -func (m *UnicastMsg) XXX_DiscardUnknown() { - xxx_messageInfo_UnicastMsg.DiscardUnknown(m) -} - -var xxx_messageInfo_UnicastMsg proto.InternalMessageInfo - -func (m *UnicastMsg) GetChainId() uint32 { - if m != nil { - return m.ChainId - } - return 0 -} - -func (m *UnicastMsg) GetAddr() string { - if m != nil { - return m.Addr - } - return "" -} - -func (m *UnicastMsg) GetMsgType() uint32 { - if m != nil { - return m.MsgType - } - return 0 -} - -func (m *UnicastMsg) GetMsgBody() []byte { - if m != nil { - return m.MsgBody - } - return nil -} - -func (m *UnicastMsg) GetPeerId() string { - if m != nil { - return m.PeerId - } - return "" -} - -func (m *UnicastMsg) GetTimestamp() *timestamp.Timestamp { - if m != nil { - return m.Timestamp - } - return nil -} - -func init() { - proto.RegisterType((*BroadcastMsg)(nil), "p2ppb.BroadcastMsg") - proto.RegisterType((*UnicastMsg)(nil), "p2ppb.UnicastMsg") -} - -func init() { proto.RegisterFile("message.proto", fileDescriptor_33c57e4bae7b9afd) } - -var fileDescriptor_33c57e4bae7b9afd = []byte{ - // 242 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x8f, 0xb1, 0x4e, 0xc3, 0x30, - 0x10, 0x86, 0x65, 0x9a, 0xa4, 0xe4, 0x68, 0x17, 0x2f, 0x84, 0x2e, 0x44, 0x9d, 0x32, 0xa5, 0x52, - 0x59, 0x98, 0xbb, 0x75, 0x60, 0x89, 0xca, 0x5c, 0x39, 0xbd, 0xc3, 0x44, 0xc2, 0xf1, 0x29, 0x36, - 0x43, 0x5e, 0x8b, 0x67, 0xe0, 0xc1, 0x50, 0x1c, 0x05, 0xc4, 0x10, 0x09, 0xb6, 0x5c, 0xbe, 0x5f, - 0xd6, 0xf7, 0xc1, 0xda, 0x90, 0x73, 0x4a, 0x53, 0xc9, 0x9d, 0xf5, 0x56, 0xc6, 0xbc, 0x67, 0xae, - 0x37, 0xf7, 0xda, 0x5a, 0xfd, 0x46, 0xbb, 0xf0, 0xb3, 0x7e, 0x7f, 0xd9, 0xf9, 0xc6, 0x90, 0xf3, - 0xca, 0xf0, 0xb8, 0xdb, 0x7e, 0x08, 0x58, 0x1d, 0x3a, 0xab, 0xf0, 0xa2, 0x9c, 0x7f, 0x72, 0x5a, - 0xde, 0xc1, 0xf5, 0xe5, 0x55, 0x35, 0xed, 0xb9, 0xc1, 0x4c, 0xe4, 0xa2, 0x58, 0x57, 0xcb, 0x70, - 0x1f, 0x71, 0x40, 0xc6, 0xe9, 0xb3, 0xef, 0x99, 0xb2, 0xab, 0x11, 0x19, 0xa7, 0x4f, 0x3d, 0xd3, - 0x84, 0x6a, 0x8b, 0x7d, 0xb6, 0xc8, 0x45, 0xb1, 0x0a, 0xe8, 0x60, 0xb1, 0x97, 0xb7, 0xb0, 0x64, - 0xa2, 0x6e, 0x78, 0x2f, 0xca, 0x45, 0x91, 0x56, 0xc9, 0x70, 0x1e, 0x51, 0x3e, 0x42, 0xfa, 0x6d, - 0x93, 0xc5, 0xb9, 0x28, 0x6e, 0xf6, 0x9b, 0x72, 0xf4, 0x2d, 0x27, 0xdf, 0xf2, 0x34, 0x2d, 0xaa, - 0x9f, 0xf1, 0xf6, 0x53, 0x00, 0x3c, 0xb7, 0xcd, 0x1f, 0x94, 0x25, 0x44, 0x0a, 0xb1, 0x0b, 0xba, - 0x69, 0x15, 0xbe, 0x7f, 0x65, 0x2c, 0xe6, 0x33, 0xa2, 0xd9, 0x8c, 0x78, 0x3e, 0x23, 0xf9, 0x47, - 0x46, 0x9d, 0x04, 0xfc, 0xf0, 0x15, 0x00, 0x00, 0xff, 0xff, 0x9c, 0xdc, 0x49, 0x95, 0xbb, 0x01, - 0x00, 0x00, -} diff --git a/p2p/pb/message.proto b/p2p/pb/message.proto deleted file mode 100644 index f7728af6d4..0000000000 --- a/p2p/pb/message.proto +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2018 IoTeX -// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no -// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent -// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache -// License 2.0 that can be found in the LICENSE file. - -// To compile the proto, run: -// protoc --go_out=plugins=grpc:. *.proto -syntax = "proto3"; - -package p2ppb; - -import "google/protobuf/timestamp.proto"; - -message BroadcastMsg { - uint32 chain_id = 1; - uint32 msg_type = 2; - bytes msg_body = 3; - string peer_id = 4; - google.protobuf.Timestamp timestamp = 5; -} - -message UnicastMsg { - uint32 chain_id = 1; - string addr = 2; - uint32 msg_type = 3; - bytes msg_body = 4; - string peer_id = 5; - google.protobuf.Timestamp timestamp = 6; -} diff --git a/proto/rpc/rpc.proto b/proto/rpc/rpc.proto index 946cf56edf..c32093a298 100644 --- a/proto/rpc/rpc.proto +++ b/proto/rpc/rpc.proto @@ -29,3 +29,29 @@ message Consensus { google.protobuf.Timestamp timestamp = 4; bytes data = 5; } + +enum MessageType { + UNKNOWN = 0; + ACTION = 1; + BLOCK = 2; + CONSENSUS = 3; + BLOCK_REQUEST = 4; + TEST = 10001; +} + +message BroadcastMsg { + uint32 chain_id = 1; + MessageType msg_type = 2; + bytes msg_body = 3; + string peer_id = 4; + google.protobuf.Timestamp timestamp = 5; +} + +message UnicastMsg { + uint32 chain_id = 1; + string addr = 2; + MessageType msg_type = 3; + bytes msg_body = 4; + string peer_id = 5; + google.protobuf.Timestamp timestamp = 6; +} \ No newline at end of file diff --git a/protogen/iotexrpc/rpc.pb.go b/protogen/iotexrpc/rpc.pb.go index e20dd31164..dc55074908 100644 --- a/protogen/iotexrpc/rpc.pb.go +++ b/protogen/iotexrpc/rpc.pb.go @@ -21,6 +21,43 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package +type MessageType int32 + +const ( + MessageType_UNKNOWN MessageType = 0 + MessageType_ACTION MessageType = 1 + MessageType_BLOCK MessageType = 2 + MessageType_CONSENSUS MessageType = 3 + MessageType_BLOCK_REQUEST MessageType = 4 + MessageType_TEST MessageType = 10001 +) + +var MessageType_name = map[int32]string{ + 0: "UNKNOWN", + 1: "ACTION", + 2: "BLOCK", + 3: "CONSENSUS", + 4: "BLOCK_REQUEST", + 10001: "TEST", +} + +var MessageType_value = map[string]int32{ + "UNKNOWN": 0, + "ACTION": 1, + "BLOCK": 2, + "CONSENSUS": 3, + "BLOCK_REQUEST": 4, + "TEST": 10001, +} + +func (x MessageType) String() string { + return proto.EnumName(MessageType_name, int32(x)) +} + +func (MessageType) EnumDescriptor() ([]byte, []int) { + return fileDescriptor_59d40974ffbedc26, []int{0} +} + type Consensus_ConsensusMessageType int32 const ( @@ -164,34 +201,199 @@ func (m *Consensus) GetData() []byte { return nil } +type BroadcastMsg struct { + ChainId uint32 `protobuf:"varint,1,opt,name=chain_id,json=chainId,proto3" json:"chain_id,omitempty"` + MsgType MessageType `protobuf:"varint,2,opt,name=msg_type,json=msgType,proto3,enum=iotexrpc.MessageType" json:"msg_type,omitempty"` + MsgBody []byte `protobuf:"bytes,3,opt,name=msg_body,json=msgBody,proto3" json:"msg_body,omitempty"` + PeerId string `protobuf:"bytes,4,opt,name=peer_id,json=peerId,proto3" json:"peer_id,omitempty"` + Timestamp *timestamp.Timestamp `protobuf:"bytes,5,opt,name=timestamp,proto3" json:"timestamp,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *BroadcastMsg) Reset() { *m = BroadcastMsg{} } +func (m *BroadcastMsg) String() string { return proto.CompactTextString(m) } +func (*BroadcastMsg) ProtoMessage() {} +func (*BroadcastMsg) Descriptor() ([]byte, []int) { + return fileDescriptor_59d40974ffbedc26, []int{2} +} + +func (m *BroadcastMsg) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_BroadcastMsg.Unmarshal(m, b) +} +func (m *BroadcastMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_BroadcastMsg.Marshal(b, m, deterministic) +} +func (m *BroadcastMsg) XXX_Merge(src proto.Message) { + xxx_messageInfo_BroadcastMsg.Merge(m, src) +} +func (m *BroadcastMsg) XXX_Size() int { + return xxx_messageInfo_BroadcastMsg.Size(m) +} +func (m *BroadcastMsg) XXX_DiscardUnknown() { + xxx_messageInfo_BroadcastMsg.DiscardUnknown(m) +} + +var xxx_messageInfo_BroadcastMsg proto.InternalMessageInfo + +func (m *BroadcastMsg) GetChainId() uint32 { + if m != nil { + return m.ChainId + } + return 0 +} + +func (m *BroadcastMsg) GetMsgType() MessageType { + if m != nil { + return m.MsgType + } + return MessageType_UNKNOWN +} + +func (m *BroadcastMsg) GetMsgBody() []byte { + if m != nil { + return m.MsgBody + } + return nil +} + +func (m *BroadcastMsg) GetPeerId() string { + if m != nil { + return m.PeerId + } + return "" +} + +func (m *BroadcastMsg) GetTimestamp() *timestamp.Timestamp { + if m != nil { + return m.Timestamp + } + return nil +} + +type UnicastMsg struct { + ChainId uint32 `protobuf:"varint,1,opt,name=chain_id,json=chainId,proto3" json:"chain_id,omitempty"` + Addr string `protobuf:"bytes,2,opt,name=addr,proto3" json:"addr,omitempty"` + MsgType MessageType `protobuf:"varint,3,opt,name=msg_type,json=msgType,proto3,enum=iotexrpc.MessageType" json:"msg_type,omitempty"` + MsgBody []byte `protobuf:"bytes,4,opt,name=msg_body,json=msgBody,proto3" json:"msg_body,omitempty"` + PeerId string `protobuf:"bytes,5,opt,name=peer_id,json=peerId,proto3" json:"peer_id,omitempty"` + Timestamp *timestamp.Timestamp `protobuf:"bytes,6,opt,name=timestamp,proto3" json:"timestamp,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *UnicastMsg) Reset() { *m = UnicastMsg{} } +func (m *UnicastMsg) String() string { return proto.CompactTextString(m) } +func (*UnicastMsg) ProtoMessage() {} +func (*UnicastMsg) Descriptor() ([]byte, []int) { + return fileDescriptor_59d40974ffbedc26, []int{3} +} + +func (m *UnicastMsg) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_UnicastMsg.Unmarshal(m, b) +} +func (m *UnicastMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_UnicastMsg.Marshal(b, m, deterministic) +} +func (m *UnicastMsg) XXX_Merge(src proto.Message) { + xxx_messageInfo_UnicastMsg.Merge(m, src) +} +func (m *UnicastMsg) XXX_Size() int { + return xxx_messageInfo_UnicastMsg.Size(m) +} +func (m *UnicastMsg) XXX_DiscardUnknown() { + xxx_messageInfo_UnicastMsg.DiscardUnknown(m) +} + +var xxx_messageInfo_UnicastMsg proto.InternalMessageInfo + +func (m *UnicastMsg) GetChainId() uint32 { + if m != nil { + return m.ChainId + } + return 0 +} + +func (m *UnicastMsg) GetAddr() string { + if m != nil { + return m.Addr + } + return "" +} + +func (m *UnicastMsg) GetMsgType() MessageType { + if m != nil { + return m.MsgType + } + return MessageType_UNKNOWN +} + +func (m *UnicastMsg) GetMsgBody() []byte { + if m != nil { + return m.MsgBody + } + return nil +} + +func (m *UnicastMsg) GetPeerId() string { + if m != nil { + return m.PeerId + } + return "" +} + +func (m *UnicastMsg) GetTimestamp() *timestamp.Timestamp { + if m != nil { + return m.Timestamp + } + return nil +} + func init() { + proto.RegisterEnum("iotexrpc.MessageType", MessageType_name, MessageType_value) proto.RegisterEnum("iotexrpc.Consensus_ConsensusMessageType", Consensus_ConsensusMessageType_name, Consensus_ConsensusMessageType_value) proto.RegisterType((*BlockSync)(nil), "iotexrpc.BlockSync") proto.RegisterType((*Consensus)(nil), "iotexrpc.Consensus") + proto.RegisterType((*BroadcastMsg)(nil), "iotexrpc.BroadcastMsg") + proto.RegisterType((*UnicastMsg)(nil), "iotexrpc.UnicastMsg") } func init() { proto.RegisterFile("proto/rpc/rpc.proto", fileDescriptor_59d40974ffbedc26) } var fileDescriptor_59d40974ffbedc26 = []byte{ - // 307 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0xcf, 0x4f, 0xc2, 0x30, - 0x1c, 0xc5, 0x9d, 0x0c, 0x02, 0x5f, 0x50, 0x49, 0x25, 0x66, 0xe1, 0x22,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0x64, 0xa4, 0xcb, 0xb2, 0xc1, 0x4e, 0x65, 0x42, 0xc0, 0x5f, 0xc7, 0x36, 0x0e, 0xaa, 0x3d, - 0x2f, 0x6c, 0x31, 0xa7, 0xfb, 0x0f, 0xd0, 0xf9, 0xef, 0x2c, 0xd2, 0x82, 0xfa, 0x9c, 0xcd, 0xe6, - 0xb3, 0xc5, 0xcb, 0x7b, 0xfb, 0x8c, 0x5c, 0x41, 0x73, 0x3c, 0x7d, 0x9b, 0xb1, 0xc5, 0x78, 0x32, - 0x9e, 0x2e, 0xdb, 0xde, 0xe8, 0xf1, 0xe3, 0x3e, 0x49, 0xed, 0x26, 0x5b, 0x51, 0x2e, 0x77, 0x91, - 0x03, 0x50, 0x5a, 0x7e, 0x21, 0xb7, 0x85, 0xb9, 0xe3, 0x52, 0x1f, 0x9e, 0x3c, 0x41, 0x11, 0x95, - 0x84, 0xab, 0x9a, 0x8b, 0x86, 0x7f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x96, 0xb2, 0x1c, 0x69, 0xb4, - 0x01, 0x00, 0x00, + // 511 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0xcf, 0x6e, 0xd3, 0x40, + 0x10, 0xc6, 0xeb, 0xc4, 0xf9, 0xe3, 0x49, 0x02, 0x66, 0x29, 0x90, 0xf6, 0x42, 0x94, 0x53, 0x84, + 0x84, 0x83, 0x5a, 0x40, 0x1c, 0xb8, 0x34, 0xc1, 0x87, 0xa8, 0x8d, 0x5d, 0xd6, 0x8e, 0x90, 0x38, + 0x10, 0x39, 0xde, 0xc5, 0x31, 0x34, 0x5e, 0x6b, 0x77, 0x23, 0x91, 0xc7, 0xe0, 0xb1, 0x78, 0x06, + 0x5e, 0x85, 0x03, 0xf2, 0xb8, 0x69, 0x5a, 0x89, 0x4a, 0xe4, 0x60, 0xe9, 0xfb, 0x66, 0x67, 0x67, + 0xfc, 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 0xa0, 0x83, 0x27, 0x73, 0xea, + 0x7e, 0x9c, 0xb9, 0x41, 0x68, 0x9b, 0xc4, 0x02, 0x33, 0x2c, 0xd4, 0x4f, 0x6f, 0xf4, 0xf6, 0xf3, + 0xeb, 0x24, 0xd5, 0xcb, 0xf5, 0xc2, 0x89, 0xc5, 0x6a, 0x88, 0xe4, 0xb9, 0x14, 0xdf, 0x78, 0xac, + 0x4b, 0xf3, 0x32, 0x16, 0xf2, 0xfa, 0xe7, 0x4b, 0x78, 0x36, 0xdc, 0x46, 0xb3, 0xa8, 0x63, 0xe9, + 0xf4, 0x6f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x64, 0x17, 0xfd, 0x18, 0xbe, 0x03, 0x00, 0x00, } diff --git a/protogen/utils.go b/protogen/utils.go index edcaf30550..8be60c0142 100644 --- a/protogen/utils.go +++ b/protogen/utils.go @@ -16,62 +16,40 @@ import ( "github.com/iotexproject/iotex-core/protogen/testingpb" ) -// Magic header to identify IoTeX traffic -const ( - MagicBroadcastMsgHeader uint32 = 4689 -) - -const ( - // UnknownProtoMsgType is an unknown message type that is not expected - UnknownProtoMsgType uint32 = 0 - // MsgTxProtoMsgType is for transactions broadcasted within the network - MsgTxProtoMsgType uint32 = 1 - // MsgBlockProtoMsgType is for blocks broadcasted within the network - MsgBlockProtoMsgType uint32 = 2 - // MsgBlockSyncReqType is for requests among peers to sync blocks - MsgBlockSyncReqType uint32 = 3 - // MsgActionType is the action message - MsgActionType uint32 = 5 - // MsgConsensusType is for consensus message - MsgConsensusType uint32 = 6 - // TestPayloadType is a test payload message type - TestPayloadType uint32 = 10001 -) - -// GetTypeFromProtoMsg retrieves the proto message type -func GetTypeFromProtoMsg(msg proto.Message) (uint32, error) { +// GetTypeFromRPCMsg retrieves the proto message type +func GetTypeFromRPCMsg(msg proto.Message) (iotexrpc.MessageType, error) { switch msg.(type) { case *iotextypes.Block: - return MsgBlockProtoMsgType, nil + return iotexrpc.MessageType_BLOCK, nil case *iotexrpc.BlockSync: - return MsgBlockSyncReqType, nil + return iotexrpc.MessageType_BLOCK_REQUEST, nil case *iotextypes.Action: - return MsgActionType, nil + return iotexrpc.MessageType_ACTION, nil case *iotexrpc.Consensus: - return MsgConsensusType, nil + return iotexrpc.MessageType_CONSENSUS, nil case *testingpb.TestPayload: - return TestPayloadType, nil + return iotexrpc.MessageType_TEST, nil default: - return UnknownProtoMsgType, errors.New("UnknownProtoMsgType proto message type") + return iotexrpc.MessageType_UNKNOWN, errors.New("unknown RPC message type") } } -// TypifyProtoMsg unmarshal a proto message based on the given MessageType -func TypifyProtoMsg(tp uint32, msg []byte) (proto.Message, error) { +// TypifyRPCMsg unmarshal a proto message based on the given MessageType +func TypifyRPCMsg(t iotexrpc.MessageType, msg []byte) (proto.Message, error) { var m proto.Message - switch tp { - case MsgBlockProtoMsgType: + switch t { + case iotexrpc.MessageType_BLOCK: m = &iotextypes.Block{} - case MsgConsensusType: + case iotexrpc.MessageType_CONSENSUS: m = &iotexrpc.Consensus{} - case MsgBlockSyncReqType: + case iotexrpc.MessageType_BLOCK_REQUEST: m = &iotexrpc.BlockSync{} - case MsgActionType: + case iotexrpc.MessageType_ACTION: m = &iotextypes.Action{} - case TestPayloadType: + case iotexrpc.MessageType_TEST: m = &testingpb.TestPayload{} default: - return nil, errors.New("UnknownProtoMsgType proto message type") + return nil, errors.New("unknown RPC message type") } err := proto.Unmarshal(msg, m) diff --git a/tools/actioninjector.v2/internal/client/client_test.go b/tools/actioninjector.v2/internal/client/client_test.go index 8899058fea..ae8647e870 100644 --- a/tools/actioninjector.v2/internal/client/client_test.go +++ b/tools/actioninjector.v2/internal/client/client_test.go @@ -6,6 +6,8 @@ import ( "math/big" "testing" + "github.com/iotexproject/iotex-core/testutil" + "github.com/iotexproject/iotex-core/config" "github.com/golang/mock/gomock" @@ -28,6 +30,7 @@ func TestClient(t *testing.T) { b := testaddress.Addrinfo["bravo"].String() cfg := config.Default + cfg.API.Port = testutil.RandomPort() ctx := context.Background() mockCtrl := gomock.NewController(t)