Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(peer) some _minor_ safety features #419

Merged
merged 6 commits into from
Dec 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion blocks/blockstore/write_cache_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package blockstore

import (
"testing"

ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
syncds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
"github.com/jbenet/go-ipfs/blocks"
"testing"
)

func TestReturnsErrorWhenSizeNegative(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ func splitAddresses(addrs []string) (maddrs []ma.Multiaddr, pids []peer.ID, err
if err != nil {
return nil, nil, cmds.ClientError("invalid peer address: " + err.Error())
}
id, err := peer.DecodePrettyID(path.Base(addr))
if err != nil {
return nil, nil, err
}
pids[i] = id
maddrs[i] = a
pids[i] = peer.DecodePrettyID(path.Base(addr))
}
return
}
Expand All @@ -165,7 +169,7 @@ func peersWithAddresses(ps peer.Peerstore, addrs []string) ([]peer.Peer, error)

peers := make([]peer.Peer, len(pids))
for i, pid := range pids {
p, err := ps.Get(pid)
p, err := ps.FindOrCreate(pid)
if err != nil {
return nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func initIdentity(cfg *config.Identity, peers peer.Peerstore, online bool) (peer

// get peer from peerstore (so it is constructed there)
id := peer.ID(b58.Decode(cfg.PeerID))
self, err := peers.Get(id)
self, err := peers.FindOrCreate(id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -265,7 +265,12 @@ func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerst
}

// setup peer
npeer, err := pstore.Get(peer.DecodePrettyID(p.PeerID))
id, err := peer.DecodePrettyID(p.PeerID)
if err != nil {
log.Criticalf("Bootstrapping error: %v", err)
continue
}
npeer, err := pstore.FindOrCreate(id)
if err != nil {
log.Criticalf("Bootstrapping error: %v", err)
continue
Expand Down
5 changes: 3 additions & 2 deletions core/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ func NewMockNode() (*IpfsNode, error) {
return nil, err
}

p, err := peer.WithKeyPair(sk, pk)
nd.Peerstore = peer.NewPeerstore()

p, err := nd.Peerstore.WithKeyPair(sk, pk)
if err != nil {
return nil, err
}

nd.Peerstore = peer.NewPeerstore()
nd.Identity, err = nd.Peerstore.Add(p)
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions crypto/spipe/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
bfish "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.crypto/blowfish"
"hash"

bfish "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.crypto/blowfish"

proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"

ci "github.com/jbenet/go-ipfs/crypto"
Expand Down Expand Up @@ -368,7 +369,7 @@ func getOrConstructPeer(peers peer.Peerstore, rpk ci.PubKey) (peer.Peer, error)
return nil, err
}

npeer, err := peers.Get(rid)
npeer, err := peers.FindOrCreate(rid)
if err != nil {
return nil, err // unexpected error happened.
}
Expand Down
3 changes: 2 additions & 1 deletion crypto/spipe/spipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/jbenet/go-ipfs/peer"
"github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/pipes"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)

func getPeer(tb testing.TB) peer.Peer {
Expand All @@ -17,7 +18,7 @@ func getPeer(tb testing.TB) peer.Peer {
tb.Fatal(err)
}

p, err := peer.WithKeyPair(privk, pubk)
p, err := testutil.NewPeerWithKeyPair(privk, pubk)
if err != nil {
tb.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions exchange/bitswap/bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
blocks "github.com/jbenet/go-ipfs/blocks"
blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
peer "github.com/jbenet/go-ipfs/peer"
mock "github.com/jbenet/go-ipfs/routing/mock"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)

func TestClose(t *testing.T) {
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
g := NewSessionGenerator(net, rs)

block := blocks.NewBlock([]byte("block"))
rs.Announce(peer.WithIDString("testing"), block.Key()) // but not on network
rs.Announce(testutil.NewPeerWithIDString("testing"), block.Key()) // but not on network

solo := g.Next()

Expand Down
8 changes: 4 additions & 4 deletions exchange/bitswap/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

blocks "github.com/jbenet/go-ipfs/blocks"
pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)

func TestAppendWanted(t *testing.T) {
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestCopyProtoByValue(t *testing.T) {

func TestToNetMethodSetsPeer(t *testing.T) {
m := New()
p := peer.WithIDString("X")
p := testutil.NewPeerWithIDString("X")
netmsg, err := m.ToNet(p)
if err != nil {
t.Fatal(err)
Expand All @@ -107,7 +107,7 @@ func TestToNetFromNetPreservesWantList(t *testing.T) {
original.AddWanted(u.Key("T"))
original.AddWanted(u.Key("F"))

p := peer.WithIDString("X")
p := testutil.NewPeerWithIDString("X")
netmsg, err := original.ToNet(p)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestToAndFromNetMessage(t *testing.T) {
original.AddBlock(blocks.NewBlock([]byte("F")))
original.AddBlock(blocks.NewBlock([]byte("M")))

p := peer.WithIDString("X")
p := testutil.NewPeerWithIDString("X")
netmsg, err := original.ToNet(p)
if err != nil {
t.Fatal(err)
Expand Down
3 changes: 2 additions & 1 deletion exchange/bitswap/strategy/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
blocks "github.com/jbenet/go-ipfs/blocks"
message "github.com/jbenet/go-ipfs/exchange/bitswap/message"
peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)

type peerAndStrategist struct {
Expand All @@ -16,7 +17,7 @@ type peerAndStrategist struct {

func newPeerAndStrategist(idStr string) peerAndStrategist {
return peerAndStrategist{
Peer: peer.WithIDString(idStr),
Peer: testutil.NewPeerWithIDString(idStr),
Strategy: New(true),
}
}
Expand Down
13 changes: 7 additions & 6 deletions exchange/bitswap/testnet/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)

func TestSendRequestToCooperativePeer(t *testing.T) {
Expand All @@ -18,8 +19,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) {

t.Log("Get two network adapters")

initiator := net.Adapter(peer.WithIDString("initiator"))
recipient := net.Adapter(peer.WithID(idOfRecipient))
initiator := net.Adapter(testutil.NewPeerWithIDString("initiator"))
recipient := net.Adapter(testutil.NewPeerWithID(idOfRecipient))

expectedStr := "response from recipient"
recipient.SetDelegate(lambda(func(
Expand All @@ -43,7 +44,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
message := bsmsg.New()
message.AddBlock(blocks.NewBlock([]byte("data")))
response, err := initiator.SendRequest(
context.Background(), peer.WithID(idOfRecipient), message)
context.Background(), testutil.NewPeerWithID(idOfRecipient), message)
if err != nil {
t.Fatal(err)
}
Expand All @@ -61,8 +62,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
net := VirtualNetwork()
idOfResponder := []byte("responder")
waiter := net.Adapter(peer.WithIDString("waiter"))
responder := net.Adapter(peer.WithID(idOfResponder))
waiter := net.Adapter(testutil.NewPeerWithIDString("waiter"))
responder := net.Adapter(testutil.NewPeerWithID(idOfResponder))

var wg sync.WaitGroup

Expand Down Expand Up @@ -107,7 +108,7 @@ func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
messageSentAsync := bsmsg.New()
messageSentAsync.AddBlock(blocks.NewBlock([]byte("data")))
errSending := waiter.SendMessage(
context.Background(), peer.WithID(idOfResponder), messageSentAsync)
context.Background(), testutil.NewPeerWithID(idOfResponder), messageSentAsync)
if errSending != nil {
t.Fatal(errSending)
}
Expand Down
8 changes: 5 additions & 3 deletions exchange/bitswap/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func NewSessionGenerator(
return SessionGenerator{
net: net,
rs: rs,
ps: peer.NewPeerstore(),
seq: 0,
}
}
Expand All @@ -24,11 +25,12 @@ type SessionGenerator struct {
seq int
net tn.Network
rs mock.RoutingServer
ps peer.Peerstore
}

func (g *SessionGenerator) Next() Instance {
g.seq++
return session(g.net, g.rs, []byte(string(g.seq)))
return session(g.net, g.rs, g.ps, []byte(string(g.seq)))
}

func (g *SessionGenerator) Instances(n int) []Instance {
Expand All @@ -51,8 +53,8 @@ type Instance struct {
// NB: It's easy make mistakes by providing the same peer ID to two different
// sessions. To safeguard, use the SessionGenerator to generate sessions. It's
// just a much better idea.
func session(net tn.Network, rs mock.RoutingServer, id peer.ID) Instance {
p := peer.WithID(id)
func session(net tn.Network, rs mock.RoutingServer, ps peer.Peerstore, id peer.ID) Instance {
p := ps.WithID(id)

adapter := net.Adapter(p)
htc := rs.Client(p)
Expand Down
2 changes: 1 addition & 1 deletion importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/jbenet/go-ipfs/importer/chunk"
uio "github.com/jbenet/go-ipfs/unixfs/io"
u "github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/testutil"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)

// NOTE:
Expand Down
4 changes: 2 additions & 2 deletions namesys/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (

ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
ci "github.com/jbenet/go-ipfs/crypto"
"github.com/jbenet/go-ipfs/peer"
mock "github.com/jbenet/go-ipfs/routing/mock"
u "github.com/jbenet/go-ipfs/util"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)

func TestRoutingResolve(t *testing.T) {
local := peer.WithIDString("testID")
local := testutil.NewPeerWithIDString("testID")
lds := ds.NewMapDatastore()
d := mock.NewMockRouter(local, lds)

Expand Down
3 changes: 2 additions & 1 deletion net/conn/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

ci "github.com/jbenet/go-ipfs/crypto"
peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
Expand All @@ -21,7 +22,7 @@ func setupPeer(addr string) (peer.Peer, error) {
return nil, err
}

p, err := peer.WithKeyPair(sk, pk)
p, err := testutil.NewPeerWithKeyPair(sk, pk)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion net/mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
msg "github.com/jbenet/go-ipfs/net/message"
pb "github.com/jbenet/go-ipfs/net/mux/internal/pb"
peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
Expand All @@ -30,7 +31,7 @@ func newPeer(t *testing.T, id string) peer.Peer {
return nil
}

return peer.WithID(peer.ID(mh))
return testutil.NewPeerWithID(peer.ID(mh))
}

func testMsg(t *testing.T, m msg.NetMessage, data []byte) {
Expand Down
3 changes: 2 additions & 1 deletion net/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

msg "github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Expand All @@ -32,7 +33,7 @@ func newPeer(t *testing.T, id string) peer.Peer {
return nil
}

return peer.WithID(peer.ID(mh))
return testutil.NewPeerWithID(peer.ID(mh))
}

func TestServiceHandler(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion net/swarm/simul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

peer "github.com/jbenet/go-ipfs/peer"
"github.com/jbenet/go-ipfs/util/testutil"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
Expand All @@ -28,7 +29,7 @@ func TestSimultOpen(t *testing.T) {
var wg sync.WaitGroup
connect := func(s *Swarm, dst peer.Peer) {
// copy for other peer
cp := peer.WithID(dst.ID())
cp := testutil.NewPeerWithID(dst.ID())
cp.AddAddress(dst.Addresses()[0])

if _, err := s.Dial(cp); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions net/swarm/swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
msg "github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
testutil "github.com/jbenet/go-ipfs/util/testutil"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
Expand Down Expand Up @@ -43,7 +44,7 @@ func setupPeer(t *testing.T, addr string) peer.Peer {
t.Fatal(err)
}

p, err := peer.WithKeyPair(sk, pk)
p, err := testutil.NewPeerWithKeyPair(sk, pk)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -84,7 +85,7 @@ func SubtestSwarm(t *testing.T, addrs []string, MsgNum int) {
connect := func(s *Swarm, dst peer.Peer) {
// copy for other peer

cp, err := s.peers.Get(dst.ID())
cp, err := s.peers.FindOrCreate(dst.ID())
if err != nil {
t.Fatal(err)
}
Expand Down
Loading