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

da interface: add max blob size method #23

Merged
merged 15 commits into from
Dec 7, 2023
3 changes: 3 additions & 0 deletions da.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package da

// DA defines very generic interface for interaction with Data Availability layers.
type DA interface {
// MaxBlobSize returns the max blob size
MaxBlobSize() (uint64, error)

// Get returns Blob for each given ID, or an error.
//
// Error should be returned if ID is not formatted properly, there is no Blob for given ID or any other client-level
Expand Down
14 changes: 13 additions & 1 deletion proto/da/da.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package da;

// DAService is the protobuf service definition for interaction with Data Availability layers.
service DAService {
// MaxBlobSize returns the maximum blob size
rpc MaxBlobSize(MaxBlobSizeRequest) returns (MaxBlobSizeResponse) {}

// Get returns Blob for each given ID, or an error.
rpc Get(GetRequest) returns (GetResponse) {}

Expand Down Expand Up @@ -39,6 +42,15 @@ message Proof {
bytes value = 1;
}

// MaxBlobSizeRequest is the request type for the MaxBlobSize rpc method.
message MaxBlobSizeRequest {
}

// MaxBlobSizeResponse is the response type for the MaxBlobSize rpc method.
message MaxBlobSizeResponse {
uint64 max_blob_size = 1;
}

// GetRequest is the request type for the Get rpc method.
message GetRequest {
repeated ID ids = 1;
Expand Down Expand Up @@ -89,4 +101,4 @@ message ValidateRequest {
// ValidateResponse is the response type for the Validate rpc method.
message ValidateResponse {
repeated bool results = 1;
}
}
10 changes: 10 additions & 0 deletions proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ func (c *Client) Stop() error {
return c.conn.Close()
}

// MaxBlobSize returns the DA MaxBlobSize
func (c *Client) MaxBlobSize() (uint64, error) {
req := &pbda.MaxBlobSizeRequest{}
resp, err := c.client.MaxBlobSize(context.TODO(), req)
if err != nil {
return 0, err
}
return resp.MaxBlobSize, nil
tuxcanfly marked this conversation as resolved.
Show resolved Hide resolved
}

// Get returns Blob for each given ID, or an error.
func (c *Client) Get(ids []da.ID) ([]da.Blob, error) {
req := &pbda.GetRequest{
Expand Down
5 changes: 5 additions & 0 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ type proxySrv struct {
target da.DA
}

func (p *proxySrv) MaxBlobSize(ctx context.Context, request *pbda.MaxBlobSizeRequest) (*pbda.MaxBlobSizeResponse, error) {
maxBlobSize, err := p.target.MaxBlobSize()
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(ids)
Expand Down
29 changes: 21 additions & 8 deletions test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,47 @@ import (
"github.com/rollkit/go-da"
)

// DefaultMaxBlobSize is the default max blob size
const DefaultMaxBlobSize = 64 * 64 * 482

// DummyDA is a simple implementation of in-memory DA. Not production ready! Intended only for testing!
//
// Data is stored in a map, where key is a serialized sequence number. This key is returned as ID.
// Commitments are simply hashes, and proofs are ED25519 signatures.
type DummyDA struct {
mu *sync.Mutex // protects data and height
data map[uint64][]kvp
height uint64
privKey ed25519.PrivateKey
pubKey ed25519.PublicKey
mu *sync.Mutex // protects data and height
data map[uint64][]kvp
maxBlobSize uint64
height uint64
privKey ed25519.PrivateKey
pubKey ed25519.PublicKey
}

type kvp struct {
key, value []byte
}

// NewDummyDA create new instance of DummyDA
func NewDummyDA() *DummyDA {
func NewDummyDA(opts ...func(*DummyDA) *DummyDA) *DummyDA {
da := &DummyDA{
mu: new(sync.Mutex),
data: make(map[uint64][]kvp),
mu: new(sync.Mutex),
data: make(map[uint64][]kvp),
maxBlobSize: DefaultMaxBlobSize,
}
for _, f := range opts {
da = f(da)
}
da.pubKey, da.privKey, _ = ed25519.GenerateKey(rand.Reader)
return da
}

var _ da.DA = &DummyDA{}

// MaxBlobSize returns the max blob size in bytes.
func (d *DummyDA) MaxBlobSize() (uint64, error) {
return d.maxBlobSize, nil
}

// Get returns Blobs for given IDs.
func (d *DummyDA) Get(ids []da.ID) ([]da.Blob, error) {
d.mu.Lock()
Expand Down
Loading