Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
8711e2b6
Commit
8711e2b6
authored
6 years ago
by
b00ris
Committed by
Guillaume Ballet
6 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
whisper: add light mode check to handshake (#16725)
parent
cf33d8b8
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
126 additions
and
18 deletions
+126
-18
config.go
cmd/geth/config.go
+3
-0
main.go
cmd/geth/main.go
+1
-0
flags.go
cmd/utils/flags.go
+7
-0
api.go
whisper/whisperv6/api.go
+4
-4
config.go
whisper/whisperv6/config.go
+6
-4
peer.go
whisper/whisperv6/peer.go
+9
-1
peer_test.go
whisper/whisperv6/peer_test.go
+61
-0
whisper.go
whisper/whisperv6/whisper.go
+35
-9
No files found.
cmd/geth/config.go
View file @
8711e2b6
...
...
@@ -168,6 +168,9 @@ func makeFullNode(ctx *cli.Context) *node.Node {
if
ctx
.
GlobalIsSet
(
utils
.
WhisperMinPOWFlag
.
Name
)
{
cfg
.
Shh
.
MinimumAcceptedPOW
=
ctx
.
Float64
(
utils
.
WhisperMinPOWFlag
.
Name
)
}
if
ctx
.
GlobalIsSet
(
utils
.
WhisperRestrictConnectionBetweenLightClientsFlag
.
Name
)
{
cfg
.
Shh
.
RestrictConnectionBetweenLightClients
=
true
}
utils
.
RegisterShhService
(
stack
,
&
cfg
.
Shh
)
}
...
...
This diff is collapsed.
Click to expand it.
cmd/geth/main.go
View file @
8711e2b6
...
...
@@ -151,6 +151,7 @@ var (
utils
.
WhisperEnabledFlag
,
utils
.
WhisperMaxMessageSizeFlag
,
utils
.
WhisperMinPOWFlag
,
utils
.
WhisperRestrictConnectionBetweenLightClientsFlag
,
}
metricsFlags
=
[]
cli
.
Flag
{
...
...
This diff is collapsed.
Click to expand it.
cmd/utils/flags.go
View file @
8711e2b6
...
...
@@ -567,6 +567,10 @@ var (
Usage
:
"Minimum POW accepted"
,
Value
:
whisper
.
DefaultMinimumPoW
,
}
WhisperRestrictConnectionBetweenLightClientsFlag
=
cli
.
BoolFlag
{
Name
:
"shh.restrict-light"
,
Usage
:
"Restrict connection between two whisper light clients"
,
}
// Metrics flags
MetricsEnabledFlag
=
cli
.
BoolFlag
{
...
...
@@ -1099,6 +1103,9 @@ func SetShhConfig(ctx *cli.Context, stack *node.Node, cfg *whisper.Config) {
if
ctx
.
GlobalIsSet
(
WhisperMinPOWFlag
.
Name
)
{
cfg
.
MinimumAcceptedPOW
=
ctx
.
GlobalFloat64
(
WhisperMinPOWFlag
.
Name
)
}
if
ctx
.
GlobalIsSet
(
WhisperRestrictConnectionBetweenLightClientsFlag
.
Name
)
{
cfg
.
RestrictConnectionBetweenLightClients
=
true
}
}
// SetEthConfig applies eth-related command line flags to the config.
...
...
This diff is collapsed.
Click to expand it.
whisper/whisperv6/api.go
View file @
8711e2b6
...
...
@@ -195,14 +195,14 @@ func (api *PublicWhisperAPI) DeleteSymKey(ctx context.Context, id string) bool {
// MakeLightClient turns the node into light client, which does not forward
// any incoming messages, and sends only messages originated in this node.
func
(
api
*
PublicWhisperAPI
)
MakeLightClient
(
ctx
context
.
Context
)
bool
{
api
.
w
.
lightClient
=
true
return
api
.
w
.
lightClient
api
.
w
.
SetLightClientMode
(
true
)
return
api
.
w
.
LightClientMode
()
}
// CancelLightClient cancels light client mode.
func
(
api
*
PublicWhisperAPI
)
CancelLightClient
(
ctx
context
.
Context
)
bool
{
api
.
w
.
lightClient
=
false
return
!
api
.
w
.
lightClient
api
.
w
.
SetLightClientMode
(
false
)
return
!
api
.
w
.
LightClientMode
()
}
//go:generate gencodec -type NewMessage -field-override newMessageOverride -out gen_newmessage_json.go
...
...
This diff is collapsed.
Click to expand it.
whisper/whisperv6/config.go
View file @
8711e2b6
...
...
@@ -18,12 +18,14 @@ package whisperv6
// Config represents the configuration state of a whisper node.
type
Config
struct
{
MaxMessageSize
uint32
`toml:",omitempty"`
MinimumAcceptedPOW
float64
`toml:",omitempty"`
MaxMessageSize
uint32
`toml:",omitempty"`
MinimumAcceptedPOW
float64
`toml:",omitempty"`
RestrictConnectionBetweenLightClients
bool
`toml:",omitempty"`
}
// DefaultConfig represents (shocker!) the default configuration.
var
DefaultConfig
=
Config
{
MaxMessageSize
:
DefaultMaxMessageSize
,
MinimumAcceptedPOW
:
DefaultMinimumPoW
,
MaxMessageSize
:
DefaultMaxMessageSize
,
MinimumAcceptedPOW
:
DefaultMinimumPoW
,
RestrictConnectionBetweenLightClients
:
true
,
}
This diff is collapsed.
Click to expand it.
whisper/whisperv6/peer.go
View file @
8711e2b6
...
...
@@ -79,11 +79,14 @@ func (peer *Peer) stop() {
func
(
peer
*
Peer
)
handshake
()
error
{
// Send the handshake status message asynchronously
errc
:=
make
(
chan
error
,
1
)
isLightNode
:=
peer
.
host
.
LightClientMode
()
isRestrictedLightNodeConnection
:=
peer
.
host
.
LightClientModeConnectionRestricted
()
go
func
()
{
pow
:=
peer
.
host
.
MinPow
()
powConverted
:=
math
.
Float64bits
(
pow
)
bloom
:=
peer
.
host
.
BloomFilter
()
errc
<-
p2p
.
SendItems
(
peer
.
ws
,
statusCode
,
ProtocolVersion
,
powConverted
,
bloom
)
errc
<-
p2p
.
SendItems
(
peer
.
ws
,
statusCode
,
ProtocolVersion
,
powConverted
,
bloom
,
isLightNode
)
}()
// Fetch the remote status packet and verify protocol match
...
...
@@ -127,6 +130,11 @@ func (peer *Peer) handshake() error {
}
}
isRemotePeerLightNode
,
err
:=
s
.
Bool
()
if
isRemotePeerLightNode
&&
isLightNode
&&
isRestrictedLightNodeConnection
{
return
fmt
.
Errorf
(
"peer [%x] is useless: two light client communication restricted"
,
peer
.
ID
())
}
if
err
:=
<-
errc
;
err
!=
nil
{
return
fmt
.
Errorf
(
"peer [%x] failed to send status packet: %v"
,
peer
.
ID
(),
err
)
}
...
...
This diff is collapsed.
Click to expand it.
whisper/whisperv6/peer_test.go
View file @
8711e2b6
...
...
@@ -34,6 +34,7 @@ import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/rlp"
)
var
keys
=
[]
string
{
...
...
@@ -507,3 +508,63 @@ func waitForServersToStart(t *testing.T) {
}
t
.
Fatalf
(
"Failed to start all the servers, running: %d"
,
started
)
}
//two generic whisper node handshake
func
TestPeerHandshakeWithTwoFullNode
(
t
*
testing
.
T
)
{
w1
:=
Whisper
{}
p1
:=
newPeer
(
&
w1
,
p2p
.
NewPeer
(
discover
.
NodeID
{},
"test"
,
[]
p2p
.
Cap
{}),
&
rwStub
{[]
interface
{}{
ProtocolVersion
,
uint64
(
123
),
make
([]
byte
,
BloomFilterSize
),
false
}})
err
:=
p1
.
handshake
()
if
err
!=
nil
{
t
.
Fatal
()
}
}
//two generic whisper node handshake. one don't send light flag
func
TestHandshakeWithOldVersionWithoutLightModeFlag
(
t
*
testing
.
T
)
{
w1
:=
Whisper
{}
p1
:=
newPeer
(
&
w1
,
p2p
.
NewPeer
(
discover
.
NodeID
{},
"test"
,
[]
p2p
.
Cap
{}),
&
rwStub
{[]
interface
{}{
ProtocolVersion
,
uint64
(
123
),
make
([]
byte
,
BloomFilterSize
)}})
err
:=
p1
.
handshake
()
if
err
!=
nil
{
t
.
Fatal
()
}
}
//two light nodes handshake. restriction disabled
func
TestTwoLightPeerHandshakeRestrictionOff
(
t
*
testing
.
T
)
{
w1
:=
Whisper
{}
w1
.
settings
.
Store
(
restrictConnectionBetweenLightClientsIdx
,
false
)
w1
.
SetLightClientMode
(
true
)
p1
:=
newPeer
(
&
w1
,
p2p
.
NewPeer
(
discover
.
NodeID
{},
"test"
,
[]
p2p
.
Cap
{}),
&
rwStub
{[]
interface
{}{
ProtocolVersion
,
uint64
(
123
),
make
([]
byte
,
BloomFilterSize
),
true
}})
err
:=
p1
.
handshake
()
if
err
!=
nil
{
t
.
FailNow
()
}
}
//two light nodes handshake. restriction enabled
func
TestTwoLightPeerHandshakeError
(
t
*
testing
.
T
)
{
w1
:=
Whisper
{}
w1
.
settings
.
Store
(
restrictConnectionBetweenLightClientsIdx
,
true
)
w1
.
SetLightClientMode
(
true
)
p1
:=
newPeer
(
&
w1
,
p2p
.
NewPeer
(
discover
.
NodeID
{},
"test"
,
[]
p2p
.
Cap
{}),
&
rwStub
{[]
interface
{}{
ProtocolVersion
,
uint64
(
123
),
make
([]
byte
,
BloomFilterSize
),
true
}})
err
:=
p1
.
handshake
()
if
err
==
nil
{
t
.
FailNow
()
}
}
type
rwStub
struct
{
payload
[]
interface
{}
}
func
(
stub
*
rwStub
)
ReadMsg
()
(
p2p
.
Msg
,
error
)
{
size
,
r
,
err
:=
rlp
.
EncodeToReader
(
stub
.
payload
)
if
err
!=
nil
{
return
p2p
.
Msg
{},
err
}
return
p2p
.
Msg
{
Code
:
statusCode
,
Size
:
uint32
(
size
),
Payload
:
r
},
nil
}
func
(
stub
*
rwStub
)
WriteMsg
(
m
p2p
.
Msg
)
error
{
return
nil
}
This diff is collapsed.
Click to expand it.
whisper/whisperv6/whisper.go
View file @
8711e2b6
...
...
@@ -49,12 +49,14 @@ type Statistics struct {
}
const
(
maxMsgSizeIdx
=
iota
// Maximal message length allowed by the whisper node
overflowIdx
// Indicator of message queue overflow
minPowIdx
// Minimal PoW required by the whisper node
minPowToleranceIdx
// Minimal PoW tolerated by the whisper node for a limited time
bloomFilterIdx
// Bloom filter for topics of interest for this node
bloomFilterToleranceIdx
// Bloom filter tolerated by the whisper node for a limited time
maxMsgSizeIdx
=
iota
// Maximal message length allowed by the whisper node
overflowIdx
// Indicator of message queue overflow
minPowIdx
// Minimal PoW required by the whisper node
minPowToleranceIdx
// Minimal PoW tolerated by the whisper node for a limited time
bloomFilterIdx
// Bloom filter for topics of interest for this node
bloomFilterToleranceIdx
// Bloom filter tolerated by the whisper node for a limited time
lightClientModeIdx
// Light client mode. (does not forward any messages)
restrictConnectionBetweenLightClientsIdx
// Restrict connection between two light clients
)
// Whisper represents a dark communication interface through the Ethereum
...
...
@@ -82,8 +84,6 @@ type Whisper struct {
syncAllowance
int
// maximum time in seconds allowed to process the whisper-related messages
lightClient
bool
// indicates is this node is pure light client (does not forward any messages)
statsMu
sync
.
Mutex
// guard stats
stats
Statistics
// Statistics of whisper node
...
...
@@ -113,6 +113,7 @@ func New(cfg *Config) *Whisper {
whisper
.
settings
.
Store
(
minPowIdx
,
cfg
.
MinimumAcceptedPOW
)
whisper
.
settings
.
Store
(
maxMsgSizeIdx
,
cfg
.
MaxMessageSize
)
whisper
.
settings
.
Store
(
overflowIdx
,
false
)
whisper
.
settings
.
Store
(
restrictConnectionBetweenLightClientsIdx
,
cfg
.
RestrictConnectionBetweenLightClients
)
// p2p whisper sub protocol handler
whisper
.
protocol
=
p2p
.
Protocol
{
...
...
@@ -276,6 +277,31 @@ func (whisper *Whisper) SetMinimumPowTest(val float64) {
whisper
.
settings
.
Store
(
minPowToleranceIdx
,
val
)
}
//SetLightClientMode makes node light client (does not forward any messages)
func
(
whisper
*
Whisper
)
SetLightClientMode
(
v
bool
)
{
whisper
.
settings
.
Store
(
lightClientModeIdx
,
v
)
}
//LightClientMode indicates is this node is light client (does not forward any messages)
func
(
whisper
*
Whisper
)
LightClientMode
()
bool
{
val
,
exist
:=
whisper
.
settings
.
Load
(
lightClientModeIdx
)
if
!
exist
||
val
==
nil
{
return
false
}
v
,
ok
:=
val
.
(
bool
)
return
v
&&
ok
}
//LightClientModeConnectionRestricted indicates that connection to light client in light client mode not allowed
func
(
whisper
*
Whisper
)
LightClientModeConnectionRestricted
()
bool
{
val
,
exist
:=
whisper
.
settings
.
Load
(
restrictConnectionBetweenLightClientsIdx
)
if
!
exist
||
val
==
nil
{
return
false
}
v
,
ok
:=
val
.
(
bool
)
return
v
&&
ok
}
func
(
whisper
*
Whisper
)
notifyPeersAboutPowRequirementChange
(
pow
float64
)
{
arr
:=
whisper
.
getPeers
()
for
_
,
p
:=
range
arr
{
...
...
@@ -672,7 +698,7 @@ func (whisper *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
trouble
:=
false
for
_
,
env
:=
range
envelopes
{
cached
,
err
:=
whisper
.
add
(
env
,
whisper
.
lightClient
)
cached
,
err
:=
whisper
.
add
(
env
,
whisper
.
LightClientMode
()
)
if
err
!=
nil
{
trouble
=
true
log
.
Error
(
"bad envelope received, peer will be disconnected"
,
"peer"
,
p
.
peer
.
ID
(),
"err"
,
err
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment