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

feat!: adding Namespace to remaining endpoints #35

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ go-da defines a generic Data Availability interface for modular blockchains.

## DA Interface

| Method | Params | Return |
| ----------- |-------------------------------| -------------|
| `MaxBlobSize` | | `uint64` |
| `Get` | `ids []ID` | `[]Blobs` |
| `GetIDs` | `height uint64` | `[]ID` |
| `Commit` | `blobs []Blob` | `[]Commitment` |
| `Validate` | `ids []Blob, proofs []Proof` | `[]bool` |
| Method | Params | Return |
| ------------- | -------------------------------------------------------- | --------------- |
| `MaxBlobSize` | | `uint64` |
| `Get` | `ids []ID, namespace Namespace` | `[]Blobs` |
| `GetIDs` | `height uint64, namespace Namespace` | `[]ID` |
| `Commit` | `blobs []Blob, namespace Namespace` | `[]Commitment` |
| `Validate` | `ids []Blob, proofs []Proof, namespace Namespace` | `[]bool` |
| `Submit` | `blobs []Blob, gasPrice float64, namespace Namespace` | `[]ID, []Proof` |

## Implementations

Expand Down
16 changes: 5 additions & 11 deletions da.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,23 @@ type DA interface {
//
// Error should be returned if ID is not formatted properly, there is no Blob for given ID or any other client-level
// error occurred (dropped connection, timeout, etc).
Get(ctx context.Context, ids []ID) ([]Blob, error)
Get(ctx context.Context, ids []ID, namespace Namespace) ([]Blob, error)

// GetIDs returns IDs of all Blobs located in DA at given height.
GetIDs(ctx context.Context, height uint64) ([]ID, error)
GetIDs(ctx context.Context, height uint64, namespace Namespace) ([]ID, error)

// Commit creates a Commitment for each given Blob.
Commit(ctx context.Context, blobs []Blob) ([]Commitment, error)
Commit(ctx context.Context, blobs []Blob, namespace Namespace) ([]Commitment, error)

// Submit submits the Blobs to Data Availability layer.
//
// This method is synchronous. Upon successful submission to Data Availability layer, it returns ID identifying blob
// in DA and Proof of inclusion.
// If options is nil, default options are used.
Submit(ctx context.Context, blobs []Blob, opts *SubmitOptions) ([]ID, []Proof, error)
Submit(ctx context.Context, blobs []Blob, gasPrice float64, namespace Namespace) ([]ID, []Proof, error)

// Validate validates Commitments against the corresponding Proofs. This should be possible without retrieving the Blobs.
Validate(ctx context.Context, ids []ID, proofs []Proof) ([]bool, error)
}

// SubmitOptions are the parameters used for blob submission.
type SubmitOptions struct {
GasPrice float64
Namespace Namespace
Validate(ctx context.Context, ids []ID, proofs []Proof, namespace Namespace) ([]bool, error)
}

// Namespace is an optional parameter used to set the location a blob should be
Expand Down
4 changes: 4 additions & 0 deletions proto/da/da.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ message MaxBlobSizeResponse {
// GetRequest is the request type for the Get rpc method.
message GetRequest {
repeated ID ids = 1;
Namespace namespace = 2;
}

// GetResponse is the response type for the Get rpc method.
Expand All @@ -69,6 +70,7 @@ message GetResponse {
// GetIDsRequest is the request type for the GetIDs rpc method.
message GetIDsRequest {
uint64 height = 1;
Namespace namespace = 2;
}

// GetIDsResponse is the response type for the GetIDs rpc method.
Expand All @@ -79,6 +81,7 @@ message GetIDsResponse {
// CommitRequest is the request type for the Commit rpc method.
message CommitRequest {
repeated Blob blobs = 1;
Namespace namespace = 2;
}

// CommitResponse is the response type for the Commit rpc method.
Expand All @@ -103,6 +106,7 @@ message SubmitResponse {
message ValidateRequest {
repeated ID ids = 1;
repeated Proof proofs = 2;
Namespace namespace = 3;
}

// ValidateResponse is the response type for the Validate rpc method.
Expand Down
27 changes: 15 additions & 12 deletions proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ func (c *Client) MaxBlobSize(ctx context.Context) (uint64, error) {
}

// Get returns Blob for each given ID, or an error.
func (c *Client) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
func (c *Client) Get(ctx context.Context, ids []da.ID, namespace da.Namespace) ([]da.Blob, error) {
req := &pbda.GetRequest{
Ids: make([]*pbda.ID, len(ids)),
Ids: make([]*pbda.ID, len(ids)),
Namespace: &pbda.Namespace{Value: namespace},
}
for i := range ids {
req.Ids[i] = &pbda.ID{Value: ids[i]}
Expand All @@ -64,8 +65,8 @@ func (c *Client) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
}

// GetIDs returns IDs of all Blobs located in DA at given height.
func (c *Client) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
req := &pbda.GetIDsRequest{Height: height}
func (c *Client) GetIDs(ctx context.Context, height uint64, namespace da.Namespace) ([]da.ID, error) {
req := &pbda.GetIDsRequest{Height: height, Namespace: &pbda.Namespace{Value: namespace}}
resp, err := c.client.GetIDs(ctx, req)
if err != nil {
return nil, err
Expand All @@ -75,9 +76,10 @@ func (c *Client) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
}

// Commit creates a Commitment for each given Blob.
func (c *Client) Commit(ctx context.Context, blobs []da.Blob) ([]da.Commitment, error) {
func (c *Client) Commit(ctx context.Context, blobs []da.Blob, namespace da.Namespace) ([]da.Commitment, error) {
req := &pbda.CommitRequest{
Blobs: blobsDA2PB(blobs),
Blobs: blobsDA2PB(blobs),
Namespace: &pbda.Namespace{Value: namespace},
}

resp, err := c.client.Commit(ctx, req)
Expand All @@ -89,11 +91,11 @@ func (c *Client) Commit(ctx context.Context, blobs []da.Blob) ([]da.Commitment,
}

// Submit submits the Blobs to Data Availability layer.
func (c *Client) Submit(ctx context.Context, blobs []da.Blob, opts *da.SubmitOptions) ([]da.ID, []da.Proof, error) {
func (c *Client) Submit(ctx context.Context, blobs []da.Blob, gasPrice float64, namespace da.Namespace) ([]da.ID, []da.Proof, error) {
req := &pbda.SubmitRequest{
Blobs: blobsDA2PB(blobs),
GasPrice: opts.GasPrice,
Namespace: &pbda.Namespace{Value: opts.Namespace},
GasPrice: gasPrice,
Namespace: &pbda.Namespace{Value: namespace},
}

resp, err := c.client.Submit(ctx, req)
Expand All @@ -112,10 +114,11 @@ func (c *Client) Submit(ctx context.Context, blobs []da.Blob, opts *da.SubmitOpt
}

// Validate validates Commitments against the corresponding Proofs. This should be possible without retrieving the Blobs.
func (c *Client) Validate(ctx context.Context, ids []da.ID, proofs []da.Proof) ([]bool, error) {
func (c *Client) Validate(ctx context.Context, ids []da.ID, proofs []da.Proof, namespace da.Namespace) ([]bool, error) {
req := &pbda.ValidateRequest{
Ids: idsDA2PB(ids),
Proofs: proofsDA2PB(proofs),
Ids: idsDA2PB(ids),
Proofs: proofsDA2PB(proofs),
Namespace: &pbda.Namespace{Value: namespace},
}
resp, err := c.client.Validate(ctx, req)
return resp.Results, err
Expand Down
17 changes: 8 additions & 9 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@
target da.DA
}

func (p *proxySrv) MaxBlobSize(ctx context.Context, request *pbda.MaxBlobSizeRequest) (*pbda.MaxBlobSizeResponse, error) {
func (p *proxySrv) MaxBlobSize(
ctx context.Context, request *pbda.MaxBlobSizeRequest,
) (*pbda.MaxBlobSizeResponse, error) {

Check warning on line 29 in proxy/server.go

View check run for this annotation

Codecov / codecov/patch

proxy/server.go#L29

Added line #L29 was not covered by tests
maxBlobSize, err := p.target.MaxBlobSize(ctx)
return &pbda.MaxBlobSizeResponse{MaxBlobSize: maxBlobSize}, err
}

func (p *proxySrv) Get(ctx context.Context, request *pbda.GetRequest) (*pbda.GetResponse, error) {
ids := idsPB2DA(request.Ids)
blobs, err := p.target.Get(ctx, ids)
blobs, err := p.target.Get(ctx, ids, request.Namespace.GetValue())
return &pbda.GetResponse{Blobs: blobsDA2PB(blobs)}, err
}

func (p *proxySrv) GetIDs(ctx context.Context, request *pbda.GetIDsRequest) (*pbda.GetIDsResponse, error) {
ids, err := p.target.GetIDs(ctx, request.Height)
ids, err := p.target.GetIDs(ctx, request.Height, request.Namespace.GetValue())
if err != nil {
return nil, err
}
Expand All @@ -46,7 +48,7 @@

func (p *proxySrv) Commit(ctx context.Context, request *pbda.CommitRequest) (*pbda.CommitResponse, error) {
blobs := blobsPB2DA(request.Blobs)
commits, err := p.target.Commit(ctx, blobs)
commits, err := p.target.Commit(ctx, blobs, request.Namespace.GetValue())
if err != nil {
return nil, err
}
Expand All @@ -57,10 +59,7 @@
func (p *proxySrv) Submit(ctx context.Context, request *pbda.SubmitRequest) (*pbda.SubmitResponse, error) {
blobs := blobsPB2DA(request.Blobs)

ids, proofs, err := p.target.Submit(ctx, blobs, &da.SubmitOptions{
GasPrice: request.GasPrice,
Namespace: request.Namespace.GetValue(),
})
ids, proofs, err := p.target.Submit(ctx, blobs, request.GasPrice, request.Namespace.GetValue())
if err != nil {
return nil, err
}
Expand All @@ -82,7 +81,7 @@
ids := idsPB2DA(request.Ids)
proofs := proofsPB2DA(request.Proofs)
//TODO implement me
validity, err := p.target.Validate(ctx, ids, proofs)
validity, err := p.target.Validate(ctx, ids, proofs, request.Namespace.GetValue())
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (d *DummyDA) MaxBlobSize(ctx context.Context) (uint64, error) {
}

// Get returns Blobs for given IDs.
func (d *DummyDA) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
func (d *DummyDA) Get(ctx context.Context, ids []da.ID, _ da.Namespace) ([]da.Blob, error) {
d.mu.Lock()
defer d.mu.Unlock()
blobs := make([]da.Blob, len(ids))
Expand All @@ -79,7 +79,7 @@ func (d *DummyDA) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
}

// GetIDs returns IDs of Blobs at given DA height.
func (d *DummyDA) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
func (d *DummyDA) GetIDs(ctx context.Context, height uint64, _ da.Namespace) ([]da.ID, error) {
d.mu.Lock()
defer d.mu.Unlock()
kvps := d.data[height]
Expand All @@ -91,7 +91,7 @@ func (d *DummyDA) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
}

// Commit returns cryptographic Commitments for given blobs.
func (d *DummyDA) Commit(ctx context.Context, blobs []da.Blob) ([]da.Commitment, error) {
func (d *DummyDA) Commit(ctx context.Context, blobs []da.Blob, _ da.Namespace) ([]da.Commitment, error) {
commits := make([]da.Commitment, len(blobs))
for i, blob := range blobs {
commits[i] = d.getHash(blob)
Expand All @@ -100,7 +100,7 @@ func (d *DummyDA) Commit(ctx context.Context, blobs []da.Blob) ([]da.Commitment,
}

// Submit stores blobs in DA layer.
func (d *DummyDA) Submit(ctx context.Context, blobs []da.Blob, opts *da.SubmitOptions) ([]da.ID, []da.Proof, error) {
func (d *DummyDA) Submit(ctx context.Context, blobs []da.Blob, gasPrice float64, namespace da.Namespace) ([]da.ID, []da.Proof, error) {
d.mu.Lock()
defer d.mu.Unlock()
ids := make([]da.ID, len(blobs))
Expand All @@ -117,7 +117,7 @@ func (d *DummyDA) Submit(ctx context.Context, blobs []da.Blob, opts *da.SubmitOp
}

// Validate checks the Proofs for given IDs.
func (d *DummyDA) Validate(ctx context.Context, ids []da.ID, proofs []da.Proof) ([]bool, error) {
func (d *DummyDA) Validate(ctx context.Context, ids []da.ID, proofs []da.Proof, _ da.Namespace) ([]bool, error) {
if len(ids) != len(proofs) {
return nil, errors.New("number of IDs doesn't equal to number of proofs")
}
Expand Down
49 changes: 18 additions & 31 deletions test/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/rollkit/go-da"
)

var testNamespace = da.Namespace([]byte("test"))

// RunDATestSuite runs all tests against given DA
func RunDATestSuite(t *testing.T, d da.DA) {
t.Run("Basic DA test", func(t *testing.T) {
Expand All @@ -34,67 +36,58 @@ func BasicDATest(t *testing.T, d da.DA) {
msg2 := []byte("message 2")

ctx := context.TODO()
id1, proof1, err := d.Submit(ctx, []da.Blob{msg1}, &da.SubmitOptions{
GasPrice: 0,
Namespace: []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
})
id1, proof1, err := d.Submit(ctx, []da.Blob{msg1}, 0, testNamespace)
assert.NoError(t, err)
assert.NotEmpty(t, id1)
assert.NotEmpty(t, proof1)

id2, proof2, err := d.Submit(ctx, []da.Blob{msg2}, &da.SubmitOptions{
GasPrice: 0,
Namespace: []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
})
id2, proof2, err := d.Submit(ctx, []da.Blob{msg2}, 0, testNamespace)
assert.NoError(t, err)
assert.NotEmpty(t, id2)
assert.NotEmpty(t, proof2)

id3, proof3, err := d.Submit(ctx, []da.Blob{msg1}, &da.SubmitOptions{
GasPrice: 0,
Namespace: []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
})
id3, proof3, err := d.Submit(ctx, []da.Blob{msg1}, 0, testNamespace)
assert.NoError(t, err)
assert.NotEmpty(t, id3)
assert.NotEmpty(t, proof3)

assert.NotEqual(t, id1, id2)
assert.NotEqual(t, id1, id3)

ret, err := d.Get(ctx, id1)
ret, err := d.Get(ctx, id1, testNamespace)
assert.NoError(t, err)
assert.Equal(t, []da.Blob{msg1}, ret)

commitment1, err := d.Commit(ctx, []da.Blob{msg1})
commitment1, err := d.Commit(ctx, []da.Blob{msg1}, []byte{})
assert.NoError(t, err)
assert.NotEmpty(t, commitment1)

commitment2, err := d.Commit(ctx, []da.Blob{msg2})
commitment2, err := d.Commit(ctx, []da.Blob{msg2}, []byte{})
assert.NoError(t, err)
assert.NotEmpty(t, commitment2)

oks, err := d.Validate(ctx, id1, proof1)
oks, err := d.Validate(ctx, id1, proof1, testNamespace)
assert.NoError(t, err)
assert.NotEmpty(t, oks)
for _, ok := range oks {
assert.True(t, ok)
}

oks, err = d.Validate(ctx, id2, proof2)
oks, err = d.Validate(ctx, id2, proof2, testNamespace)
assert.NoError(t, err)
assert.NotEmpty(t, oks)
for _, ok := range oks {
assert.True(t, ok)
}

oks, err = d.Validate(ctx, id1, proof2)
oks, err = d.Validate(ctx, id1, proof2, testNamespace)
assert.NoError(t, err)
assert.NotEmpty(t, oks)
for _, ok := range oks {
assert.False(t, ok)
}

oks, err = d.Validate(ctx, id2, proof1)
oks, err = d.Validate(ctx, id2, proof1, testNamespace)
assert.NoError(t, err)
assert.NotEmpty(t, oks)
for _, ok := range oks {
Expand All @@ -105,7 +98,7 @@ func BasicDATest(t *testing.T, d da.DA) {
// CheckErrors ensures that errors are handled properly by DA.
func CheckErrors(t *testing.T, d da.DA) {
ctx := context.TODO()
blob, err := d.Get(ctx, []da.ID{[]byte("invalid")})
blob, err := d.Get(ctx, []da.ID{[]byte("invalid")}, testNamespace)
assert.Error(t, err)
assert.Empty(t, blob)
}
Expand All @@ -115,10 +108,7 @@ func GetIDsTest(t *testing.T, d da.DA) {
msgs := [][]byte{[]byte("msg1"), []byte("msg2"), []byte("msg3")}

ctx := context.TODO()
ids, proofs, err := d.Submit(ctx, msgs, &da.SubmitOptions{
GasPrice: 0,
Namespace: []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
})
ids, proofs, err := d.Submit(ctx, msgs, 0, []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0})
assert.NoError(t, err)
assert.Len(t, ids, len(msgs))
assert.Len(t, proofs, len(msgs))
Expand All @@ -130,12 +120,12 @@ func GetIDsTest(t *testing.T, d da.DA) {
// As we're the only user, we don't need to handle external data (that could be submitted in real world).
// There is no notion of height, so we need to scan the DA to get test data back.
for i := uint64(1); !found && !time.Now().After(end); i++ {
ret, err := d.GetIDs(ctx, i)
ret, err := d.GetIDs(ctx, i, []byte{})
if err != nil {
t.Error("failed to get IDs:", err)
}
if len(ret) > 0 {
blobs, err := d.Get(ctx, ret)
blobs, err := d.Get(ctx, ret, testNamespace)
assert.NoError(t, err)

// Submit ensures atomicity of batch, so it makes sense to compare actual blobs (bodies) only when lengths
Expand Down Expand Up @@ -164,18 +154,15 @@ func ConcurrentReadWriteTest(t *testing.T, d da.DA) {
go func() {
defer wg.Done()
for i := uint64(1); i <= 100; i++ {
_, err := d.GetIDs(ctx, i)
_, err := d.GetIDs(ctx, i, []byte{})
assert.NoError(t, err)
}
}()

go func() {
defer wg.Done()
for i := uint64(1); i <= 100; i++ {
_, _, err := d.Submit(ctx, [][]byte{[]byte("test")}, &da.SubmitOptions{
GasPrice: 0,
Namespace: []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
})
_, _, err := d.Submit(ctx, [][]byte{[]byte("test")}, 0, []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0})
assert.NoError(t, err)
}
}()
Expand Down
Loading
Loading