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

fix(list): throw error if allocation is invalid #599

Merged
merged 14 commits into from
Oct 26, 2022
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
13 changes: 11 additions & 2 deletions zboxcore/sdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,12 @@ func (a *Allocation) ListDirFromAuthTicket(authTicket string, lookupHash string)
listReq.ctx = a.ctx
listReq.remotefilepathhash = lookupHash
listReq.authToken = at
ref := listReq.GetListFromBlobbers()
ref, err := listReq.GetListFromBlobbers()

if err != nil {
return nil, err
}

if ref != nil {
return ref, nil
}
Expand Down Expand Up @@ -685,7 +690,11 @@ func (a *Allocation) ListDir(path string) (*ListResult, error) {
listReq.consensusThresh = a.consensusThreshold
listReq.ctx = a.ctx
listReq.remotefilepath = path
ref := listReq.GetListFromBlobbers()
ref, err := listReq.GetListFromBlobbers()
if err != nil {
return nil, err
}

if ref != nil {
return ref, nil
}
Expand Down
10 changes: 8 additions & 2 deletions zboxcore/sdk/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,8 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) {
a.Blobbers = nil
}
},
wantErr: true,
errMsg: "error from server list response: ",
},
{
name: "Test_Success",
Expand Down Expand Up @@ -1738,8 +1740,10 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) {
setupMockGetFileInfoResponse(t, &mockClient)
a.InitAllocation()
sdkInitialized = true
for i := 0; i < numBlobbers; i++ {
a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{})
if len(a.Blobbers) == 0 {
for i := 0; i < numBlobbers; i++ {
a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{})
}
}

got, err := a.ListDirFromAuthTicket(authTicket, tt.parameters.lookupHash)
Expand Down Expand Up @@ -1977,6 +1981,8 @@ func TestAllocation_listDir(t *testing.T) {
setupMockHttpResponse(t, mockClient, "TestAllocation_listDir", testCaseName, a, http.MethodGet, http.StatusBadRequest, []byte(""))
return nil
},
wantErr: true,
errMsg: "error from server list response: ",
},
{
name: "Test_Success",
Expand Down
18 changes: 15 additions & 3 deletions zboxcore/sdk/listworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,22 @@ func (req *ListRequest) getlistFromBlobbers() []*listResponse {
return listInfos
}

func (req *ListRequest) GetListFromBlobbers() *ListResult {
func (req *ListRequest) GetListFromBlobbers() (*ListResult, error) {
lR := req.getlistFromBlobbers()
result := &ListResult{}
selected := make(map[string]*ListResult)
childResultMap := make(map[string]*ListResult)
var err error
var errNum int
for i := 0; i < len(lR); i++ {
req.consensus = 0
ti := lR[i]
if ti.err != nil || ti.ref == nil {
if ti.err != nil {
err = ti.err
errNum++
continue
cnlangzi marked this conversation as resolved.
Show resolved Hide resolved
}
if ti.ref == nil {
continue
}

Expand Down Expand Up @@ -217,5 +224,10 @@ func (req *ListRequest) GetListFromBlobbers() *ListResult {
result.Size += child.Size
}
}
return result

if errNum == len(lR) {
return nil, err
}

return result, nil
}
2 changes: 1 addition & 1 deletion zboxcore/sdk/listworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func TestListRequest_GetListFromBlobbers(t *testing.T) {
Baseurl: tt.name + mockBlobberUrl + strconv.Itoa(i),
})
}
got := req.GetListFromBlobbers()
got, _ := req.GetListFromBlobbers()
expectedResult := &ListResult{
Type: mockType,
Size: -1,
Expand Down