Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

celestia-da: add client context #33

Merged
merged 2 commits into from
Jan 12, 2024
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
23 changes: 12 additions & 11 deletions celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ func NewCelestiaDA(client *rpc.Client, namespace share.Namespace, gasPrice float
}

// MaxBlobSize returns the max blob size
func (c *CelestiaDA) MaxBlobSize() (uint64, error) {
func (c *CelestiaDA) MaxBlobSize(ctx context.Context) (uint64, error) {
// TODO: pass-through query to node, app
tuxcanfly marked this conversation as resolved.
Show resolved Hide resolved
return appconsts.DefaultMaxBytes, nil
}

// Get returns Blob for each given ID, or an error.
func (c *CelestiaDA) Get(ids []da.ID) ([]da.Blob, error) {
func (c *CelestiaDA) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
var blobs []da.Blob
for _, id := range ids {
height, commitment := splitID(id)
blob, err := c.client.Blob.Get(c.ctx, height, c.namespace, commitment)
blob, err := c.client.Blob.Get(ctx, height, c.namespace, commitment)
if err != nil {
return nil, err
}
Expand All @@ -57,9 +58,9 @@ func (c *CelestiaDA) Get(ids []da.ID) ([]da.Blob, error) {
}

// GetIDs returns IDs of all Blobs located in DA at given height.
func (c *CelestiaDA) GetIDs(height uint64) ([]da.ID, error) {
func (c *CelestiaDA) GetIDs(ctx context.Context, height uint64) ([]da.ID, error) {
var ids []da.ID
blobs, err := c.client.Blob.GetAll(c.ctx, height, []share.Namespace{c.namespace})
blobs, err := c.client.Blob.GetAll(ctx, height, []share.Namespace{c.namespace})
if err != nil {
if strings.Contains(err.Error(), blob.ErrBlobNotFound.Error()) {
return nil, nil
Expand All @@ -73,13 +74,13 @@ func (c *CelestiaDA) GetIDs(height uint64) ([]da.ID, error) {
}

// Commit creates a Commitment for each given Blob.
func (c *CelestiaDA) Commit(daBlobs []da.Blob) ([]da.Commitment, error) {
func (c *CelestiaDA) Commit(ctx context.Context, daBlobs []da.Blob) ([]da.Commitment, error) {
_, commitments, err := c.blobsAndCommitments(daBlobs)
return commitments, err
}

// Submit submits the Blobs to Data Availability layer.
func (c *CelestiaDA) Submit(daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) {
func (c *CelestiaDA) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) {
blobs, commitments, err := c.blobsAndCommitments(daBlobs)
if err != nil {
return nil, nil, err
Expand All @@ -97,7 +98,7 @@ func (c *CelestiaDA) Submit(daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.
options.GasLimit = types.EstimateGas(blobSizes, appconsts.DefaultGasPerBlobByte, auth.DefaultTxSizeCostPerByte)
options.Fee = sdktypes.NewInt(int64(math.Ceil(gasPrice * float64(options.GasLimit)))).Int64()
}
height, err := c.client.Blob.Submit(c.ctx, blobs, options)
height, err := c.client.Blob.Submit(ctx, blobs, options)
if err != nil {
return nil, nil, err
}
Expand All @@ -106,7 +107,7 @@ func (c *CelestiaDA) Submit(daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.
proofs := make([]da.Proof, len(daBlobs))
for i, commitment := range commitments {
ids[i] = makeID(height, commitment)
proof, err := c.client.Blob.GetProof(c.ctx, height, c.namespace, commitment)
proof, err := c.client.Blob.GetProof(ctx, height, c.namespace, commitment)
tuxcanfly marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -140,7 +141,7 @@ func (c *CelestiaDA) blobsAndCommitments(daBlobs []da.Blob) ([]*blob.Blob, []da.
}

// Validate validates Commitments against the corresponding Proofs. This should be possible without retrieving the Blobs.
func (c *CelestiaDA) Validate(ids []da.ID, daProofs []da.Proof) ([]bool, error) {
func (c *CelestiaDA) Validate(ctx context.Context, ids []da.ID, daProofs []da.Proof) ([]bool, error) {
var included []bool
var proofs []*blob.Proof
for _, daProof := range daProofs {
Expand All @@ -156,7 +157,7 @@ func (c *CelestiaDA) Validate(ids []da.ID, daProofs []da.Proof) ([]bool, error)
// TODO(tzdybal): for some reason, if proof doesn't match commitment, API returns (false, "blob: invalid proof")
// but analysis of the code in celestia-node implies this should never happen - maybe it's caused by openrpc?
// there is no way of gently handling errors here, but returned value is fine for us
isIncluded, _ := c.client.Blob.Included(c.ctx, height, c.namespace, proofs[i], commitment)
isIncluded, _ := c.client.Blob.Included(ctx, height, c.namespace, proofs[i], commitment)
included = append(included, isIncluded)
}
return included, nil
Expand Down
27 changes: 14 additions & 13 deletions celestia/celestia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,58 +51,59 @@ func teardown(m *mockDA) {

// TestCelestiaDA is the test suite function.
func TestCelestiaDA(t *testing.T) {
ctx := context.TODO()
tuxcanfly marked this conversation as resolved.
Show resolved Hide resolved
m := setup(t)
defer teardown(m)

t.Run("MaxBlobSize", func(t *testing.T) {
maxBlobSize, err := m.MaxBlobSize()
maxBlobSize, err := m.MaxBlobSize(ctx)
assert.NoError(t, err)
assert.Equal(t, uint64(appconsts.DefaultMaxBytes), maxBlobSize)
})

t.Run("Get_empty", func(t *testing.T) {
blobs, err := m.Get(nil)
blobs, err := m.Get(ctx, nil)
assert.NoError(t, err)
assert.Equal(t, 0, len(blobs))
})

t.Run("GetIDs_empty", func(t *testing.T) {
blobs, err := m.GetIDs(0)
blobs, err := m.GetIDs(ctx, 0)
assert.NoError(t, err)
assert.Equal(t, 0, len(blobs))
})

t.Run("Commit_empty", func(t *testing.T) {
commitments, err := m.Commit(nil)
commitments, err := m.Commit(ctx, nil)
assert.NoError(t, err)
assert.Equal(t, 0, len(commitments))
})

t.Run("Submit_empty", func(t *testing.T) {
blobs, proofs, err := m.Submit(nil, -1)
blobs, proofs, err := m.Submit(ctx, nil, -1)
assert.NoError(t, err)
assert.Equal(t, 0, len(blobs))
assert.Equal(t, 0, len(proofs))
})

t.Run("Validate_empty", func(t *testing.T) {
valids, err := m.Validate(nil, nil)
valids, err := m.Validate(ctx, nil, nil)
assert.NoError(t, err)
assert.Equal(t, 0, len(valids))
})

t.Run("Get_existing", func(t *testing.T) {
commitment, err := hex.DecodeString("1b454951cd722b2cf7be5b04554b76ccf48f65a7ad6af45055006994ce70fd9d")
assert.NoError(t, err)
blobs, err := m.Get([]ID{makeID(42, commitment)})
blobs, err := m.Get(ctx, []ID{makeID(42, commitment)})
assert.NoError(t, err)
assert.Equal(t, 1, len(blobs))
blob1 := blobs[0]
assert.Equal(t, "This is an example of some blob data", string(blob1))
})

t.Run("GetIDs_existing", func(t *testing.T) {
ids, err := m.GetIDs(42)
ids, err := m.GetIDs(ctx, 42)
assert.NoError(t, err)
assert.Equal(t, 1, len(ids))
id1 := ids[0]
Expand All @@ -112,28 +113,28 @@ func TestCelestiaDA(t *testing.T) {
})

t.Run("Commit_existing", func(t *testing.T) {
commitments, err := m.Commit([]Blob{[]byte{0x00, 0x01, 0x02}})
commitments, err := m.Commit(ctx, []Blob{[]byte{0x00, 0x01, 0x02}})
assert.NoError(t, err)
assert.Equal(t, 1, len(commitments))
})

t.Run("Submit_existing", func(t *testing.T) {
blobs, proofs, err := m.Submit([]Blob{[]byte{0x00, 0x01, 0x02}}, -1)
blobs, proofs, err := m.Submit(ctx, []Blob{[]byte{0x00, 0x01, 0x02}}, -1)
assert.NoError(t, err)
assert.Equal(t, 1, len(blobs))
assert.Equal(t, 1, len(proofs))
})

t.Run("Submit_existing_with_gasprice_global", func(t *testing.T) {
m.CelestiaDA.gasPrice = 0.01
blobs, proofs, err := m.Submit([]Blob{[]byte{0x00, 0x01, 0x02}}, -1)
blobs, proofs, err := m.Submit(ctx, []Blob{[]byte{0x00, 0x01, 0x02}}, -1)
assert.NoError(t, err)
assert.Equal(t, 1, len(blobs))
assert.Equal(t, 1, len(proofs))
})

t.Run("Submit_existing_with_gasprice_override", func(t *testing.T) {
blobs, proofs, err := m.Submit([]Blob{[]byte{0x00, 0x01, 0x02}}, 0.5)
blobs, proofs, err := m.Submit(ctx, []Blob{[]byte{0x00, 0x01, 0x02}}, 0.5)
assert.NoError(t, err)
assert.Equal(t, 1, len(blobs))
assert.Equal(t, 1, len(proofs))
Expand All @@ -147,7 +148,7 @@ func TestCelestiaDA(t *testing.T) {
assert.NoError(t, err)
ids := []ID{makeID(42, commitment)}
proofs := []Proof{proofJSON}
valids, err := m.Validate(ids, proofs)
valids, err := m.Validate(ctx, ids, proofs)
assert.NoError(t, err)
assert.Equal(t, 1, len(valids))
})
Expand Down
4 changes: 3 additions & 1 deletion celestia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/rollkit/go-da/test"
)

const localCelestiaDevnetImageVersion = "v0.12.5"

type TestSuite struct {
suite.Suite

Expand All @@ -45,7 +47,7 @@ func (t *TestSuite) SetupSuite() {
}

// pulls an image, creates a container based on it and runs it
resource, err := pool.Run("ghcr.io/rollkit/local-celestia-devnet", "v0.12.5", []string{})
resource, err := pool.Run("ghcr.io/rollkit/local-celestia-devnet", localCelestiaDevnetImageVersion, []string{})
if err != nil {
t.Failf("Could not start resource", "error: %v\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/ipfs/go-log/v2 v2.5.1
github.com/mitchellh/go-homedir v1.1.0
github.com/ory/dockertest/v3 v3.10.0
github.com/rollkit/go-da v0.1.0
github.com/rollkit/go-da v0.2.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2103,8 +2103,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rollkit/go-da v0.1.0 h1:FAEMTNF8mTsPuiUgYt2dQSMzw8iYPjiWq7692CS2mbY=
github.com/rollkit/go-da v0.1.0/go.mod h1:Kef0XI5ecEKd3TXzI8S+9knAUJnZg0svh2DuXoCsPlM=
github.com/rollkit/go-da v0.2.0 h1:rNpWBa2inczgZ955ky3wy8FbrMajzVbm0UfbBGzm5UE=
github.com/rollkit/go-da v0.2.0/go.mod h1:Kef0XI5ecEKd3TXzI8S+9knAUJnZg0svh2DuXoCsPlM=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE=
github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
Expand Down
Loading