|
| 1 | +package rofl |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + buildRofl "github.com/oasisprotocol/cli/build/rofl" |
| 10 | + "github.com/oasisprotocol/cli/cmd/common" |
| 11 | + roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common" |
| 12 | + |
| 13 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection" |
| 14 | +) |
| 15 | + |
| 16 | +var refreshTrustRootCmd = &cobra.Command{ |
| 17 | + Use: "refresh-trust-root", |
| 18 | + Short: "Fetch latest trust root and update the rofl.yaml manifest", |
| 19 | + Args: cobra.NoArgs, |
| 20 | + Run: func(_ *cobra.Command, _ []string) { |
| 21 | + // Load manifest and deployment, and configure network/paratime/account selection. |
| 22 | + manifest, deployment, npa := roflCommon.LoadManifestAndSetNPA(nil) |
| 23 | + |
| 24 | + // Establish connection with the target network. |
| 25 | + ctx := context.Background() |
| 26 | + conn, err := connection.Connect(ctx, npa.Network) |
| 27 | + cobra.CheckErr(err) |
| 28 | + |
| 29 | + // Determine actual height (respects --height flag if set). |
| 30 | + height, err := common.GetActualHeight(ctx, conn.Consensus().Core()) |
| 31 | + cobra.CheckErr(err) |
| 32 | + |
| 33 | + blk, err := conn.Consensus().Core().GetBlock(ctx, height) |
| 34 | + cobra.CheckErr(err) |
| 35 | + |
| 36 | + if deployment.TrustRoot == nil { |
| 37 | + deployment.TrustRoot = &buildRofl.TrustRootConfig{} |
| 38 | + } |
| 39 | + deployment.TrustRoot.Height = uint64(height) |
| 40 | + deployment.TrustRoot.Hash = blk.Hash.Hex() |
| 41 | + |
| 42 | + if err = manifest.Save(); err != nil { |
| 43 | + cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err)) |
| 44 | + } |
| 45 | + |
| 46 | + fmt.Printf("Updated '%s' with trust root: height=%d, hash=%s\n", manifest.SourceFileName(), height, blk.Hash.Hex()) |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +func init() { |
| 51 | + refreshTrustRootCmd.Flags().AddFlagSet(common.SelectorNPFlags) |
| 52 | + refreshTrustRootCmd.Flags().AddFlagSet(common.HeightFlag) |
| 53 | + refreshTrustRootCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags) |
| 54 | +} |
0 commit comments