Skip to content

Commit 0086fd2

Browse files
committed
cmd/rofl: Add refresh-trust-root subcommand
Fetch the latest consensus trust root and update the rofl.yaml app manifest automatically instead of editing it by hand.
1 parent faeefa4 commit 0086fd2

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

cmd/rofl/refresh_trust_root.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package rofl
2+
3+
import "testing"
4+
5+
func TestRefreshTrustRootUse(t *testing.T) {
6+
if refreshTrustRootCmd.Use != "refresh-trust-root" {
7+
t.Fatalf("unexpected Use for command: %s", refreshTrustRootCmd.Use)
8+
}
9+
}
10+
11+
func TestRefreshTrustRootCommandRegistered(t *testing.T) {
12+
found := false
13+
for _, c := range Cmd.Commands() {
14+
if c == refreshTrustRootCmd || c.Name() == "refresh-trust-root" {
15+
found = true
16+
break
17+
}
18+
}
19+
if !found {
20+
t.Fatalf("refresh-trust-root command not registered under rofl.Cmd")
21+
}
22+
}
23+
24+
func TestRefreshTrustRootCommandFlags(t *testing.T) {
25+
// The command should expose selector flags (network/paratime), height and deployment flags.
26+
names := []string{"network", "paratime", "no-paratime", "height", "deployment"}
27+
for _, n := range names {
28+
if refreshTrustRootCmd.Flags().Lookup(n) == nil {
29+
t.Errorf("expected flag %q to be present", n)
30+
}
31+
}
32+
}

cmd/rofl/rofl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func init() {
2525
Cmd.AddCommand(listCmd)
2626
Cmd.AddCommand(setDefaultCmd)
2727
Cmd.AddCommand(trustRootCmd)
28+
Cmd.AddCommand(refreshTrustRootCmd)
2829
Cmd.AddCommand(build.Cmd)
2930
Cmd.AddCommand(identityCmd)
3031
Cmd.AddCommand(secretCmd)

0 commit comments

Comments
 (0)