mirror of https://github.com/ethereum/go-ethereum
swarm: ctx propagation; bmt fixes; pss generic notification framework (#17150)
* cmd/swarm: minor cli flag text adjustments * swarm/api/http: sticky footer for swarm landing page using flex * swarm/api/http: sticky footer for error pages and fix for multiple choices * cmd/swarm, swarm/storage, swarm: fix mingw on windows test issues * cmd/swarm: update description of swarm cmd * swarm: added network ID test * cmd/swarm: support for smoke tests on the production swarm cluster * cmd/swarm/swarm-smoke: simplify cluster logic as per suggestion * swarm: propagate ctx to internal apis (#754) * swarm/metrics: collect disk measurements * swarm/bmt: fix io.Writer interface * Write now tolerates arbitrary variable buffers * added variable buffer tests * Write loop and finalise optimisation * refactor / rename * add tests for empty input * swarm/pss: (UPDATE) Generic notifications package (#744) swarm/pss: Generic package for creating pss notification svcs * swarm: Adding context to more functions * swarm/api: change colour of landing page in templates * swarm/api: change landing page to react to enter keypresspull/17164/head
parent
30bdf817a0
commit
b3711af051
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,266 @@ |
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package network |
||||
|
||||
import ( |
||||
"bytes" |
||||
"context" |
||||
"flag" |
||||
"fmt" |
||||
"math/rand" |
||||
"strings" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/node" |
||||
"github.com/ethereum/go-ethereum/p2p" |
||||
"github.com/ethereum/go-ethereum/p2p/discover" |
||||
"github.com/ethereum/go-ethereum/p2p/simulations" |
||||
"github.com/ethereum/go-ethereum/p2p/simulations/adapters" |
||||
"github.com/ethereum/go-ethereum/rpc" |
||||
) |
||||
|
||||
var ( |
||||
currentNetworkID int |
||||
cnt int |
||||
nodeMap map[int][]discover.NodeID |
||||
kademlias map[discover.NodeID]*Kademlia |
||||
) |
||||
|
||||
const ( |
||||
NumberOfNets = 4 |
||||
MaxTimeout = 6 |
||||
) |
||||
|
||||
func init() { |
||||
flag.Parse() |
||||
rand.Seed(time.Now().Unix()) |
||||
} |
||||
|
||||
/* |
||||
Run the network ID test. |
||||
The test creates one simulations.Network instance, |
||||
a number of nodes, then connects nodes with each other in this network. |
||||
|
||||
Each node gets a network ID assigned according to the number of networks. |
||||
Having more network IDs is just arbitrary in order to exclude |
||||
false positives. |
||||
|
||||
Nodes should only connect with other nodes with the same network ID. |
||||
After the setup phase, the test checks on each node if it has the |
||||
expected node connections (excluding those not sharing the network ID). |
||||
*/ |
||||
func TestNetworkID(t *testing.T) { |
||||
log.Debug("Start test") |
||||
//arbitrarily set the number of nodes. It could be any number
|
||||
numNodes := 24 |
||||
//the nodeMap maps all nodes (slice value) with the same network ID (key)
|
||||
nodeMap = make(map[int][]discover.NodeID) |
||||
//set up the network and connect nodes
|
||||
net, err := setupNetwork(numNodes) |
||||
if err != nil { |
||||
t.Fatalf("Error setting up network: %v", err) |
||||
} |
||||
defer func() { |
||||
//shutdown the snapshot network
|
||||
log.Trace("Shutting down network") |
||||
net.Shutdown() |
||||
}() |
||||
//let's sleep to ensure all nodes are connected
|
||||
time.Sleep(1 * time.Second) |
||||
//for each group sharing the same network ID...
|
||||
for _, netIDGroup := range nodeMap { |
||||
log.Trace("netIDGroup size", "size", len(netIDGroup)) |
||||
//...check that their size of the kademlia is of the expected size
|
||||
//the assumption is that it should be the size of the group minus 1 (the node itself)
|
||||
for _, node := range netIDGroup { |
||||
if kademlias[node].addrs.Size() != len(netIDGroup)-1 { |
||||
t.Fatalf("Kademlia size has not expected peer size. Kademlia size: %d, expected size: %d", kademlias[node].addrs.Size(), len(netIDGroup)-1) |
||||
} |
||||
kademlias[node].EachAddr(nil, 0, func(addr OverlayAddr, _ int, _ bool) bool { |
||||
found := false |
||||
for _, nd := range netIDGroup { |
||||
p := ToOverlayAddr(nd.Bytes()) |
||||
if bytes.Equal(p, addr.Address()) { |
||||
found = true |
||||
} |
||||
} |
||||
if !found { |
||||
t.Fatalf("Expected node not found for node %s", node.String()) |
||||
} |
||||
return true |
||||
}) |
||||
} |
||||
} |
||||
log.Info("Test terminated successfully") |
||||
} |
||||
|
||||
// setup simulated network with bzz/discovery and pss services.
|
||||
// connects nodes in a circle
|
||||
// if allowRaw is set, omission of builtin pss encryption is enabled (see PssParams)
|
||||
func setupNetwork(numnodes int) (net *simulations.Network, err error) { |
||||
log.Debug("Setting up network") |
||||
quitC := make(chan struct{}) |
||||
errc := make(chan error) |
||||
nodes := make([]*simulations.Node, numnodes) |
||||
if numnodes < 16 { |
||||
return nil, fmt.Errorf("Minimum sixteen nodes in network") |
||||
} |
||||
adapter := adapters.NewSimAdapter(newServices()) |
||||
//create the network
|
||||
net = simulations.NewNetwork(adapter, &simulations.NetworkConfig{ |
||||
ID: "NetworkIdTestNet", |
||||
DefaultService: "bzz", |
||||
}) |
||||
log.Debug("Creating networks and nodes") |
||||
|
||||
var connCount int |
||||
|
||||
//create nodes and connect them to each other
|
||||
for i := 0; i < numnodes; i++ { |
||||
log.Trace("iteration: ", "i", i) |
||||
nodeconf := adapters.RandomNodeConfig() |
||||
nodes[i], err = net.NewNodeWithConfig(nodeconf) |
||||
if err != nil { |
||||
return nil, fmt.Errorf("error creating node %d: %v", i, err) |
||||
} |
||||
err = net.Start(nodes[i].ID()) |
||||
if err != nil { |
||||
return nil, fmt.Errorf("error starting node %d: %v", i, err) |
||||
} |
||||
client, err := nodes[i].Client() |
||||
if err != nil { |
||||
return nil, fmt.Errorf("create node %d rpc client fail: %v", i, err) |
||||
} |
||||
//now setup and start event watching in order to know when we can upload
|
||||
ctx, watchCancel := context.WithTimeout(context.Background(), MaxTimeout*time.Second) |
||||
defer watchCancel() |
||||
watchSubscriptionEvents(ctx, nodes[i].ID(), client, errc, quitC) |
||||
//on every iteration we connect to all previous ones
|
||||
for k := i - 1; k >= 0; k-- { |
||||
connCount++ |
||||
log.Debug(fmt.Sprintf("Connecting node %d with node %d; connection count is %d", i, k, connCount)) |
||||
err = net.Connect(nodes[i].ID(), nodes[k].ID()) |
||||
if err != nil { |
||||
if !strings.Contains(err.Error(), "already connected") { |
||||
return nil, fmt.Errorf("error connecting nodes: %v", err) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
//now wait until the number of expected subscriptions has been finished
|
||||
//`watchSubscriptionEvents` will write with a `nil` value to errc
|
||||
for err := range errc { |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
//`nil` received, decrement count
|
||||
connCount-- |
||||
log.Trace("count down", "cnt", connCount) |
||||
//all subscriptions received
|
||||
if connCount == 0 { |
||||
close(quitC) |
||||
break |
||||
} |
||||
} |
||||
log.Debug("Network setup phase terminated") |
||||
return net, nil |
||||
} |
||||
|
||||
func newServices() adapters.Services { |
||||
kademlias = make(map[discover.NodeID]*Kademlia) |
||||
kademlia := func(id discover.NodeID) *Kademlia { |
||||
if k, ok := kademlias[id]; ok { |
||||
return k |
||||
} |
||||
addr := NewAddrFromNodeID(id) |
||||
params := NewKadParams() |
||||
params.MinProxBinSize = 2 |
||||
params.MaxBinSize = 3 |
||||
params.MinBinSize = 1 |
||||
params.MaxRetries = 1000 |
||||
params.RetryExponent = 2 |
||||
params.RetryInterval = 1000000 |
||||
kademlias[id] = NewKademlia(addr.Over(), params) |
||||
return kademlias[id] |
||||
} |
||||
return adapters.Services{ |
||||
"bzz": func(ctx *adapters.ServiceContext) (node.Service, error) { |
||||
addr := NewAddrFromNodeID(ctx.Config.ID) |
||||
hp := NewHiveParams() |
||||
hp.Discovery = false |
||||
cnt++ |
||||
//assign the network ID
|
||||
currentNetworkID = cnt % NumberOfNets |
||||
if ok := nodeMap[currentNetworkID]; ok == nil { |
||||
nodeMap[currentNetworkID] = make([]discover.NodeID, 0) |
||||
} |
||||
//add this node to the group sharing the same network ID
|
||||
nodeMap[currentNetworkID] = append(nodeMap[currentNetworkID], ctx.Config.ID) |
||||
log.Debug("current network ID:", "id", currentNetworkID) |
||||
config := &BzzConfig{ |
||||
OverlayAddr: addr.Over(), |
||||
UnderlayAddr: addr.Under(), |
||||
HiveParams: hp, |
||||
NetworkID: uint64(currentNetworkID), |
||||
} |
||||
return NewBzz(config, kademlia(ctx.Config.ID), nil, nil, nil), nil |
||||
}, |
||||
} |
||||
} |
||||
|
||||
func watchSubscriptionEvents(ctx context.Context, id discover.NodeID, client *rpc.Client, errc chan error, quitC chan struct{}) { |
||||
events := make(chan *p2p.PeerEvent) |
||||
sub, err := client.Subscribe(context.Background(), "admin", events, "peerEvents") |
||||
if err != nil { |
||||
log.Error(err.Error()) |
||||
errc <- fmt.Errorf("error getting peer events for node %v: %s", id, err) |
||||
return |
||||
} |
||||
go func() { |
||||
defer func() { |
||||
sub.Unsubscribe() |
||||
log.Trace("watch subscription events: unsubscribe", "id", id) |
||||
}() |
||||
|
||||
for { |
||||
select { |
||||
case <-quitC: |
||||
return |
||||
case <-ctx.Done(): |
||||
select { |
||||
case errc <- ctx.Err(): |
||||
case <-quitC: |
||||
} |
||||
return |
||||
case e := <-events: |
||||
if e.Type == p2p.PeerEventTypeAdd { |
||||
errc <- nil |
||||
} |
||||
case err := <-sub.Err(): |
||||
if err != nil { |
||||
select { |
||||
case errc <- fmt.Errorf("error getting peer events for node %v: %v", id, err): |
||||
case <-quitC: |
||||
} |
||||
return |
||||
} |
||||
} |
||||
} |
||||
}() |
||||
} |
@ -0,0 +1,394 @@ |
||||
package notify |
||||
|
||||
import ( |
||||
"crypto/ecdsa" |
||||
"fmt" |
||||
"sync" |
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/p2p" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
"github.com/ethereum/go-ethereum/swarm/log" |
||||
"github.com/ethereum/go-ethereum/swarm/pss" |
||||
) |
||||
|
||||
const ( |
||||
// sent from requester to updater to request start of notifications
|
||||
MsgCodeStart = iota |
||||
|
||||
// sent from updater to requester, contains a notification plus a new symkey to replace the old
|
||||
MsgCodeNotifyWithKey |
||||
|
||||
// sent from updater to requester, contains a notification
|
||||
MsgCodeNotify |
||||
|
||||
// sent from requester to updater to request stop of notifications (currently unused)
|
||||
MsgCodeStop |
||||
MsgCodeMax |
||||
) |
||||
|
||||
const ( |
||||
DefaultAddressLength = 1 |
||||
symKeyLength = 32 // this should be gotten from source
|
||||
) |
||||
|
||||
var ( |
||||
// control topic is used before symmetric key issuance completes
|
||||
controlTopic = pss.Topic{0x00, 0x00, 0x00, 0x01} |
||||
) |
||||
|
||||
// when code is MsgCodeStart, Payload is address
|
||||
// when code is MsgCodeNotifyWithKey, Payload is notification | symkey
|
||||
// when code is MsgCodeNotify, Payload is notification
|
||||
// when code is MsgCodeStop, Payload is address
|
||||
type Msg struct { |
||||
Code byte |
||||
Name []byte |
||||
Payload []byte |
||||
namestring string |
||||
} |
||||
|
||||
// NewMsg creates a new notification message object
|
||||
func NewMsg(code byte, name string, payload []byte) *Msg { |
||||
return &Msg{ |
||||
Code: code, |
||||
Name: []byte(name), |
||||
Payload: payload, |
||||
namestring: name, |
||||
} |
||||
} |
||||
|
||||
// NewMsgFromPayload decodes a serialized message payload into a new notification message object
|
||||
func NewMsgFromPayload(payload []byte) (*Msg, error) { |
||||
msg := &Msg{} |
||||
err := rlp.DecodeBytes(payload, msg) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
msg.namestring = string(msg.Name) |
||||
return msg, nil |
||||
} |
||||
|
||||
// a notifier has one sendBin entry for each address space it sends messages to
|
||||
type sendBin struct { |
||||
address pss.PssAddress |
||||
symKeyId string |
||||
count int |
||||
} |
||||
|
||||
// represents a single notification service
|
||||
// only subscription address bins that match the address of a notification client have entries.
|
||||
type notifier struct { |
||||
bins map[string]*sendBin |
||||
topic pss.Topic // identifies the resource for pss receiver
|
||||
threshold int // amount of address bytes used in bins
|
||||
updateC <-chan []byte |
||||
quitC chan struct{} |
||||
} |
||||
|
||||
func (n *notifier) removeSubscription() { |
||||
n.quitC <- struct{}{} |
||||
} |
||||
|
||||
// represents an individual subscription made by a public key at a specific address/neighborhood
|
||||
type subscription struct { |
||||
pubkeyId string |
||||
address pss.PssAddress |
||||
handler func(string, []byte) error |
||||
} |
||||
|
||||
// Controller is the interface to control, add and remove notification services and subscriptions
|
||||
type Controller struct { |
||||
pss *pss.Pss |
||||
notifiers map[string]*notifier |
||||
subscriptions map[string]*subscription |
||||
mu sync.Mutex |
||||
} |
||||
|
||||
// NewController creates a new Controller object
|
||||
func NewController(ps *pss.Pss) *Controller { |
||||
ctrl := &Controller{ |
||||
pss: ps, |
||||
notifiers: make(map[string]*notifier), |
||||
subscriptions: make(map[string]*subscription), |
||||
} |
||||
ctrl.pss.Register(&controlTopic, ctrl.Handler) |
||||
return ctrl |
||||
} |
||||
|
||||
// IsActive is used to check if a notification service exists for a specified id string
|
||||
// Returns true if exists, false if not
|
||||
func (c *Controller) IsActive(name string) bool { |
||||
c.mu.Lock() |
||||
defer c.mu.Unlock() |
||||
return c.isActive(name) |
||||
} |
||||
|
||||
func (c *Controller) isActive(name string) bool { |
||||
_, ok := c.notifiers[name] |
||||
return ok |
||||
} |
||||
|
||||
// Subscribe is used by a client to request notifications from a notification service provider
|
||||
// It will create a MsgCodeStart message and send asymmetrically to the provider using its public key and routing address
|
||||
// The handler function is a callback that will be called when notifications are received
|
||||
// Fails if the request pss cannot be sent or if the update message could not be serialized
|
||||
func (c *Controller) Subscribe(name string, pubkey *ecdsa.PublicKey, address pss.PssAddress, handler func(string, []byte) error) error { |
||||
c.mu.Lock() |
||||
defer c.mu.Unlock() |
||||
msg := NewMsg(MsgCodeStart, name, c.pss.BaseAddr()) |
||||
c.pss.SetPeerPublicKey(pubkey, controlTopic, &address) |
||||
pubkeyId := hexutil.Encode(crypto.FromECDSAPub(pubkey)) |
||||
smsg, err := rlp.EncodeToBytes(msg) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
err = c.pss.SendAsym(pubkeyId, controlTopic, smsg) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
c.subscriptions[name] = &subscription{ |
||||
pubkeyId: pubkeyId, |
||||
address: address, |
||||
handler: handler, |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// Unsubscribe, perhaps unsurprisingly, undoes the effects of Subscribe
|
||||
// Fails if the subscription does not exist, if the request pss cannot be sent or if the update message could not be serialized
|
||||
func (c *Controller) Unsubscribe(name string) error { |
||||
c.mu.Lock() |
||||
defer c.mu.Unlock() |
||||
sub, ok := c.subscriptions[name] |
||||
if !ok { |
||||
return fmt.Errorf("Unknown subscription '%s'", name) |
||||
} |
||||
msg := NewMsg(MsgCodeStop, name, sub.address) |
||||
smsg, err := rlp.EncodeToBytes(msg) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
err = c.pss.SendAsym(sub.pubkeyId, controlTopic, smsg) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
delete(c.subscriptions, name) |
||||
return nil |
||||
} |
||||
|
||||
// NewNotifier is used by a notification service provider to create a new notification service
|
||||
// It takes a name as identifier for the resource, a threshold indicating the granularity of the subscription address bin
|
||||
// It then starts an event loop which listens to the supplied update channel and executes notifications on channel receives
|
||||
// Fails if a notifier already is registered on the name
|
||||
//func (c *Controller) NewNotifier(name string, threshold int, contentFunc func(string) ([]byte, error)) error {
|
||||
func (c *Controller) NewNotifier(name string, threshold int, updateC <-chan []byte) (func(), error) { |
||||
c.mu.Lock() |
||||
if c.isActive(name) { |
||||
c.mu.Unlock() |
||||
return nil, fmt.Errorf("Notification service %s already exists in controller", name) |
||||
} |
||||
quitC := make(chan struct{}) |
||||
c.notifiers[name] = ¬ifier{ |
||||
bins: make(map[string]*sendBin), |
||||
topic: pss.BytesToTopic([]byte(name)), |
||||
threshold: threshold, |
||||
updateC: updateC, |
||||
quitC: quitC, |
||||
//contentFunc: contentFunc,
|
||||
} |
||||
c.mu.Unlock() |
||||
go func() { |
||||
for { |
||||
select { |
||||
case <-quitC: |
||||
return |
||||
case data := <-updateC: |
||||
c.notify(name, data) |
||||
} |
||||
} |
||||
}() |
||||
|
||||
return c.notifiers[name].removeSubscription, nil |
||||
} |
||||
|
||||
// RemoveNotifier is used to stop a notification service.
|
||||
// It cancels the event loop listening to the notification provider's update channel
|
||||
func (c *Controller) RemoveNotifier(name string) error { |
||||
c.mu.Lock() |
||||
defer c.mu.Unlock() |
||||
currentNotifier, ok := c.notifiers[name] |
||||
if !ok { |
||||
return fmt.Errorf("Unknown notification service %s", name) |
||||
} |
||||
currentNotifier.removeSubscription() |
||||
delete(c.notifiers, name) |
||||
return nil |
||||
} |
||||
|
||||
// Notify is called by a notification service provider to issue a new notification
|
||||
// It takes the name of the notification service and the data to be sent.
|
||||
// It fails if a notifier with this name does not exist or if data could not be serialized
|
||||
// Note that it does NOT fail on failure to send a message
|
||||
func (c *Controller) notify(name string, data []byte) error { |
||||
c.mu.Lock() |
||||
defer c.mu.Unlock() |
||||
if !c.isActive(name) { |
||||
return fmt.Errorf("Notification service %s doesn't exist", name) |
||||
} |
||||
msg := NewMsg(MsgCodeNotify, name, data) |
||||
smsg, err := rlp.EncodeToBytes(msg) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
for _, m := range c.notifiers[name].bins { |
||||
log.Debug("sending pss notify", "name", name, "addr", fmt.Sprintf("%x", m.address), "topic", fmt.Sprintf("%x", c.notifiers[name].topic), "data", data) |
||||
go func(m *sendBin) { |
||||
err = c.pss.SendSym(m.symKeyId, c.notifiers[name].topic, smsg) |
||||
if err != nil { |
||||
log.Warn("Failed to send notify to addr %x: %v", m.address, err) |
||||
} |
||||
}(m) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// check if we already have the bin
|
||||
// if we do, retrieve the symkey from it and increment the count
|
||||
// if we dont make a new symkey and a new bin entry
|
||||
func (c *Controller) addToBin(ntfr *notifier, address []byte) (symKeyId string, pssAddress pss.PssAddress, err error) { |
||||
|
||||
// parse the address from the message and truncate if longer than our bins threshold
|
||||
if len(address) > ntfr.threshold { |
||||
address = address[:ntfr.threshold] |
||||
} |
||||
|
||||
pssAddress = pss.PssAddress(address) |
||||
hexAddress := fmt.Sprintf("%x", address) |
||||
currentBin, ok := ntfr.bins[hexAddress] |
||||
if ok { |
||||
currentBin.count++ |
||||
symKeyId = currentBin.symKeyId |
||||
} else { |
||||
symKeyId, err = c.pss.GenerateSymmetricKey(ntfr.topic, &pssAddress, false) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
ntfr.bins[hexAddress] = &sendBin{ |
||||
address: address, |
||||
symKeyId: symKeyId, |
||||
count: 1, |
||||
} |
||||
} |
||||
return symKeyId, pssAddress, nil |
||||
} |
||||
|
||||
func (c *Controller) handleStartMsg(msg *Msg, keyid string) (err error) { |
||||
|
||||
keyidbytes, err := hexutil.Decode(keyid) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
pubkey, err := crypto.UnmarshalPubkey(keyidbytes) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
// if name is not registered for notifications we will not react
|
||||
currentNotifier, ok := c.notifiers[msg.namestring] |
||||
if !ok { |
||||
return fmt.Errorf("Subscribe attempted on unknown resource '%s'", msg.namestring) |
||||
} |
||||
|
||||
// add to or open new bin
|
||||
symKeyId, pssAddress, err := c.addToBin(currentNotifier, msg.Payload) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
// add to address book for send initial notify
|
||||
symkey, err := c.pss.GetSymmetricKey(symKeyId) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
err = c.pss.SetPeerPublicKey(pubkey, controlTopic, &pssAddress) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
// TODO this is set to zero-length byte pending decision on protocol for initial message, whether it should include message or not, and how to trigger the initial message so that current state of MRU is sent upon subscription
|
||||
notify := []byte{} |
||||
replyMsg := NewMsg(MsgCodeNotifyWithKey, msg.namestring, make([]byte, len(notify)+symKeyLength)) |
||||
copy(replyMsg.Payload, notify) |
||||
copy(replyMsg.Payload[len(notify):], symkey) |
||||
sReplyMsg, err := rlp.EncodeToBytes(replyMsg) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return c.pss.SendAsym(keyid, controlTopic, sReplyMsg) |
||||
} |
||||
|
||||
func (c *Controller) handleNotifyWithKeyMsg(msg *Msg) error { |
||||
symkey := msg.Payload[len(msg.Payload)-symKeyLength:] |
||||
topic := pss.BytesToTopic(msg.Name) |
||||
|
||||
// \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) |
||||
return c.subscriptions[msg.namestring].handler(msg.namestring, msg.Payload[:len(msg.Payload)-symKeyLength]) |
||||
} |
||||
|
||||
func (c *Controller) handleStopMsg(msg *Msg) error { |
||||
// if name is not registered for notifications we will not react
|
||||
currentNotifier, ok := c.notifiers[msg.namestring] |
||||
if !ok { |
||||
return fmt.Errorf("Unsubscribe attempted on unknown resource '%s'", msg.namestring) |
||||
} |
||||
|
||||
// parse the address from the message and truncate if longer than our bins' address length threshold
|
||||
address := msg.Payload |
||||
if len(msg.Payload) > currentNotifier.threshold { |
||||
address = address[:currentNotifier.threshold] |
||||
} |
||||
|
||||
// remove the entry from the bin if it exists, and remove the bin if it's the last remaining one
|
||||
hexAddress := fmt.Sprintf("%x", address) |
||||
currentBin, ok := currentNotifier.bins[hexAddress] |
||||
if !ok { |
||||
return fmt.Errorf("found no active bin for address %s", hexAddress) |
||||
} |
||||
currentBin.count-- |
||||
if currentBin.count == 0 { // if no more clients in this bin, remove it
|
||||
delete(currentNotifier.bins, hexAddress) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// Handler is the pss topic handler to be used to process notification service messages
|
||||
// It should be registered in the pss of both to any notification service provides and clients using the service
|
||||
func (c *Controller) Handler(smsg []byte, p *p2p.Peer, asymmetric bool, keyid string) error { |
||||
c.mu.Lock() |
||||
defer c.mu.Unlock() |
||||
log.Debug("notify controller handler", "keyid", keyid) |
||||
|
||||
// see if the message is valid
|
||||
msg, err := NewMsgFromPayload(smsg) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
switch msg.Code { |
||||
case MsgCodeStart: |
||||
return c.handleStartMsg(msg, keyid) |
||||
case MsgCodeNotifyWithKey: |
||||
return c.handleNotifyWithKeyMsg(msg) |
||||
case MsgCodeNotify: |
||||
return c.subscriptions[msg.namestring].handler(msg.namestring, msg.Payload) |
||||
case MsgCodeStop: |
||||
return c.handleStopMsg(msg) |
||||
} |
||||
|
||||
return fmt.Errorf("Invalid message code: %d", msg.Code) |
||||
} |
@ -0,0 +1,252 @@ |
||||
package notify |
||||
|
||||
import ( |
||||
"bytes" |
||||
"context" |
||||
"flag" |
||||
"fmt" |
||||
"os" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/node" |
||||
"github.com/ethereum/go-ethereum/p2p/discover" |
||||
"github.com/ethereum/go-ethereum/p2p/simulations" |
||||
"github.com/ethereum/go-ethereum/p2p/simulations/adapters" |
||||
"github.com/ethereum/go-ethereum/swarm/network" |
||||
"github.com/ethereum/go-ethereum/swarm/pss" |
||||
"github.com/ethereum/go-ethereum/swarm/state" |
||||
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" |
||||
) |
||||
|
||||
var ( |
||||
loglevel = flag.Int("l", 3, "loglevel") |
||||
psses map[string]*pss.Pss |
||||
w *whisper.Whisper |
||||
wapi *whisper.PublicWhisperAPI |
||||
) |
||||
|
||||
func init() { |
||||
flag.Parse() |
||||
hs := log.StreamHandler(os.Stderr, log.TerminalFormat(true)) |
||||
hf := log.LvlFilterHandler(log.Lvl(*loglevel), hs) |
||||
h := log.CallerFileHandler(hf) |
||||
log.Root().SetHandler(h) |
||||
|
||||
w = whisper.New(&whisper.DefaultConfig) |
||||
wapi = whisper.NewPublicWhisperAPI(w) |
||||
psses = make(map[string]*pss.Pss) |
||||
} |
||||
|
||||
// Creates a client node and notifier node
|
||||
// Client sends pss notifications requests
|
||||
// notifier sends initial notification with symmetric key, and
|
||||
// second notification symmetrically encrypted
|
||||
func TestStart(t *testing.T) { |
||||
adapter := adapters.NewSimAdapter(newServices(false)) |
||||
net := simulations.NewNetwork(adapter, &simulations.NetworkConfig{ |
||||
ID: "0", |
||||
DefaultService: "bzz", |
||||
}) |
||||
leftNodeConf := adapters.RandomNodeConfig() |
||||
leftNodeConf.Services = []string{"bzz", "pss"} |
||||
leftNode, err := net.NewNodeWithConfig(leftNodeConf) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
err = net.Start(leftNode.ID()) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
rightNodeConf := adapters.RandomNodeConfig() |
||||
rightNodeConf.Services = []string{"bzz", "pss"} |
||||
rightNode, err := net.NewNodeWithConfig(rightNodeConf) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
err = net.Start(rightNode.ID()) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
err = net.Connect(rightNode.ID(), leftNode.ID()) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
leftRpc, err := leftNode.Client() |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
rightRpc, err := rightNode.Client() |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
var leftAddr string |
||||
err = leftRpc.Call(&leftAddr, "pss_baseAddr") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
var rightAddr string |
||||
err = rightRpc.Call(&rightAddr, "pss_baseAddr") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
var leftPub string |
||||
err = leftRpc.Call(&leftPub, "pss_getPublicKey") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
var rightPub string |
||||
err = rightRpc.Call(&rightPub, "pss_getPublicKey") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
rsrcName := "foo.eth" |
||||
rsrcTopic := pss.BytesToTopic([]byte(rsrcName)) |
||||
|
||||
// wait for kademlia table to populate
|
||||
time.Sleep(time.Second) |
||||
|
||||
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) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer rightSub.Unsubscribe() |
||||
|
||||
updateC := make(chan []byte) |
||||
updateMsg := []byte{} |
||||
ctrlClient := NewController(psses[rightPub]) |
||||
ctrlNotifier := NewController(psses[leftPub]) |
||||
ctrlNotifier.NewNotifier("foo.eth", 2, updateC) |
||||
|
||||
pubkeybytes, err := hexutil.Decode(leftPub) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
pubkey, err := crypto.UnmarshalPubkey(pubkeybytes) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
addrbytes, err := hexutil.Decode(leftAddr) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
ctrlClient.Subscribe(rsrcName, pubkey, addrbytes, func(s string, b []byte) error { |
||||
if s != "foo.eth" || !bytes.Equal(updateMsg, b) { |
||||
t.Fatalf("unexpected result in client handler: '%s':'%x'", s, b) |
||||
} |
||||
log.Info("client handler receive", "s", s, "b", b) |
||||
return nil |
||||
}) |
||||
|
||||
var inMsg *pss.APIMsg |
||||
select { |
||||
case inMsg = <-rmsgC: |
||||
case <-ctx.Done(): |
||||
t.Fatal(ctx.Err()) |
||||
} |
||||
|
||||
dMsg, err := NewMsgFromPayload(inMsg.Msg) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if dMsg.namestring != rsrcName { |
||||
t.Fatalf("expected name '%s', got '%s'", rsrcName, dMsg.namestring) |
||||
} |
||||
if !bytes.Equal(dMsg.Payload[:len(updateMsg)], updateMsg) { |
||||
t.Fatalf("expected payload first %d bytes '%x', got '%x'", len(updateMsg), updateMsg, dMsg.Payload[:len(updateMsg)]) |
||||
} |
||||
if len(updateMsg)+symKeyLength != len(dMsg.Payload) { |
||||
t.Fatalf("expected payload length %d, have %d", len(updateMsg)+symKeyLength, len(dMsg.Payload)) |
||||
} |
||||
|
||||
rightSubUpdate, err := rightRpc.Subscribe(ctx, "pss", rmsgC, "receive", rsrcTopic) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer rightSubUpdate.Unsubscribe() |
||||
|
||||
updateMsg = []byte("plugh") |
||||
updateC <- updateMsg |
||||
select { |
||||
case inMsg = <-rmsgC: |
||||
case <-ctx.Done(): |
||||
log.Error("timed out waiting for msg", "topic", fmt.Sprintf("%x", rsrcTopic)) |
||||
t.Fatal(ctx.Err()) |
||||
} |
||||
dMsg, err = NewMsgFromPayload(inMsg.Msg) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if dMsg.namestring != rsrcName { |
||||
t.Fatalf("expected name %s, got %s", rsrcName, dMsg.namestring) |
||||
} |
||||
if !bytes.Equal(dMsg.Payload, updateMsg) { |
||||
t.Fatalf("expected payload '%x', got '%x'", updateMsg, dMsg.Payload) |
||||
} |
||||
|
||||
} |
||||
|
||||
func newServices(allowRaw bool) adapters.Services { |
||||
stateStore := state.NewInmemoryStore() |
||||
kademlias := make(map[discover.NodeID]*network.Kademlia) |
||||
kademlia := func(id discover.NodeID) *network.Kademlia { |
||||
if k, ok := kademlias[id]; ok { |
||||
return k |
||||
} |
||||
addr := network.NewAddrFromNodeID(id) |
||||
params := network.NewKadParams() |
||||
params.MinProxBinSize = 2 |
||||
params.MaxBinSize = 3 |
||||
params.MinBinSize = 1 |
||||
params.MaxRetries = 1000 |
||||
params.RetryExponent = 2 |
||||
params.RetryInterval = 1000000 |
||||
kademlias[id] = network.NewKademlia(addr.Over(), params) |
||||
return kademlias[id] |
||||
} |
||||
return adapters.Services{ |
||||
"pss": func(ctx *adapters.ServiceContext) (node.Service, error) { |
||||
ctxlocal, cancel := context.WithTimeout(context.Background(), time.Second) |
||||
defer cancel() |
||||
keys, err := wapi.NewKeyPair(ctxlocal) |
||||
privkey, err := w.GetPrivateKey(keys) |
||||
pssp := pss.NewPssParams().WithPrivateKey(privkey) |
||||
pssp.MsgTTL = time.Second * 30 |
||||
pssp.AllowRaw = allowRaw |
||||
pskad := kademlia(ctx.Config.ID) |
||||
ps, err := pss.NewPss(pskad, pssp) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
//psses[common.ToHex(crypto.FromECDSAPub(&privkey.PublicKey))] = ps
|
||||
psses[hexutil.Encode(crypto.FromECDSAPub(&privkey.PublicKey))] = ps |
||||
return ps, nil |
||||
}, |
||||
"bzz": func(ctx *adapters.ServiceContext) (node.Service, error) { |
||||
addr := network.NewAddrFromNodeID(ctx.Config.ID) |
||||
hp := network.NewHiveParams() |
||||
hp.Discovery = false |
||||
config := &network.BzzConfig{ |
||||
OverlayAddr: addr.Over(), |
||||
UnderlayAddr: addr.Under(), |
||||
HiveParams: hp, |
||||
} |
||||
return network.NewBzz(config, kademlia(ctx.Config.ID), stateStore, nil, nil), nil |
||||
}, |
||||
} |
||||
} |
Loading…
Reference in new issue