diff --git a/cmd/sentry/sentry/sentry_grpc_server.go b/cmd/sentry/sentry/sentry_grpc_server.go index b20c9227427..c3eb38ac641 100644 --- a/cmd/sentry/sentry/sentry_grpc_server.go +++ b/cmd/sentry/sentry/sentry_grpc_server.go @@ -773,6 +773,28 @@ func (ss *GrpcServer) findBestPeersWithPermit(peerCount int) []*PeerInfo { return foundPeers } +func (ss *GrpcServer) findPeerByMinBlock(minBlock uint64) (*PeerInfo, bool) { + // Choose a peer that we can send this request to, with maximum number of permits + var foundPeerInfo *PeerInfo + var maxPermits int + now := time.Now() + ss.rangePeers(func(peerInfo *PeerInfo) bool { + if peerInfo.Height() >= minBlock { + deadlines := peerInfo.ClearDeadlines(now, false /* givePermit */) + //fmt.Printf("%d deadlines for peer %s\n", deadlines, peerID) + if deadlines < maxPermitsPerPeer { + permits := maxPermitsPerPeer - deadlines + if permits > maxPermits { + maxPermits = permits + foundPeerInfo = peerInfo + } + } + } + return true + }) + return foundPeerInfo, maxPermits > 0 +} + func (ss *GrpcServer) SendMessageByMinBlock(_ context.Context, inreq *proto_sentry.SendMessageByMinBlockRequest) (*proto_sentry.SentPeers, error) { reply := &proto_sentry.SentPeers{} msgcode := eth.FromProto[ss.Protocol.Version][inreq.Data.Id] @@ -781,6 +803,14 @@ func (ss *GrpcServer) SendMessageByMinBlock(_ context.Context, inreq *proto_sent msgcode != eth.GetPooledTransactionsMsg { return reply, fmt.Errorf("sendMessageByMinBlock not implemented for message Id: %s", inreq.Data.Id) } + if !ss.GetStatus().PassivePeers { + peerInfo, found := ss.findPeerByMinBlock(inreq.MinBlock) + if found { + ss.writePeer("sendMessageByMinBlock", peerInfo, msgcode, inreq.Data.Data, 30*time.Second) + reply.Peers = []*proto_types.H512{gointerfaces.ConvertHashToH512(peerInfo.ID())} + return reply, nil + } + } peerInfos := ss.findBestPeersWithPermit(int(inreq.MaxPeers)) reply.Peers = make([]*proto_types.H512, len(peerInfos)) for i, peerInfo := range peerInfos { diff --git a/cmd/sentry/sentry/sentry_multi_client.go b/cmd/sentry/sentry/sentry_multi_client.go index f0a7625d060..2b6312c1aa6 100644 --- a/cmd/sentry/sentry/sentry_multi_client.go +++ b/cmd/sentry/sentry/sentry_multi_client.go @@ -262,6 +262,7 @@ type MultiClient struct { Engine consensus.Engine blockReader services.HeaderAndCanonicalReader logPeerInfo bool + passivePeers bool historyV3 bool } @@ -307,6 +308,7 @@ func NewMultiClient( logPeerInfo: logPeerInfo, forkValidator: forkValidator, historyV3: historyV3, + passivePeers: chainConfig.TerminalTotalDifficultyPassed, } cs.ChainConfig = chainConfig cs.forks = forkid.GatherForks(cs.ChainConfig) @@ -758,6 +760,7 @@ func (cs *MultiClient) makeStatusData() *proto_sentry.StatusData { Genesis: gointerfaces.ConvertHashToH256(s.genesisHash), Forks: s.forks, }, + PassivePeers: cs.passivePeers, } } diff --git a/go.mod b/go.mod index 9bb65fcd1da..4bc8ca582c6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon go 1.18 require ( - github.com/ledgerwatch/erigon-lib v0.0.0-20221210085350-2f641bdeb4fe + github.com/ledgerwatch/erigon-lib v0.0.0-20221211145319-874c497dda90 github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20221117232719-cf68648bf146 github.com/ledgerwatch/log/v3 v3.6.0 github.com/ledgerwatch/secp256k1 v1.0.0 diff --git a/go.sum b/go.sum index e0d3cc0cf73..f9c7dd5c5ce 100644 --- a/go.sum +++ b/go.sum @@ -561,8 +561,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= -github.com/ledgerwatch/erigon-lib v0.0.0-20221210085350-2f641bdeb4fe h1:bCQEQpM07ludipLTi/CTdQVGFqIrWvgQ1//Ndpx0TIY= -github.com/ledgerwatch/erigon-lib v0.0.0-20221210085350-2f641bdeb4fe/go.mod h1:cckLLWh0v0OHNSjeq6bpzdvEjZ1HMLKavLBF19tpqeg= +github.com/ledgerwatch/erigon-lib v0.0.0-20221211145319-874c497dda90 h1:ZsCnxEiUWDgp9ed/TeAnF15nVha4TRiL/1RGRecgs/c= +github.com/ledgerwatch/erigon-lib v0.0.0-20221211145319-874c497dda90/go.mod h1:Cy/yMqN6ufAXayVYLEIKnRoYz25PK9Uxmp1yil+wA/A= github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20221117232719-cf68648bf146 h1:BBoJuTSC1Z41wvz26l+HBJCXEBrPh3LXjkJd4CT6n1E= github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20221117232719-cf68648bf146/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo= github.com/ledgerwatch/log/v3 v3.6.0 h1:JBUSK1epPyutUrz7KYDTcJtQLEHnehECRpKbM1ugy5M=