Skip to content

Commit

Permalink
Merge pull request ethereum#471 from ethersphere/pss-externalparams
Browse files Browse the repository at this point in the history
swarm/pss: Move pssparams to swarm api config
  • Loading branch information
nolash authored Apr 30, 2018
2 parents 876134d + 318c19a commit 14d8ad2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
3 changes: 3 additions & 0 deletions swarm/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/swarm/network"
"github.com/ethereum/go-ethereum/swarm/pss"
"github.com/ethereum/go-ethereum/swarm/services/swap"
"github.com/ethereum/go-ethereum/swarm/storage"
)
Expand All @@ -47,6 +48,7 @@ type Config struct {
*storage.LocalStoreParams
*network.HiveParams
Swap *swap.SwapParams
Pss *pss.PssParams
//*network.SyncParams
Contract common.Address
EnsRoot common.Address
Expand Down Expand Up @@ -77,6 +79,7 @@ func NewConfig() (self *Config) {
HiveParams: network.NewHiveParams(),
//SyncParams: network.NewDefaultSyncParams(),
Swap: swap.NewDefaultSwapParams(),
Pss: pss.NewPssParams(),
ListenAddr: DefaultHTTPListenAddr,
Port: DefaultHTTPPort,
Path: node.DefaultDataDir(),
Expand Down
7 changes: 5 additions & 2 deletions swarm/pss/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,12 @@ func newServices() adapters.Services {
defer cancel()
keys, err := wapi.NewKeyPair(ctxlocal)
privkey, err := w.GetPrivateKey(keys)
psparams := pss.NewPssParams(privkey)
psparams := pss.NewPssParams().WithPrivateKey(privkey)
pskad := kademlia(ctx.Config.ID)
ps := pss.NewPss(pskad, psparams)
ps, err := pss.NewPss(pskad, psparams)
if err != nil {
return nil, err
}
pshparams := pss.NewHandshakeParams()
pshparams.SymKeySendLimit = sendLimit
err = pss.SetHandshakeController(ps, pshparams)
Expand Down
15 changes: 11 additions & 4 deletions swarm/pss/pss.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,19 @@ type PssParams struct {
}

// Sane defaults for Pss
func NewPssParams(privatekey *ecdsa.PrivateKey) *PssParams {
func NewPssParams() *PssParams {
return &PssParams{
MsgTTL: defaultMsgTTL,
CacheTTL: defaultDigestCacheTTL,
privateKey: privatekey,
SymKeyCacheCapacity: defaultSymKeyCacheCapacity,
}
}

func (self *PssParams) WithPrivateKey(privatekey *ecdsa.PrivateKey) *PssParams {
self.privateKey = privatekey
return self
}

// Toplevel pss object, takes care of message sending, receiving, decryption and encryption, message handler dispatchers and message forwarding.
//
// Implements node.Service
Expand Down Expand Up @@ -131,7 +135,10 @@ func (self *Pss) String() string {
//
// In addition to params, it takes a swarm network overlay
// and a DPA storage for message cache storage.
func NewPss(k network.Overlay, params *PssParams) *Pss {
func NewPss(k network.Overlay, params *PssParams) (*Pss, error) {
if params.privateKey == nil {
return nil, errors.New("missing private key for pss")
}
cap := p2p.Cap{
Name: pssProtocolName,
Version: pssVersion,
Expand Down Expand Up @@ -169,7 +176,7 @@ func NewPss(k network.Overlay, params *PssParams) *Pss {
ps.hashPool.Put(hashfunc)
}

return ps
return ps, nil
}

/////////////////////////////////////////////////////////////////////
Expand Down
23 changes: 16 additions & 7 deletions swarm/pss/pss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestCache(t *testing.T) {
t.Fatal(err)
}
ps := newTestPss(privkey, nil, nil)
pp := NewPssParams(privkey)
pp := NewPssParams().WithPrivateKey(privkey)
data := []byte("foo")
datatwo := []byte("bar")
datathree := []byte("baz")
Expand Down Expand Up @@ -232,8 +232,11 @@ func TestAddressMatch(t *testing.T) {
t.Fatalf("Could not generate private key: %v", err)
}
privkey, err := w.GetPrivateKey(keys)
pssp := NewPssParams(privkey)
ps := NewPss(kad, pssp)
pssp := NewPssParams().WithPrivateKey(privkey)
ps, err := NewPss(kad, pssp)
if err != nil {
t.Fatal(err.Error())
}

pssmsg := &PssMsg{
To: remoteaddr,
Expand Down Expand Up @@ -1341,11 +1344,14 @@ func newServices(allowRaw bool) adapters.Services {
defer cancel()
keys, err := wapi.NewKeyPair(ctxlocal)
privkey, err := w.GetPrivateKey(keys)
pssp := NewPssParams(privkey)
pssp := NewPssParams().WithPrivateKey(privkey)
pssp.MsgTTL = time.Second * 30
pssp.AllowRaw = allowRaw
pskad := kademlia(ctx.Config.ID)
ps := NewPss(pskad, pssp)
ps, err := NewPss(pskad, pssp)
if err != nil {
return nil, err
}

ping := &Ping{
OutC: make(chan bool),
Expand Down Expand Up @@ -1405,11 +1411,14 @@ func newTestPss(privkey *ecdsa.PrivateKey, overlay network.Overlay, ppextra *Pss
}

// create pss
pp := NewPssParams(privkey)
pp := NewPssParams().WithPrivateKey(privkey)
if ppextra != nil {
pp.SymKeyCacheCapacity = ppextra.SymKeyCacheCapacity
}
ps := NewPss(overlay, pp)
ps, err := NewPss(overlay, pp)
if err != nil {
return nil
}
ps.Start(nil)

return ps
Expand Down
7 changes: 5 additions & 2 deletions swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,11 @@ func NewSwarm(ctx *node.ServiceContext, backend chequebook.Backend, config *api.
self.bzz = network.NewBzz(bzzconfig, to, stateStore, stream.Spec, self.streamer.Run)

// Pss = postal service over swarm (devp2p over bzz)
pssparams := pss.NewPssParams(self.privateKey)
self.ps = pss.NewPss(to, pssparams)
config.Pss = config.Pss.WithPrivateKey(self.privateKey)
self.ps, err = pss.NewPss(to, config.Pss)
if err != nil {
return nil, err
}
if pss.IsActiveHandshake {
pss.SetHandshakeController(self.ps, pss.NewHandshakeParams())
}
Expand Down

0 comments on commit 14d8ad2

Please sign in to comment.