Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ clean:

mockgen:
mockgen -source=./pkg/pdp/aggregator/interface.go -destination=./internal/mocks/aggregator.go -package=mocks
mockgen -source=./pkg/pdp/curio/client.go -destination=./internal/mocks/curio_client.go -package=mocks
mockgen -source=./pkg/pdp/types/api.go -destination=./internal/mocks/pdp_api.go -package=mocks
mockgen -source=./internal/ipldstore/ipldstore.go -destination=./internal/mocks/ipldstore.go -package=mocks
mockgen -source=./pkg/pdp/aggregator/steps.go -destination=./internal/mocks/steps.go -package=mocks
mockgen -destination=./internal/mocks/sender_eth_client.go -package=mocks github.com/storacha/piri/pkg/pdp/tasks SenderETHClient
Expand Down
83 changes: 27 additions & 56 deletions cmd/cli/client/pdp/proofset/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"

"github.com/storacha/piri/cmd/cliutil"
"github.com/storacha/piri/pkg/config"
"github.com/storacha/piri/pkg/pdp/curio"
"github.com/storacha/piri/pkg/pdp/httpapi/client"
"github.com/storacha/piri/pkg/pdp/types"
)

var (
Expand Down Expand Up @@ -48,19 +46,9 @@ func doCreate(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("loading config: %w", err)
}

id, err := cliutil.ReadPrivateKeyFromPEM(cfg.KeyFile)
pdpClient, err := client.NewFromConfig(cfg)
if err != nil {
return fmt.Errorf("loading key file: %w", err)
}

nodeAuth, err := curio.CreateCurioJWTAuthHeader("storacha", id)
if err != nil {
return fmt.Errorf("generating node JWT: %w", err)
}

nodeURL, err := url.Parse(cfg.NodeURL)
if err != nil {
return fmt.Errorf("parsing node URL: %w", err)
return fmt.Errorf("creating pdp client: %w", err)
}

recordKeeper, err := cmd.Flags().GetString("record-keeper")
Expand All @@ -71,17 +59,14 @@ func doCreate(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("record keeper address (%s) is invalid", recordKeeper)
}

pdpClient := curio.New(http.DefaultClient, nodeURL, nodeAuth)
statusRef, err := pdpClient.CreateProofSet(ctx, curio.CreateProofSet{
RecordKeeper: recordKeeper,
})
txHash, err := pdpClient.CreateProofSet(ctx, common.HexToAddress(recordKeeper))
if err != nil {
return fmt.Errorf("creating proofset: %w", err)
}
// Write initial status to stderr
stderr := cmd.ErrOrStderr()
fmt.Fprintf(stderr, "Proof set being created, check status at:\n")
fmt.Fprintf(stderr, "%s\n", statusRef.URL)
fmt.Fprintf(stderr, "Proof set being created, transaction hash:\n")
fmt.Fprintf(stderr, "%s\n", txHash.String())

wait, err := cmd.Flags().GetBool("wait")
if err != nil {
Expand All @@ -93,26 +78,26 @@ func doCreate(cmd *cobra.Command, _ []string) error {
}

// Poll for status updates
return pollProofSetStatus(ctx, pdpClient, statusRef.URL, cmd.OutOrStdout(), stderr)
return pollProofSetStatus(ctx, pdpClient, txHash, cmd.OutOrStdout(), stderr)
}

// pollProofSetStatus polls the proof set status until creation is complete
func pollProofSetStatus(ctx context.Context, client curio.PDPClient, statusURL string, stdout, stderr io.Writer) error {
func pollProofSetStatus(ctx context.Context, client types.ProofSetAPI, txHash common.Hash, stdout, stderr io.Writer) error {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

spinnerChars := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
spinnerIndex := 0

var lastStatus *curio.ProofSetStatus
var lastStatus *types.ProofSetStatus
var lastOutput string

for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
status, err := checkStatus(ctx, client, statusURL)
status, err := checkStatus(ctx, client, txHash)
if err != nil {
return fmt.Errorf("checking status: %w", err)
}
Expand All @@ -121,22 +106,17 @@ func pollProofSetStatus(ctx context.Context, client curio.PDPClient, statusURL s
var output strings.Builder
output.WriteString(fmt.Sprintf("\r%s Polling proof set status...\n", spinnerChars[spinnerIndex]))
output.WriteString(fmt.Sprintf(" Status: %s\n", status.TxStatus))
output.WriteString(fmt.Sprintf(" Transaction Hash: %s\n", status.CreateMessageHash))
output.WriteString(fmt.Sprintf(" Created: %t\n", status.ProofsetCreated))
output.WriteString(fmt.Sprintf(" Service: %s\n", status.Service))
output.WriteString(fmt.Sprintf(" Transaction Hash: %s\n", status.TxHash))
output.WriteString(fmt.Sprintf(" Created: %t\n", status.Created))

if status.OK != nil {
output.WriteString(fmt.Sprintf(" Ready: %t\n", *status.OK))
}

if status.ProofSetId != nil {
output.WriteString(fmt.Sprintf(" ProofSet ID: %d\n", *status.ProofSetId))
if status.ID != 0 {
output.WriteString(fmt.Sprintf(" ProofSet ID: %d\n", status.ID))
}

currentOutput := output.String()

// Only update display if status changed
if lastStatus == nil || !statusEqual(lastStatus, &status) || currentOutput != lastOutput {
if lastStatus == nil || !statusEqual(lastStatus, status) || currentOutput != lastOutput {
// Clear previous lines
if lastOutput != "" {
lines := strings.Count(lastOutput, "\n")
Expand All @@ -146,27 +126,27 @@ func pollProofSetStatus(ctx context.Context, client curio.PDPClient, statusURL s
// Write new status
fmt.Fprint(stderr, currentOutput)

lastStatus = &status
lastStatus = status
lastOutput = currentOutput
}

// Update spinner
spinnerIndex = (spinnerIndex + 1) % len(spinnerChars)

// Check if creation is complete
if status.ProofSetId != nil {
if status.Created {
// Clear the status display
lines := strings.Count(lastOutput, "\n")
fmt.Fprintf(stderr, "\033[%dA\033[K", lines)

// Write final status to stderr
fmt.Fprintf(stderr, "✓ Proof set created successfully!\n")
fmt.Fprintf(stderr, " Transaction Hash: %s\n", status.CreateMessageHash)
fmt.Fprintf(stderr, " Service: %s\n", status.Service)
fmt.Fprintf(stderr, " ProofSet ID: %d\n", *status.ProofSetId)
fmt.Fprintf(stderr, " Transaction Hash: %s\n", status.TxHash)
fmt.Fprintf(stderr, " ProofSet ID: %d\n", status.ID)
time.Sleep(time.Second)

// Write only the ProofSet ID to stdout for redirection
fmt.Fprintf(stdout, "%d\n", *status.ProofSetId)
fmt.Fprintf(stdout, "%d\n", status.ID)

return nil
}
Expand All @@ -175,31 +155,22 @@ func pollProofSetStatus(ctx context.Context, client curio.PDPClient, statusURL s
}

// statusEqual compares two ProofSetStatus structs for equality
func statusEqual(a, b *curio.ProofSetStatus) bool {
func statusEqual(a, b *types.ProofSetStatus) bool {
if a == nil || b == nil {
return a == b
}

if a.CreateMessageHash != b.CreateMessageHash ||
a.ProofsetCreated != b.ProofsetCreated ||
a.Service != b.Service ||
if a.TxHash != b.TxHash ||
a.Created != b.Created ||
a.TxStatus != b.TxStatus {
return false
}

// Compare OK pointers
if (a.OK == nil) != (b.OK == nil) {
return false
}
if a.OK != nil && *a.OK != *b.OK {
return false
}

// Compare ProofSetId pointers
if (a.ProofSetId == nil) != (b.ProofSetId == nil) {
if (a.ID == 0) != (b.ID == 0) {
return false
}
if a.ProofSetId != nil && *a.ProofSetId != *b.ProofSetId {
if a.ID != 0 && a.ID != b.ID {
return false
}

Expand Down
22 changes: 4 additions & 18 deletions cmd/cli/client/pdp/proofset/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package proofset
import (
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/spf13/cobra"

"github.com/storacha/piri/cmd/cliutil"
"github.com/storacha/piri/pkg/config"
"github.com/storacha/piri/pkg/pdp/curio"
"github.com/storacha/piri/pkg/pdp/httpapi/client"
)

var (
Expand Down Expand Up @@ -41,28 +38,17 @@ func doGet(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("loading config: %w", err)
}

id, err := cliutil.ReadPrivateKeyFromPEM(cfg.KeyFile)
api, err := client.NewFromConfig(cfg)
if err != nil {
return fmt.Errorf("loading key file: %w", err)
}

nodeAuth, err := curio.CreateCurioJWTAuthHeader("storacha", id)
if err != nil {
return fmt.Errorf("generating node JWT: %w", err)
}

nodeURL, err := url.Parse(cfg.NodeURL)
if err != nil {
return fmt.Errorf("parsing node URL: %w", err)
return fmt.Errorf("creating client: %w", err)
}

proofSetID, err := cmd.Flags().GetUint64("proofset-id")
if err != nil {
return fmt.Errorf("parsing proofset ID: %w", err)
}

client := curio.New(http.DefaultClient, nodeURL, nodeAuth)
proofSet, err := client.GetProofSet(ctx, proofSetID)
proofSet, err := api.GetProofSet(ctx, proofSetID)
if err != nil {
return fmt.Errorf("getting proof set status: %w", err)
}
Expand Down
42 changes: 14 additions & 28 deletions cmd/cli/client/pdp/proofset/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"

"github.com/storacha/piri/cmd/cliutil"
"github.com/storacha/piri/pkg/config"
"github.com/storacha/piri/pkg/pdp/curio"
"github.com/storacha/piri/pkg/pdp/httpapi/client"
"github.com/storacha/piri/pkg/pdp/types"
)

var (
Expand All @@ -25,11 +24,11 @@ var (

func init() {
StatusCmd.Flags().String(
"ref-url",
"txhash",
"",
"The reference URL of a proof set, e.g. /pdp/proof-sets/created/<TX_HASH>",
"The transaction hash resulting from a proof set create message",
)
cobra.CheckErr(StatusCmd.MarkFlagRequired("ref-url"))
cobra.CheckErr(StatusCmd.MarkFlagRequired("txhash"))
}

func doStatus(cmd *cobra.Command, _ []string) error {
Expand All @@ -40,27 +39,16 @@ func doStatus(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("loading config: %w", err)
}

id, err := cliutil.ReadPrivateKeyFromPEM(cfg.KeyFile)
api, err := client.NewFromConfig(cfg)
if err != nil {
return fmt.Errorf("loading key file: %w", err)
return fmt.Errorf("creating client: %w", err)
}

nodeAuth, err := curio.CreateCurioJWTAuthHeader("storacha", id)
txhash, err := cmd.Flags().GetString("txhash")
if err != nil {
return fmt.Errorf("generating node JWT: %w", err)
return fmt.Errorf("parsing txHash: %w", err)
}

nodeURL, err := url.Parse(cfg.NodeURL)
if err != nil {
return fmt.Errorf("parsing node URL: %w", err)
}

client := curio.New(http.DefaultClient, nodeURL, nodeAuth)
refURL, err := cmd.Flags().GetString("ref-url")
if err != nil {
return fmt.Errorf("parsing ref URL: %w", err)
}
status, err := checkStatus(ctx, client, refURL)
status, err := checkStatus(ctx, api, common.HexToHash(txhash))
if err != nil {
return fmt.Errorf("getting proof set status: %w", err)
}
Expand All @@ -72,12 +60,10 @@ func doStatus(cmd *cobra.Command, _ []string) error {
return nil
}

func checkStatus(ctx context.Context, client curio.PDPClient, refURL string) (curio.ProofSetStatus, error) {
status, err := client.ProofSetCreationStatus(ctx, curio.StatusRef{
URL: refURL,
})
func checkStatus(ctx context.Context, client types.ProofSetAPI, txHash common.Hash) (*types.ProofSetStatus, error) {
status, err := client.GetProofSetStatus(ctx, txHash)
if err != nil {
return curio.ProofSetStatus{}, fmt.Errorf("getting proof set status: %w", err)
return nil, err
}
return status, nil
}
2 changes: 2 additions & 0 deletions cmd/cli/client/ucan/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ipfs/go-cid"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/storacha/go-ucanto/core/ipld/hash/sha256"
"github.com/storacha/go-ucanto/did"

Expand All @@ -35,6 +36,7 @@ func init() {
cobra.CheckErr(UploadCmd.MarkFlagRequired("blob"))

UploadCmd.PersistentFlags().String("proof", "", "CAR file containing storage proof authorizing client invocations")
cobra.CheckErr(viper.BindPFlag("proof", UploadCmd.PersistentFlags().Lookup("proof")))
cobra.CheckErr(UploadCmd.MarkPersistentFlagRequired("proof"))

}
Expand Down
Loading
Loading