Skip to content

Commit

Permalink
fix(p2p): Avoid generating p2p keys in DefaultP2P, for faster tests
Browse files Browse the repository at this point in the history
Currently, the p2p tests (as well as some tests in lib) spend a lot of time
generating actual crypto keys due to calling DefaultP2P, which are then
replaced by test keys from the libp2p package. Avoiding this useless work
speeds up the p2p tests by about 5x.

Performance numbers from before this change, taken from qri-io/dataset#141:
Run 0
ok      github.com/qri-io/qri/api      18.428s
ok      github.com/qri-io/qri/cmd      59.639s
ok      github.com/qri-io/qri/config   46.602s
ok      github.com/qri-io/qri/lib      52.682s
ok      github.com/qri-io/qri/p2p      40.562s
Run 1
ok      github.com/qri-io/qri/api      21.280s
ok      github.com/qri-io/qri/cmd      55.533s
ok      github.com/qri-io/qri/config   51.034s
ok      github.com/qri-io/qri/lib      44.018s
ok      github.com/qri-io/qri/p2p      49.323s
Run 2
ok      github.com/qri-io/qri/api      15.110s
ok      github.com/qri-io/qri/cmd      57.857s
ok      github.com/qri-io/qri/config   43.257s
ok      github.com/qri-io/qri/lib      49.521s
ok      github.com/qri-io/qri/p2p      36.730s

After this change:
Run 0
ok      github.com/qri-io/qri/api      13.866s
ok      github.com/qri-io/qri/cmd      61.417s
ok      github.com/qri-io/qri/config   32.168s
ok      github.com/qri-io/qri/lib      32.685s
ok      github.com/qri-io/qri/p2p       8.008s
Run 1
ok      github.com/qri-io/qri/api      18.329s
ok      github.com/qri-io/qri/cmd      68.314s
ok      github.com/qri-io/qri/config   43.116s
ok      github.com/qri-io/qri/lib      32.584s
ok      github.com/qri-io/qri/p2p       6.926s
  • Loading branch information
dustmop committed Aug 28, 2018
1 parent 62bcc5f commit 97a1b6e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
39 changes: 27 additions & 12 deletions config/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ type P2P struct {
// DefaultP2P generates sensible settings for p2p, generating a new randomized
// private key & peer id
func DefaultP2P() *P2P {
r := rand.Reader
p := NewP2P()
_ = p.GeneratePrivateKeyAndPeerID()
return p
}

// NewP2P generates a p2p struct with only addresses, no keys or peer id
func NewP2P() *P2P {
p2p := &P2P{
Enabled: true,
HTTPGatewayAddr: "https://ipfs.io",
Expand All @@ -71,20 +77,29 @@ func DefaultP2P() *P2P {
},
ProfileReplication: "full",
}
return p2p
}

// GeneratePrivateKeyAndPeerID generates a new random private key and peer id
func (cfg *P2P) GeneratePrivateKeyAndPeerID() error {
r := rand.Reader
// Generate a key pair for this host
if priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r); err == nil {
if pdata, err := priv.Bytes(); err == nil {
p2p.PrivKey = base64.StdEncoding.EncodeToString(pdata)
}

// Obtain Peer ID from public key
if pid, err := peer.IDFromPublicKey(pub); err == nil {
p2p.PeerID = pid.Pretty()
}
priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
if err != nil {
return err
}

return p2p
pdata, err := priv.Bytes()
if err != nil {
return err
}
cfg.PrivKey = base64.StdEncoding.EncodeToString(pdata)
// Obtain Peer ID from public key
pid, err := peer.IDFromPublicKey(pub)
if err != nil {
return err
}
cfg.PeerID = pid.Pretty()
return nil
}

// DecodePrivateKey generates a PrivKey instance from base64-encoded config file bytes
Expand Down
8 changes: 7 additions & 1 deletion p2p/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ func NewTestQriNode(r repo.Repo, options ...func(o *config.P2P)) (p2ptest.Testab
// NewQriNode creates a new node, providing no arguments will use
// default configuration
func NewQriNode(r repo.Repo, options ...func(o *config.P2P)) (node *QriNode, err error) {
cfg := config.DefaultP2P()
cfg := config.NewP2P()
for _, opt := range options {
opt(cfg)
}
if len(cfg.PrivKey) == 0 {
err = cfg.GeneratePrivateKeyAndPeerID()
if err != nil {
return nil, err
}
}
// if err := cfg.Validate(r); err != nil {
// return nil, err
// }
Expand Down

0 comments on commit 97a1b6e

Please sign in to comment.