Skip to content
Snippets Groups Projects
Commit 12408bad authored by Wolfgang Welz's avatar Wolfgang Welz
Browse files

fix: remove useless proto calls in gossip

parent b16c5630
No related branches found
No related tags found
No related merge requests found
......@@ -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))
}
......@@ -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:
......
......@@ -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()
......
// 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,
}
......@@ -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 {
......
......@@ -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)
......
......@@ -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
}
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))
}))
}
......@@ -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()
......@@ -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()
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment