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(nodebuilder/blob/cmd): Allow submit multiple blobs through file input #3008

Merged
merged 22 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fdb3e24
add submit multiple blobs from file feature
sontrinh16 Dec 13, 2023
71da02a
minor update cmd description
sontrinh16 Dec 14, 2023
96172b4
Merge branch 'main' into submit_multiple_blobs
mgl2150 Dec 15, 2023
f26d521
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Dec 15, 2023
333dd43
Merge branch 'main' into submit_multiple_blobs
neitdung Dec 18, 2023
dd27a07
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Jan 3, 2024
4656f15
minor
sontrinh16 Jan 9, 2024
203afe1
minor
sontrinh16 Jan 9, 2024
a434c02
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Jan 9, 2024
0344c0e
refactor
sontrinh16 Jan 9, 2024
efa7c46
Merge branch 'submit_multiple_blobs' of https://github.com/decentrio/…
sontrinh16 Jan 9, 2024
7587f80
minor typo and nit
sontrinh16 Jan 9, 2024
9ccfbb1
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Jan 11, 2024
4d829f7
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Jan 14, 2024
5cdecc8
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Jan 16, 2024
5109390
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Jan 18, 2024
62c950b
resolve conflict
sontrinh16 Jan 25, 2024
0f2175c
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Jan 29, 2024
c51d456
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Jan 30, 2024
627ab57
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Feb 5, 2024
b5e6e9f
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Feb 7, 2024
455122c
Merge branch 'main' into submit_multiple_blobs
sontrinh16 Feb 12, 2024
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
117 changes: 104 additions & 13 deletions nodebuilder/blob/cmd/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"encoding/base64"
"errors"
"fmt"
"path/filepath"
"reflect"
"strconv"

Expand All @@ -18,6 +20,10 @@ var (

fee int64
gasLimit uint64

// FlagFileInput allows the user to provide file path to the json file
// for submitting multiple blobs.
FlagFileInput = "input-file"
)

func init() {
Expand Down Expand Up @@ -54,6 +60,8 @@ func init() {

// unset the default value to avoid users confusion
submitCmd.PersistentFlags().Lookup("fee").DefValue = "0"

submitCmd.PersistentFlags().String(FlagFileInput, "", "Specify the file input")
}

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -130,11 +138,46 @@ var getAllCmd = &cobra.Command{
}

var submitCmd = &cobra.Command{
Use: "submit [namespace] [blobData]",
Args: cobra.ExactArgs(2),
Short: "Submit the blob at the given namespace.\n" +
Use: "submit [namespace] [blobData]",
Args: func(cmd *cobra.Command, args []string) error {
path, err := cmd.Flags().GetString(FlagFileInput)
if err != nil {
return err
}

// If there is a file path input we'll check for the file extension
if path != "" {
if filepath.Ext(path) != ".json" {
return fmt.Errorf("invalid file extension, require json got %s", filepath.Ext(path))
}

return nil
}

if len(args) < 2 {
return errors.New("PayForBlobs requires two arguments: namespace and blobData")
}

return nil
},
Short: "Submit the blob(s) at the given namespace(s).\n" +
"User can use namespace and blobData as argument for single blob submission \n" +
"or use --input-file flag with the path to a json file for multiple blobs submission, \n" +
`where the json file contains:

{
"Blobs": [
{
"namespace": "0x00010203040506070809",
"blobData": "0x676d"
},
{
"namespace": "0x42690c204d39600fddd3",
"blobData": "0x676d"
}
]
}` +
"Note:\n" +
"* only one blob is allowed to submit through the RPC.\n" +
"* fee and gas.limit params will be calculated automatically if they are not provided as arguments",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cmdnode.ParseClientFromCtx(cmd.Context())
Expand All @@ -143,33 +186,81 @@ var submitCmd = &cobra.Command{
}
defer client.Close()

namespace, err := cmdnode.ParseV0Namespace(args[0])
path, err := cmd.Flags().GetString(FlagFileInput)
if err != nil {
return fmt.Errorf("error parsing a namespace:%v", err)
return err
}

parsedBlob, err := blob.NewBlobV0(namespace, []byte(args[1]))
// In case of no file input, get the namespace and blob from the arguments
if path == "" {
parsedBlob, err := getBlobFromArguments(args[0], args[1])
if err != nil {
return err
}

height, err := client.Blob.Submit(
cmd.Context(),
[]*blob.Blob{parsedBlob},
&blob.SubmitOptions{Fee: fee, GasLimit: gasLimit},
)

response := struct {
Height uint64 `json:"height"`
Commitments []blob.Commitment `json:"commitments"`
}{
Height: height,
Commitments: []blob.Commitment{parsedBlob.Commitment},
}
return cmdnode.PrintOutput(response, err, nil)
}

paresdBlobs, err := parseSubmitBlobs(path)
if err != nil {
return fmt.Errorf("error creating a blob:%v", err)
return err
}

var blobs []*blob.Blob
var conmmitments []blob.Commitment
for _, paresdBlob := range paresdBlobs {
blob, err := getBlobFromArguments(paresdBlob.Namespace, paresdBlob.BlobData)
if err != nil {
return err
}
blobs = append(blobs, blob)
conmmitments = append(conmmitments, blob.Commitment)
}

height, err := client.Blob.Submit(
cmd.Context(),
[]*blob.Blob{parsedBlob},
blobs,
&blob.SubmitOptions{Fee: fee, GasLimit: gasLimit},
)

response := struct {
Height uint64 `json:"height"`
Commitment blob.Commitment `json:"commitment"`
Height uint64 `json:"height"`
Commitments []blob.Commitment `json:"commitments"`
}{
Height: height,
Commitment: parsedBlob.Commitment,
Height: height,
Commitments: conmmitments,
}
return cmdnode.PrintOutput(response, err, nil)
},
}

func getBlobFromArguments(namespaceArg, blobArg string) (*blob.Blob, error) {
namespace, err := cmdnode.ParseV0Namespace(namespaceArg)
if err != nil {
return nil, fmt.Errorf("error parsing a namespace:%v", err)
}

parsedBlob, err := blob.NewBlobV0(namespace, []byte(blobArg))
if err != nil {
return nil, fmt.Errorf("error creating a blob:%v", err)
}

return parsedBlob, nil
}

var getProofCmd = &cobra.Command{
Use: "get-proof [height] [namespace] [commitment]",
Args: cobra.ExactArgs(3),
Expand Down
32 changes: 32 additions & 0 deletions nodebuilder/blob/cmd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"encoding/json"
"os"
)

// Define the raw content from the file input.
type blobs struct {
Blobs []blobJSON
}

type blobJSON struct {
Namespace string
BlobData string
}

func parseSubmitBlobs(path string) ([]blobJSON, error) {
var rawBlobs blobs

content, err := os.ReadFile(path)
if err != nil {
return []blobJSON{}, err
}

err = json.Unmarshal(content, &rawBlobs)
if err != nil {
return []blobJSON{}, err
}

return rawBlobs.Blobs, err
}