-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphsync): implement do-no-send-cids extension
Provides full implementation of do-no-send-cids on responder side
- Loading branch information
1 parent
690e260
commit 3537b23
Showing
7 changed files
with
295 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cidset | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/ipfs/go-graphsync/ipldutil" | ||
"github.com/ipld/go-ipld-prime/fluent" | ||
cidlink "github.com/ipld/go-ipld-prime/linking/cid" | ||
basicnode "github.com/ipld/go-ipld-prime/node/basic" | ||
) | ||
|
||
// EncodeCidSet encodes a cid set into bytes for the do-no-send-cids extension | ||
func EncodeCidSet(cids *cid.Set) ([]byte, error) { | ||
list := fluent.MustBuildList(basicnode.Style.List, cids.Len(), func(la fluent.ListAssembler) { | ||
_ = cids.ForEach(func(c cid.Cid) error { | ||
la.AssembleValue().AssignLink(cidlink.Link{Cid: c}) | ||
return nil | ||
}) | ||
}) | ||
return ipldutil.EncodeNode(list) | ||
} | ||
|
||
// DecodeCidSet decode a cid set from data for the do-no-send-cids extension | ||
func DecodeCidSet(data []byte) (*cid.Set, error) { | ||
list, err := ipldutil.DecodeNode(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
set := cid.NewSet() | ||
iter := list.ListIterator() | ||
for !iter.Done() { | ||
_, next, err := iter.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
link, err := next.AsLink() | ||
if err != nil { | ||
return nil, err | ||
} | ||
asCidLink, ok := link.(cidlink.Link) | ||
if !ok { | ||
return nil, errors.New("contained non CID link") | ||
} | ||
set.Add(asCidLink.Cid) | ||
} | ||
return set, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cidset | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/ipfs/go-graphsync/testutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDecodeEncodeCidSet(t *testing.T) { | ||
cids := testutil.GenerateCids(10) | ||
set := cid.NewSet() | ||
for _, c := range cids { | ||
set.Add(c) | ||
} | ||
encoded, err := EncodeCidSet(set) | ||
require.NoError(t, err, "encode errored") | ||
decodedCidSet, err := DecodeCidSet(encoded) | ||
require.NoError(t, err, "decode errored") | ||
require.Equal(t, decodedCidSet.Len(), set.Len()) | ||
err = decodedCidSet.ForEach(func(c cid.Cid) error { | ||
require.True(t, set.Has(c)) | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.