Skip to content

Commit

Permalink
fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Jul 3, 2024
1 parent 9d73b73 commit fb043fa
Showing 1 changed file with 86 additions and 32 deletions.
118 changes: 86 additions & 32 deletions blob/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ func TestBlobService_Get(t *testing.T) {

appBlobs, err := blobtest.GenerateV0Blobs([]int{blobSize0, blobSize1}, false)
require.NoError(t, err)
blobs0, err := convertBlobs(appBlobs...)
blobsWithDiffNamespaces, err := convertBlobs(appBlobs...)
require.NoError(t, err)

appBlobs, err = blobtest.GenerateV0Blobs([]int{blobSize2, blobSize3}, true)
require.NoError(t, err)
blobs1, err := convertBlobs(appBlobs...)
blobsWithSameNamespace, err := convertBlobs(appBlobs...)
require.NoError(t, err)

service := createService(ctx, t, append(blobs0, blobs1...))
service := createService(ctx, t, append(blobsWithDiffNamespaces, blobsWithSameNamespace...))
test := []struct {
name string
doFn func() (interface{}, error)
Expand All @@ -60,7 +60,10 @@ func TestBlobService_Get(t *testing.T) {
{
name: "get single blob",
doFn: func() (interface{}, error) {
b, err := service.Get(ctx, 1, blobs0[0].Namespace(), blobs0[0].Commitment)
b, err := service.Get(ctx, 1,
blobsWithDiffNamespaces[0].Namespace(),
blobsWithDiffNamespaces[0].Commitment,
)
return []*Blob{b}, err
},
expectedResult: func(res interface{}, err error) {
Expand All @@ -71,13 +74,13 @@ func TestBlobService_Get(t *testing.T) {
assert.True(t, ok)
assert.Len(t, blobs, 1)

assert.Equal(t, blobs0[0].Commitment, blobs[0].Commitment)
assert.Equal(t, blobsWithDiffNamespaces[0].Commitment, blobs[0].Commitment)
},
},
{
name: "get all with the same namespace",
doFn: func() (interface{}, error) {
return service.GetAll(ctx, 1, []share.Namespace{blobs1[0].Namespace()})
return service.GetAll(ctx, 1, []share.Namespace{blobsWithSameNamespace[0].Namespace()})
},
expectedResult: func(res interface{}, err error) {
require.NoError(t, err)
Expand All @@ -88,19 +91,25 @@ func TestBlobService_Get(t *testing.T) {

assert.Len(t, blobs, 2)

for i := range blobs1 {
require.Equal(t, blobs1[i].Commitment, blobs[i].Commitment)
for i := range blobsWithSameNamespace {
require.Equal(t, blobsWithSameNamespace[i].Commitment, blobs[i].Commitment)
}
},
},
{
name: "verify indexes",
doFn: func() (interface{}, error) {
b0, err := service.Get(ctx, 1, blobs0[0].Namespace(), blobs0[0].Commitment)
b0, err := service.Get(ctx, 1,
blobsWithDiffNamespaces[0].Namespace(),
blobsWithDiffNamespaces[0].Commitment,
)
require.NoError(t, err)
b1, err := service.Get(ctx, 1, blobs0[1].Namespace(), blobs0[1].Commitment)
b1, err := service.Get(ctx, 1,
blobsWithDiffNamespaces[1].Namespace(),
blobsWithDiffNamespaces[1].Commitment,
)
require.NoError(t, err)
b23, err := service.GetAll(ctx, 1, []share.Namespace{blobs1[0].Namespace()})
b23, err := service.GetAll(ctx, 1, []share.Namespace{blobsWithSameNamespace[0].Namespace()})
require.NoError(t, err)
return []*Blob{b0, b1, b23[0], b23[1]}, nil
},
Expand Down Expand Up @@ -138,7 +147,12 @@ func TestBlobService_Get(t *testing.T) {
doFn: func() (interface{}, error) {
nid, err := share.NewBlobNamespaceV0(tmrand.Bytes(7))
require.NoError(t, err)
b, err := service.GetAll(ctx, 1, []share.Namespace{blobs0[0].Namespace(), nid, blobs0[1].Namespace()})
b, err := service.GetAll(ctx, 1,
[]share.Namespace{
blobsWithDiffNamespaces[0].Namespace(), nid,
blobsWithDiffNamespaces[1].Namespace(),
},
)
return b, err
},
expectedResult: func(res interface{}, err error) {
Expand All @@ -149,14 +163,17 @@ func TestBlobService_Get(t *testing.T) {
assert.NotEmpty(t, blobs)
assert.Len(t, blobs, 2)
// check the order
require.True(t, bytes.Equal(blobs[0].Namespace(), blobs0[0].Namespace()))
require.True(t, bytes.Equal(blobs[1].Namespace(), blobs0[1].Namespace()))
require.True(t, bytes.Equal(blobs[0].Namespace(), blobsWithDiffNamespaces[0].Namespace()))
require.True(t, bytes.Equal(blobs[1].Namespace(), blobsWithDiffNamespaces[1].Namespace()))
},
},
{
name: "get blob with incorrect commitment",
doFn: func() (interface{}, error) {
b, err := service.Get(ctx, 1, blobs0[0].Namespace(), blobs0[1].Commitment)
b, err := service.Get(ctx, 1,
blobsWithDiffNamespaces[0].Namespace(),
blobsWithDiffNamespaces[1].Commitment,
)
return []*Blob{b}, err
},
expectedResult: func(res interface{}, err error) {
Expand Down Expand Up @@ -189,7 +206,10 @@ func TestBlobService_Get(t *testing.T) {
{
name: "get proof",
doFn: func() (interface{}, error) {
proof, err := service.GetProof(ctx, 1, blobs0[1].Namespace(), blobs0[1].Commitment)
proof, err := service.GetProof(ctx, 1,
blobsWithDiffNamespaces[1].Namespace(),
blobsWithDiffNamespaces[1].Commitment,
)
return proof, err
},
expectedResult: func(res interface{}, err error) {
Expand All @@ -216,17 +236,24 @@ func TestBlobService_Get(t *testing.T) {
t.Fatal("could not prove the shares")
}

rawShares, err := BlobsToShares(blobs0[1])
rawShares, err := BlobsToShares(blobsWithDiffNamespaces[1])
require.NoError(t, err)
verifyFn(t, rawShares, proof, blobs0[1].Namespace())
verifyFn(t, rawShares, proof, blobsWithDiffNamespaces[1].Namespace())
},
},
{
name: "verify inclusion",
doFn: func() (interface{}, error) {
proof, err := service.GetProof(ctx, 1, blobs0[0].Namespace(), blobs0[0].Commitment)
proof, err := service.GetProof(ctx, 1,
blobsWithDiffNamespaces[0].Namespace(),
blobsWithDiffNamespaces[0].Commitment,
)
require.NoError(t, err)
return service.Included(ctx, 1, blobs0[0].Namespace(), proof, blobs0[0].Commitment)
return service.Included(ctx, 1,
blobsWithDiffNamespaces[0].Namespace(),
proof,
blobsWithDiffNamespaces[0].Commitment,
)
},
expectedResult: func(res interface{}, err error) {
require.NoError(t, err)
Expand All @@ -238,9 +265,16 @@ func TestBlobService_Get(t *testing.T) {
{
name: "verify inclusion fails with different proof",
doFn: func() (interface{}, error) {
proof, err := service.GetProof(ctx, 1, blobs0[1].Namespace(), blobs0[1].Commitment)
proof, err := service.GetProof(ctx, 1,
blobsWithDiffNamespaces[1].Namespace(),
blobsWithDiffNamespaces[1].Commitment,
)
require.NoError(t, err)
return service.Included(ctx, 1, blobs0[0].Namespace(), proof, blobs0[0].Commitment)
return service.Included(ctx, 1,
blobsWithDiffNamespaces[0].Namespace(),
proof,
blobsWithDiffNamespaces[0].Commitment,
)
},
expectedResult: func(res interface{}, err error) {
require.Error(t, err)
Expand All @@ -258,7 +292,10 @@ func TestBlobService_Get(t *testing.T) {
blob, err := convertBlobs(appBlob...)
require.NoError(t, err)

proof, err := service.GetProof(ctx, 1, blobs0[1].Namespace(), blobs0[1].Commitment)
proof, err := service.GetProof(ctx, 1,
blobsWithDiffNamespaces[1].Namespace(),
blobsWithDiffNamespaces[1].Commitment,
)
require.NoError(t, err)
return service.Included(ctx, 1, blob[0].Namespace(), proof, blob[0].Commitment)
},
Expand All @@ -272,11 +309,17 @@ func TestBlobService_Get(t *testing.T) {
{
name: "count proofs for the blob",
doFn: func() (interface{}, error) {
proof0, err := service.GetProof(ctx, 1, blobs0[0].Namespace(), blobs0[0].Commitment)
proof0, err := service.GetProof(ctx, 1,
blobsWithDiffNamespaces[0].Namespace(),
blobsWithDiffNamespaces[0].Commitment,
)
if err != nil {
return nil, err
}
proof1, err := service.GetProof(ctx, 1, blobs0[1].Namespace(), blobs0[1].Commitment)
proof1, err := service.GetProof(ctx, 1,
blobsWithDiffNamespaces[1].Namespace(),
blobsWithDiffNamespaces[1].Commitment,
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -308,13 +351,16 @@ func TestBlobService_Get(t *testing.T) {
blobs, ok := i.([]*Blob)
require.True(t, ok)
assert.Nil(t, blobs)
assert.Nil(t, err)
assert.NoError(t, err)
},
},
{
name: "marshal proof",
doFn: func() (interface{}, error) {
proof, err := service.GetProof(ctx, 1, blobs0[1].Namespace(), blobs0[1].Commitment)
proof, err := service.GetProof(ctx, 1,
blobsWithDiffNamespaces[1].Namespace(),
blobsWithDiffNamespaces[1].Commitment,
)
require.NoError(t, err)
return json.Marshal(proof)
},
Expand All @@ -325,7 +371,10 @@ func TestBlobService_Get(t *testing.T) {
var proof Proof
require.NoError(t, json.Unmarshal(jsonData, &proof))

newProof, err := service.GetProof(ctx, 1, blobs0[1].Namespace(), blobs0[1].Commitment)
newProof, err := service.GetProof(ctx, 1,
blobsWithDiffNamespaces[1].Namespace(),
blobsWithDiffNamespaces[1].Commitment,
)
require.NoError(t, err)
require.NoError(t, proof.equal(*newProof))
},
Expand All @@ -340,23 +389,28 @@ func TestBlobService_Get(t *testing.T) {
GetSharesByNamespace(gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(
func(ctx context.Context, h *header.ExtendedHeader, ns share.Namespace) (share.NamespacedShares, error) {
if ns.Equals(blobs0[0].Namespace()) {
if ns.Equals(blobsWithDiffNamespaces[0].Namespace()) {
return nil, errors.New("internal error")
}
return shareService.GetSharesByNamespace(ctx, h, ns)
}).AnyTimes()

service.shareGetter = shareGetterMock
return service.GetAll(ctx, 1, []share.Namespace{blobs0[0].Namespace(), blobs1[0].Namespace()})
return service.GetAll(ctx, 1,
[]share.Namespace{
blobsWithDiffNamespaces[0].Namespace(),
blobsWithSameNamespace[0].Namespace(),
},
)
},
expectedResult: func(res interface{}, err error) {
blobs, ok := res.([]*Blob)
assert.True(t, ok)
assert.Error(t, err)
assert.Contains(t, err.Error(), "internal error")
assert.Equal(t, blobs[0].Namespace(), blobs1[0].Namespace())
assert.Equal(t, blobs[0].Namespace(), blobsWithSameNamespace[0].Namespace())
assert.NotEmpty(t, blobs)
assert.Len(t, blobs, len(blobs1))
assert.Len(t, blobs, len(blobsWithSameNamespace))
},
},
}
Expand Down

0 comments on commit fb043fa

Please sign in to comment.