erigon-pulse/txpool/test_util.go

109 lines
3.4 KiB
Go
Raw Normal View History

2021-07-04 22:13:33 +00:00
/*
Copyright 2021 Erigon contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package txpool
import (
"context"
"sync"
2021-07-04 22:13:33 +00:00
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
"google.golang.org/protobuf/types/known/emptypb"
)
type MockSentry struct {
sentry.UnimplementedSentryServer
streams map[sentry.MessageId][]sentry.Sentry_MessagesServer
StreamWg sync.WaitGroup
peersStream sentry.Sentry_PeersServer
sentMessages []*sentry.OutboundMessageData
ctx context.Context
2021-07-04 22:13:33 +00:00
}
func NewMockSentry(ctx context.Context) *MockSentry {
2021-07-04 22:13:33 +00:00
return &MockSentry{}
}
// Stream returns stream, waiting if necessary
func (ms *MockSentry) Send(req *sentry.InboundMessage) (errs []error) {
ms.StreamWg.Wait()
for _, stream := range ms.streams[req.Id] {
if err := stream.Send(req); err != nil {
errs = append(errs, err)
}
}
return errs
2021-07-04 22:13:33 +00:00
}
func (ms *MockSentry) PenalizePeer(context.Context, *sentry.PenalizePeerRequest) (*emptypb.Empty, error) {
2021-07-04 22:13:33 +00:00
return nil, nil
}
func (ms *MockSentry) PeerMinBlock(context.Context, *sentry.PeerMinBlockRequest) (*emptypb.Empty, error) {
2021-07-04 22:13:33 +00:00
return nil, nil
}
func (ms *MockSentry) SendMessageByMinBlock(_ context.Context, r *sentry.SendMessageByMinBlockRequest) (*sentry.SentPeers, error) {
ms.sentMessages = append(ms.sentMessages, r.Data)
2021-07-04 22:13:33 +00:00
return nil, nil
}
func (ms *MockSentry) SendMessageById(_ context.Context, r *sentry.SendMessageByIdRequest) (*sentry.SentPeers, error) {
ms.sentMessages = append(ms.sentMessages, r.Data)
2021-07-04 22:13:33 +00:00
return nil, nil
}
func (ms *MockSentry) SendMessageToRandomPeers(_ context.Context, r *sentry.SendMessageToRandomPeersRequest) (*sentry.SentPeers, error) {
ms.sentMessages = append(ms.sentMessages, r.Data)
2021-07-04 22:13:33 +00:00
return nil, nil
}
func (ms *MockSentry) SendMessageToAll(_ context.Context, r *sentry.OutboundMessageData) (*sentry.SentPeers, error) {
ms.sentMessages = append(ms.sentMessages, r)
2021-07-04 22:13:33 +00:00
return nil, nil
}
func (ms *MockSentry) SentMessage(i int) *sentry.OutboundMessageData {
return ms.sentMessages[i]
2021-07-04 22:13:33 +00:00
}
func (ms *MockSentry) SetStatus(context.Context, *sentry.StatusData) (*sentry.SetStatusReply, error) {
return &sentry.SetStatusReply{Protocol: sentry.Protocol_ETH66}, nil
}
func (ms *MockSentry) Messages(req *sentry.MessagesRequest, stream sentry.Sentry_MessagesServer) error {
if ms.streams == nil {
ms.streams = map[sentry.MessageId][]sentry.Sentry_MessagesServer{}
}
for _, id := range req.Ids {
ms.streams[id] = append(ms.streams[id], stream)
}
ms.StreamWg.Done()
select {
case <-ms.ctx.Done():
return nil
case <-stream.Context().Done():
return nil
}
}
func (ms *MockSentry) PeerCount(_ context.Context, req *sentry.PeerCountRequest) (*sentry.PeerCountReply, error) {
return &sentry.PeerCountReply{Count: 1}, nil
2021-07-04 22:13:33 +00:00
}
2021-07-12 13:18:24 +00:00
func (ms *MockSentry) Peers(req *sentry.PeersRequest, stream sentry.Sentry_PeersServer) error {
ms.peersStream = stream
ms.StreamWg.Done()
select {
case <-ms.ctx.Done():
return nil
case <-stream.Context().Done():
return nil
}
2021-07-12 13:18:24 +00:00
}