Repository navigation
Expand file tree
/
Copy pathcommitments.go
More file actions
139 lines (115 loc) · 4 KB
/
Copy pathcommitments.go
File metadata and controls
139 lines (115 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
package commitments
import (
"encoding/json"
"fmt"
"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/internal/jsonrpc"
"github.com/cartesi/rollups-node/internal/read"
"github.com/spf13/cobra"
)
var Cmd = &cobra.Command{
Use: "commitments <application> [epoch index] [tournament address] [commitment]",
Short: "Reads commitments",
Example: examples,
Args: cobra.RangeArgs(1, 4), // nolint: mnd
Run: run,
Long: `
Supported Environment Variables:
CARTESI_DATABASE_CONNECTION Database connection string`,
}
const examples = `# Read specific commitment:
cartesi-rollups-cli read commitments echo-dapp 10 0x0073a8637d98649717bdc02ecb439c80aa8a10d0 0xdb99c9cdb2e2070a4e4e633c2e6874648dfe3971d14da843465b3d950df3dd19
# Read all commitments:
cartesi-rollups-cli read commitments echo-dapp
# Read all commitments with filter:
cartesi-rollups-cli read commitments echo-dapp --epoch-index 10 --tournament-address 0x0073a8637d98649717bdc02ecb439c80aa8a10d0
# Read all commitments with pagination:
cartesi-rollups-cli read commitments echo-dapp --limit 10 --offset 10
`
var (
epochIndex string
tournamentAddress string
limit uint64
offset uint64
)
func init() {
Cmd.Flags().StringVar(&epochIndex, "epoch-index", "",
"Filter commitments by epoch index (decimal or hex encoded)")
Cmd.Flags().StringVar(&tournamentAddress, "tournament-address", "",
"Filter commitments by tournament address (hex encoded)")
Cmd.Flags().Uint64Var(&limit, "limit", 50,
"Maximum number of results to return")
Cmd.Flags().Uint64Var(&offset, "offset", 0,
"Starting point for the list of results")
origHelpFunc := Cmd.HelpFunc()
Cmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
command.Flags().Lookup("verbose").Hidden = false
command.Flags().Lookup("database-connection").Hidden = false
origHelpFunc(command, strings)
})
Cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if limit > jsonrpc.LIST_ITEM_LIMIT {
return fmt.Errorf("limit cannot exceed %d", jsonrpc.LIST_ITEM_LIMIT)
} else if limit == 0 {
limit = jsonrpc.LIST_ITEM_LIMIT
}
return nil
}
}
func run(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
var (
readServ read.ReadService
err error
)
addr := cmd.Flags().Lookup("jsonrpc-api-address").Value.String()
if addr != "" {
readServ, err = read.NewJsonrpcReadService(ctx, addr)
} else {
dsn, err := config.GetDatabaseConnection()
cobra.CheckErr(err)
readServ, err = read.NewRepositoryReadService(ctx, dsn.String())
}
cobra.CheckErr(err)
defer readServ.Close()
var result json.RawMessage
if len(args) >= 4 {
var params jsonrpc.GetCommitmentParams
params.Application = args[0]
// Convert from decimal to hexadecimal
epochIndexNumeric, err := config.ToUint64FromDecimalOrHexString(args[1])
if err != nil {
cobra.CheckErr(err)
}
params.EpochIndex = fmt.Sprintf("0x%02x", epochIndexNumeric)
params.TournamentAddress = args[2]
params.Commitment = args[3]
result, err = readServ.GetCommitment(ctx, params)
} else if len(args) >= 1 {
var params jsonrpc.ListCommitmentsParams
params.Application = args[0]
// Add epoch index filter if provided
if cmd.Flags().Changed("epoch-index") {
// Convert from decimal to hexadecimal
epochIndexNumeric, err := config.ToUint64FromDecimalOrHexString(epochIndex)
if err != nil {
cobra.CheckErr(err)
}
epochIndexConverted := fmt.Sprintf("0x%02x", epochIndexNumeric)
params.EpochIndex = &epochIndexConverted
}
// Add tournament address filter if provided
if cmd.Flags().Changed("tournament-address") {
params.TournamentAddress = &tournamentAddress
}
params.Limit = limit
params.Offset = offset
result, err = readServ.ListCommitments(ctx, params)
}
cobra.CheckErr(err)
formatted, err := json.MarshalIndent(result, "", " ")
cobra.CheckErr(err)
fmt.Println(string(formatted))
}