mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-25 12:27:43 +08:00
this is a major refactor of the entire codebase it changes the monolithic peer.Peer into using a peer.ID and a peer.Peerstore. Other changes: - removed handshake3. - testutil vastly simplified peer - secio bugfix + debugging logs - testutil: RandKeyPair - backpressure bugfix: w.o.w. - peer: added hex enc/dec - peer: added a PeerInfo struct PeerInfo is a small struct used to pass around a peer with a set of addresses and keys. This is not meant to be a complete view of the system, but rather to model updates to the peerstore. It is used by things like the routing system. - updated peer/queue + peerset - latency metrics - testutil: use crand for PeerID gen RandPeerID generates random "valid" peer IDs. it does not NEED to generate keys because it is as if we lost the key right away. fine to read some randomness and hash it. to generate proper keys and an ID, use: sk, pk, _ := testutil.RandKeyPair() id, _ := peer.IDFromPublicKey(pk) Also added RandPeerIDFatal helper - removed old spipe - updated seccat - core: cleanup initIdentity - removed old getFromPeerList
94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package decision
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
|
sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
|
|
|
blocks "github.com/jbenet/go-ipfs/blocks"
|
|
blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
|
|
message "github.com/jbenet/go-ipfs/exchange/bitswap/message"
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
|
)
|
|
|
|
type peerAndEngine struct {
|
|
Peer peer.ID
|
|
Engine *Engine
|
|
}
|
|
|
|
func newPeerAndLedgermanager(idStr string) peerAndEngine {
|
|
return peerAndEngine{
|
|
Peer: peer.ID(idStr),
|
|
//Strategy: New(true),
|
|
Engine: NewEngine(context.TODO(),
|
|
blockstore.NewBlockstore(sync.MutexWrap(ds.NewMapDatastore()))),
|
|
}
|
|
}
|
|
|
|
func TestConsistentAccounting(t *testing.T) {
|
|
sender := newPeerAndLedgermanager("Ernie")
|
|
receiver := newPeerAndLedgermanager("Bert")
|
|
|
|
// Send messages from Ernie to Bert
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
m := message.New()
|
|
content := []string{"this", "is", "message", "i"}
|
|
m.AddBlock(blocks.NewBlock([]byte(strings.Join(content, " "))))
|
|
|
|
sender.Engine.MessageSent(receiver.Peer, m)
|
|
receiver.Engine.MessageReceived(sender.Peer, m)
|
|
}
|
|
|
|
// Ensure sender records the change
|
|
if sender.Engine.numBytesSentTo(receiver.Peer) == 0 {
|
|
t.Fatal("Sent bytes were not recorded")
|
|
}
|
|
|
|
// Ensure sender and receiver have the same values
|
|
if sender.Engine.numBytesSentTo(receiver.Peer) != receiver.Engine.numBytesReceivedFrom(sender.Peer) {
|
|
t.Fatal("Inconsistent book-keeping. Strategies don't agree")
|
|
}
|
|
|
|
// Ensure sender didn't record receving anything. And that the receiver
|
|
// didn't record sending anything
|
|
if receiver.Engine.numBytesSentTo(sender.Peer) != 0 || sender.Engine.numBytesReceivedFrom(receiver.Peer) != 0 {
|
|
t.Fatal("Bert didn't send bytes to Ernie")
|
|
}
|
|
}
|
|
|
|
func TestPeerIsAddedToPeersWhenMessageReceivedOrSent(t *testing.T) {
|
|
|
|
sanfrancisco := newPeerAndLedgermanager("sf")
|
|
seattle := newPeerAndLedgermanager("sea")
|
|
|
|
m := message.New()
|
|
|
|
sanfrancisco.Engine.MessageSent(seattle.Peer, m)
|
|
seattle.Engine.MessageReceived(sanfrancisco.Peer, m)
|
|
|
|
if seattle.Peer == sanfrancisco.Peer {
|
|
t.Fatal("Sanity Check: Peers have same Key!")
|
|
}
|
|
|
|
if !peerIsPartner(seattle.Peer, sanfrancisco.Engine) {
|
|
t.Fatal("Peer wasn't added as a Partner")
|
|
}
|
|
|
|
if !peerIsPartner(sanfrancisco.Peer, seattle.Engine) {
|
|
t.Fatal("Peer wasn't added as a Partner")
|
|
}
|
|
}
|
|
|
|
func peerIsPartner(p peer.ID, e *Engine) bool {
|
|
for _, partner := range e.Peers() {
|
|
if partner == p {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|