Skip to content

Commit 8711e2b

Browse files
b00risgballet
authored andcommitted
whisper: add light mode check to handshake (#16725)
1 parent cf33d8b commit 8711e2b

8 files changed

Lines changed: 126 additions & 18 deletions

File tree

‎cmd/geth/config.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ func makeFullNode(ctx *cli.Context) *node.Node {
168168
if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
169169
cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
170170
}
171+
if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
172+
cfg.Shh.RestrictConnectionBetweenLightClients = true
173+
}
171174
utils.RegisterShhService(stack, &cfg.Shh)
172175
}
173176

‎cmd/geth/main.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ var (
151151
utils.WhisperEnabledFlag,
152152
utils.WhisperMaxMessageSizeFlag,
153153
utils.WhisperMinPOWFlag,
154+
utils.WhisperRestrictConnectionBetweenLightClientsFlag,
154155
}
155156

156157
metricsFlags = []cli.Flag{

‎cmd/utils/flags.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ var (
567567
Usage: "Minimum POW accepted",
568568
Value: whisper.DefaultMinimumPoW,
569569
}
570+
WhisperRestrictConnectionBetweenLightClientsFlag = cli.BoolFlag{
571+
Name: "shh.restrict-light",
572+
Usage: "Restrict connection between two whisper light clients",
573+
}
570574

571575
// Metrics flags
572576
MetricsEnabledFlag = cli.BoolFlag{
@@ -1099,6 +1103,9 @@ func SetShhConfig(ctx *cli.Context, stack *node.Node, cfg *whisper.Config) {
10991103
if ctx.GlobalIsSet(WhisperMinPOWFlag.Name) {
11001104
cfg.MinimumAcceptedPOW = ctx.GlobalFloat64(WhisperMinPOWFlag.Name)
11011105
}
1106+
if ctx.GlobalIsSet(WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
1107+
cfg.RestrictConnectionBetweenLightClients = true
1108+
}
11021109
}
11031110

11041111
// SetEthConfig applies eth-related command line flags to the config.

‎whisper/whisperv6/api.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ func (api *PublicWhisperAPI) DeleteSymKey(ctx context.Context, id string) bool {
195195
// MakeLightClient turns the node into light client, which does not forward
196196
// any incoming messages, and sends only messages originated in this node.
197197
func (api *PublicWhisperAPI) MakeLightClient(ctx context.Context) bool {
198-
api.w.lightClient = true
199-
return api.w.lightClient
198+
api.w.SetLightClientMode(true)
199+
return api.w.LightClientMode()
200200
}
201201

202202
// CancelLightClient cancels light client mode.
203203
func (api *PublicWhisperAPI) CancelLightClient(ctx context.Context) bool {
204-
api.w.lightClient = false
205-
return !api.w.lightClient
204+
api.w.SetLightClientMode(false)
205+
return !api.w.LightClientMode()
206206
}
207207

208208
//go:generate gencodec -type NewMessage -field-override newMessageOverride -out gen_newmessage_json.go

‎whisper/whisperv6/config.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ package whisperv6
1818

1919
// Config represents the configuration state of a whisper node.
2020
type Config struct {
21-
MaxMessageSize uint32 `toml:",omitempty"`
22-
MinimumAcceptedPOW float64 `toml:",omitempty"`
21+
MaxMessageSize uint32 `toml:",omitempty"`
22+
MinimumAcceptedPOW float64 `toml:",omitempty"`
23+
RestrictConnectionBetweenLightClients bool `toml:",omitempty"`
2324
}
2425

2526
// DefaultConfig represents (shocker!) the default configuration.
2627
var DefaultConfig = Config{
27-
MaxMessageSize: DefaultMaxMessageSize,
28-
MinimumAcceptedPOW: DefaultMinimumPoW,
28+
MaxMessageSize: DefaultMaxMessageSize,
29+
MinimumAcceptedPOW: DefaultMinimumPoW,
30+
RestrictConnectionBetweenLightClients: true,
2931
}

‎whisper/whisperv6/peer.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ func (peer *Peer) stop() {
7979
func (peer *Peer) handshake() error {
8080
// Send the handshake status message asynchronously
8181
errc := make(chan error, 1)
82+
isLightNode := peer.host.LightClientMode()
83+
isRestrictedLightNodeConnection := peer.host.LightClientModeConnectionRestricted()
8284
go func() {
8385
pow := peer.host.MinPow()
8486
powConverted := math.Float64bits(pow)
8587
bloom := peer.host.BloomFilter()
86-
errc <- p2p.SendItems(peer.ws, statusCode, ProtocolVersion, powConverted, bloom)
88+
89+
errc <- p2p.SendItems(peer.ws, statusCode, ProtocolVersion, powConverted, bloom, isLightNode)
8790
}()
8891

8992
// Fetch the remote status packet and verify protocol match
@@ -127,6 +130,11 @@ func (peer *Peer) handshake() error {
127130
}
128131
}
129132

133+
isRemotePeerLightNode, err := s.Bool()
134+
if isRemotePeerLightNode && isLightNode && isRestrictedLightNodeConnection {
135+
return fmt.Errorf("peer [%x] is useless: two light client communication restricted", peer.ID())
136+
}
137+
130138
if err := <-errc; err != nil {
131139
return fmt.Errorf("peer [%x] failed to send status packet: %v", peer.ID(), err)
132140
}

‎whisper/whisperv6/peer_test.go‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/ethereum/go-ethereum/p2p"
3535
"github.com/ethereum/go-ethereum/p2p/discover"
3636
"github.com/ethereum/go-ethereum/p2p/nat"
37+
"github.com/ethereum/go-ethereum/rlp"
3738
)
3839

3940
var keys = []string{
@@ -507,3 +508,63 @@ func waitForServersToStart(t *testing.T) {
507508
}
508509
t.Fatalf("Failed to start all the servers, running: %d", started)
509510
}
511+
512+
//two generic whisper node handshake
513+
func TestPeerHandshakeWithTwoFullNode(t *testing.T) {
514+
w1 := Whisper{}
515+
p1 := newPeer(&w1, p2p.NewPeer(discover.NodeID{}, "test", []p2p.Cap{}), &rwStub{[]interface{}{ProtocolVersion, uint64(123), make([]byte, BloomFilterSize), false}})
516+
err := p1.handshake()
517+
if err != nil {
518+
t.Fatal()
519+
}
520+
}
521+
522+
//two generic whisper node handshake. one don't send light flag
523+
func TestHandshakeWithOldVersionWithoutLightModeFlag(t *testing.T) {
524+
w1 := Whisper{}
525+
p1 := newPeer(&w1, p2p.NewPeer(discover.NodeID{}, "test", []p2p.Cap{}), &rwStub{[]interface{}{ProtocolVersion, uint64(123), make([]byte, BloomFilterSize)}})
526+
err := p1.handshake()
527+
if err != nil {
528+
t.Fatal()
529+
}
530+
}
531+
532+
//two light nodes handshake. restriction disabled
533+
func TestTwoLightPeerHandshakeRestrictionOff(t *testing.T) {
534+
w1 := Whisper{}
535+
w1.settings.Store(restrictConnectionBetweenLightClientsIdx, false)
536+
w1.SetLightClientMode(true)
537+
p1 := newPeer(&w1, p2p.NewPeer(discover.NodeID{}, "test", []p2p.Cap{}), &rwStub{[]interface{}{ProtocolVersion, uint64(123), make([]byte, BloomFilterSize), true}})
538+
err := p1.handshake()
539+
if err != nil {
540+
t.FailNow()
541+
}
542+
}
543+
544+
//two light nodes handshake. restriction enabled
545+
func TestTwoLightPeerHandshakeError(t *testing.T) {
546+
w1 := Whisper{}
547+
w1.settings.Store(restrictConnectionBetweenLightClientsIdx, true)
548+
w1.SetLightClientMode(true)
549+
p1 := newPeer(&w1, p2p.NewPeer(discover.NodeID{}, "test", []p2p.Cap{}), &rwStub{[]interface{}{ProtocolVersion, uint64(123), make([]byte, BloomFilterSize), true}})
550+
err := p1.handshake()
551+
if err == nil {
552+
t.FailNow()
553+
}
554+
}
555+
556+
type rwStub struct {
557+
payload []interface{}
558+
}
559+
560+
func (stub *rwStub) ReadMsg() (p2p.Msg, error) {
561+
size, r, err := rlp.EncodeToReader(stub.payload)
562+
if err != nil {
563+
return p2p.Msg{}, err
564+
}
565+
return p2p.Msg{Code: statusCode, Size: uint32(size), Payload: r}, nil
566+
}
567+
568+
func (stub *rwStub) WriteMsg(m p2p.Msg) error {
569+
return nil
570+
}

‎whisper/whisperv6/whisper.go‎

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ type Statistics struct {
4949
}
5050

5151
const (
52-
maxMsgSizeIdx = iota // Maximal message length allowed by the whisper node
53-
overflowIdx // Indicator of message queue overflow
54-
minPowIdx // Minimal PoW required by the whisper node
55-
minPowToleranceIdx // Minimal PoW tolerated by the whisper node for a limited time
56-
bloomFilterIdx // Bloom filter for topics of interest for this node
57-
bloomFilterToleranceIdx // Bloom filter tolerated by the whisper node for a limited time
52+
maxMsgSizeIdx = iota // Maximal message length allowed by the whisper node
53+
overflowIdx // Indicator of message queue overflow
54+
minPowIdx // Minimal PoW required by the whisper node
55+
minPowToleranceIdx // Minimal PoW tolerated by the whisper node for a limited time
56+
bloomFilterIdx // Bloom filter for topics of interest for this node
57+
bloomFilterToleranceIdx // Bloom filter tolerated by the whisper node for a limited time
58+
lightClientModeIdx // Light client mode. (does not forward any messages)
59+
restrictConnectionBetweenLightClientsIdx // Restrict connection between two light clients
5860
)
5961

6062
// Whisper represents a dark communication interface through the Ethereum
@@ -82,8 +84,6 @@ type Whisper struct {
8284

8385
syncAllowance int // maximum time in seconds allowed to process the whisper-related messages
8486

85-
lightClient bool // indicates is this node is pure light client (does not forward any messages)
86-
8787
statsMu sync.Mutex // guard stats
8888
stats Statistics // Statistics of whisper node
8989

@@ -113,6 +113,7 @@ func New(cfg *Config) *Whisper {
113113
whisper.settings.Store(minPowIdx, cfg.MinimumAcceptedPOW)
114114
whisper.settings.Store(maxMsgSizeIdx, cfg.MaxMessageSize)
115115
whisper.settings.Store(overflowIdx, false)
116+
whisper.settings.Store(restrictConnectionBetweenLightClientsIdx, cfg.RestrictConnectionBetweenLightClients)
116117

117118
// p2p whisper sub protocol handler
118119
whisper.protocol = p2p.Protocol{
@@ -276,6 +277,31 @@ func (whisper *Whisper) SetMinimumPowTest(val float64) {
276277
whisper.settings.Store(minPowToleranceIdx, val)
277278
}
278279

280+
//SetLightClientMode makes node light client (does not forward any messages)
281+
func (whisper *Whisper) SetLightClientMode(v bool) {
282+
whisper.settings.Store(lightClientModeIdx, v)
283+
}
284+
285+
//LightClientMode indicates is this node is light client (does not forward any messages)
286+
func (whisper *Whisper) LightClientMode() bool {
287+
val, exist := whisper.settings.Load(lightClientModeIdx)
288+
if !exist || val == nil {
289+
return false
290+
}
291+
v, ok := val.(bool)
292+
return v && ok
293+
}
294+
295+
//LightClientModeConnectionRestricted indicates that connection to light client in light client mode not allowed
296+
func (whisper *Whisper) LightClientModeConnectionRestricted() bool {
297+
val, exist := whisper.settings.Load(restrictConnectionBetweenLightClientsIdx)
298+
if !exist || val == nil {
299+
return false
300+
}
301+
v, ok := val.(bool)
302+
return v && ok
303+
}
304+
279305
func (whisper *Whisper) notifyPeersAboutPowRequirementChange(pow float64) {
280306
arr := whisper.getPeers()
281307
for _, p := range arr {
@@ -672,7 +698,7 @@ func (whisper *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
672698

673699
trouble := false
674700
for _, env := range envelopes {
675-
cached, err := whisper.add(env, whisper.lightClient)
701+
cached, err := whisper.add(env, whisper.LightClientMode())
676702
if err != nil {
677703
trouble = true
678704
log.Error("bad envelope received, peer will be disconnected", "peer", p.peer.ID(), "err", err)

0 commit comments

Comments
 (0)