From d003d29d6670e9b3deb90d7ac85d66a402d20d89 Mon Sep 17 00:00:00 2001 From: Dhruba Basu <7675102+dhrubabasu@users.noreply.github.com> Date: Fri, 8 Mar 2024 23:12:16 -0500 Subject: [PATCH] Remove legacy p2p message handling (#2833) Co-authored-by: Stephen Buttolph --- proto/message/tx.proto | 16 -- proto/pb/message/tx.pb.go | 232 ------------------------- vms/avm/network/network.go | 44 ----- vms/avm/network/network_test.go | 179 ------------------- vms/components/message/codec.go | 34 ---- vms/components/message/handler.go | 29 ---- vms/components/message/handler_test.go | 40 ----- vms/components/message/message.go | 84 --------- vms/components/message/message_test.go | 49 ------ vms/components/message/tx.go | 18 -- vms/components/message/tx_test.go | 34 ---- vms/platformvm/network/network.go | 63 +------ vms/platformvm/network/network_test.go | 130 -------------- 13 files changed, 8 insertions(+), 944 deletions(-) delete mode 100644 proto/message/tx.proto delete mode 100644 proto/pb/message/tx.pb.go delete mode 100644 vms/components/message/codec.go delete mode 100644 vms/components/message/handler.go delete mode 100644 vms/components/message/handler_test.go delete mode 100644 vms/components/message/message.go delete mode 100644 vms/components/message/message_test.go delete mode 100644 vms/components/message/tx.go delete mode 100644 vms/components/message/tx_test.go diff --git a/proto/message/tx.proto b/proto/message/tx.proto deleted file mode 100644 index d651e7c0727..00000000000 --- a/proto/message/tx.proto +++ /dev/null @@ -1,16 +0,0 @@ -syntax = "proto3"; - -package message; - -option go_package = "github.com/ava-labs/avalanchego/proto/pb/message"; - -message Message { - oneof message { - Tx tx = 1; - } -} - -message Tx { - // The byte representation of this transaction. - bytes tx = 1; -} diff --git a/proto/pb/message/tx.pb.go b/proto/pb/message/tx.pb.go deleted file mode 100644 index 4320fdc0d12..00000000000 --- a/proto/pb/message/tx.pb.go +++ /dev/null @@ -1,232 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.30.0 -// protoc (unknown) -// source: message/tx.proto - -package message - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -type Message struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - // Types that are assignable to Message: - // - // *Message_Tx - Message isMessage_Message `protobuf_oneof:"message"` -} - -func (x *Message) Reset() { - *x = Message{} - if protoimpl.UnsafeEnabled { - mi := &file_message_tx_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Message) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Message) ProtoMessage() {} - -func (x *Message) ProtoReflect() protoreflect.Message { - mi := &file_message_tx_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use Message.ProtoReflect.Descriptor instead. -func (*Message) Descriptor() ([]byte, []int) { - return file_message_tx_proto_rawDescGZIP(), []int{0} -} - -func (m *Message) GetMessage() isMessage_Message { - if m != nil { - return m.Message - } - return nil -} - -func (x *Message) GetTx() *Tx { - if x, ok := x.GetMessage().(*Message_Tx); ok { - return x.Tx - } - return nil -} - -type isMessage_Message interface { - isMessage_Message() -} - -type Message_Tx struct { - Tx *Tx `protobuf:"bytes,1,opt,name=tx,proto3,oneof"` -} - -func (*Message_Tx) isMessage_Message() {} - -type Tx struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - // The byte representation of this transaction. - Tx []byte `protobuf:"bytes,1,opt,name=tx,proto3" json:"tx,omitempty"` -} - -func (x *Tx) Reset() { - *x = Tx{} - if protoimpl.UnsafeEnabled { - mi := &file_message_tx_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Tx) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Tx) ProtoMessage() {} - -func (x *Tx) ProtoReflect() protoreflect.Message { - mi := &file_message_tx_proto_msgTypes[1] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use Tx.ProtoReflect.Descriptor instead. -func (*Tx) Descriptor() ([]byte, []int) { - return file_message_tx_proto_rawDescGZIP(), []int{1} -} - -func (x *Tx) GetTx() []byte { - if x != nil { - return x.Tx - } - return nil -} - -var File_message_tx_proto protoreflect.FileDescriptor - -var file_message_tx_proto_rawDesc = []byte{ - 0x0a, 0x10, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2f, 0x74, 0x78, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x12, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x33, 0x0a, 0x07, 0x4d, - 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x1d, 0x0a, 0x02, 0x74, 0x78, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2e, 0x54, 0x78, 0x48, - 0x00, 0x52, 0x02, 0x74, 0x78, 0x42, 0x09, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, - 0x22, 0x14, 0x0a, 0x02, 0x54, 0x78, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x78, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x0c, 0x52, 0x02, 0x74, 0x78, 0x42, 0x32, 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, - 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x76, 0x61, 0x2d, 0x6c, 0x61, 0x62, 0x73, 0x2f, 0x61, 0x76, - 0x61, 0x6c, 0x61, 0x6e, 0x63, 0x68, 0x65, 0x67, 0x6f, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, - 0x70, 0x62, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, -} - -var ( - file_message_tx_proto_rawDescOnce sync.Once - file_message_tx_proto_rawDescData = file_message_tx_proto_rawDesc -) - -func file_message_tx_proto_rawDescGZIP() []byte { - file_message_tx_proto_rawDescOnce.Do(func() { - file_message_tx_proto_rawDescData = protoimpl.X.CompressGZIP(file_message_tx_proto_rawDescData) - }) - return file_message_tx_proto_rawDescData -} - -var file_message_tx_proto_msgTypes = make([]protoimpl.MessageInfo, 2) -var file_message_tx_proto_goTypes = []interface{}{ - (*Message)(nil), // 0: message.Message - (*Tx)(nil), // 1: message.Tx -} -var file_message_tx_proto_depIdxs = []int32{ - 1, // 0: message.Message.tx:type_name -> message.Tx - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name -} - -func init() { file_message_tx_proto_init() } -func file_message_tx_proto_init() { - if File_message_tx_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_message_tx_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Message); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_message_tx_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Tx); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } - file_message_tx_proto_msgTypes[0].OneofWrappers = []interface{}{ - (*Message_Tx)(nil), - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_message_tx_proto_rawDesc, - NumEnums: 0, - NumMessages: 2, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_message_tx_proto_goTypes, - DependencyIndexes: file_message_tx_proto_depIdxs, - MessageInfos: file_message_tx_proto_msgTypes, - }.Build() - File_message_tx_proto = out.File - file_message_tx_proto_rawDesc = nil - file_message_tx_proto_goTypes = nil - file_message_tx_proto_depIdxs = nil -} diff --git a/vms/avm/network/network.go b/vms/avm/network/network.go index 049a9a3e08c..3e4dd9af459 100644 --- a/vms/avm/network/network.go +++ b/vms/avm/network/network.go @@ -8,7 +8,6 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" - "go.uber.org/zap" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/network/p2p" @@ -18,7 +17,6 @@ import ( "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/vms/avm/txs" "github.com/ava-labs/avalanchego/vms/avm/txs/mempool" - "github.com/ava-labs/avalanchego/vms/components/message" ) const txGossipHandlerID = 0 @@ -182,48 +180,6 @@ func (n *Network) PullGossip(ctx context.Context) { gossip.Every(ctx, n.log, n.txPullGossiper, n.txPullGossipFrequency) } -func (n *Network) AppGossip(ctx context.Context, nodeID ids.NodeID, msgBytes []byte) error { - n.log.Debug("called AppGossip message handler", - zap.Stringer("nodeID", nodeID), - zap.Int("messageLen", len(msgBytes)), - ) - - msgIntf, err := message.Parse(msgBytes) - if err != nil { - n.log.Debug("forwarding AppGossip message to SDK network", - zap.String("reason", "failed to parse message"), - ) - - return n.Network.AppGossip(ctx, nodeID, msgBytes) - } - - msg, ok := msgIntf.(*message.Tx) - if !ok { - n.log.Debug("dropping unexpected message", - zap.Stringer("nodeID", nodeID), - ) - return nil - } - - tx, err := n.parser.ParseTx(msg.Tx) - if err != nil { - n.log.Verbo("received invalid tx", - zap.Stringer("nodeID", nodeID), - zap.Binary("tx", msg.Tx), - zap.Error(err), - ) - return nil - } - - if err := n.mempool.Add(tx); err != nil { - n.log.Debug("tx failed to be added to the mempool", - zap.Stringer("txID", tx.ID()), - zap.Error(err), - ) - } - return nil -} - // IssueTxFromRPC attempts to add a tx to the mempool, after verifying it. If // the tx is added to the mempool, it will attempt to push gossip the tx to // random peers in the network. diff --git a/vms/avm/network/network_test.go b/vms/avm/network/network_test.go index 43d4541c765..f6ce4d9b2ba 100644 --- a/vms/avm/network/network_test.go +++ b/vms/avm/network/network_test.go @@ -21,8 +21,6 @@ import ( "github.com/ava-labs/avalanchego/vms/avm/fxs" "github.com/ava-labs/avalanchego/vms/avm/txs" "github.com/ava-labs/avalanchego/vms/avm/txs/mempool" - "github.com/ava-labs/avalanchego/vms/components/avax" - "github.com/ava-labs/avalanchego/vms/components/message" "github.com/ava-labs/avalanchego/vms/nftfx" "github.com/ava-labs/avalanchego/vms/propertyfx" "github.com/ava-labs/avalanchego/vms/secp256k1fx" @@ -51,183 +49,6 @@ var ( errTest = errors.New("test error") ) -func TestNetworkAppGossip(t *testing.T) { - testTx := &txs.Tx{ - Unsigned: &txs.BaseTx{ - BaseTx: avax.BaseTx{ - NetworkID: 1, - BlockchainID: ids.GenerateTestID(), - Ins: []*avax.TransferableInput{}, - Outs: []*avax.TransferableOutput{}, - }, - }, - } - - parser, err := txs.NewParser( - time.Time{}, - []fxs.Fx{ - &secp256k1fx.Fx{}, - }, - ) - require.NoError(t, err) - require.NoError(t, testTx.Initialize(parser.Codec())) - - type test struct { - name string - msgBytesFunc func() []byte - mempoolFunc func(*gomock.Controller) mempool.Mempool - txVerifierFunc func(*gomock.Controller) TxVerifier - } - - tests := []test{ - { - name: "invalid message bytes", - msgBytesFunc: func() []byte { - return []byte{0x00} - }, - }, - { - name: "invalid tx bytes", - msgBytesFunc: func() []byte { - msg := message.Tx{ - Tx: []byte{0x00}, - } - msgBytes, err := message.Build(&msg) - require.NoError(t, err) - return msgBytes - }, - }, - { - name: "tx already in mempool", - msgBytesFunc: func() []byte { - msg := message.Tx{ - Tx: testTx.Bytes(), - } - msgBytes, err := message.Build(&msg) - require.NoError(t, err) - return msgBytes - }, - mempoolFunc: func(ctrl *gomock.Controller) mempool.Mempool { - mempool := mempool.NewMockMempool(ctrl) - mempool.EXPECT().Get(gomock.Any()).Return(testTx, true) - return mempool - }, - }, - { - name: "tx previously dropped", - msgBytesFunc: func() []byte { - msg := message.Tx{ - Tx: testTx.Bytes(), - } - msgBytes, err := message.Build(&msg) - require.NoError(t, err) - return msgBytes - }, - mempoolFunc: func(ctrl *gomock.Controller) mempool.Mempool { - mempool := mempool.NewMockMempool(ctrl) - mempool.EXPECT().Get(gomock.Any()).Return(nil, false) - mempool.EXPECT().GetDropReason(gomock.Any()).Return(errTest) - return mempool - }, - }, - { - name: "transaction invalid", - msgBytesFunc: func() []byte { - msg := message.Tx{ - Tx: testTx.Bytes(), - } - msgBytes, err := message.Build(&msg) - require.NoError(t, err) - return msgBytes - }, - mempoolFunc: func(ctrl *gomock.Controller) mempool.Mempool { - mempool := mempool.NewMockMempool(ctrl) - mempool.EXPECT().Get(gomock.Any()).Return(nil, false) - mempool.EXPECT().GetDropReason(gomock.Any()).Return(nil) - mempool.EXPECT().MarkDropped(gomock.Any(), gomock.Any()) - return mempool - }, - txVerifierFunc: func(ctrl *gomock.Controller) TxVerifier { - txVerifier := executor.NewMockManager(ctrl) - txVerifier.EXPECT().VerifyTx(gomock.Any()).Return(errTest) - return txVerifier - }, - }, - { - name: "happy path", - msgBytesFunc: func() []byte { - msg := message.Tx{ - Tx: testTx.Bytes(), - } - msgBytes, err := message.Build(&msg) - require.NoError(t, err) - return msgBytes - }, - mempoolFunc: func(ctrl *gomock.Controller) mempool.Mempool { - mempool := mempool.NewMockMempool(ctrl) - mempool.EXPECT().Get(gomock.Any()).Return(nil, false) - mempool.EXPECT().GetDropReason(gomock.Any()).Return(nil) - mempool.EXPECT().Add(gomock.Any()).Return(nil) - mempool.EXPECT().Len().Return(0) - mempool.EXPECT().RequestBuildBlock() - return mempool - }, - txVerifierFunc: func(ctrl *gomock.Controller) TxVerifier { - txVerifier := executor.NewMockManager(ctrl) - txVerifier.EXPECT().VerifyTx(gomock.Any()).Return(nil) - return txVerifier - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - require := require.New(t) - ctrl := gomock.NewController(t) - - parser, err := txs.NewParser( - time.Time{}, - []fxs.Fx{ - &secp256k1fx.Fx{}, - &nftfx.Fx{}, - &propertyfx.Fx{}, - }, - ) - require.NoError(err) - - mempoolFunc := func(ctrl *gomock.Controller) mempool.Mempool { - return mempool.NewMockMempool(ctrl) - } - if tt.mempoolFunc != nil { - mempoolFunc = tt.mempoolFunc - } - - txVerifierFunc := func(ctrl *gomock.Controller) TxVerifier { - return executor.NewMockManager(ctrl) - } - if tt.txVerifierFunc != nil { - txVerifierFunc = tt.txVerifierFunc - } - - snowCtx := snowtest.Context(t, ids.Empty) - n, err := New( - logging.NoLog{}, - ids.EmptyNodeID, - ids.Empty, - snowCtx.ValidatorState, - parser, - txVerifierFunc(ctrl), - mempoolFunc(ctrl), - common.NewMockSender(ctrl), - prometheus.NewRegistry(), - testConfig, - ) - require.NoError(err) - require.NoError(n.AppGossip(context.Background(), ids.GenerateTestNodeID(), tt.msgBytesFunc())) - }) - } -} - func TestNetworkIssueTxFromRPC(t *testing.T) { type test struct { name string diff --git a/vms/components/message/codec.go b/vms/components/message/codec.go deleted file mode 100644 index 5614125b1ce..00000000000 --- a/vms/components/message/codec.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package message - -import ( - "time" - - "github.com/ava-labs/avalanchego/codec" - "github.com/ava-labs/avalanchego/codec/linearcodec" - "github.com/ava-labs/avalanchego/utils" - "github.com/ava-labs/avalanchego/utils/units" -) - -const ( - CodecVersion = 0 - - maxMessageSize = 512 * units.KiB -) - -var Codec codec.Manager - -func init() { - Codec = codec.NewManager(maxMessageSize) - lc := linearcodec.NewDefault(time.Time{}) - - err := utils.Err( - lc.RegisterType(&Tx{}), - Codec.RegisterCodec(CodecVersion, lc), - ) - if err != nil { - panic(err) - } -} diff --git a/vms/components/message/handler.go b/vms/components/message/handler.go deleted file mode 100644 index 2af2f55a3f0..00000000000 --- a/vms/components/message/handler.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package message - -import ( - "go.uber.org/zap" - - "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/avalanchego/utils/logging" -) - -var _ Handler = NoopHandler{} - -type Handler interface { - HandleTx(nodeID ids.NodeID, requestID uint32, msg *Tx) error -} - -type NoopHandler struct { - Log logging.Logger -} - -func (h NoopHandler) HandleTx(nodeID ids.NodeID, requestID uint32, _ *Tx) error { - h.Log.Debug("dropping unexpected Tx message", - zap.Stringer("nodeID", nodeID), - zap.Uint32("requestID", requestID), - ) - return nil -} diff --git a/vms/components/message/handler_test.go b/vms/components/message/handler_test.go deleted file mode 100644 index bc2342838ef..00000000000 --- a/vms/components/message/handler_test.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package message - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/avalanchego/utils/logging" -) - -type CounterHandler struct { - Tx int -} - -func (h *CounterHandler) HandleTx(ids.NodeID, uint32, *Tx) error { - h.Tx++ - return nil -} - -func TestHandleTx(t *testing.T) { - require := require.New(t) - - handler := CounterHandler{} - msg := Tx{} - - require.NoError(msg.Handle(&handler, ids.EmptyNodeID, 0)) - require.Equal(1, handler.Tx) -} - -func TestNoopHandler(t *testing.T) { - handler := NoopHandler{ - Log: logging.NoLog{}, - } - - require.NoError(t, handler.HandleTx(ids.EmptyNodeID, 0, nil)) -} diff --git a/vms/components/message/message.go b/vms/components/message/message.go deleted file mode 100644 index a33d4104430..00000000000 --- a/vms/components/message/message.go +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package message - -import ( - "errors" - "fmt" - - "google.golang.org/protobuf/proto" - - "github.com/ava-labs/avalanchego/ids" - - pb "github.com/ava-labs/avalanchego/proto/pb/message" -) - -var ( - _ Message = (*Tx)(nil) - - ErrUnexpectedCodecVersion = errors.New("unexpected codec version") - errUnknownMessageType = errors.New("unknown message type") -) - -type Message interface { - // Handle this message with the correct message handler - Handle(handler Handler, nodeID ids.NodeID, requestID uint32) error - - // initialize should be called whenever a message is built or parsed - initialize([]byte) - - // Bytes returns the binary representation of this message - // - // Bytes should only be called after being initialized - Bytes() []byte -} - -type message []byte - -func (m *message) initialize(bytes []byte) { - *m = bytes -} - -func (m *message) Bytes() []byte { - return *m -} - -func Parse(bytes []byte) (Message, error) { - var ( - msg Message - protoMsg pb.Message - ) - - if err := proto.Unmarshal(bytes, &protoMsg); err == nil { - // This message was encoded with proto. - switch m := protoMsg.GetMessage().(type) { - case *pb.Message_Tx: - msg = &Tx{ - Tx: m.Tx.Tx, - } - default: - return nil, fmt.Errorf("%w: %T", errUnknownMessageType, protoMsg.GetMessage()) - } - } else { - // This message wasn't encoded with proto. - // It must have been encoded with avalanchego's codec. - // TODO remove else statement remove once all nodes support proto encoding. - // i.e. when all nodes are on v1.11.0 or later. - version, err := Codec.Unmarshal(bytes, &msg) - if err != nil { - return nil, err - } - if version != CodecVersion { - return nil, ErrUnexpectedCodecVersion - } - } - msg.initialize(bytes) - return msg, nil -} - -func Build(msg Message) ([]byte, error) { - bytes, err := Codec.Marshal(CodecVersion, &msg) - msg.initialize(bytes) - return bytes, err -} diff --git a/vms/components/message/message_test.go b/vms/components/message/message_test.go deleted file mode 100644 index 946241deca9..00000000000 --- a/vms/components/message/message_test.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package message - -import ( - "testing" - - "github.com/stretchr/testify/require" - "google.golang.org/protobuf/proto" - - "github.com/ava-labs/avalanchego/codec" - - pb "github.com/ava-labs/avalanchego/proto/pb/message" -) - -func TestParseGibberish(t *testing.T) { - randomBytes := []byte{0, 1, 2, 3, 4, 5} - _, err := Parse(randomBytes) - require.ErrorIs(t, err, codec.ErrUnknownVersion) -} - -func TestParseProto(t *testing.T) { - require := require.New(t) - - txBytes := []byte{'y', 'e', 'e', 't'} - protoMsg := pb.Message{ - Message: &pb.Message_Tx{ - Tx: &pb.Tx{ - Tx: txBytes, - }, - }, - } - msgBytes, err := proto.Marshal(&protoMsg) - require.NoError(err) - - parsedMsgIntf, err := Parse(msgBytes) - require.NoError(err) - - require.IsType(&Tx{}, parsedMsgIntf) - parsedMsg := parsedMsgIntf.(*Tx) - - require.Equal(txBytes, parsedMsg.Tx) - - // Parse invalid message - _, err = Parse([]byte{1, 3, 3, 7}) - // Can't parse as proto so it falls back to using avalanchego's codec - require.ErrorIs(err, codec.ErrUnknownVersion) -} diff --git a/vms/components/message/tx.go b/vms/components/message/tx.go deleted file mode 100644 index 4eced181823..00000000000 --- a/vms/components/message/tx.go +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package message - -import "github.com/ava-labs/avalanchego/ids" - -var _ Message = (*Tx)(nil) - -type Tx struct { - message - - Tx []byte `serialize:"true"` -} - -func (msg *Tx) Handle(handler Handler, nodeID ids.NodeID, requestID uint32) error { - return handler.HandleTx(nodeID, requestID, msg) -} diff --git a/vms/components/message/tx_test.go b/vms/components/message/tx_test.go deleted file mode 100644 index 8c52828b797..00000000000 --- a/vms/components/message/tx_test.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package message - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/ava-labs/avalanchego/utils" - "github.com/ava-labs/avalanchego/utils/units" -) - -func TestTx(t *testing.T) { - require := require.New(t) - - tx := utils.RandomBytes(256 * units.KiB) - builtMsg := Tx{ - Tx: tx, - } - builtMsgBytes, err := Build(&builtMsg) - require.NoError(err) - require.Equal(builtMsgBytes, builtMsg.Bytes()) - - parsedMsgIntf, err := Parse(builtMsgBytes) - require.NoError(err) - require.Equal(builtMsgBytes, parsedMsgIntf.Bytes()) - - require.IsType(&Tx{}, parsedMsgIntf) - parsedMsg := parsedMsgIntf.(*Tx) - - require.Equal(tx, parsedMsg.Tx) -} diff --git a/vms/platformvm/network/network.go b/vms/platformvm/network/network.go index 0c78f9a8408..613c5554a08 100644 --- a/vms/platformvm/network/network.go +++ b/vms/platformvm/network/network.go @@ -17,7 +17,6 @@ import ( "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/snow/validators" "github.com/ava-labs/avalanchego/utils/logging" - "github.com/ava-labs/avalanchego/vms/components/message" "github.com/ava-labs/avalanchego/vms/platformvm/txs" "github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool" ) @@ -192,11 +191,6 @@ func (n *Network) PullGossip(ctx context.Context) { } func (n *Network) AppGossip(ctx context.Context, nodeID ids.NodeID, msgBytes []byte) error { - n.log.Debug("called AppGossip message handler", - zap.Stringer("nodeID", nodeID), - zap.Int("messageLen", len(msgBytes)), - ) - if n.partialSyncPrimaryNetwork { n.log.Debug("dropping AppGossip message", zap.String("reason", "primary network is not being fully synced"), @@ -204,63 +198,22 @@ func (n *Network) AppGossip(ctx context.Context, nodeID ids.NodeID, msgBytes []b return nil } - msgIntf, err := message.Parse(msgBytes) - if err != nil { - n.log.Debug("forwarding AppGossip to p2p network", - zap.String("reason", "failed to parse message"), - ) - - return n.Network.AppGossip(ctx, nodeID, msgBytes) - } - - msg, ok := msgIntf.(*message.Tx) - if !ok { - n.log.Debug("dropping unexpected message", - zap.Stringer("nodeID", nodeID), - ) - return nil - } - - tx, err := txs.Parse(txs.Codec, msg.Tx) - if err != nil { - n.log.Verbo("received invalid tx", - zap.Stringer("nodeID", nodeID), - zap.Binary("tx", msg.Tx), - zap.Error(err), - ) - return nil - } - - // Returning an error here would result in shutting down the chain. Logging - // is already included inside addTxToMempool, so there's nothing to do with - // the returned error here. - _ = n.addTxToMempool(tx) - return nil + return n.Network.AppGossip(ctx, nodeID, msgBytes) } func (n *Network) IssueTxFromRPC(tx *txs.Tx) error { - // TODO: We should still push the transaction to some peers when partial - // syncing. - if err := n.addTxToMempool(tx); err != nil { - return err - } - n.txPushGossiper.Add(tx) - return nil -} - -func (n *Network) addTxToMempool(tx *txs.Tx) error { // If we are partially syncing the Primary Network, we should not be // maintaining the transaction mempool locally. + // + // TODO: We should still push the transaction to some peers when partial + // syncing. if n.partialSyncPrimaryNetwork { return errMempoolDisabledWithPartialSync } - err := n.mempool.Add(tx) - if err != nil { - n.log.Debug("tx failed to be added to the mempool", - zap.Stringer("txID", tx.ID()), - zap.Error(err), - ) + if err := n.mempool.Add(tx); err != nil { + return err } - return err + n.txPushGossiper.Add(tx) + return nil } diff --git a/vms/platformvm/network/network_test.go b/vms/platformvm/network/network_test.go index 822a4b47878..758cb894b4c 100644 --- a/vms/platformvm/network/network_test.go +++ b/vms/platformvm/network/network_test.go @@ -16,9 +16,6 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/snow/snowtest" - "github.com/ava-labs/avalanchego/utils/logging" - "github.com/ava-labs/avalanchego/vms/components/avax" - "github.com/ava-labs/avalanchego/vms/components/message" "github.com/ava-labs/avalanchego/vms/platformvm/txs" "github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool" ) @@ -56,133 +53,6 @@ func (t testTxVerifier) VerifyTx(*txs.Tx) error { return t.err } -func TestNetworkAppGossip(t *testing.T) { - testTx := &txs.Tx{ - Unsigned: &txs.BaseTx{ - BaseTx: avax.BaseTx{ - NetworkID: 1, - BlockchainID: ids.GenerateTestID(), - Ins: []*avax.TransferableInput{}, - Outs: []*avax.TransferableOutput{}, - }, - }, - } - require.NoError(t, testTx.Initialize(txs.Codec)) - - type test struct { - name string - msgBytesFunc func() []byte - mempoolFunc func(*gomock.Controller) mempool.Mempool - partialSyncPrimaryNetwork bool - } - - tests := []test{ - { - // Shouldn't attempt to issue or gossip the tx - name: "invalid message bytes", - msgBytesFunc: func() []byte { - return []byte{0x00} - }, - mempoolFunc: func(*gomock.Controller) mempool.Mempool { - return nil - }, - }, - { - // Shouldn't attempt to issue or gossip the tx - name: "invalid tx bytes", - msgBytesFunc: func() []byte { - msg := message.Tx{ - Tx: []byte{0x00}, - } - msgBytes, err := message.Build(&msg) - require.NoError(t, err) - return msgBytes - }, - mempoolFunc: func(ctrl *gomock.Controller) mempool.Mempool { - return mempool.NewMockMempool(ctrl) - }, - }, - { - name: "issuance succeeds", - msgBytesFunc: func() []byte { - msg := message.Tx{ - Tx: testTx.Bytes(), - } - msgBytes, err := message.Build(&msg) - require.NoError(t, err) - return msgBytes - }, - mempoolFunc: func(ctrl *gomock.Controller) mempool.Mempool { - mempool := mempool.NewMockMempool(ctrl) - mempool.EXPECT().Get(gomock.Any()).Return(nil, false) - mempool.EXPECT().GetDropReason(gomock.Any()).Return(nil) - mempool.EXPECT().Add(gomock.Any()).Return(nil) - mempool.EXPECT().Len().Return(0) - mempool.EXPECT().RequestBuildBlock(false) - return mempool - }, - }, - { - // Issue returns error because tx was dropped. We shouldn't gossip the tx. - name: "issuance fails", - msgBytesFunc: func() []byte { - msg := message.Tx{ - Tx: testTx.Bytes(), - } - msgBytes, err := message.Build(&msg) - require.NoError(t, err) - return msgBytes - }, - mempoolFunc: func(ctrl *gomock.Controller) mempool.Mempool { - mempool := mempool.NewMockMempool(ctrl) - mempool.EXPECT().Get(gomock.Any()).Return(nil, false) - mempool.EXPECT().GetDropReason(gomock.Any()).Return(errTest) - return mempool - }, - }, - { - name: "shouldn't AppGossip if primary network is not being fully synced", - msgBytesFunc: func() []byte { - msg := message.Tx{ - Tx: testTx.Bytes(), - } - msgBytes, err := message.Build(&msg) - require.NoError(t, err) - return msgBytes - }, - mempoolFunc: func(ctrl *gomock.Controller) mempool.Mempool { - return mempool.NewMockMempool(ctrl) - }, - partialSyncPrimaryNetwork: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - require := require.New(t) - ctx := context.Background() - ctrl := gomock.NewController(t) - - snowCtx := snowtest.Context(t, ids.Empty) - n, err := New( - logging.NoLog{}, - ids.EmptyNodeID, - ids.Empty, - snowCtx.ValidatorState, - testTxVerifier{}, - tt.mempoolFunc(ctrl), - tt.partialSyncPrimaryNetwork, - common.NewMockSender(ctrl), - prometheus.NewRegistry(), - DefaultConfig, - ) - require.NoError(err) - - require.NoError(n.AppGossip(ctx, ids.GenerateTestNodeID(), tt.msgBytesFunc())) - }) - } -} - func TestNetworkIssueTxFromRPC(t *testing.T) { tx := &txs.Tx{}