From 12408bad076925f3816112404929e1e914c2d93f Mon Sep 17 00:00:00 2001 From: Wolfgang Welz <welzwo@gmail.com> Date: Sat, 4 Jan 2020 13:49:04 +0100 Subject: [PATCH] fix: remove useless proto calls in gossip --- packages/gossip/events.go | 26 +++++------ packages/gossip/manager.go | 7 +-- packages/gossip/manager_test.go | 10 ++--- packages/gossip/proto/message.pb.go | 43 ++++++++++--------- packages/gossip/proto/message.proto | 4 +- .../transactionspammer/transactionspammer.go | 6 +-- plugins/gossip/gossip.go | 10 +---- plugins/gossip/plugin.go | 20 +++------ plugins/tangle/solidifier.go | 18 ++------ plugins/tangle/solidifier_test.go | 26 +++-------- plugins/webapi-send-data/plugin.go | 11 +---- 11 files changed, 68 insertions(+), 113 deletions(-) diff --git a/packages/gossip/events.go b/packages/gossip/events.go index 7792214b..5483e3f1 100644 --- a/packages/gossip/events.go +++ b/packages/gossip/events.go @@ -3,32 +3,38 @@ package gossip import ( "github.com/iotaledger/autopeering-sim/peer" "github.com/iotaledger/hive.go/events" + "github.com/iotaledger/iota.go/trinary" ) // Events contains all the events related to the gossip protocol. var Events = struct { - // A TransactionReceived event is triggered when a new transaction is received by the gossip protocol. - TransactionReceived *events.Event // A NeighborDropped event is triggered when a neighbor has been dropped. NeighborDropped *events.Event + // A TransactionReceived event is triggered when a new transaction is received by the gossip protocol. + TransactionReceived *events.Event // A RequestTransaction should be triggered for a transaction to be requested through the gossip protocol. RequestTransaction *events.Event }{ - TransactionReceived: events.NewEvent(transactionReceived), NeighborDropped: events.NewEvent(neighborDropped), + TransactionReceived: events.NewEvent(transactionReceived), RequestTransaction: events.NewEvent(requestTransaction), } -type TransactionReceivedEvent struct { - Body []byte +type NeighborDroppedEvent struct { Peer *peer.Peer } +type TransactionReceivedEvent struct { + Data []byte // transaction data + Peer *peer.Peer // peer that send the transaction +} + type RequestTransactionEvent struct { - Hash []byte // hash of the transaction to request + Hash trinary.Trytes // hash of the transaction to request } -type NeighborDroppedEvent struct { - Peer *peer.Peer + +func neighborDropped(handler interface{}, params ...interface{}) { + handler.(func(*NeighborDroppedEvent))(params[0].(*NeighborDroppedEvent)) } func transactionReceived(handler interface{}, params ...interface{}) { @@ -38,7 +44,3 @@ func transactionReceived(handler interface{}, params ...interface{}) { func requestTransaction(handler interface{}, params ...interface{}) { handler.(func(*RequestTransactionEvent))(params[0].(*RequestTransactionEvent)) } - -func neighborDropped(handler interface{}, params ...interface{}) { - handler.(func(*NeighborDroppedEvent))(params[0].(*NeighborDroppedEvent)) -} diff --git a/packages/gossip/manager.go b/packages/gossip/manager.go index 897f1c50..fb1073bf 100644 --- a/packages/gossip/manager.go +++ b/packages/gossip/manager.go @@ -20,6 +20,7 @@ const ( maxPacketSize = 2048 ) +// GetTransaction defines a function that returns the transaction data with the given hash. type GetTransaction func(txHash []byte) ([]byte, error) type Manager struct { @@ -130,7 +131,7 @@ func (m *Manager) RequestTransaction(txHash []byte, to ...peer.ID) { // If no peer is provided, it is send to all neighbors. func (m *Manager) SendTransaction(txData []byte, to ...peer.ID) { tx := &pb.Transaction{ - Body: txData, + Data: txData, } m.send(marshal(tx), to...) } @@ -255,8 +256,8 @@ func (m *Manager) handlePacket(data []byte, n *neighbor) error { if err := proto.Unmarshal(data[1:], msg); err != nil { return errors.Wrap(err, "invalid message") } - m.log.Debugw("Received Transaction", "data", msg.GetBody()) - Events.TransactionReceived.Trigger(&TransactionReceivedEvent{Body: msg.GetBody(), Peer: n.peer}) + m.log.Debugw("Received Transaction", "data", msg.GetData()) + Events.TransactionReceived.Trigger(&TransactionReceivedEvent{Data: msg.GetData(), Peer: n.peer}) // Incoming Transaction request case pb.MTransactionRequest: diff --git a/packages/gossip/manager_test.go b/packages/gossip/manager_test.go index 789b8699..6d0fd452 100644 --- a/packages/gossip/manager_test.go +++ b/packages/gossip/manager_test.go @@ -173,7 +173,7 @@ func TestP2PSend(t *testing.T) { wg.Wait() eventMock.On("transactionReceivedEvent", &TransactionReceivedEvent{ - Body: testTxData, + Data: testTxData, Peer: peerA, }).Once() eventMock.On("neighborDroppedEvent", &NeighborDroppedEvent{Peer: peerA}).Once() @@ -212,7 +212,7 @@ func TestP2PSendTwice(t *testing.T) { wg.Wait() eventMock.On("transactionReceivedEvent", &TransactionReceivedEvent{ - Body: testTxData, + Data: testTxData, Peer: peerA, }).Twice() eventMock.On("neighborDroppedEvent", &NeighborDroppedEvent{Peer: peerA}).Once() @@ -265,7 +265,7 @@ func TestBroadcast(t *testing.T) { wg.Wait() eventMock.On("transactionReceivedEvent", &TransactionReceivedEvent{ - Body: testTxData, + Data: testTxData, Peer: peerA, }).Twice() eventMock.On("neighborDroppedEvent", &NeighborDroppedEvent{Peer: peerA}).Twice() @@ -317,7 +317,7 @@ func TestSingleSend(t *testing.T) { wg.Wait() eventMock.On("transactionReceivedEvent", &TransactionReceivedEvent{ - Body: testTxData, + Data: testTxData, Peer: peerA, }).Once() eventMock.On("neighborDroppedEvent", &NeighborDroppedEvent{Peer: peerA}).Twice() @@ -376,7 +376,7 @@ func TestTxRequest(t *testing.T) { txHash := []byte("Hello!") eventMock.On("transactionReceivedEvent", &TransactionReceivedEvent{ - Body: testTxData, + Data: testTxData, Peer: peerB, }).Once() eventMock.On("neighborDroppedEvent", &NeighborDroppedEvent{Peer: peerA}).Once() diff --git a/packages/gossip/proto/message.pb.go b/packages/gossip/proto/message.pb.go index b18bb751..06ab295e 100644 --- a/packages/gossip/proto/message.pb.go +++ b/packages/gossip/proto/message.pb.go @@ -1,5 +1,5 @@ // Code generated by protoc-gen-go. DO NOT EDIT. -// source: proto/message.proto +// source: packages/gossip/proto/message.proto package proto @@ -21,8 +21,8 @@ var _ = math.Inf const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package type Transaction struct { - // body of the tx - Body []byte `protobuf:"bytes,1,opt,name=body,proto3" json:"body,omitempty"` + // transaction data + Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -32,7 +32,7 @@ func (m *Transaction) Reset() { *m = Transaction{} } func (m *Transaction) String() string { return proto.CompactTextString(m) } func (*Transaction) ProtoMessage() {} func (*Transaction) Descriptor() ([]byte, []int) { - return fileDescriptor_33f3a5e1293a7bcd, []int{0} + return fileDescriptor_fcce9e84825f2fa5, []int{0} } func (m *Transaction) XXX_Unmarshal(b []byte) error { @@ -53,9 +53,9 @@ func (m *Transaction) XXX_DiscardUnknown() { var xxx_messageInfo_Transaction proto.InternalMessageInfo -func (m *Transaction) GetBody() []byte { +func (m *Transaction) GetData() []byte { if m != nil { - return m.Body + return m.Data } return nil } @@ -72,7 +72,7 @@ func (m *TransactionRequest) Reset() { *m = TransactionRequest{} } func (m *TransactionRequest) String() string { return proto.CompactTextString(m) } func (*TransactionRequest) ProtoMessage() {} func (*TransactionRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_33f3a5e1293a7bcd, []int{1} + return fileDescriptor_fcce9e84825f2fa5, []int{1} } func (m *TransactionRequest) XXX_Unmarshal(b []byte) error { @@ -105,17 +105,20 @@ func init() { proto.RegisterType((*TransactionRequest)(nil), "proto.TransactionRequest") } -func init() { proto.RegisterFile("proto/message.proto", fileDescriptor_33f3a5e1293a7bcd) } - -var fileDescriptor_33f3a5e1293a7bcd = []byte{ - // 137 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0x28, 0xca, 0x2f, - 0xc9, 0xd7, 0xcf, 0x4d, 0x2d, 0x2e, 0x4e, 0x4c, 0x4f, 0xd5, 0x03, 0xf3, 0x84, 0x58, 0xc1, 0x94, - 0x92, 0x22, 0x17, 0x77, 0x48, 0x51, 0x62, 0x5e, 0x71, 0x62, 0x72, 0x49, 0x66, 0x7e, 0x9e, 0x90, - 0x10, 0x17, 0x4b, 0x52, 0x7e, 0x4a, 0xa5, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x4f, 0x10, 0x98, 0xad, - 0xa4, 0xc1, 0x25, 0x84, 0xa4, 0x24, 0x28, 0xb5, 0xb0, 0x34, 0xb5, 0xb8, 0x04, 0xa4, 0x32, 0x23, - 0xb1, 0x38, 0x03, 0xa6, 0x12, 0xc4, 0x76, 0x52, 0x8e, 0x52, 0x4c, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, - 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0x4e, 0x2c, 0xc8, 0x2f, 0x2e, 0x4e, 0xcd, 0x49, 0xd5, 0x4f, - 0x07, 0xd2, 0x99, 0x05, 0xfa, 0x60, 0x1b, 0x93, 0xd8, 0xc0, 0x94, 0x31, 0x20, 0x00, 0x00, 0xff, - 0xff, 0x34, 0x46, 0xa5, 0x0f, 0x96, 0x00, 0x00, 0x00, +func init() { + proto.RegisterFile("packages/gossip/proto/message.proto", fileDescriptor_fcce9e84825f2fa5) +} + +var fileDescriptor_fcce9e84825f2fa5 = []byte{ + // 155 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x2e, 0x48, 0x4c, 0xce, + 0x4e, 0x4c, 0x4f, 0x2d, 0xd6, 0x4f, 0xcf, 0x2f, 0x2e, 0xce, 0x2c, 0xd0, 0x2f, 0x28, 0xca, 0x2f, + 0xc9, 0xd7, 0xcf, 0x4d, 0x2d, 0x2e, 0x06, 0x8a, 0xea, 0x81, 0x79, 0x42, 0xac, 0x60, 0x4a, 0x49, + 0x91, 0x8b, 0x3b, 0xa4, 0x28, 0x31, 0xaf, 0x38, 0x31, 0xb9, 0x24, 0x33, 0x3f, 0x4f, 0x48, 0x88, + 0x8b, 0x25, 0x25, 0xb1, 0x24, 0x51, 0x82, 0x51, 0x81, 0x51, 0x83, 0x27, 0x08, 0xcc, 0x56, 0xd2, + 0xe0, 0x12, 0x42, 0x52, 0x12, 0x94, 0x5a, 0x58, 0x9a, 0x5a, 0x5c, 0x02, 0x52, 0x99, 0x91, 0x58, + 0x9c, 0x01, 0x53, 0x09, 0x62, 0x3b, 0x99, 0x47, 0x99, 0xa6, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, + 0x25, 0xe7, 0xe7, 0xea, 0x67, 0xe6, 0x97, 0x24, 0xe6, 0xa4, 0xa6, 0xa4, 0xa7, 0x16, 0x81, 0xdc, + 0x91, 0x91, 0x99, 0x9b, 0x0b, 0x64, 0x61, 0x75, 0x5a, 0x12, 0x1b, 0x98, 0x32, 0x06, 0x04, 0x00, + 0x00, 0xff, 0xff, 0x9f, 0x78, 0x4d, 0x0f, 0xba, 0x00, 0x00, 0x00, } diff --git a/packages/gossip/proto/message.proto b/packages/gossip/proto/message.proto index 6be94fdf..8d6bc285 100644 --- a/packages/gossip/proto/message.proto +++ b/packages/gossip/proto/message.proto @@ -5,8 +5,8 @@ option go_package = "github.com/iotaledger/goshimmer/packages/gossip/proto"; package proto; message Transaction { - // body of the tx - bytes body = 1; + // transaction data + bytes data = 1; } message TransactionRequest { diff --git a/packages/transactionspammer/transactionspammer.go b/packages/transactionspammer/transactionspammer.go index 75ccf787..f95573c7 100644 --- a/packages/transactionspammer/transactionspammer.go +++ b/packages/transactionspammer/transactionspammer.go @@ -4,9 +4,7 @@ import ( "sync" "time" - "github.com/golang/protobuf/proto" "github.com/iotaledger/goshimmer/packages/gossip" - pb "github.com/iotaledger/goshimmer/packages/gossip/proto" "github.com/iotaledger/goshimmer/packages/model/meta_transaction" "github.com/iotaledger/goshimmer/packages/model/value_transaction" "github.com/iotaledger/goshimmer/plugins/autopeering/local" @@ -64,9 +62,7 @@ func Start(tps uint) { continue } - mtx := &pb.Transaction{Body: tx.MetaTransaction.GetBytes()} - b, _ := proto.Marshal(mtx) - gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Body: b, Peer: &local.INSTANCE.Peer}) + gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: tx.GetBytes(), Peer: &local.INSTANCE.Peer}) if sentCounter >= tps { duration := time.Since(start) diff --git a/plugins/gossip/gossip.go b/plugins/gossip/gossip.go index 3ce2dc71..834a4edf 100644 --- a/plugins/gossip/gossip.go +++ b/plugins/gossip/gossip.go @@ -3,11 +3,9 @@ package gossip import ( "fmt" - "github.com/golang/protobuf/proto" "github.com/iotaledger/autopeering-sim/logger" "github.com/iotaledger/goshimmer/packages/errors" gp "github.com/iotaledger/goshimmer/packages/gossip" - pb "github.com/iotaledger/goshimmer/packages/gossip/proto" "github.com/iotaledger/goshimmer/packages/gossip/server" "github.com/iotaledger/goshimmer/packages/typeutils" "github.com/iotaledger/goshimmer/plugins/autopeering/local" @@ -74,11 +72,5 @@ func getTransaction(hash []byte) ([]byte, error) { if tx == nil { return nil, fmt.Errorf("transaction not found: hash=%s", hash) } - - pTx := &pb.TransactionRequest{ - Hash: tx.GetBytes(), - } - b, _ := proto.Marshal(pTx) - - return b, nil + return tx.GetBytes(), nil } diff --git a/plugins/gossip/plugin.go b/plugins/gossip/plugin.go index 963a830e..39fd6f19 100644 --- a/plugins/gossip/plugin.go +++ b/plugins/gossip/plugin.go @@ -1,12 +1,11 @@ package gossip import ( - "github.com/golang/protobuf/proto" "github.com/iotaledger/autopeering-sim/peer/service" "github.com/iotaledger/autopeering-sim/selection" "github.com/iotaledger/goshimmer/packages/gossip" - pb "github.com/iotaledger/goshimmer/packages/gossip/proto" "github.com/iotaledger/goshimmer/packages/model/value_transaction" + "github.com/iotaledger/goshimmer/packages/typeutils" "github.com/iotaledger/goshimmer/plugins/tangle" "github.com/iotaledger/hive.go/daemon" "github.com/iotaledger/hive.go/events" @@ -55,21 +54,12 @@ func configureEvents() { })) tangle.Events.TransactionSolid.Attach(events.NewClosure(func(tx *value_transaction.ValueTransaction) { - log.Info("gossip solid tx", tx.MetaTransaction.GetHash()) - t := &pb.Transaction{ - Body: tx.MetaTransaction.GetBytes(), - } - b, err := proto.Marshal(t) - if err != nil { - return - } - go mgr.SendTransaction(b) + log.Debugf("gossip solid tx: hash=%s", tx.GetHash()) + go mgr.SendTransaction(tx.GetBytes()) })) gossip.Events.RequestTransaction.Attach(events.NewClosure(func(ev *gossip.RequestTransactionEvent) { - pTx := &pb.TransactionRequest{} - proto.Unmarshal(ev.Hash, pTx) - log.Info("Tx Requested:", string(pTx.Hash)) - go mgr.RequestTransaction(pTx.Hash) + log.Debugf("gossip tx request: hash=%s", ev.Hash) + go mgr.RequestTransaction(typeutils.StringToBytes(ev.Hash)) })) } diff --git a/plugins/tangle/solidifier.go b/plugins/tangle/solidifier.go index 130deda4..dddd71e9 100644 --- a/plugins/tangle/solidifier.go +++ b/plugins/tangle/solidifier.go @@ -4,10 +4,8 @@ import ( "runtime" "time" - "github.com/golang/protobuf/proto" "github.com/iotaledger/goshimmer/packages/errors" "github.com/iotaledger/goshimmer/packages/gossip" - pb "github.com/iotaledger/goshimmer/packages/gossip/proto" "github.com/iotaledger/goshimmer/packages/model/approvers" "github.com/iotaledger/goshimmer/packages/model/meta_transaction" "github.com/iotaledger/goshimmer/packages/model/transactionmetadata" @@ -38,13 +36,7 @@ func configureSolidifier(plugin *node.Plugin) { unsolidTxs = NewUnsolidTxs() gossip.Events.TransactionReceived.Attach(events.NewClosure(func(ev *gossip.TransactionReceivedEvent) { - pTx := &pb.Transaction{} - if err := proto.Unmarshal(ev.Body, pTx); err != nil { - log.Warningf("invalid transaction: %s", err) - return - } - - metaTx := meta_transaction.FromBytes(pTx.GetBody()) + metaTx := meta_transaction.FromBytes(ev.Data) if err := metaTx.Validate(); err != nil { log.Warningf("invalid transaction: %s", err) return @@ -231,11 +223,9 @@ func updateUnsolidTxs(tx *value_transaction.ValueTransaction) { } } -func requestTransaction(tx string) { - log.Info("Requesting tx: ", tx) - req := &pb.TransactionRequest{Hash: []byte(tx)} - b, _ := proto.Marshal(req) - gossip.Events.RequestTransaction.Trigger(&gossip.RequestTransactionEvent{Hash: b}) +func requestTransaction(hash trinary.Trytes) { + log.Infof("Requesting hash: hash=%s", hash) + gossip.Events.RequestTransaction.Trigger(&gossip.RequestTransactionEvent{Hash: hash}) } var WORKER_COUNT = runtime.NumCPU() diff --git a/plugins/tangle/solidifier_test.go b/plugins/tangle/solidifier_test.go index be845077..7b8aaf1d 100644 --- a/plugins/tangle/solidifier_test.go +++ b/plugins/tangle/solidifier_test.go @@ -5,9 +5,7 @@ import ( "sync" "testing" - "github.com/golang/protobuf/proto" "github.com/iotaledger/goshimmer/packages/gossip" - pb "github.com/iotaledger/goshimmer/packages/gossip/proto" "github.com/iotaledger/goshimmer/packages/model/meta_transaction" "github.com/iotaledger/goshimmer/packages/model/value_transaction" "github.com/iotaledger/hive.go/events" @@ -56,28 +54,18 @@ func TestSolidifier(t *testing.T) { })) gossip.Events.RequestTransaction.Attach(events.NewClosure(func(ev *gossip.RequestTransactionEvent) { - tx := &pb.Transaction{Body: transaction3.MetaTransaction.GetBytes()} - b, _ := proto.Marshal(tx) - gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Body: b}) + require.Equal(t, transaction3.GetHash(), ev.Hash) + // return the transaction data + gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: transaction3.GetBytes()}) })) // issue transactions wg.Add(4) - tx := &pb.Transaction{Body: transaction1.MetaTransaction.GetBytes()} - b, _ := proto.Marshal(tx) - gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Body: b}) - tx = &pb.Transaction{Body: transaction2.MetaTransaction.GetBytes()} - b, _ = proto.Marshal(tx) - gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Body: b}) - - // tx = &pb.Transaction{Body: transaction3.MetaTransaction.GetBytes()} - // b, _ = proto.Marshal(tx) - // gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Body: b}) - - tx = &pb.Transaction{Body: transaction4.MetaTransaction.GetBytes()} - b, _ = proto.Marshal(tx) - gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Body: b}) + gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: transaction1.GetBytes()}) + gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: transaction2.GetBytes()}) + // gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: transaction3.GetBytes()}) + gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: transaction4.GetBytes()}) // wait until all are solid wg.Wait() diff --git a/plugins/webapi-send-data/plugin.go b/plugins/webapi-send-data/plugin.go index 8fdf3746..b36da536 100644 --- a/plugins/webapi-send-data/plugin.go +++ b/plugins/webapi-send-data/plugin.go @@ -4,9 +4,7 @@ import ( "net/http" "time" - "github.com/golang/protobuf/proto" "github.com/iotaledger/goshimmer/packages/gossip" - pb "github.com/iotaledger/goshimmer/packages/gossip/proto" "github.com/iotaledger/goshimmer/packages/model/meta_transaction" "github.com/iotaledger/goshimmer/packages/model/value_transaction" "github.com/iotaledger/goshimmer/plugins/autopeering/local" @@ -59,13 +57,8 @@ func SendDataHandler(c echo.Context) error { log.Warning("PoW failed", err) } - transactionHash := tx.GetHash() - - mtx := &pb.Transaction{Body: tx.MetaTransaction.GetBytes()} - b, _ := proto.Marshal(mtx) - gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Body: b, Peer: &local.INSTANCE.Peer}) - - return requestSuccessful(c, transactionHash) + gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: tx.GetBytes(), Peer: &local.INSTANCE.Peer}) + return requestSuccessful(c, tx.GetHash()) } func requestSuccessful(c echo.Context, txHash string) error { -- GitLab