erigon-pulse/txpool/test_util.go

116 lines
3.0 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"
2021-07-14 18:17:20 +00:00
"sync"
2021-07-04 22:13:33 +00:00
2021-07-14 16:14:36 +00:00
"github.com/ledgerwatch/erigon-lib/gointerfaces"
2021-07-04 22:13:33 +00:00
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
2021-08-14 01:49:21 +00:00
"google.golang.org/protobuf/types/known/emptypb"
2021-07-04 22:13:33 +00:00
)
2021-08-03 07:49:25 +00:00
//go:generate moq -stub -out mocks_test.go . Pool
2021-07-04 22:13:33 +00:00
type MockSentry struct {
2021-07-26 02:06:17 +00:00
*sentry.SentryServerMock
streams map[sentry.MessageId][]sentry.Sentry_MessagesServer
2021-07-14 16:14:36 +00:00
peersStreams []sentry.Sentry_PeersServer
2021-07-14 18:17:20 +00:00
StreamWg sync.WaitGroup
ctx context.Context
2021-07-14 18:17:20 +00:00
lock sync.RWMutex
2021-07-04 22:13:33 +00:00
}
func NewMockSentry(ctx context.Context) *MockSentry {
2021-07-26 02:06:17 +00:00
return &MockSentry{ctx: ctx, SentryServerMock: &sentry.SentryServerMock{}}
2021-07-04 22:13:33 +00:00
}
2022-03-19 04:38:37 +00:00
var peerID PeerID = gointerfaces.ConvertHashToH256([32]byte{0x12, 0x34, 0x50}) // "12345"
2021-07-14 16:14:36 +00:00
func (ms *MockSentry) Send(req *sentry.InboundMessage) (errs []error) {
2021-07-14 18:17:20 +00:00
ms.lock.RLock()
defer ms.lock.RUnlock()
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) SetStatus(context.Context, *sentry.StatusData) (*sentry.SetStatusReply, error) {
2021-08-14 01:49:21 +00:00
return &sentry.SetStatusReply{}, nil
}
func (ms *MockSentry) HandShake(context.Context, *emptypb.Empty) (*sentry.HandShakeReply, error) {
return &sentry.HandShakeReply{Protocol: sentry.Protocol_ETH66}, nil
}
func (ms *MockSentry) Messages(req *sentry.MessagesRequest, stream sentry.Sentry_MessagesServer) error {
2021-07-14 18:17:20 +00:00
ms.lock.Lock()
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)
}
2021-07-14 18:17:20 +00:00
ms.lock.Unlock()
ms.StreamWg.Done()
select {
case <-ms.ctx.Done():
return nil
case <-stream.Context().Done():
return nil
}
}
2021-07-12 13:18:24 +00:00
func (ms *MockSentry) Peers(req *sentry.PeersRequest, stream sentry.Sentry_PeersServer) error {
2021-07-14 18:17:20 +00:00
ms.lock.Lock()
2021-07-14 16:14:36 +00:00
ms.peersStreams = append(ms.peersStreams, stream)
2021-07-14 18:17:20 +00:00
ms.lock.Unlock()
ms.StreamWg.Done()
select {
case <-ms.ctx.Done():
return nil
case <-stream.Context().Done():
return nil
}
2021-07-12 13:18:24 +00:00
}
2021-07-26 00:57:49 +00:00
func toHashes(h ...byte) (out Hashes) {
2021-07-26 00:57:49 +00:00
for i := range h {
hash := [32]byte{h[i]}
out = append(out, hash[:]...)
2021-07-26 00:57:49 +00:00
}
return out
}
2021-07-26 03:04:53 +00:00
func testRlps(num int) [][]byte {
rlps := make([][]byte, num)
for i := 0; i < num; i++ {
rlps[i] = []byte{1}
}
return rlps
}
func toPeerIDs(h ...byte) (out []PeerID) {
2021-07-26 03:04:53 +00:00
for i := range h {
hash := [32]byte{h[i]}
out = append(out, gointerfaces.ConvertHashToH256(hash))
2021-07-26 03:04:53 +00:00
}
return out
}