From 1010156065a4ede2cd2e32bb0c91acfb3031f2a1 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 09:06:17 +0700 Subject: [PATCH] encode txs --- gointerfaces/sentry/mocks.go | 204 ++++++++++++++++++++++++----------- gointerfaces/test_util.go | 2 +- txpool/fetch_test.go | 53 ++++++--- txpool/mocks.go | 15 +-- txpool/pool.go | 10 +- txpool/send.go | 12 +-- txpool/test_util.go | 30 +----- 7 files changed, 204 insertions(+), 122 deletions(-) diff --git a/gointerfaces/sentry/mocks.go b/gointerfaces/sentry/mocks.go index c67921e49..9edb63f53 100644 --- a/gointerfaces/sentry/mocks.go +++ b/gointerfaces/sentry/mocks.go @@ -184,9 +184,6 @@ type SentryServerMock struct { // Messages calls MessagesFunc. func (mock *SentryServerMock) Messages(messagesRequest *MessagesRequest, sentry_MessagesServer Sentry_MessagesServer) error { - if mock.MessagesFunc == nil { - panic("SentryServerMock.MessagesFunc: method is nil but SentryServer.Messages was just called") - } callInfo := struct { MessagesRequest *MessagesRequest Sentry_MessagesServer Sentry_MessagesServer @@ -197,6 +194,12 @@ func (mock *SentryServerMock) Messages(messagesRequest *MessagesRequest, sentry_ mock.lockMessages.Lock() mock.calls.Messages = append(mock.calls.Messages, callInfo) mock.lockMessages.Unlock() + if mock.MessagesFunc == nil { + var ( + errOut error + ) + return errOut + } return mock.MessagesFunc(messagesRequest, sentry_MessagesServer) } @@ -219,9 +222,6 @@ func (mock *SentryServerMock) MessagesCalls() []struct { // PeerCount calls PeerCountFunc. func (mock *SentryServerMock) PeerCount(contextMoqParam context.Context, peerCountRequest *PeerCountRequest) (*PeerCountReply, error) { - if mock.PeerCountFunc == nil { - panic("SentryServerMock.PeerCountFunc: method is nil but SentryServer.PeerCount was just called") - } callInfo := struct { ContextMoqParam context.Context PeerCountRequest *PeerCountRequest @@ -232,6 +232,13 @@ func (mock *SentryServerMock) PeerCount(contextMoqParam context.Context, peerCou mock.lockPeerCount.Lock() mock.calls.PeerCount = append(mock.calls.PeerCount, callInfo) mock.lockPeerCount.Unlock() + if mock.PeerCountFunc == nil { + var ( + peerCountReplyOut *PeerCountReply + errOut error + ) + return peerCountReplyOut, errOut + } return mock.PeerCountFunc(contextMoqParam, peerCountRequest) } @@ -254,9 +261,6 @@ func (mock *SentryServerMock) PeerCountCalls() []struct { // PeerMinBlock calls PeerMinBlockFunc. func (mock *SentryServerMock) PeerMinBlock(contextMoqParam context.Context, peerMinBlockRequest *PeerMinBlockRequest) (*emptypb.Empty, error) { - if mock.PeerMinBlockFunc == nil { - panic("SentryServerMock.PeerMinBlockFunc: method is nil but SentryServer.PeerMinBlock was just called") - } callInfo := struct { ContextMoqParam context.Context PeerMinBlockRequest *PeerMinBlockRequest @@ -267,6 +271,13 @@ func (mock *SentryServerMock) PeerMinBlock(contextMoqParam context.Context, peer mock.lockPeerMinBlock.Lock() mock.calls.PeerMinBlock = append(mock.calls.PeerMinBlock, callInfo) mock.lockPeerMinBlock.Unlock() + if mock.PeerMinBlockFunc == nil { + var ( + emptyOut *emptypb.Empty + errOut error + ) + return emptyOut, errOut + } return mock.PeerMinBlockFunc(contextMoqParam, peerMinBlockRequest) } @@ -289,9 +300,6 @@ func (mock *SentryServerMock) PeerMinBlockCalls() []struct { // Peers calls PeersFunc. func (mock *SentryServerMock) Peers(peersRequest *PeersRequest, sentry_PeersServer Sentry_PeersServer) error { - if mock.PeersFunc == nil { - panic("SentryServerMock.PeersFunc: method is nil but SentryServer.Peers was just called") - } callInfo := struct { PeersRequest *PeersRequest Sentry_PeersServer Sentry_PeersServer @@ -302,6 +310,12 @@ func (mock *SentryServerMock) Peers(peersRequest *PeersRequest, sentry_PeersServ mock.lockPeers.Lock() mock.calls.Peers = append(mock.calls.Peers, callInfo) mock.lockPeers.Unlock() + if mock.PeersFunc == nil { + var ( + errOut error + ) + return errOut + } return mock.PeersFunc(peersRequest, sentry_PeersServer) } @@ -324,9 +338,6 @@ func (mock *SentryServerMock) PeersCalls() []struct { // PenalizePeer calls PenalizePeerFunc. func (mock *SentryServerMock) PenalizePeer(contextMoqParam context.Context, penalizePeerRequest *PenalizePeerRequest) (*emptypb.Empty, error) { - if mock.PenalizePeerFunc == nil { - panic("SentryServerMock.PenalizePeerFunc: method is nil but SentryServer.PenalizePeer was just called") - } callInfo := struct { ContextMoqParam context.Context PenalizePeerRequest *PenalizePeerRequest @@ -337,6 +348,13 @@ func (mock *SentryServerMock) PenalizePeer(contextMoqParam context.Context, pena mock.lockPenalizePeer.Lock() mock.calls.PenalizePeer = append(mock.calls.PenalizePeer, callInfo) mock.lockPenalizePeer.Unlock() + if mock.PenalizePeerFunc == nil { + var ( + emptyOut *emptypb.Empty + errOut error + ) + return emptyOut, errOut + } return mock.PenalizePeerFunc(contextMoqParam, penalizePeerRequest) } @@ -359,9 +377,6 @@ func (mock *SentryServerMock) PenalizePeerCalls() []struct { // SendMessageById calls SendMessageByIdFunc. func (mock *SentryServerMock) SendMessageById(contextMoqParam context.Context, sendMessageByIdRequest *SendMessageByIdRequest) (*SentPeers, error) { - if mock.SendMessageByIdFunc == nil { - panic("SentryServerMock.SendMessageByIdFunc: method is nil but SentryServer.SendMessageById was just called") - } callInfo := struct { ContextMoqParam context.Context SendMessageByIdRequest *SendMessageByIdRequest @@ -372,6 +387,13 @@ func (mock *SentryServerMock) SendMessageById(contextMoqParam context.Context, s mock.lockSendMessageById.Lock() mock.calls.SendMessageById = append(mock.calls.SendMessageById, callInfo) mock.lockSendMessageById.Unlock() + if mock.SendMessageByIdFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageByIdFunc(contextMoqParam, sendMessageByIdRequest) } @@ -394,9 +416,6 @@ func (mock *SentryServerMock) SendMessageByIdCalls() []struct { // SendMessageByMinBlock calls SendMessageByMinBlockFunc. func (mock *SentryServerMock) SendMessageByMinBlock(contextMoqParam context.Context, sendMessageByMinBlockRequest *SendMessageByMinBlockRequest) (*SentPeers, error) { - if mock.SendMessageByMinBlockFunc == nil { - panic("SentryServerMock.SendMessageByMinBlockFunc: method is nil but SentryServer.SendMessageByMinBlock was just called") - } callInfo := struct { ContextMoqParam context.Context SendMessageByMinBlockRequest *SendMessageByMinBlockRequest @@ -407,6 +426,13 @@ func (mock *SentryServerMock) SendMessageByMinBlock(contextMoqParam context.Cont mock.lockSendMessageByMinBlock.Lock() mock.calls.SendMessageByMinBlock = append(mock.calls.SendMessageByMinBlock, callInfo) mock.lockSendMessageByMinBlock.Unlock() + if mock.SendMessageByMinBlockFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageByMinBlockFunc(contextMoqParam, sendMessageByMinBlockRequest) } @@ -429,9 +455,6 @@ func (mock *SentryServerMock) SendMessageByMinBlockCalls() []struct { // SendMessageToAll calls SendMessageToAllFunc. func (mock *SentryServerMock) SendMessageToAll(contextMoqParam context.Context, outboundMessageData *OutboundMessageData) (*SentPeers, error) { - if mock.SendMessageToAllFunc == nil { - panic("SentryServerMock.SendMessageToAllFunc: method is nil but SentryServer.SendMessageToAll was just called") - } callInfo := struct { ContextMoqParam context.Context OutboundMessageData *OutboundMessageData @@ -442,6 +465,13 @@ func (mock *SentryServerMock) SendMessageToAll(contextMoqParam context.Context, mock.lockSendMessageToAll.Lock() mock.calls.SendMessageToAll = append(mock.calls.SendMessageToAll, callInfo) mock.lockSendMessageToAll.Unlock() + if mock.SendMessageToAllFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageToAllFunc(contextMoqParam, outboundMessageData) } @@ -464,9 +494,6 @@ func (mock *SentryServerMock) SendMessageToAllCalls() []struct { // SendMessageToRandomPeers calls SendMessageToRandomPeersFunc. func (mock *SentryServerMock) SendMessageToRandomPeers(contextMoqParam context.Context, sendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest) (*SentPeers, error) { - if mock.SendMessageToRandomPeersFunc == nil { - panic("SentryServerMock.SendMessageToRandomPeersFunc: method is nil but SentryServer.SendMessageToRandomPeers was just called") - } callInfo := struct { ContextMoqParam context.Context SendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest @@ -477,6 +504,13 @@ func (mock *SentryServerMock) SendMessageToRandomPeers(contextMoqParam context.C mock.lockSendMessageToRandomPeers.Lock() mock.calls.SendMessageToRandomPeers = append(mock.calls.SendMessageToRandomPeers, callInfo) mock.lockSendMessageToRandomPeers.Unlock() + if mock.SendMessageToRandomPeersFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageToRandomPeersFunc(contextMoqParam, sendMessageToRandomPeersRequest) } @@ -499,9 +533,6 @@ func (mock *SentryServerMock) SendMessageToRandomPeersCalls() []struct { // SetStatus calls SetStatusFunc. func (mock *SentryServerMock) SetStatus(contextMoqParam context.Context, statusData *StatusData) (*SetStatusReply, error) { - if mock.SetStatusFunc == nil { - panic("SentryServerMock.SetStatusFunc: method is nil but SentryServer.SetStatus was just called") - } callInfo := struct { ContextMoqParam context.Context StatusData *StatusData @@ -512,6 +543,13 @@ func (mock *SentryServerMock) SetStatus(contextMoqParam context.Context, statusD mock.lockSetStatus.Lock() mock.calls.SetStatus = append(mock.calls.SetStatus, callInfo) mock.lockSetStatus.Unlock() + if mock.SetStatusFunc == nil { + var ( + setStatusReplyOut *SetStatusReply + errOut error + ) + return setStatusReplyOut, errOut + } return mock.SetStatusFunc(contextMoqParam, statusData) } @@ -534,14 +572,14 @@ func (mock *SentryServerMock) SetStatusCalls() []struct { // mustEmbedUnimplementedSentryServer calls mustEmbedUnimplementedSentryServerFunc. func (mock *SentryServerMock) mustEmbedUnimplementedSentryServer() { - if mock.mustEmbedUnimplementedSentryServerFunc == nil { - panic("SentryServerMock.mustEmbedUnimplementedSentryServerFunc: method is nil but SentryServer.mustEmbedUnimplementedSentryServer was just called") - } callInfo := struct { }{} mock.lockmustEmbedUnimplementedSentryServer.Lock() mock.calls.mustEmbedUnimplementedSentryServer = append(mock.calls.mustEmbedUnimplementedSentryServer, callInfo) mock.lockmustEmbedUnimplementedSentryServer.Unlock() + if mock.mustEmbedUnimplementedSentryServerFunc == nil { + return + } mock.mustEmbedUnimplementedSentryServerFunc() } @@ -742,9 +780,6 @@ type SentryClientMock struct { // Messages calls MessagesFunc. func (mock *SentryClientMock) Messages(ctx context.Context, in *MessagesRequest, opts ...grpc.CallOption) (Sentry_MessagesClient, error) { - if mock.MessagesFunc == nil { - panic("SentryClientMock.MessagesFunc: method is nil but SentryClient.Messages was just called") - } callInfo := struct { Ctx context.Context In *MessagesRequest @@ -757,6 +792,13 @@ func (mock *SentryClientMock) Messages(ctx context.Context, in *MessagesRequest, mock.lockMessages.Lock() mock.calls.Messages = append(mock.calls.Messages, callInfo) mock.lockMessages.Unlock() + if mock.MessagesFunc == nil { + var ( + sentry_MessagesClientOut Sentry_MessagesClient + errOut error + ) + return sentry_MessagesClientOut, errOut + } return mock.MessagesFunc(ctx, in, opts...) } @@ -781,9 +823,6 @@ func (mock *SentryClientMock) MessagesCalls() []struct { // PeerCount calls PeerCountFunc. func (mock *SentryClientMock) PeerCount(ctx context.Context, in *PeerCountRequest, opts ...grpc.CallOption) (*PeerCountReply, error) { - if mock.PeerCountFunc == nil { - panic("SentryClientMock.PeerCountFunc: method is nil but SentryClient.PeerCount was just called") - } callInfo := struct { Ctx context.Context In *PeerCountRequest @@ -796,6 +835,13 @@ func (mock *SentryClientMock) PeerCount(ctx context.Context, in *PeerCountReques mock.lockPeerCount.Lock() mock.calls.PeerCount = append(mock.calls.PeerCount, callInfo) mock.lockPeerCount.Unlock() + if mock.PeerCountFunc == nil { + var ( + peerCountReplyOut *PeerCountReply + errOut error + ) + return peerCountReplyOut, errOut + } return mock.PeerCountFunc(ctx, in, opts...) } @@ -820,9 +866,6 @@ func (mock *SentryClientMock) PeerCountCalls() []struct { // PeerMinBlock calls PeerMinBlockFunc. func (mock *SentryClientMock) PeerMinBlock(ctx context.Context, in *PeerMinBlockRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { - if mock.PeerMinBlockFunc == nil { - panic("SentryClientMock.PeerMinBlockFunc: method is nil but SentryClient.PeerMinBlock was just called") - } callInfo := struct { Ctx context.Context In *PeerMinBlockRequest @@ -835,6 +878,13 @@ func (mock *SentryClientMock) PeerMinBlock(ctx context.Context, in *PeerMinBlock mock.lockPeerMinBlock.Lock() mock.calls.PeerMinBlock = append(mock.calls.PeerMinBlock, callInfo) mock.lockPeerMinBlock.Unlock() + if mock.PeerMinBlockFunc == nil { + var ( + emptyOut *emptypb.Empty + errOut error + ) + return emptyOut, errOut + } return mock.PeerMinBlockFunc(ctx, in, opts...) } @@ -859,9 +909,6 @@ func (mock *SentryClientMock) PeerMinBlockCalls() []struct { // Peers calls PeersFunc. func (mock *SentryClientMock) Peers(ctx context.Context, in *PeersRequest, opts ...grpc.CallOption) (Sentry_PeersClient, error) { - if mock.PeersFunc == nil { - panic("SentryClientMock.PeersFunc: method is nil but SentryClient.Peers was just called") - } callInfo := struct { Ctx context.Context In *PeersRequest @@ -874,6 +921,13 @@ func (mock *SentryClientMock) Peers(ctx context.Context, in *PeersRequest, opts mock.lockPeers.Lock() mock.calls.Peers = append(mock.calls.Peers, callInfo) mock.lockPeers.Unlock() + if mock.PeersFunc == nil { + var ( + sentry_PeersClientOut Sentry_PeersClient + errOut error + ) + return sentry_PeersClientOut, errOut + } return mock.PeersFunc(ctx, in, opts...) } @@ -898,9 +952,6 @@ func (mock *SentryClientMock) PeersCalls() []struct { // PenalizePeer calls PenalizePeerFunc. func (mock *SentryClientMock) PenalizePeer(ctx context.Context, in *PenalizePeerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { - if mock.PenalizePeerFunc == nil { - panic("SentryClientMock.PenalizePeerFunc: method is nil but SentryClient.PenalizePeer was just called") - } callInfo := struct { Ctx context.Context In *PenalizePeerRequest @@ -913,6 +964,13 @@ func (mock *SentryClientMock) PenalizePeer(ctx context.Context, in *PenalizePeer mock.lockPenalizePeer.Lock() mock.calls.PenalizePeer = append(mock.calls.PenalizePeer, callInfo) mock.lockPenalizePeer.Unlock() + if mock.PenalizePeerFunc == nil { + var ( + emptyOut *emptypb.Empty + errOut error + ) + return emptyOut, errOut + } return mock.PenalizePeerFunc(ctx, in, opts...) } @@ -937,9 +995,6 @@ func (mock *SentryClientMock) PenalizePeerCalls() []struct { // SendMessageById calls SendMessageByIdFunc. func (mock *SentryClientMock) SendMessageById(ctx context.Context, in *SendMessageByIdRequest, opts ...grpc.CallOption) (*SentPeers, error) { - if mock.SendMessageByIdFunc == nil { - panic("SentryClientMock.SendMessageByIdFunc: method is nil but SentryClient.SendMessageById was just called") - } callInfo := struct { Ctx context.Context In *SendMessageByIdRequest @@ -952,6 +1007,13 @@ func (mock *SentryClientMock) SendMessageById(ctx context.Context, in *SendMessa mock.lockSendMessageById.Lock() mock.calls.SendMessageById = append(mock.calls.SendMessageById, callInfo) mock.lockSendMessageById.Unlock() + if mock.SendMessageByIdFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageByIdFunc(ctx, in, opts...) } @@ -976,9 +1038,6 @@ func (mock *SentryClientMock) SendMessageByIdCalls() []struct { // SendMessageByMinBlock calls SendMessageByMinBlockFunc. func (mock *SentryClientMock) SendMessageByMinBlock(ctx context.Context, in *SendMessageByMinBlockRequest, opts ...grpc.CallOption) (*SentPeers, error) { - if mock.SendMessageByMinBlockFunc == nil { - panic("SentryClientMock.SendMessageByMinBlockFunc: method is nil but SentryClient.SendMessageByMinBlock was just called") - } callInfo := struct { Ctx context.Context In *SendMessageByMinBlockRequest @@ -991,6 +1050,13 @@ func (mock *SentryClientMock) SendMessageByMinBlock(ctx context.Context, in *Sen mock.lockSendMessageByMinBlock.Lock() mock.calls.SendMessageByMinBlock = append(mock.calls.SendMessageByMinBlock, callInfo) mock.lockSendMessageByMinBlock.Unlock() + if mock.SendMessageByMinBlockFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageByMinBlockFunc(ctx, in, opts...) } @@ -1015,9 +1081,6 @@ func (mock *SentryClientMock) SendMessageByMinBlockCalls() []struct { // SendMessageToAll calls SendMessageToAllFunc. func (mock *SentryClientMock) SendMessageToAll(ctx context.Context, in *OutboundMessageData, opts ...grpc.CallOption) (*SentPeers, error) { - if mock.SendMessageToAllFunc == nil { - panic("SentryClientMock.SendMessageToAllFunc: method is nil but SentryClient.SendMessageToAll was just called") - } callInfo := struct { Ctx context.Context In *OutboundMessageData @@ -1030,6 +1093,13 @@ func (mock *SentryClientMock) SendMessageToAll(ctx context.Context, in *Outbound mock.lockSendMessageToAll.Lock() mock.calls.SendMessageToAll = append(mock.calls.SendMessageToAll, callInfo) mock.lockSendMessageToAll.Unlock() + if mock.SendMessageToAllFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageToAllFunc(ctx, in, opts...) } @@ -1054,9 +1124,6 @@ func (mock *SentryClientMock) SendMessageToAllCalls() []struct { // SendMessageToRandomPeers calls SendMessageToRandomPeersFunc. func (mock *SentryClientMock) SendMessageToRandomPeers(ctx context.Context, in *SendMessageToRandomPeersRequest, opts ...grpc.CallOption) (*SentPeers, error) { - if mock.SendMessageToRandomPeersFunc == nil { - panic("SentryClientMock.SendMessageToRandomPeersFunc: method is nil but SentryClient.SendMessageToRandomPeers was just called") - } callInfo := struct { Ctx context.Context In *SendMessageToRandomPeersRequest @@ -1069,6 +1136,13 @@ func (mock *SentryClientMock) SendMessageToRandomPeers(ctx context.Context, in * mock.lockSendMessageToRandomPeers.Lock() mock.calls.SendMessageToRandomPeers = append(mock.calls.SendMessageToRandomPeers, callInfo) mock.lockSendMessageToRandomPeers.Unlock() + if mock.SendMessageToRandomPeersFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageToRandomPeersFunc(ctx, in, opts...) } @@ -1093,9 +1167,6 @@ func (mock *SentryClientMock) SendMessageToRandomPeersCalls() []struct { // SetStatus calls SetStatusFunc. func (mock *SentryClientMock) SetStatus(ctx context.Context, in *StatusData, opts ...grpc.CallOption) (*SetStatusReply, error) { - if mock.SetStatusFunc == nil { - panic("SentryClientMock.SetStatusFunc: method is nil but SentryClient.SetStatus was just called") - } callInfo := struct { Ctx context.Context In *StatusData @@ -1108,6 +1179,13 @@ func (mock *SentryClientMock) SetStatus(ctx context.Context, in *StatusData, opt mock.lockSetStatus.Lock() mock.calls.SetStatus = append(mock.calls.SetStatus, callInfo) mock.lockSetStatus.Unlock() + if mock.SetStatusFunc == nil { + var ( + setStatusReplyOut *SetStatusReply + errOut error + ) + return setStatusReplyOut, errOut + } return mock.SetStatusFunc(ctx, in, opts...) } diff --git a/gointerfaces/test_util.go b/gointerfaces/test_util.go index 64df6f9ee..500328691 100644 --- a/gointerfaces/test_util.go +++ b/gointerfaces/test_util.go @@ -1,3 +1,3 @@ package gointerfaces -//go:generate moq -out ./sentry/mocks.go ./sentry SentryServer SentryClient +//go:generate moq -stub -out ./sentry/mocks.go ./sentry SentryServer SentryClient diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index 2fa684170..4962ce918 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -25,7 +25,9 @@ import ( "github.com/ledgerwatch/erigon-lib/direct" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" + "github.com/ledgerwatch/erigon-lib/gointerfaces/types" "github.com/ledgerwatch/erigon-lib/log" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -39,22 +41,20 @@ func TestFetch(t *testing.T) { var networkId uint64 = 1 forks := []uint64{1, 5, 10} - mock := NewMockSentry(ctx) - sentryClient := direct.NewSentryClientDirect(direct.ETH66, mock) - pool := &PoolMock{ - IdHashKnownFunc: func(hash []byte) bool { return false }, - } + m := NewMockSentry(ctx) + sentryClient := direct.NewSentryClientDirect(direct.ETH66, m) + pool := &PoolMock{} fetch := NewFetch(ctx, []sentry.SentryClient{sentryClient}, genesisHash, networkId, forks, pool, logger) var wg sync.WaitGroup fetch.SetWaitGroup(&wg) - mock.StreamWg.Add(2) + m.StreamWg.Add(2) fetch.Start() - mock.StreamWg.Wait() + m.StreamWg.Wait() // Send one transaction id wg.Add(1) data, _ := hex.DecodeString("e1a0595e27a835cd79729ff1eeacec3120eeb6ed1464a04ec727aaca734ead961328") - errs := mock.Send(&sentry.InboundMessage{ + errs := m.Send(&sentry.InboundMessage{ Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, Data: data, PeerId: PeerId, @@ -65,6 +65,7 @@ func TestFetch(t *testing.T) { } } wg.Wait() + } func TestSendTxPropagate(t *testing.T) { @@ -72,14 +73,18 @@ func TestSendTxPropagate(t *testing.T) { ctx, cancelFn := context.WithCancel(context.Background()) defer cancelFn() - t.Run("small message", func(t *testing.T) { + t.Run("few remote txs", func(t *testing.T) { m := NewMockSentry(ctx) send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) send.BroadcastRemotePooledTxs(toHashes([32]byte{1}, [32]byte{42})) - require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, m.sentMessages[0].Id) - require.Equal(t, 68, len(m.sentMessages[0].Data)) + + calls := m.SendMessageToRandomPeersCalls() + assert.Equal(t, 1, len(calls)) + first := calls[0].SendMessageToRandomPeersRequest.Data + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id) + require.Equal(t, 68, len(first.Data)) }) - t.Run("slice large messages", func(t *testing.T) { + t.Run("much remote txs", func(t *testing.T) { m := NewMockSentry(ctx) send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) list := make(Hashes, p2pTxPacketLimit*3) @@ -88,10 +93,28 @@ func TestSendTxPropagate(t *testing.T) { copy(list[i:i+32], b) } send.BroadcastRemotePooledTxs(list) - require.Equal(t, 3, len(m.sentMessages)) + calls := m.SendMessageToRandomPeersCalls() + require.Equal(t, 3, len(calls)) for i := 0; i < 3; i++ { - require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, m.sentMessages[i].Id) - require.Less(t, 0, len(m.sentMessages[i].Data)) + call := calls[i].SendMessageToRandomPeersRequest.Data + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, call.Id) + require.Less(t, 0, len(call.Data)) } }) + t.Run("few local txs", func(t *testing.T) { + m := NewMockSentry(ctx) + m.SendMessageToAllFunc = func(contextMoqParam context.Context, outboundMessageData *sentry.OutboundMessageData) (*sentry.SentPeers, error) { + return &sentry.SentPeers{Peers: make([]*types.H512, 5)}, nil + } + send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) + send.BroadcastLocalPooledTxs(toHashes([32]byte{1}, [32]byte{42})) + + calls := m.SendMessageToAllCalls() + assert.Equal(t, 1, len(calls)) + first := calls[0].OutboundMessageData + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id) + require.Equal(t, 68, len(first.Data)) + t.Fail() + }) + } diff --git a/txpool/mocks.go b/txpool/mocks.go index bd0b363c8..715913dc8 100644 --- a/txpool/mocks.go +++ b/txpool/mocks.go @@ -55,9 +55,6 @@ type PoolMock struct { // IdHashKnown calls IdHashKnownFunc. func (mock *PoolMock) IdHashKnown(hash []byte) bool { - if mock.IdHashKnownFunc == nil { - panic("PoolMock.IdHashKnownFunc: method is nil but Pool.IdHashKnown was just called") - } callInfo := struct { Hash []byte }{ @@ -66,6 +63,12 @@ func (mock *PoolMock) IdHashKnown(hash []byte) bool { mock.lockIdHashKnown.Lock() mock.calls.IdHashKnown = append(mock.calls.IdHashKnown, callInfo) mock.lockIdHashKnown.Unlock() + if mock.IdHashKnownFunc == nil { + var ( + bOut bool + ) + return bOut + } return mock.IdHashKnownFunc(hash) } @@ -86,9 +89,6 @@ func (mock *PoolMock) IdHashKnownCalls() []struct { // NotifyNewPeer calls NotifyNewPeerFunc. func (mock *PoolMock) NotifyNewPeer(peerID PeerID) { - if mock.NotifyNewPeerFunc == nil { - panic("PoolMock.NotifyNewPeerFunc: method is nil but Pool.NotifyNewPeer was just called") - } callInfo := struct { PeerID PeerID }{ @@ -97,6 +97,9 @@ func (mock *PoolMock) NotifyNewPeer(peerID PeerID) { mock.lockNotifyNewPeer.Lock() mock.calls.NotifyNewPeer = append(mock.calls.NotifyNewPeer, callInfo) mock.lockNotifyNewPeer.Unlock() + if mock.NotifyNewPeerFunc == nil { + return + } mock.NotifyNewPeerFunc(peerID) } diff --git a/txpool/pool.go b/txpool/pool.go index fa6065f3a..304a6edec 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -22,6 +22,7 @@ import ( "time" "github.com/ledgerwatch/erigon-lib/gointerfaces/types" + "go.uber.org/zap" ) // Pool is interface for the transaction pool @@ -37,6 +38,7 @@ type Pool interface { type PoolImpl struct { recentlyConnectedPeers *recentlyConnectedPeers lastTxPropagationTimestamp time.Time + logger *zap.SugaredLogger } func NewPool() *PoolImpl { @@ -76,7 +78,13 @@ func (p *PoolImpl) Loop(ctx context.Context, send *Send, timings Timings) { // first broadcast all local txs to all peers, then non-local to random sqrt(peersAmount) peers localTxHashes = localTxHashes[:0] p.FillLocalHashesSince(last, localTxHashes) - send.BroadcastLocalPooledTxs(localTxHashes) + initialAmount := len(localTxHashes) + sentToPeers := send.BroadcastLocalPooledTxs(localTxHashes) + if initialAmount == 1 { + p.logger.Infof("local tx %x, propagated to %d peers", localTxHashes, sentToPeers) + } else { + p.logger.Infof("%d local txs propagated to %d peers", initialAmount, sentToPeers) + } remoteTxHashes = remoteTxHashes[:0] p.FillRemoteHashesSince(last, remoteTxHashes) diff --git a/txpool/send.go b/txpool/send.go index 796c24425..f5ff88547 100644 --- a/txpool/send.go +++ b/txpool/send.go @@ -63,16 +63,14 @@ func (f *Send) notifyTests() { } } -func (f *Send) BroadcastLocalPooledTxs(txs Hashes) { +func (f *Send) BroadcastLocalPooledTxs(txs Hashes) (sentToPeers int) { defer f.notifyTests() if len(txs) == 0 { return } - initialAmount := len(txs) avgPeersPerSent65 := 0 avgPeersPerSent66 := 0 - initialTxs := txs for len(txs) > 0 { var pending Hashes if len(txs) > p2pTxPacketLimit { @@ -123,15 +121,11 @@ func (f *Send) BroadcastLocalPooledTxs(txs Hashes) { return } avgPeersPerSent66 += len(peers.Peers) + f.logger.Warnf("yay: %d", avgPeersPerSent66) } } } - if initialAmount == 1 { - f.logger.Infof("local tx %x, propageted to %d peers", initialTxs, avgPeersPerSent65+avgPeersPerSent66) - } else { - f.logger.Infof("%d local txs propagated to %d peers", initialAmount, avgPeersPerSent65+avgPeersPerSent66) - } - return + return avgPeersPerSent65 + avgPeersPerSent66 } func (f *Send) BroadcastRemotePooledTxs(txs Hashes) { diff --git a/txpool/test_util.go b/txpool/test_util.go index c4911ea0e..3582d1e5e 100644 --- a/txpool/test_util.go +++ b/txpool/test_util.go @@ -22,28 +22,25 @@ import ( "github.com/ledgerwatch/erigon-lib/gointerfaces" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" - "google.golang.org/protobuf/types/known/emptypb" ) -//go:generate moq -out mocks.go . Pool +//go:generate moq -stub -out mocks.go . Pool type MockSentry struct { - sentry.UnimplementedSentryServer + *sentry.SentryServerMock streams map[sentry.MessageId][]sentry.Sentry_MessagesServer peersStreams []sentry.Sentry_PeersServer StreamWg sync.WaitGroup - sentMessages []*sentry.OutboundMessageData ctx context.Context lock sync.RWMutex } func NewMockSentry(ctx context.Context) *MockSentry { - return &MockSentry{ctx: ctx} + return &MockSentry{ctx: ctx, SentryServerMock: &sentry.SentryServerMock{}} } var PeerId PeerID = gointerfaces.ConvertBytesToH512([]byte("12345")) -// Stream returns stream, waiting if necessary func (ms *MockSentry) Send(req *sentry.InboundMessage) (errs []error) { ms.lock.RLock() defer ms.lock.RUnlock() @@ -55,24 +52,6 @@ func (ms *MockSentry) Send(req *sentry.InboundMessage) (errs []error) { return errs } -func (ms *MockSentry) PenalizePeer(context.Context, *sentry.PenalizePeerRequest) (*emptypb.Empty, error) { - return nil, nil -} -func (ms *MockSentry) SendMessageById(_ context.Context, r *sentry.SendMessageByIdRequest) (*sentry.SentPeers, error) { - ms.sentMessages = append(ms.sentMessages, r.Data) - return nil, nil -} -func (ms *MockSentry) SendMessageToRandomPeers(_ context.Context, r *sentry.SendMessageToRandomPeersRequest) (*sentry.SentPeers, error) { - ms.sentMessages = append(ms.sentMessages, r.Data) - return nil, nil -} -func (ms *MockSentry) SendMessageToAll(_ context.Context, r *sentry.OutboundMessageData) (*sentry.SentPeers, error) { - ms.sentMessages = append(ms.sentMessages, r) - return nil, nil -} -func (ms *MockSentry) SentMessage(i int) *sentry.OutboundMessageData { - return ms.sentMessages[i] -} func (ms *MockSentry) SetStatus(context.Context, *sentry.StatusData) (*sentry.SetStatusReply, error) { return &sentry.SetStatusReply{Protocol: sentry.Protocol_ETH66}, nil } @@ -93,9 +72,6 @@ func (ms *MockSentry) Messages(req *sentry.MessagesRequest, stream sentry.Sentry return nil } } -func (ms *MockSentry) PeerCount(_ context.Context, req *sentry.PeerCountRequest) (*sentry.PeerCountReply, error) { - return &sentry.PeerCountReply{Count: 1}, nil -} func (ms *MockSentry) Peers(req *sentry.PeersRequest, stream sentry.Sentry_PeersServer) error { ms.lock.Lock()