|
| 1 | +// x/scavenge/client/cli/tx_commit_solution.go |
| 2 | + |
1 | 3 | package cli |
2 | 4 |
|
3 | 5 | import ( |
4 | | - "strconv" |
| 6 | + "crypto/sha256" |
| 7 | + "encoding/hex" |
5 | 8 |
|
6 | 9 | "github.com/cosmos/cosmos-sdk/client" |
7 | 10 | "github.com/cosmos/cosmos-sdk/client/flags" |
8 | 11 | "github.com/cosmos/cosmos-sdk/client/tx" |
9 | 12 | "github.com/spf13/cobra" |
| 13 | + |
10 | 14 | "scavenge/x/scavenge/types" |
11 | 15 | ) |
12 | 16 |
|
13 | | -var _ = strconv.Itoa(0) |
14 | | - |
15 | 17 | func CmdCommitSolution() *cobra.Command { |
16 | 18 | cmd := &cobra.Command{ |
17 | | - Use: "commit-solution [solution-hash] [solution-scavenger-hash]", |
| 19 | + // pass a solution as the only argument |
| 20 | + Use: "commit-solution [solution]", |
18 | 21 | Short: "Broadcast message commit-solution", |
19 | | - Args: cobra.ExactArgs(2), |
20 | | - RunE: func(cmd *cobra.Command, args []string) (err error) { |
21 | | - argSolutionHash := args[0] |
22 | | - argSolutionScavengerHash := args[1] |
23 | | - |
| 22 | + // set the number of arguments to 1 |
| 23 | + Args: cobra.ExactArgs(1), |
| 24 | + RunE: func(cmd *cobra.Command, args []string) error { |
24 | 25 | clientCtx, err := client.GetClientTxContext(cmd) |
25 | 26 | if err != nil { |
26 | 27 | return err |
27 | 28 | } |
28 | 29 |
|
29 | | - msg := types.NewMsgCommitSolution( |
30 | | - clientCtx.GetFromAddress().String(), |
31 | | - argSolutionHash, |
32 | | - argSolutionScavengerHash, |
33 | | - ) |
| 30 | + solution := args[0] |
| 31 | + |
| 32 | + // find a hash of the solution |
| 33 | + solutionHash := sha256.Sum256([]byte(solution)) |
| 34 | + |
| 35 | + // convert the solution hash to string |
| 36 | + solutionHashString := hex.EncodeToString(solutionHash[:]) |
| 37 | + |
| 38 | + // convert a scavenger address to string |
| 39 | + var scavenger = clientCtx.GetFromAddress().String() |
| 40 | + |
| 41 | + // find the hash of solution and scavenger address |
| 42 | + var solutionScavengerHash = sha256.Sum256([]byte(solution + scavenger)) |
| 43 | + |
| 44 | + // convert the hash to string |
| 45 | + var solutionScavengerHashString = hex.EncodeToString(solutionScavengerHash[:]) |
| 46 | + |
| 47 | + // create a new message |
| 48 | + msg := types.NewMsgCommitSolution(clientCtx.GetFromAddress().String(), string(solutionHashString), string(solutionScavengerHashString)) |
34 | 49 | if err := msg.ValidateBasic(); err != nil { |
35 | 50 | return err |
36 | 51 | } |
| 52 | + |
| 53 | + // broadcast the transaction with the message to the blockchain |
37 | 54 | return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) |
38 | 55 | }, |
39 | 56 | } |
|
0 commit comments