graphsync_test.go 8.06 KB
Newer Older
1 2 3 4
package graphsync

import (
	"context"
5
	"fmt"
6 7
	"math"
	"math/rand"
8 9 10 11
	"reflect"
	"testing"
	"time"

12 13
	"github.com/ipld/go-ipld-prime/linking/cid"

14 15 16
	"github.com/ipfs/go-block-format"
	cid "github.com/ipfs/go-cid"

17 18 19 20 21 22 23 24 25
	gsmsg "github.com/ipfs/go-graphsync/message"
	gsnet "github.com/ipfs/go-graphsync/network"
	"github.com/ipfs/go-graphsync/testbridge"
	"github.com/ipfs/go-graphsync/testutil"
	ipld "github.com/ipld/go-ipld-prime"
	"github.com/libp2p/go-libp2p-peer"
	mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)

26 27 28 29 30
type receivedMessage struct {
	message gsmsg.GraphSyncMessage
	sender  peer.ID
}

31 32
// Receiver is an interface for receiving messages from the GraphSyncNetwork.
type receiver struct {
33
	messageReceived chan receivedMessage
34 35 36 37 38 39
}

func (r *receiver) ReceiveMessage(
	ctx context.Context,
	sender peer.ID,
	incoming gsmsg.GraphSyncMessage) {
40

41 42
	select {
	case <-ctx.Done():
43
	case r.messageReceived <- receivedMessage{incoming, sender}:
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	}
}

func (r *receiver) ReceiveError(err error) {
}

func TestMakeRequestToNetwork(t *testing.T) {
	// create network
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
	defer cancel()
	mn := mocknet.New(ctx)

	// setup network
	host1, err := mn.GenPeer()
	if err != nil {
		t.Fatal("error generating host")
	}
	host2, err := mn.GenPeer()
	if err != nil {
		t.Fatal("error generating host")
	}
	err = mn.LinkAll()
	if err != nil {
		t.Fatal("error linking hosts")
	}

	gsnet1 := gsnet.NewFromLibp2pHost(host1)

	// setup receiving peer to just record message coming in
	gsnet2 := gsnet.NewFromLibp2pHost(host2)
	r := &receiver{
76
		messageReceived: make(chan receivedMessage),
77 78 79
	}
	gsnet2.SetDelegate(r)

80 81
	blockStore := make(map[ipld.Link][]byte)
	loader, storer := testbridge.NewMockStore(blockStore)
82
	bridge := testbridge.NewMockIPLDBridge()
83
	graphSync := New(ctx, gsnet1, bridge, loader, storer)
84 85 86 87 88 89 90

	cids := testutil.GenerateCids(5)
	spec := testbridge.NewMockSelectorSpec(cids)
	requestCtx, requestCancel := context.WithCancel(ctx)
	defer requestCancel()
	graphSync.Request(requestCtx, host2.ID(), spec)

91
	var message receivedMessage
92 93 94
	select {
	case <-ctx.Done():
		t.Fatal("did not receive message sent")
95
	case message = <-r.messageReceived:
96 97
	}

98
	sender := message.sender
99 100 101 102
	if sender != host1.ID() {
		t.Fatal("received message from wrong node")
	}

103
	received := message.message
104 105 106 107 108 109 110 111 112 113 114 115 116
	receivedRequests := received.Requests()
	if len(receivedRequests) != 1 {
		t.Fatal("Did not add request to received message")
	}
	receivedRequest := receivedRequests[0]
	receivedSpec, err := bridge.DecodeNode(receivedRequest.Selector())
	if err != nil {
		t.Fatal("unable to decode transmitted selector")
	}
	if !reflect.DeepEqual(spec, receivedSpec) {
		t.Fatal("did not transmit selector spec correctly")
	}
}
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

func TestSendResponseToIncomingRequest(t *testing.T) {
	// create network
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
	defer cancel()
	mn := mocknet.New(ctx)

	// setup network
	host1, err := mn.GenPeer()
	if err != nil {
		t.Fatal("error generating host")
	}
	host2, err := mn.GenPeer()
	if err != nil {
		t.Fatal("error generating host")
	}
	err = mn.LinkAll()
	if err != nil {
		t.Fatal("error linking hosts")
	}

	gsnet1 := gsnet.NewFromLibp2pHost(host1)
	r := &receiver{
		messageReceived: make(chan receivedMessage),
	}
	gsnet1.SetDelegate(r)

	// setup receiving peer to just record message coming in
	gsnet2 := gsnet.NewFromLibp2pHost(host2)

	blks := testutil.GenerateBlocksOfSize(5, 100)

150 151 152 153 154
	blockStore := make(map[ipld.Link][]byte)
	for _, block := range blks {
		blockStore[cidlink.Link{Cid: block.Cid()}] = block.RawData()
	}
	loader, storer := testbridge.NewMockStore(blockStore)
155 156 157
	bridge := testbridge.NewMockIPLDBridge()

	// initialize graphsync on second node to response to requests
158
	New(ctx, gsnet2, bridge, loader, storer)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

	cids := make([]cid.Cid, 0, 7)
	for _, block := range blks {
		cids = append(cids, block.Cid())
	}
	// append block that should be deduped
	cids = append(cids, blks[0].Cid())

	unknownCid := testutil.GenerateCids(1)[0]
	cids = append(cids, unknownCid)

	spec := testbridge.NewMockSelectorSpec(cids)
	selectorData, err := bridge.EncodeNode(spec)
	if err != nil {
		t.Fatal("could not encode selector spec")
	}
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())

	message := gsmsg.New()
	message.AddRequest(gsmsg.NewRequest(requestID, selectorData, gsmsg.GraphSyncPriority(math.MaxInt32)))
	// send request across network
	gsnet1.SendMessage(ctx, host2.ID(), message)
	// read the values sent back to requestor
	var received gsmsg.GraphSyncMessage
	var receivedBlocks []blocks.Block
readAllMessages:
	for {
		select {
		case <-ctx.Done():
			t.Fatal("did not receive complete response")
		case message := <-r.messageReceived:
			sender := message.sender
			if sender != host2.ID() {
				t.Fatal("received message from wrong node")
			}

			received = message.message
			receivedBlocks = append(receivedBlocks, received.Blocks()...)
			receivedResponses := received.Responses()
			if len(receivedResponses) != 1 {
				t.Fatal("Did not receive response")
			}
			if receivedResponses[0].RequestID() != requestID {
				t.Fatal("Sent response for incorrect request id")
			}
			if receivedResponses[0].Status() != gsmsg.PartialResponse {
				break readAllMessages
			}
		}
	}

	if len(receivedBlocks) != len(blks) {
		t.Fatal("Send incorrect number of blocks or there were duplicate blocks")
	}

	// there should have been a missing CID
	if received.Responses()[0].Status() != gsmsg.RequestCompletedPartial {
		t.Fatal("transmitted full response when only partial was transmitted")
	}
}
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

func TestGraphsyncRoundTrip(t *testing.T) {
	// create network
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
	defer cancel()
	mn := mocknet.New(ctx)

	// setup network
	host1, err := mn.GenPeer()
	if err != nil {
		t.Fatal("error generating host")
	}
	host2, err := mn.GenPeer()
	if err != nil {
		t.Fatal("error generating host")
	}
	err = mn.LinkAll()
	if err != nil {
		t.Fatal("error linking hosts")
	}

	gsnet1 := gsnet.NewFromLibp2pHost(host1)

	blockStore1 := make(map[ipld.Link][]byte)
	loader1, storer1 := testbridge.NewMockStore(blockStore1)
	bridge1 := testbridge.NewMockIPLDBridge()

	// initialize graphsync on second node to response to requests
	requestor := New(ctx, gsnet1, bridge1, loader1, storer1)

	// setup receiving peer to just record message coming in
	gsnet2 := gsnet.NewFromLibp2pHost(host2)

	blks := testutil.GenerateBlocksOfSize(5, 100)

	blockStore2 := make(map[ipld.Link][]byte)
	for _, block := range blks {
		blockStore2[cidlink.Link{Cid: block.Cid()}] = block.RawData()
	}
	loader2, storer2 := testbridge.NewMockStore(blockStore2)
	bridge2 := testbridge.NewMockIPLDBridge()

	// initialize graphsync on second node to response to requests
	New(ctx, gsnet2, bridge2, loader2, storer2)

	cids := make([]cid.Cid, 0, 7)
	for _, block := range blks {
		cids = append(cids, block.Cid())
	}
	// append block that should be deduped
	cids = append(cids, blks[0].Cid())

	unknownCid := testutil.GenerateCids(1)[0]
	cids = append(cids, unknownCid)

	spec := testbridge.NewMockSelectorSpec(cids)

	progressChan, errChan := requestor.Request(ctx, host2.ID(), spec)

	responses := testutil.CollectResponses(ctx, t, progressChan)
	errs := testutil.CollectErrors(ctx, t, errChan)

	expectedErr := fmt.Sprintf("Remote Peer Is Missing Block: %s", unknownCid.String())
	if len(errs) != 1 || errs[0].Error() != expectedErr {
		t.Fatal("did not transmit error for missing CID")
	}

	if len(responses) != 6 {
		t.Fatal("did not traverse all nodes")
	}
	for i, response := range responses {
		k := response.LastBlock.Link.(cidlink.Link).Cid
		var expectedCid cid.Cid
		if i == 5 {
			expectedCid = blks[0].Cid()
		} else {
			expectedCid = blks[i].Cid()
		}
		if k != expectedCid {
			t.Fatal("did not send the correct cids in order")
		}
	}

	// verify data was stored in blockstore
	if len(blockStore1) != 5 {
		t.Fatal("did not store all blocks")
	}
	for link, data := range blockStore1 {
		block, err := blocks.NewBlockWithCid(data, link.(cidlink.Link).Cid)
		if err != nil || !testutil.ContainsBlock(blks, block) {
			t.Fatal("Stored wrong block")
		}
	}
}