|
| 1 | +package rofl |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + consensus "github.com/oasisprotocol/oasis-core/go/consensus/api" |
| 11 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection" |
| 12 | + |
| 13 | + buildRofl "github.com/oasisprotocol/cli/build/rofl" |
| 14 | + "github.com/oasisprotocol/cli/cmd/common" |
| 15 | + roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common" |
| 16 | +) |
| 17 | + |
| 18 | +var setTrustRootCmd = &cobra.Command{ |
| 19 | + Use: "set-trust-root [<height>] [<hash>]", |
| 20 | + Short: "Set the trust root in the ROFL app manifest", |
| 21 | + Long: "Set the consensus block height and hash of the trust root in the ROFL app manifest.\n\n" + |
| 22 | + "If no arguments are passed, the latest finalized block is used. If only the height is\n" + |
| 23 | + "passed, the corresponding block hash is fetched from the network.", |
| 24 | + Args: cobra.RangeArgs(0, 2), |
| 25 | + Run: func(_ *cobra.Command, args []string) { |
| 26 | + // Load manifest and deployment, and configure network/paratime/account selection. |
| 27 | + manifest, deployment, npa := roflCommon.LoadManifestAndSetNPA(nil) |
| 28 | + |
| 29 | + var ( |
| 30 | + height uint64 |
| 31 | + hash string |
| 32 | + ) |
| 33 | + switch len(args) { |
| 34 | + case 2: |
| 35 | + // Both height and hash passed; set them directly without querying the network. |
| 36 | + h, err := strconv.ParseUint(args[0], 10, 64) |
| 37 | + if err != nil { |
| 38 | + cobra.CheckErr(fmt.Errorf("malformed height: %w", err)) |
| 39 | + } |
| 40 | + height = h |
| 41 | + hash = args[1] |
| 42 | + default: |
| 43 | + // Determine the block height to query (latest by default). |
| 44 | + queryHeight := consensus.HeightLatest |
| 45 | + if len(args) == 1 { |
| 46 | + h, err := strconv.ParseInt(args[0], 10, 64) |
| 47 | + if err != nil { |
| 48 | + cobra.CheckErr(fmt.Errorf("malformed height: %w", err)) |
| 49 | + } |
| 50 | + queryHeight = h |
| 51 | + } |
| 52 | + |
| 53 | + // Establish connection with the target network and fetch the block. |
| 54 | + ctx := context.Background() |
| 55 | + conn, err := connection.Connect(ctx, npa.Network) |
| 56 | + cobra.CheckErr(err) |
| 57 | + |
| 58 | + blk, err := conn.Consensus().Core().GetBlock(ctx, queryHeight) |
| 59 | + cobra.CheckErr(err) |
| 60 | + |
| 61 | + height = uint64(blk.Height) |
| 62 | + hash = blk.Hash.Hex() |
| 63 | + } |
| 64 | + |
| 65 | + if deployment.TrustRoot == nil { |
| 66 | + deployment.TrustRoot = &buildRofl.TrustRootConfig{} |
| 67 | + } |
| 68 | + deployment.TrustRoot.Height = height |
| 69 | + deployment.TrustRoot.Hash = hash |
| 70 | + |
| 71 | + if err := manifest.Save(); err != nil { |
| 72 | + cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err)) |
| 73 | + } |
| 74 | + |
| 75 | + out, err := common.PrettyJSONMarshal(deployment.TrustRoot) |
| 76 | + cobra.CheckErr(err) |
| 77 | + fmt.Printf("Updated '%s' with trust root:\n%s\n", manifest.SourceFileName(), string(out)) |
| 78 | + }, |
| 79 | +} |
| 80 | + |
| 81 | +func init() { |
| 82 | + setTrustRootCmd.Flags().AddFlagSet(common.SelectorNPFlags) |
| 83 | + setTrustRootCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags) |
| 84 | +} |
0 commit comments