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

Implement response to data pull/push requests #574

Merged
merged 2 commits into from
Nov 12, 2019
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
24 changes: 16 additions & 8 deletions datatransfer/impl/graphsync/graphsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ func (impl *graphsyncImpl) sendRequest(selector ipld.Node, isPull bool, voucher
return tid, nil
}

func (impl *graphsyncImpl) sendResponse(isAccepted bool, to peer.ID, tid datatransfer.TransferID) (datatransfer.TransferID, error) {
resp := message.NewResponse(tid, isAccepted)

if err := impl.dataTransferNetwork.SendMessage(context.TODO(), to, resp); err != nil {
return 0, err
}
return tid, nil
}

// close an open channel (effectively a cancel)
func (impl *graphsyncImpl) CloseDataTransferChannel(x datatransfer.ChannelID) {}

Expand All @@ -134,20 +143,19 @@ func (impl *graphsyncImpl) InProgressChannels() map[datatransfer.ChannelID]datat
return nil
}

// TODO: implement for https://github.com/filecoin-project/go-data-transfer/issues/14
// ReceiveRequest takes an incoming request, validates the voucher and processes the message.
func (receiver *graphsyncReceiver) ReceiveRequest(
ctx context.Context,
sender peer.ID,
incoming message.DataTransferRequest) {

voucher, err := receiver.validateVoucher(sender, incoming)
if err != nil {
fmt.Printf(err.Error())
}
if voucher == nil {
fmt.Printf("\nincoming voucher failed to validate: %v\n", incoming)
}
var success bool
// not yet doing anything else with the voucher
_, err := receiver.validateVoucher(sender, incoming)
if err == nil { success = true }

// not yet doing anything else with the transfer ID
_, err = receiver.impl.sendResponse(success, sender, incoming.TransferID())
}

// validateVoucher converts a voucher in an incoming message to its appropriate
Expand Down
232 changes: 116 additions & 116 deletions datatransfer/impl/graphsync/graphsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,137 +456,137 @@ func TestSendResponseToIncomingRequest(t *testing.T) {
voucherBytes, err := voucher.ToBytes()
require.NoError(t, err)
_ = message.NewRequest(id, isPull, voucher.Type(), voucherBytes, baseCid, buffer.Bytes())
//request := message.NewRequest(id, isPull, voucher.Type(), voucherBytes, baseCid, buffer.Bytes())
//require.NoError(t, dtnet1.SendMessage(ctx, host2.ID(), request))
//var messageReceived receivedMessage
//select {
//case <-ctx.Done():
// t.Fatal("did not receive message sent")
//case messageReceived = <-r.messageReceived:
//}
//
//sv.verifyExpectations(t)
//
//sender := messageReceived.sender
//require.Equal(t, sender, host2.ID())
//
//received := messageReceived.message
//require.False(t, received.IsRequest())
//receivedResponse, ok := received.(message.DataTransferResponse)
//require.True(t, ok)
//
//assert.Equal(t, receivedResponse.TransferID(), id)
//require.True(t, receivedResponse.Accepted())
//
request := message.NewRequest(id, isPull, voucher.Type(), voucherBytes, baseCid, buffer.Bytes())
require.NoError(t, dtnet1.SendMessage(ctx, host2.ID(), request))
var messageReceived receivedMessage
select {
case <-ctx.Done():
t.Fatal("did not receive message sent")
case messageReceived = <-r.messageReceived:
}

sv.verifyExpectations(t)

sender := messageReceived.sender
require.Equal(t, sender, host2.ID())

received := messageReceived.message
require.False(t, received.IsRequest())
receivedResponse, ok := received.(message.DataTransferResponse)
require.True(t, ok)

assert.Equal(t, receivedResponse.TransferID(), id)
require.True(t, receivedResponse.Accepted())

})

// TODO: get passing to complete https://github.com/filecoin-project/go-data-transfer/issues/14
t.Run("Response to push with error validation", func(t *testing.T) {
//
//sv := newSV()
//sv.expectErrorPush()
//err = dt.RegisterVoucherType(reflect.TypeOf(&fakeDTType{}), sv)
//require.NoError(t, err)
//
//isPull := false
//
//voucherBytes, err := voucher.ToBytes()
//require.NoError(t, err)
//request := message.NewRequest(id, isPull, voucher.Type(), voucherBytes, baseCid, buffer.Bytes())
//require.NoError(t, dtnet1.SendMessage(ctx, host2.ID(), request))
//
//var messageReceived receivedMessage
//select {
//case <-ctx.Done():
// t.Fatal("did not receive message sent")
//case messageReceived = <-r.messageReceived:
//}
//
//sv.verifyExpectations(t)
//
//sender := messageReceived.sender
//require.Equal(t, sender, host2.ID())
//
//received := messageReceived.message
//require.False(t, received.IsRequest())
//receivedResponse, ok := received.(message.DataTransferResponse)
//require.True(t, ok)
//
//require.Equal(t, receivedResponse.TransferID(), id)
//require.False(t, receivedResponse.Accepted())
sv := newSV()
sv.expectErrorPush()
dt := NewGraphSyncDataTransfer(ctx, host2, bs)
err = dt.RegisterVoucherType(reflect.TypeOf(&fakeDTType{}), sv)
require.NoError(t, err)

isPull := false

voucherBytes, err := voucher.ToBytes()
require.NoError(t, err)
request := message.NewRequest(id, isPull, voucher.Type(), voucherBytes, baseCid, buffer.Bytes())
require.NoError(t, dtnet1.SendMessage(ctx, host2.ID(), request))

var messageReceived receivedMessage
select {
case <-ctx.Done():
t.Fatal("did not receive message sent")
case messageReceived = <-r.messageReceived:
}

sv.verifyExpectations(t)

sender := messageReceived.sender
require.Equal(t, sender, host2.ID())

received := messageReceived.message
require.False(t, received.IsRequest())
receivedResponse, ok := received.(message.DataTransferResponse)
require.True(t, ok)

require.Equal(t, receivedResponse.TransferID(), id)
require.False(t, receivedResponse.Accepted())
})

// TODO: get passing to complete https://github.com/filecoin-project/go-data-transfer/issues/17
t.Run("Response to pull with successful validation", func(t *testing.T) {
sv := newSV()
sv.expectSuccessPull()

dt := NewGraphSyncDataTransfer(ctx, host2, bs)
err = dt.RegisterVoucherType(reflect.TypeOf(&fakeDTType{}), sv)
require.NoError(t, err)

isPull := true

//sv := newSV()
//sv.expectSuccessPull()
//err = dt.RegisterVoucherType(reflect.TypeOf(&fakeDTType{}), sv)
//require.NoError(t, err)
//
//isPull := true
//
//voucherBytes, err := voucher.ToBytes()
//require.NoError(t, err)
//request := message.NewRequest(id, isPull, voucher.Type(), voucherBytes, baseCid, buffer.Bytes())
//
//require.NoError(t, dtnet1.SendMessage(ctx, host2.ID(), request))
//var messageReceived receivedMessage
//select {
//case <-ctx.Done():
// t.Fatal("did not receive message sent")
//case messageReceived = <-r.messageReceived:
//}
//
//sv.verifyExpectations(t)
//
//sender := messageReceived.sender
//require.Equal(t, sender, host2.ID())
//
//received := messageReceived.message
//require.False(t, received.IsRequest())
//receivedResponse, ok := received.(message.DataTransferResponse)
//require.True(t, ok)
//
//require.Equal(t, receivedResponse.TransferID(), id)
//require.True(t, receivedResponse.Accepted())
voucherBytes, err := voucher.ToBytes()
require.NoError(t, err)
request := message.NewRequest(id, isPull, voucher.Type(), voucherBytes, baseCid, buffer.Bytes())

require.NoError(t, dtnet1.SendMessage(ctx, host2.ID(), request))
var messageReceived receivedMessage
select {
case <-ctx.Done():
t.Fatal("did not receive message sent")
case messageReceived = <-r.messageReceived:
}

sv.verifyExpectations(t)

sender := messageReceived.sender
require.Equal(t, sender, host2.ID())

received := messageReceived.message
require.False(t, received.IsRequest())
receivedResponse, ok := received.(message.DataTransferResponse)
require.True(t, ok)

require.Equal(t, receivedResponse.TransferID(), id)
require.True(t, receivedResponse.Accepted())
})

// TODO: get passing to complete https://github.com/filecoin-project/go-data-transfer/issues/17
t.Run("Response to push with error validation", func(t *testing.T) {
sv := newSV()
sv.expectErrorPull()

dt := NewGraphSyncDataTransfer(ctx, host2, bs)
err = dt.RegisterVoucherType(reflect.TypeOf(&fakeDTType{}), sv)
require.NoError(t, err)

isPull := true
voucherBytes, err := voucher.ToBytes()
require.NoError(t, err)
request := message.NewRequest(id, isPull, voucher.Type(), voucherBytes, baseCid, buffer.Bytes())
require.NoError(t, dtnet1.SendMessage(ctx, host2.ID(), request))

var messageReceived receivedMessage
select {
case <-ctx.Done():
t.Fatal("did not receive message sent")
case messageReceived = <-r.messageReceived:
}

sv.verifyExpectations(t)

sender := messageReceived.sender
require.Equal(t, sender, host2.ID())

received := messageReceived.message
require.False(t, received.IsRequest())
receivedResponse, ok := received.(message.DataTransferResponse)
require.True(t, ok)

//sv := newSV()
//sv.expectErrorPull()
//
//err = dt.RegisterVoucherType(reflect.TypeOf(&fakeDTType{}), sv)
//require.NoError(t, err)
//
//isPull := true
//voucherBytes, err := voucher.ToBytes()
//require.NoError(t, err)
//request := message.NewRequest(id, isPull, voucher.Type(), voucherBytes, baseCid, buffer.Bytes())
//require.NoError(t, dtnet1.SendMessage(ctx, host2.ID(), request))
//
//var messageReceived receivedMessage
//select {
//case <-ctx.Done():
// t.Fatal("did not receive message sent")
//case messageReceived = <-r.messageReceived:
//}
//
//sv.verifyExpectations(t)
//
//sender := messageReceived.sender
//require.Equal(t, sender, host2.ID())
//
//received := messageReceived.message
//require.False(t, received.IsRequest())
//receivedResponse, ok := received.(message.DataTransferResponse)
//require.True(t, ok)
//
//require.Equal(t, receivedResponse.TransferID(), id)
//require.False(t, receivedResponse.Accepted())
require.Equal(t, receivedResponse.TransferID(), id)
require.False(t, receivedResponse.Accepted())
})
}

Expand Down