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
197d609b
Commit
197d609b
authored
Nov 26, 2018
by
lash
Committed by
Anton Evangelatov
Nov 26, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
swarm/pss: Message handler refactor (#18169)
parent
ca228569
Changes
10
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
644 additions
and
109 deletions
+644
-109
kademlia.go
swarm/network/kademlia.go
+17
-9
api.go
swarm/pss/api.go
+9
-3
client.go
swarm/pss/client/client.go
+1
-1
handshake.go
swarm/pss/handshake.go
+1
-1
notify.go
swarm/pss/notify/notify.go
+2
-2
notify_test.go
swarm/pss/notify/notify_test.go
+2
-2
protocol_test.go
swarm/pss/protocol_test.go
+3
-2
pss.go
swarm/pss/pss.go
+131
-40
pss_test.go
swarm/pss/pss_test.go
+446
-47
types.go
swarm/pss/types.go
+32
-2
No files found.
swarm/network/kademlia.go
View file @
197d609b
...
...
@@ -81,14 +81,15 @@ func NewKadParams() *KadParams {
// Kademlia is a table of live peers and a db of known peers (node records)
type
Kademlia
struct
{
lock
sync
.
RWMutex
*
KadParams
// Kademlia configuration parameters
base
[]
byte
// immutable baseaddress of the table
addrs
*
pot
.
Pot
// pots container for known peer addresses
conns
*
pot
.
Pot
// pots container for live peer connections
depth
uint8
// stores the last current depth of saturation
nDepth
int
// stores the last neighbourhood depth
nDepthC
chan
int
// returned by DepthC function to signal neighbourhood depth change
addrCountC
chan
int
// returned by AddrCountC function to signal peer count change
*
KadParams
// Kademlia configuration parameters
base
[]
byte
// immutable baseaddress of the table
addrs
*
pot
.
Pot
// pots container for known peer addresses
conns
*
pot
.
Pot
// pots container for live peer connections
depth
uint8
// stores the last current depth of saturation
nDepth
int
// stores the last neighbourhood depth
nDepthC
chan
int
// returned by DepthC function to signal neighbourhood depth change
addrCountC
chan
int
// returned by AddrCountC function to signal peer count change
Pof
func
(
pot
.
Val
,
pot
.
Val
,
int
)
(
int
,
bool
)
// function for calculating kademlia routing distance between two addresses
}
// NewKademlia creates a Kademlia table for base address addr
...
...
@@ -103,6 +104,7 @@ func NewKademlia(addr []byte, params *KadParams) *Kademlia {
KadParams
:
params
,
addrs
:
pot
.
NewPot
(
nil
,
0
),
conns
:
pot
.
NewPot
(
nil
,
0
),
Pof
:
pof
,
}
}
...
...
@@ -289,6 +291,7 @@ func (k *Kademlia) On(p *Peer) (uint8, bool) {
// neighbourhood depth on each change.
// Not receiving from the returned channel will block On function
// when the neighbourhood depth is changed.
// TODO: Why is this exported, and if it should be; why can't we have more subscribers than one?
func
(
k
*
Kademlia
)
NeighbourhoodDepthC
()
<-
chan
int
{
k
.
lock
.
Lock
()
defer
k
.
lock
.
Unlock
()
...
...
@@ -429,7 +432,12 @@ func (k *Kademlia) eachAddr(base []byte, o int, f func(*BzzAddr, int, bool) bool
// neighbourhoodDepth returns the proximity order that defines the distance of
// the nearest neighbour set with cardinality >= MinProxBinSize
// if there is altogether less than MinProxBinSize peers it returns 0
// caller must hold the lock
func
(
k
*
Kademlia
)
NeighbourhoodDepth
()
(
depth
int
)
{
k
.
lock
.
RLock
()
defer
k
.
lock
.
RUnlock
()
return
k
.
neighbourhoodDepth
()
}
func
(
k
*
Kademlia
)
neighbourhoodDepth
()
(
depth
int
)
{
if
k
.
conns
.
Size
()
<
k
.
MinProxBinSize
{
return
0
...
...
swarm/pss/api.go
View file @
197d609b
...
...
@@ -51,7 +51,7 @@ func NewAPI(ps *Pss) *API {
//
// All incoming messages to the node matching this topic will be encapsulated in the APIMsg
// struct and sent to the subscriber
func
(
pssapi
*
API
)
Receive
(
ctx
context
.
Context
,
topic
Topic
)
(
*
rpc
.
Subscription
,
error
)
{
func
(
pssapi
*
API
)
Receive
(
ctx
context
.
Context
,
topic
Topic
,
raw
bool
,
prox
bool
)
(
*
rpc
.
Subscription
,
error
)
{
notifier
,
supported
:=
rpc
.
NotifierFromContext
(
ctx
)
if
!
supported
{
return
nil
,
fmt
.
Errorf
(
"Subscribe not supported"
)
...
...
@@ -59,7 +59,7 @@ func (pssapi *API) Receive(ctx context.Context, topic Topic) (*rpc.Subscription,
psssub
:=
notifier
.
CreateSubscription
()
h
andler
:=
func
(
msg
[]
byte
,
p
*
p2p
.
Peer
,
asymmetric
bool
,
keyid
string
)
error
{
h
ndlr
:=
NewHandler
(
func
(
msg
[]
byte
,
p
*
p2p
.
Peer
,
asymmetric
bool
,
keyid
string
)
error
{
apimsg
:=
&
APIMsg
{
Msg
:
hexutil
.
Bytes
(
msg
),
Asymmetric
:
asymmetric
,
...
...
@@ -69,9 +69,15 @@ func (pssapi *API) Receive(ctx context.Context, topic Topic) (*rpc.Subscription,
log
.
Warn
(
fmt
.
Sprintf
(
"notification on pss sub topic rpc (sub %v) msg %v failed!"
,
psssub
.
ID
,
msg
))
}
return
nil
})
if
raw
{
hndlr
.
caps
.
raw
=
true
}
if
prox
{
hndlr
.
caps
.
prox
=
true
}
deregf
:=
pssapi
.
Register
(
&
topic
,
h
andle
r
)
deregf
:=
pssapi
.
Register
(
&
topic
,
h
ndl
r
)
go
func
()
{
defer
deregf
()
select
{
...
...
swarm/pss/client/client.go
View file @
197d609b
...
...
@@ -236,7 +236,7 @@ func (c *Client) RunProtocol(ctx context.Context, proto *p2p.Protocol) error {
topichex
:=
topicobj
.
String
()
msgC
:=
make
(
chan
pss
.
APIMsg
)
c
.
peerPool
[
topicobj
]
=
make
(
map
[
string
]
*
pssRPCRW
)
sub
,
err
:=
c
.
rpc
.
Subscribe
(
ctx
,
"pss"
,
msgC
,
"receive"
,
topichex
)
sub
,
err
:=
c
.
rpc
.
Subscribe
(
ctx
,
"pss"
,
msgC
,
"receive"
,
topichex
,
false
,
false
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"pss event subscription failed: %v"
,
err
)
}
...
...
swarm/pss/handshake.go
View file @
197d609b
...
...
@@ -486,7 +486,7 @@ func (api *HandshakeAPI) Handshake(pubkeyid string, topic Topic, sync bool, flus
// Activate handshake functionality on a topic
func
(
api
*
HandshakeAPI
)
AddHandshake
(
topic
Topic
)
error
{
api
.
ctrl
.
deregisterFuncs
[
topic
]
=
api
.
ctrl
.
pss
.
Register
(
&
topic
,
api
.
ctrl
.
handler
)
api
.
ctrl
.
deregisterFuncs
[
topic
]
=
api
.
ctrl
.
pss
.
Register
(
&
topic
,
NewHandler
(
api
.
ctrl
.
handler
)
)
return
nil
}
...
...
swarm/pss/notify/notify.go
View file @
197d609b
...
...
@@ -113,7 +113,7 @@ func NewController(ps *pss.Pss) *Controller {
notifiers
:
make
(
map
[
string
]
*
notifier
),
subscriptions
:
make
(
map
[
string
]
*
subscription
),
}
ctrl
.
pss
.
Register
(
&
controlTopic
,
ctrl
.
Handler
)
ctrl
.
pss
.
Register
(
&
controlTopic
,
pss
.
NewHandler
(
ctrl
.
Handler
)
)
return
ctrl
}
...
...
@@ -336,7 +336,7 @@ func (c *Controller) handleNotifyWithKeyMsg(msg *Msg) error {
// \TODO keep track of and add actual address
updaterAddr
:=
pss
.
PssAddress
([]
byte
{})
c
.
pss
.
SetSymmetricKey
(
symkey
,
topic
,
&
updaterAddr
,
true
)
c
.
pss
.
Register
(
&
topic
,
c
.
Handler
)
c
.
pss
.
Register
(
&
topic
,
pss
.
NewHandler
(
c
.
Handler
)
)
return
c
.
subscriptions
[
msg
.
namestring
]
.
handler
(
msg
.
namestring
,
msg
.
Payload
[
:
len
(
msg
.
Payload
)
-
symKeyLength
])
}
...
...
swarm/pss/notify/notify_test.go
View file @
197d609b
...
...
@@ -121,7 +121,7 @@ func TestStart(t *testing.T) {
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
2
)
defer
cancel
()
rmsgC
:=
make
(
chan
*
pss
.
APIMsg
)
rightSub
,
err
:=
rightRpc
.
Subscribe
(
ctx
,
"pss"
,
rmsgC
,
"receive"
,
controlTopic
)
rightSub
,
err
:=
rightRpc
.
Subscribe
(
ctx
,
"pss"
,
rmsgC
,
"receive"
,
controlTopic
,
false
,
false
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -174,7 +174,7 @@ func TestStart(t *testing.T) {
t
.
Fatalf
(
"expected payload length %d, have %d"
,
len
(
updateMsg
)
+
symKeyLength
,
len
(
dMsg
.
Payload
))
}
rightSubUpdate
,
err
:=
rightRpc
.
Subscribe
(
ctx
,
"pss"
,
rmsgC
,
"receive"
,
rsrcTopic
)
rightSubUpdate
,
err
:=
rightRpc
.
Subscribe
(
ctx
,
"pss"
,
rmsgC
,
"receive"
,
rsrcTopic
,
false
,
false
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
swarm/pss/protocol_test.go
View file @
197d609b
...
...
@@ -92,7 +92,7 @@ func testProtocol(t *testing.T) {
lmsgC
:=
make
(
chan
APIMsg
)
lctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
10
)
defer
cancel
()
lsub
,
err
:=
clients
[
0
]
.
Subscribe
(
lctx
,
"pss"
,
lmsgC
,
"receive"
,
topic
)
lsub
,
err
:=
clients
[
0
]
.
Subscribe
(
lctx
,
"pss"
,
lmsgC
,
"receive"
,
topic
,
false
,
false
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -100,7 +100,7 @@ func testProtocol(t *testing.T) {
rmsgC
:=
make
(
chan
APIMsg
)
rctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
10
)
defer
cancel
()
rsub
,
err
:=
clients
[
1
]
.
Subscribe
(
rctx
,
"pss"
,
rmsgC
,
"receive"
,
topic
)
rsub
,
err
:=
clients
[
1
]
.
Subscribe
(
rctx
,
"pss"
,
rmsgC
,
"receive"
,
topic
,
false
,
false
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -130,6 +130,7 @@ func testProtocol(t *testing.T) {
log
.
Debug
(
"lnode ok"
)
case
cerr
:=
<-
lctx
.
Done
()
:
t
.
Fatalf
(
"test message timed out: %v"
,
cerr
)
return
}
select
{
case
<-
rmsgC
:
...
...
swarm/pss/pss.go
View file @
197d609b
This diff is collapsed.
Click to expand it.
swarm/pss/pss_test.go
View file @
197d609b
This diff is collapsed.
Click to expand it.
swarm/pss/types.go
View file @
197d609b
...
...
@@ -159,9 +159,39 @@ func (msg *PssMsg) String() string {
}
// Signature for a message handler function for a PssMsg
//
// Implementations of this type are passed to Pss.Register together with a topic,
type
Handler
func
(
msg
[]
byte
,
p
*
p2p
.
Peer
,
asymmetric
bool
,
keyid
string
)
error
type
HandlerFunc
func
(
msg
[]
byte
,
p
*
p2p
.
Peer
,
asymmetric
bool
,
keyid
string
)
error
type
handlerCaps
struct
{
raw
bool
prox
bool
}
// Handler defines code to be executed upon reception of content.
type
handler
struct
{
f
HandlerFunc
caps
*
handlerCaps
}
// NewHandler returns a new message handler
func
NewHandler
(
f
HandlerFunc
)
*
handler
{
return
&
handler
{
f
:
f
,
caps
:
&
handlerCaps
{},
}
}
// WithRaw is a chainable method that allows raw messages to be handled.
func
(
h
*
handler
)
WithRaw
()
*
handler
{
h
.
caps
.
raw
=
true
return
h
}
// WithProxBin is a chainable method that allows sending messages with full addresses to neighbourhoods using the kademlia depth as reference
func
(
h
*
handler
)
WithProxBin
()
*
handler
{
h
.
caps
.
prox
=
true
return
h
}
// the stateStore handles saving and loading PSS peers and their corresponding keys
// it is currently unimplemented
...
...
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