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"
|
2022-04-11 03:02:44 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/types"
|
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-25 12:19:33 +00:00
|
|
|
|
2021-07-04 22:13:33 +00:00
|
|
|
type MockSentry struct {
|
2021-07-26 02:06:17 +00:00
|
|
|
*sentry.SentryServerMock
|
2021-07-14 06:44:23 +00:00
|
|
|
streams map[sentry.MessageId][]sentry.Sentry_MessagesServer
|
2022-04-23 14:43:24 +00:00
|
|
|
peersStreams []sentry.Sentry_PeerEventsServer
|
2021-07-14 18:17:20 +00:00
|
|
|
StreamWg sync.WaitGroup
|
2021-07-14 06:44:23 +00:00
|
|
|
ctx context.Context
|
2021-07-14 18:17:20 +00:00
|
|
|
lock sync.RWMutex
|
2021-07-04 22:13:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 06:44:23 +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-04-11 03:02:44 +00:00
|
|
|
var peerID types.PeerID = gointerfaces.ConvertHashToH512([64]byte{0x12, 0x34, 0x50}) // "12345"
|
2021-07-14 16:14:36 +00:00
|
|
|
|
2021-07-14 06:44:23 +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()
|
2021-07-14 06:44:23 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-14 06:44:23 +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) {
|
2022-06-28 11:39:45 +00:00
|
|
|
return &sentry.HandShakeReply{Protocol: sentry.Protocol_ETH66}, nil
|
2021-07-14 06:44:23 +00:00
|
|
|
}
|
|
|
|
func (ms *MockSentry) Messages(req *sentry.MessagesRequest, stream sentry.Sentry_MessagesServer) error {
|
2021-07-14 18:17:20 +00:00
|
|
|
ms.lock.Lock()
|
2021-07-14 06:44:23 +00:00
|
|
|
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()
|
2021-07-14 06:44:23 +00:00
|
|
|
select {
|
|
|
|
case <-ms.ctx.Done():
|
|
|
|
return nil
|
|
|
|
case <-stream.Context().Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 13:18:24 +00:00
|
|
|
|
2022-04-23 14:43:24 +00:00
|
|
|
func (ms *MockSentry) PeerEvents(req *sentry.PeerEventsRequest, stream sentry.Sentry_PeerEventsServer) 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()
|
2021-07-14 06:44:23 +00:00
|
|
|
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
|
|
|
|
2022-04-11 03:02:44 +00:00
|
|
|
func toHashes(h ...byte) (out types.Hashes) {
|
2021-07-26 00:57:49 +00:00
|
|
|
for i := range h {
|
2021-11-21 19:35:59 +00:00
|
|
|
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
|
|
|
|
2021-12-16 20:58:40 +00:00
|
|
|
func testRlps(num int) [][]byte {
|
|
|
|
rlps := make([][]byte, num)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
rlps[i] = []byte{1}
|
|
|
|
}
|
|
|
|
return rlps
|
|
|
|
}
|
|
|
|
|
2022-04-11 03:02:44 +00:00
|
|
|
func toPeerIDs(h ...byte) (out []types.PeerID) {
|
2021-07-26 03:04:53 +00:00
|
|
|
for i := range h {
|
2022-04-10 06:14:00 +00:00
|
|
|
hash := [64]byte{h[i]}
|
|
|
|
out = append(out, gointerfaces.ConvertHashToH512(hash))
|
2021-07-26 03:04:53 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|