-
Notifications
You must be signed in to change notification settings - Fork 59
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
Properly Implement Retrieval Lookups Based on CIDs #57
Changes from all commits
39a4f78
4ca08b5
b0d3c0c
9a31d7a
9211de0
7b74974
d94a1cf
582fa86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,51 +9,164 @@ import ( | |
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/filecoin-project/go-fil-markets/piecestore" | ||
"github.com/filecoin-project/go-fil-markets/shared_testutil" | ||
) | ||
|
||
func TestStorePieceInfo(t *testing.T) { | ||
ps := piecestore.NewPieceStore(datastore.NewMapDatastore()) | ||
|
||
pieceCid := []byte{1, 2, 3, 4} | ||
initializePieceStore := func(t *testing.T) piecestore.PieceStore { | ||
ps := piecestore.NewPieceStore(datastore.NewMapDatastore()) | ||
_, err := ps.GetPieceInfo(pieceCid) | ||
assert.Error(t, err) | ||
return ps | ||
} | ||
|
||
// Add a deal info | ||
t.Run("can add deals", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
dealInfo := piecestore.DealInfo{ | ||
DealID: rand.Uint64(), | ||
SectorID: rand.Uint64(), | ||
Offset: rand.Uint64(), | ||
Length: rand.Uint64(), | ||
} | ||
err := ps.AddDealForPiece(pieceCid, dealInfo) | ||
assert.NoError(t, err) | ||
|
||
pi, err := ps.GetPieceInfo(pieceCid) | ||
assert.NoError(t, err) | ||
assert.Len(t, pi.Deals, 1) | ||
assert.Equal(t, pi.Deals[0], dealInfo) | ||
}) | ||
|
||
t.Run("adding same deal twice does not dup", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
dealInfo := piecestore.DealInfo{ | ||
DealID: rand.Uint64(), | ||
SectorID: rand.Uint64(), | ||
Offset: rand.Uint64(), | ||
Length: rand.Uint64(), | ||
} | ||
err := ps.AddDealForPiece(pieceCid, dealInfo) | ||
assert.NoError(t, err) | ||
|
||
pi, err := ps.GetPieceInfo(pieceCid) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I should think you could skip the error/value checking on the first try since it's tested above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just follow past of least resistance to get no lint-stage errors |
||
assert.NoError(t, err) | ||
assert.Len(t, pi.Deals, 1) | ||
assert.Equal(t, pi.Deals[0], dealInfo) | ||
|
||
err = ps.AddDealForPiece(pieceCid, dealInfo) | ||
assert.NoError(t, err) | ||
|
||
pi, err = ps.GetPieceInfo(pieceCid) | ||
assert.NoError(t, err) | ||
assert.Len(t, pi.Deals, 1) | ||
assert.Equal(t, pi.Deals[0], dealInfo) | ||
}) | ||
} | ||
|
||
func TestStoreCIDInfo(t *testing.T) { | ||
|
||
pieceCid1 := []byte{1, 2, 3, 4} | ||
pieceCid2 := []byte{5, 6, 7, 8} | ||
testCIDs := shared_testutil.GenerateCids(3) | ||
blockLocations := make([]piecestore.BlockLocation, 0, 3) | ||
for i := 0; i < 3; i++ { | ||
blockLocations = append(blockLocations, piecestore.BlockLocation{ | ||
RelOffset: rand.Uint64(), | ||
BlockSize: rand.Uint64(), | ||
}) | ||
} | ||
|
||
_, err := ps.GetPieceInfo(pieceCid) | ||
assert.Error(t, err) | ||
|
||
// Add a PieceInfo and some state | ||
testCid, err := cid.Decode("bafzbeigai3eoy2ccc7ybwjfz5r3rdxqrinwi4rwytly24tdbh6yk7zslrm") | ||
assert.NoError(t, err) | ||
blockInfos := []piecestore.BlockInfo{{testCid, 42, 43}} | ||
|
||
err = ps.AddBlockInfosToPiece(pieceCid, blockInfos) | ||
assert.NoError(t, err) | ||
has, err := ps.HasBlockInfo(pieceCid) | ||
assert.True(t, has) | ||
assert.NoError(t, err) | ||
has, err = ps.HasDealInfo(pieceCid) | ||
assert.False(t, has) | ||
assert.NoError(t, err) | ||
|
||
pi, err := ps.GetPieceInfo(pieceCid) | ||
assert.NoError(t, err) | ||
assert.Len(t, pi.Blocks, 1) | ||
assert.Equal(t, pi.Blocks[0], piecestore.BlockInfo{testCid, 42, 43}) | ||
|
||
dealInfo := piecestore.DealInfo{ | ||
DealID: rand.Uint64(), | ||
SectorID: rand.Uint64(), | ||
Offset: rand.Uint64(), | ||
Length: rand.Uint64(), | ||
initializePieceStore := func(t *testing.T) piecestore.PieceStore { | ||
ps := piecestore.NewPieceStore(datastore.NewMapDatastore()) | ||
_, err := ps.GetCIDInfo(testCIDs[0]) | ||
assert.Error(t, err) | ||
return ps | ||
} | ||
err = ps.AddDealForPiece(pieceCid, dealInfo) | ||
assert.NoError(t, err) | ||
|
||
has, err = ps.HasBlockInfo(pieceCid) | ||
assert.True(t, has) | ||
assert.NoError(t, err) | ||
has, err = ps.HasDealInfo(pieceCid) | ||
assert.True(t, has) | ||
assert.NoError(t, err) | ||
pi, err = ps.GetPieceInfo(pieceCid) | ||
assert.NoError(t, err) | ||
assert.Len(t, pi.Deals, 1) | ||
assert.Equal(t, pi.Deals[0], dealInfo) | ||
|
||
t.Run("can add piece block locations", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
err := ps.AddPieceBlockLocations(pieceCid1, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[0]: blockLocations[0], | ||
testCIDs[1]: blockLocations[1], | ||
testCIDs[2]: blockLocations[2], | ||
}) | ||
assert.NoError(t, err) | ||
|
||
ci, err := ps.GetCIDInfo(testCIDs[0]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[0], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[1]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[1], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[2]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[2], pieceCid1}) | ||
}) | ||
|
||
t.Run("overlapping adds", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
err := ps.AddPieceBlockLocations(pieceCid1, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[0]: blockLocations[0], | ||
testCIDs[1]: blockLocations[2], | ||
}) | ||
assert.NoError(t, err) | ||
err = ps.AddPieceBlockLocations(pieceCid2, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[1]: blockLocations[1], | ||
testCIDs[2]: blockLocations[2], | ||
}) | ||
assert.NoError(t, err) | ||
|
||
ci, err := ps.GetCIDInfo(testCIDs[0]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[0], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[1]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 2) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[2], pieceCid1}) | ||
assert.Equal(t, ci.PieceBlockLocations[1], piecestore.PieceBlockLocation{blockLocations[1], pieceCid2}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[2]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[2], pieceCid2}) | ||
}) | ||
|
||
t.Run("duplicate adds", func(t *testing.T) { | ||
ps := initializePieceStore(t) | ||
err := ps.AddPieceBlockLocations(pieceCid1, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[0]: blockLocations[0], | ||
testCIDs[1]: blockLocations[1], | ||
}) | ||
assert.NoError(t, err) | ||
err = ps.AddPieceBlockLocations(pieceCid1, map[cid.Cid]piecestore.BlockLocation{ | ||
testCIDs[1]: blockLocations[1], | ||
testCIDs[2]: blockLocations[2], | ||
}) | ||
assert.NoError(t, err) | ||
|
||
ci, err := ps.GetCIDInfo(testCIDs[0]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[0], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[1]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[1], pieceCid1}) | ||
|
||
ci, err = ps.GetCIDInfo(testCIDs[2]) | ||
assert.NoError(t, err) | ||
assert.Len(t, ci.PieceBlockLocations, 1) | ||
assert.Equal(t, ci.PieceBlockLocations[0], piecestore.PieceBlockLocation{blockLocations[2], pieceCid1}) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call it canHasPieceInfo /jk