@cryptotaxi247 / kubo / commits / 77fccaa2f

Obviated need for `.ID.Pretty()` all over the place.

Juan Batiz-Benet committed Oct 6, 2014 at 04:23 UTC 77fccaa2f3129e823fb4b9bc7c4c396448372335
10 files changed +51 -44
blockservice/blockservice.go
+2 -2
@@ -36,7 +36,7 @@ func NewBlockService(d ds.Datastore, rem exchange.Interface) (*BlockService, err
36 // AddBlock adds a particular block to the service, Putting it into the datastore.
37 func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
38 k := b.Key()
39 - log.Debug("storing [%s] in datastore", k.Pretty())
39 + log.Debug("storing [%s] in datastore", k)
40 // TODO(brian): define a block datastore with a Put method which accepts a
41 // block parameter
42 err := s.Datastore.Put(k.DsKey(), b.Data)
@@ -53,7 +53,7 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
53 // GetBlock retrieves a particular block from the service,
54 // Getting it from the datastore using the key (hash).
55 func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
56 - log.Debug("BlockService GetBlock: '%s'", k.Pretty())
56 + log.Debug("BlockService GetBlock: '%s'", k)
57 datai, err := s.Datastore.Get(k.DsKey())
58 if err == nil {
59 log.Debug("Blockservice: Got data in datastore.")
core/commands/add.go
+3 -3
@@ -52,7 +52,7 @@ func Add(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Wr
52 // }
53 //
54 // Commenting out of here, because it's already in addNode below.
55 - // fmt.Fprintf(out, "added %s %s\n", k.Pretty(), path)
55 + // fmt.Fprintf(out, "added %s %s\n", k, path)
56 }
57 return nil
58 }
@@ -110,7 +110,7 @@ func addFile(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
110 return nil, err
111 }
112
113 - log.Info("Adding file: %s = %s\n", fpath, k.Pretty())
113 + log.Info("Adding file: %s = %s\n", fpath, k)
114 for _, l := range root.Links {
115 log.Info("SubBlock: %s\n", l.Hash.B58String())
116 }
@@ -131,7 +131,7 @@ func addNode(n *core.IpfsNode, nd *dag.Node, fpath string) error {
131 return err
132 }
133
134 - u.POut("added %s %s\n", k.Pretty(), fpath)
134 + u.POut("added %s %s\n", k, fpath)
135
136 // ensure we keep it. atm no-op
137 return n.PinDagNodeRecursively(nd, -1)
core/commands/publish.go
+1 -1
@@ -29,7 +29,7 @@ func Publish(n *core.IpfsNode, args []string, opts map[string]interface{}, out i
29 if err != nil {
30 return err
31 }
32 - fmt.Fprintf(out, "published mapping %s to %s\n", u.Key(hash).Pretty(), args[0])
32 + fmt.Fprintf(out, "published mapping %s to %s\n", u.Key(hash), args[0])
33
34 return nil
35 }
crypto/spipe/handshake.go
+8 -8
@@ -49,7 +49,7 @@ func (s *SecurePipe) handshake() error {
49 return err
50 }
51
52 - // u.DOut("handshake: %s <--> %s\n", s.local.ID.Pretty(), s.remote.ID.Pretty())
52 + // u.DOut("handshake: %s <--> %s\n", s.local, s.remote)
53 myPubKey, err := s.local.PubKey.Bytes()
54 if err != nil {
55 return err
@@ -101,7 +101,7 @@ func (s *SecurePipe) handshake() error {
101 if err != nil {
102 return err
103 }
104 - u.DOut("[%s] Remote Peer Identified as %s\n", s.local.ID.Pretty(), s.remote.ID.Pretty())
104 + u.DOut("[%s] Remote Peer Identified as %s\n", s.local, s.remote)
105
106 exchange, err := selectBest(SupportedExchanges, proposeResp.GetExchanges())
107 if err != nil {
@@ -163,7 +163,7 @@ func (s *SecurePipe) handshake() error {
163 theirHandshake.Write(encoded)
164 theirHandshake.Write(exchangeResp.GetEpubkey())
165
166 - // u.POut("Remote Peer Identified as %s\n", s.remote.ID.Pretty())
166 + // u.POut("Remote Peer Identified as %s\n", s.remote)
167 ok, err := s.remote.PubKey.Verify(theirHandshake.Bytes(), exchangeResp.GetSignature())
168 if err != nil {
169 return err
@@ -205,7 +205,7 @@ func (s *SecurePipe) handshake() error {
205 return errors.New("Negotiation failed.")
206 }
207
208 - u.DOut("[%s] handshake: Got node id: %s\n", s.local.ID.Pretty(), s.remote.ID.Pretty())
208 + u.DOut("[%s] handshake: Got node id: %s\n", s.local, s.remote)
209 return nil
210 }
211
@@ -233,7 +233,7 @@ func (s *SecurePipe) handleSecureIn(hashType string, tIV, tCKey, tMKey []byte) {
233 return
234 }
235
236 - // u.DOut("[peer %s] secure in [from = %s] %d\n", s.local.ID.Pretty(), s.remote.ID.Pretty(), len(data))
236 + // u.DOut("[peer %s] secure in [from = %s] %d\n", s.local, s.remote, len(data))
237 if len(data) <= macSize {
238 continue
239 }
@@ -281,7 +281,7 @@ func (s *SecurePipe) handleSecureOut(hashType string, mIV, mCKey, mMKey []byte)
281 copy(buff[len(data):], myMac.Sum(nil))
282 myMac.Reset()
283
284 - // u.DOut("[peer %s] secure out [to = %s] %d\n", s.local.ID.Pretty(), s.remote.ID.Pretty(), len(buff))
284 + // u.DOut("[peer %s] secure out [to = %s] %d\n", s.local, s.remote, len(buff))
285 s.insecure.Out <- buff
286 }
287 }
@@ -358,7 +358,7 @@ func getOrConstructPeer(peers peer.Peerstore, rpk ci.PubKey) (*peer.Peer, error)
358 // let's verify ID
359 if !npeer.ID.Equal(rid) {
360 e := "Expected peer.ID does not match sent pubkey's hash: %v - %v"
361 - return nil, fmt.Errorf(e, npeer.ID.Pretty(), rid.Pretty())
361 + return nil, fmt.Errorf(e, npeer, rid)
362 }
363
364 if npeer.PubKey == nil {
@@ -371,7 +371,7 @@ func getOrConstructPeer(peers peer.Peerstore, rpk ci.PubKey) (*peer.Peer, error)
371 // this shouldn't ever happen, given we hashed, etc, but it could mean
372 // expected code (or protocol) invariants violated.
373 if !npeer.PubKey.Equals(rpk) {
374 - return nil, fmt.Errorf("WARNING: PubKey mismatch: %v", npeer.ID.Pretty())
374 + return nil, fmt.Errorf("WARNING: PubKey mismatch: %v", npeer)
375 }
376 return npeer, nil
377 }
exchange/bitswap/bitswap.go
+5 -5
@@ -61,7 +61,7 @@ type bitswap struct {
61 //
62 // TODO ensure only one active request per key
63 func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error) {
64 - u.DOut("Get Block %v\n", k.Pretty())
64 + u.DOut("Get Block %v\n", k)
65
66 ctx, cancelFunc := context.WithCancel(parent)
67 bs.wantlist.Add(k)
@@ -110,7 +110,7 @@ func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error)
110 // HasBlock announces the existance of a block to bitswap, potentially sending
111 // it to peers (Partners) whose WantLists include it.
112 func (bs *bitswap) HasBlock(ctx context.Context, blk blocks.Block) error {
113 - u.DOut("Has Block %v\n", blk.Key().Pretty())
113 + u.DOut("Has Block %v\n", blk.Key())
114 bs.wantlist.Remove(blk.Key())
115 bs.sendToPeersThatWant(ctx, blk)
116 return bs.routing.Provide(ctx, blk.Key())
@@ -119,7 +119,7 @@ func (bs *bitswap) HasBlock(ctx context.Context, blk blocks.Block) error {
119 // TODO(brian): handle errors
120 func (bs *bitswap) ReceiveMessage(ctx context.Context, p *peer.Peer, incoming bsmsg.BitSwapMessage) (
121 *peer.Peer, bsmsg.BitSwapMessage) {
122 - u.DOut("ReceiveMessage from %v\n", p.Key().Pretty())
122 + u.DOut("ReceiveMessage from %v\n", p.Key())
123
124 if p == nil {
125 // TODO propagate the error upward
@@ -173,10 +173,10 @@ func (bs *bitswap) send(ctx context.Context, p *peer.Peer, m bsmsg.BitSwapMessag
173 }
174
175 func (bs *bitswap) sendToPeersThatWant(ctx context.Context, block blocks.Block) {
176 - u.DOut("Sending %v to peers that want it\n", block.Key().Pretty())
176 + u.DOut("Sending %v to peers that want it\n", block.Key())
177 for _, p := range bs.strategy.Peers() {
178 if bs.strategy.BlockIsWantedByPeer(block.Key(), p) {
179 - u.DOut("%v wants %v\n", p.Key().Pretty(), block.Key().Pretty())
179 + u.DOut("%v wants %v\n", p, block.Key())
180 if bs.strategy.ShouldSendBlockToPeer(block.Key(), p) {
181 message := bsmsg.New()
182 message.AppendBlock(block)
exchange/bitswap/bitswap_test.go
+10 -10
@@ -160,49 +160,49 @@ func TestSendToWantingPeer(t *testing.T) {
160 w := sg.Next()
161 o := sg.Next()
162
163 - t.Logf("Session %v\n", me.peer.Key().Pretty())
164 - t.Logf("Session %v\n", w.peer.Key().Pretty())
165 - t.Logf("Session %v\n", o.peer.Key().Pretty())
163 + t.Logf("Session %v\n", me.peer)
164 + t.Logf("Session %v\n", w.peer)
165 + t.Logf("Session %v\n", o.peer)
166
167 alpha := bg.Next()
168
169 const timeout = 1 * time.Millisecond // FIXME don't depend on time
170
171 - t.Logf("Peer %v attempts to get %v. NB: not available\n", w.peer.Key().Pretty(), alpha.Key().Pretty())
171 + t.Logf("Peer %v attempts to get %v. NB: not available\n", w.peer, alpha.Key())
172 ctx, _ := context.WithTimeout(context.Background(), timeout)
173 _, err := w.exchange.Block(ctx, alpha.Key())
174 if err == nil {
175 - t.Fatalf("Expected %v to NOT be available", alpha.Key().Pretty())
175 + t.Fatalf("Expected %v to NOT be available", alpha.Key())
176 }
177
178 beta := bg.Next()
179 - t.Logf("Peer %v announes availability of %v\n", w.peer.Key().Pretty(), beta.Key().Pretty())
179 + t.Logf("Peer %v announes availability of %v\n", w.peer, beta.Key())
180 ctx, _ = context.WithTimeout(context.Background(), timeout)
181 if err := w.blockstore.Put(beta); err != nil {
182 t.Fatal(err)
183 }
184 w.exchange.HasBlock(ctx, beta)
185
186 - t.Logf("%v gets %v from %v and discovers it wants %v\n", me.peer.Key().Pretty(), beta.Key().Pretty(), w.peer.Key().Pretty(), alpha.Key().Pretty())
186 + t.Logf("%v gets %v from %v and discovers it wants %v\n", me.peer, beta.Key(), w.peer, alpha.Key())
187 ctx, _ = context.WithTimeout(context.Background(), timeout)
188 if _, err := me.exchange.Block(ctx, beta.Key()); err != nil {
189 t.Fatal(err)
190 }
191
192 - t.Logf("%v announces availability of %v\n", o.peer.Key().Pretty(), alpha.Key().Pretty())
192 + t.Logf("%v announces availability of %v\n", o.peer, alpha.Key())
193 ctx, _ = context.WithTimeout(context.Background(), timeout)
194 if err := o.blockstore.Put(alpha); err != nil {
195 t.Fatal(err)
196 }
197 o.exchange.HasBlock(ctx, alpha)
198
199 - t.Logf("%v requests %v\n", me.peer.Key().Pretty(), alpha.Key().Pretty())
199 + t.Logf("%v requests %v\n", me.peer, alpha.Key())
200 ctx, _ = context.WithTimeout(context.Background(), timeout)
201 if _, err := me.exchange.Block(ctx, alpha.Key()); err != nil {
202 t.Fatal(err)
203 }
204
205 - t.Logf("%v should now have %v\n", w.peer.Key().Pretty(), alpha.Key().Pretty())
205 + t.Logf("%v should now have %v\n", w.peer, alpha.Key())
206 block, err := w.blockstore.Get(alpha.Key())
207 if err != nil {
208 t.Fatal("Should not have received an error")
net/service/service.go
+1 -1
@@ -82,7 +82,7 @@ func (s *Service) sendMessage(ctx context.Context, m msg.NetMessage, rid Request
82 return err
83 }
84
85 - // u.DOut("Service send message [to = %s]\n", m.Peer().ID.Pretty())
85 + // u.DOut("Service send message [to = %s]\n", m.Peer())
86
87 // send message
88 m2 := msg.New(m.Peer(), data)
net/swarm/conn.go
+5 -7
@@ -106,7 +106,7 @@ func (s *Swarm) connSetup(c *conn.Conn) error {
106 }
107
108 if c.Peer != nil {
109 - u.DOut("Starting connection: %s\n", c.Peer.Key().Pretty())
109 + u.DOut("Starting connection: %s\n", c.Peer)
110 } else {
111 u.DOut("Starting connection: [unknown peer]\n")
112 }
@@ -115,7 +115,7 @@ func (s *Swarm) connSetup(c *conn.Conn) error {
115 return fmt.Errorf("Conn securing error: %v", err)
116 }
117
118 - u.DOut("Secured connection: %s\n", c.Peer.Key().Pretty())
118 + u.DOut("Secured connection: %s\n", c.Peer)
119
120 // add address of connection to Peer. Maybe it should happen in connSecure.
121 c.Peer.AddAddress(c.Addr)
@@ -184,8 +184,7 @@ func (s *Swarm) fanOut() {
184 continue
185 }
186
187 - // u.DOut("[peer: %s] Sent message [to = %s]\n",
188 - // s.local.ID.Pretty(), msg.Peer().ID.Pretty())
187 + // u.DOut("[peer: %s] Sent message [to = %s]\n", s.local, msg.Peer())
188
189 // queue it in the connection's buffer
190 conn.Secure.Out <- msg.Data()
@@ -208,13 +207,12 @@ func (s *Swarm) fanIn(c *conn.Conn) {
207
208 case data, ok := <-c.Secure.In:
209 if !ok {
211 - e := fmt.Errorf("Error retrieving from conn: %v", c.Peer.Key().Pretty())
210 + e := fmt.Errorf("Error retrieving from conn: %v", c.Peer)
211 s.errChan <- e
212 goto out
213 }
214
216 - // u.DOut("[peer: %s] Received message [from = %s]\n",
217 - // s.local.ID.Pretty(), c.Peer.ID.Pretty())
215 + // u.DOut("[peer: %s] Received message [from = %s]\n", s.local, c.Peer)
216
217 msg := msg.New(c.Peer, data)
218 s.Incoming <- msg
peer/peer.go
+10
@@ -16,6 +16,11 @@ import (
16 // ID is a byte slice representing the identity of a peer.
17 type ID mh.Multihash
18
19 +// String is utililty function for printing out peer ID strings.
20 +func (id ID) String() string {
21 + return id.Pretty()
22 +}
23 +
24 // Equal is utililty function for comparing two peer ID's
25 func (id ID) Equal(other ID) bool {
26 return bytes.Equal(id, other)
@@ -48,6 +53,11 @@ type Peer struct {
53 sync.RWMutex
54 }
55
56 +// String prints out the peer.
57 +func (p *Peer) String() string {
58 + return "[Peer " + p.ID.Pretty() + "]"
59 +}
60 +
61 // Key returns the ID as a Key (string) for maps.
62 func (p *Peer) Key() u.Key {
63 return u.Key(p.ID)
routing/dht/dht.go
+6 -7
@@ -76,7 +76,7 @@ func NewDHT(p *peer.Peer, ps peer.Peerstore, net inet.Network, sender inet.Sende
76
77 // Connect to a new peer at the given address, ping and add to the routing table
78 func (dht *IpfsDHT) Connect(ctx context.Context, npeer *peer.Peer) (*peer.Peer, error) {
79 - log.Debug("Connect to new peer: %s\n", npeer.ID.Pretty())
79 + log.Debug("Connect to new peer: %s\n", npeer)
80
81 // TODO(jbenet,whyrusleeping)
82 //
@@ -132,8 +132,7 @@ func (dht *IpfsDHT) HandleMessage(ctx context.Context, mes msg.NetMessage) msg.N
132
133 // Print out diagnostic
134 log.Debug("[peer: %s] Got message type: '%s' [from = %s]\n",
135 - dht.self.ID.Pretty(),
136 - Message_MessageType_name[int32(pmes.GetType())], mPeer.ID.Pretty())
135 + dht.self, Message_MessageType_name[int32(pmes.GetType())], mPeer)
136
137 // get handler for this msg type.
138 handler := dht.handlerForMsgType(pmes.GetType())
@@ -177,7 +176,7 @@ func (dht *IpfsDHT) sendRequest(ctx context.Context, p *peer.Peer, pmes *Message
176
177 // Print out diagnostic
178 log.Debug("Sent message type: '%s' [to = %s]",
180 - Message_MessageType_name[int32(pmes.GetType())], p.ID.Pretty())
179 + Message_MessageType_name[int32(pmes.GetType())], p)
180
181 rmes, err := dht.sender.SendRequest(ctx, mes)
182 if err != nil {
@@ -222,7 +221,7 @@ func (dht *IpfsDHT) putProvider(ctx context.Context, p *peer.Peer, key string) e
221 return err
222 }
223
225 - log.Debug("[%s] putProvider: %s for %s", dht.self.ID.Pretty(), p.ID.Pretty(), key)
224 + log.Debug("[%s] putProvider: %s for %s", dht.self, p, key)
225 if *rpmes.Key != *pmes.Key {
226 return errors.New("provider not added correctly")
227 }
@@ -346,7 +345,7 @@ func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
345 // Update signals to all routingTables to Update their last-seen status
346 // on the given peer.
347 func (dht *IpfsDHT) Update(p *peer.Peer) {
349 - log.Debug("updating peer: [%s] latency = %f\n", p.ID.Pretty(), p.GetLatency().Seconds())
348 + log.Debug("updating peer: [%s] latency = %f\n", p, p.GetLatency().Seconds())
349 removedCount := 0
350 for _, route := range dht.routingTables {
351 removed := route.Update(p)
@@ -402,7 +401,7 @@ func (dht *IpfsDHT) addProviders(key u.Key, peers []*Message_Peer) []*peer.Peer
401 continue
402 }
403
405 - log.Debug("[%s] adding provider: %s for %s", dht.self.ID.Pretty(), p, key)
404 + log.Debug("[%s] adding provider: %s for %s", dht.self, p, key)
405
406 // Dont add outselves to the list
407 if p.ID.Equal(dht.self.ID) {