Skip to content

Commit 6edb719

Browse files
authored
Merge pull request #561 from oasisprotocol/matevz/feature/mention-topup-command
docs: Document `oasis rofl top-up` command
2 parents 0d7d935 + 2a44a82 commit 6edb719

File tree

7 files changed

+103
-42
lines changed

7 files changed

+103
-42
lines changed

cmd/rofl/common/flags.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ var (
1616
// NoUpdateFlag is the flag for disabling the rofl.yaml manifest file update.
1717
NoUpdateFlag *flag.FlagSet
1818

19+
// TermFlags provide the term and count setting.
20+
TermFlags *flag.FlagSet
21+
1922
// DeploymentName is the name of the ROFL app deployment.
2023
DeploymentName string
2124

@@ -24,6 +27,12 @@ var (
2427

2528
// NoUpdate disables updating the rofl.yaml manifest file.
2629
NoUpdate bool
30+
31+
// Term specifies the rental base unit.
32+
Term string
33+
34+
// TermCount specific the rental base unit multiplier.
35+
TermCount uint64
2736
)
2837

2938
func init() {
@@ -35,4 +44,8 @@ func init() {
3544

3645
NoUpdateFlag = flag.NewFlagSet("", flag.ContinueOnError)
3746
NoUpdateFlag.BoolVar(&NoUpdate, "no-update-manifest", false, "do not update the manifest")
47+
48+
TermFlags = flag.NewFlagSet("", flag.ContinueOnError)
49+
TermFlags.StringVar(&Term, "term", "", "term to pay for in advance [hour, month, year]")
50+
TermFlags.Uint64Var(&TermCount, "term-count", 1, "number of terms to pay for in advance")
3851
}

cmd/rofl/deploy.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ var (
3838
deployProvider string
3939
deployOffer string
4040
deployMachine string
41-
deployTerm string
42-
deployTermCount uint64
4341
deployForce bool
4442
deployShowOffers bool
4543
deployReplaceMachine bool
@@ -150,7 +148,7 @@ var (
150148
},
151149
}
152150

153-
obtainMachine := func() (*buildRofl.Machine, error) {
151+
obtainMachine := func() (*buildRofl.Machine, *roflmarket.Instance, error) {
154152
if deployOffer != "" {
155153
machine.Offer = deployOffer
156154
}
@@ -168,19 +166,19 @@ var (
168166
}
169167
if offer == nil {
170168
showProviderOffers(ctx, npa, conn, *providerAddr)
171-
return nil, fmt.Errorf("offer '%s' not found for provider '%s'", machine.Offer, providerAddr)
169+
return nil, nil, fmt.Errorf("offer '%s' not found for provider '%s'", machine.Offer, providerAddr)
172170
}
173171

174172
fmt.Printf("Taking offer:\n")
175173
showProviderOffer(ctx, offer)
176174

177175
term := detectTerm(offer)
178-
if deployTermCount < 1 {
179-
return nil, fmt.Errorf("number of terms must be at least 1")
176+
if roflCommon.TermCount < 1 {
177+
return nil, nil, fmt.Errorf("number of terms must be at least 1")
180178
}
181179

182180
// Calculate total price.
183-
qTermCount := quantity.NewFromUint64(deployTermCount)
181+
qTermCount := quantity.NewFromUint64(roflCommon.TermCount)
184182
totalPrice, ok := offer.Payment.Native.Terms[term]
185183
if !ok {
186184
cobra.CheckErr("internal error: previously selected term not found in offer terms")
@@ -197,7 +195,7 @@ var (
197195
Offer: offer.ID,
198196
Deployment: &machineDeployment,
199197
Term: term,
200-
TermCount: deployTermCount,
198+
TermCount: roflCommon.TermCount,
201199
})
202200

203201
var sigTx, meta any
@@ -206,23 +204,27 @@ var (
206204

207205
var machineID roflmarket.InstanceID
208206
if !common.BroadcastOrExportTransaction(ctx, npa, conn, sigTx, meta, &machineID) {
209-
return nil, fmt.Errorf("broadcast transaction failed")
207+
return nil, nil, fmt.Errorf("broadcast transaction failed")
210208
}
211209

212210
rawMachineID, _ := machineID.MarshalText()
213-
cobra.CheckErr(err)
214211
machine.ID = string(rawMachineID)
215212
deployment.Machines[deployMachine] = machine
216213

217214
fmt.Printf("Created machine: %s\n", machine.ID)
218-
return machine, nil
215+
216+
var insDsc *roflmarket.Instance
217+
insDsc, err = conn.Runtime(npa.ParaTime).ROFLMarket.Instance(ctx, client.RoundLatest, *providerAddr, machineID)
218+
cobra.CheckErr(err)
219+
220+
return machine, insDsc, nil
219221
}
220222

221-
doDeployMachine := func(machine *buildRofl.Machine) error {
223+
doDeployMachine := func(machine *buildRofl.Machine) (*roflmarket.Instance, error) {
222224
// Deploy into existing machine.
223225
var machineID roflmarket.InstanceID
224226
if err = machineID.UnmarshalText([]byte(machine.ID)); err != nil {
225-
return fmt.Errorf("malformed machine ID: %w", err)
227+
return nil, fmt.Errorf("malformed machine ID: %w", err)
226228
}
227229

228230
fmt.Printf("Deploying into existing machine: %s\n", machine.ID)
@@ -237,8 +239,8 @@ var (
237239
if deployReplaceMachine {
238240
fmt.Printf("Machine instance not found. Obtaining new one...")
239241
machine.ID = ""
240-
_, err = obtainMachine()
241-
return err
242+
_, _, err = obtainMachine()
243+
return nil, err
242244
}
243245

244246
cobra.CheckErr("Machine instance not found.\nTip: If your instance expired, run this command with --replace-machine flag to replace it with a new machine.")
@@ -248,7 +250,7 @@ var (
248250
if insDsc.Deployment != nil && insDsc.Deployment.AppID != machineDeployment.AppID && !deployForce {
249251
fmt.Printf("Machine already contains a deployment of ROFL app '%s'.\n", insDsc.Deployment.AppID)
250252
fmt.Printf("You are trying to replace it with ROFL app '%s'.\n", machineDeployment.AppID)
251-
return fmt.Errorf("refusing to change existing ROFL app, use --force to override")
253+
return nil, fmt.Errorf("refusing to change existing ROFL app, use --force to override")
252254
}
253255

254256
// Prepare transaction.
@@ -269,23 +271,26 @@ var (
269271
cobra.CheckErr(err)
270272

271273
if !common.BroadcastOrExportTransaction(ctx, npa, conn, sigTx, meta, nil) {
272-
return fmt.Errorf("broadcast transaction failed")
274+
return nil, fmt.Errorf("broadcast transaction failed")
273275
}
274276

275-
return nil
277+
return insDsc, nil
276278
}
277279

280+
var insDsc *roflmarket.Instance
278281
if machine.ID == "" {
279282
// When machine is not set, we need to obtain one.
280283
fmt.Printf("No pre-existing machine configured, creating a new one...\n")
281-
machine, err = obtainMachine()
284+
machine, insDsc, err = obtainMachine()
282285
cobra.CheckErr(err)
283286
} else {
284-
cobra.CheckErr(doDeployMachine(machine))
287+
insDsc, err = doDeployMachine(machine)
288+
cobra.CheckErr(err)
285289
}
286290

287291
fmt.Printf("Deployment into machine scheduled.\n")
288-
fmt.Printf("Use `oasis rofl machine show` to see status.\n")
292+
fmt.Printf("This machine expires on %s. Use `oasis rofl machine top-up` to extend it.\n", time.Unix(int64(insDsc.PaidUntil), 0))
293+
fmt.Printf("Use `oasis rofl machine show` to check status.\n")
289294

290295
if err = manifest.Save(); err != nil {
291296
cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err))
@@ -327,11 +332,11 @@ func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
327332
cobra.CheckErr(fmt.Errorf("no payment terms available for offer '%s'", offer.ID))
328333
}
329334

330-
if deployTerm != "" {
335+
if roflCommon.Term != "" {
331336
// Custom deploy term.
332-
term = roflCommon.ParseMachineTerm(deployTerm)
337+
term = roflCommon.ParseMachineTerm(roflCommon.Term)
333338
if _, ok := offer.Payment.Native.Terms[term]; !ok {
334-
cobra.CheckErr(fmt.Errorf("term '%s' is not available for offer '%s'", deployTerm, offer.ID))
339+
cobra.CheckErr(fmt.Errorf("term '%s' is not available for offer '%s'", roflCommon.Term, offer.ID))
335340
}
336341
return
337342
}
@@ -447,8 +452,6 @@ func init() {
447452
providerFlags.StringVar(&deployProvider, "provider", "", "set the provider address")
448453
providerFlags.StringVar(&deployOffer, "offer", "", "set the provider's offer identifier")
449454
providerFlags.StringVar(&deployMachine, "machine", buildRofl.DefaultMachineName, "machine to deploy into")
450-
providerFlags.StringVar(&deployTerm, "term", "", "term to pay for in advance")
451-
providerFlags.Uint64Var(&deployTermCount, "term-count", 1, "number of terms to pay for in advance")
452455
providerFlags.BoolVar(&deployForce, "force", false, "force deployment")
453456
providerFlags.BoolVar(&deployShowOffers, "show-offers", false, "show all provider offers and quit")
454457
providerFlags.BoolVar(&deployReplaceMachine, "replace-machine", false, "rent a new machine if the provided one expired")
@@ -457,4 +460,5 @@ func init() {
457460
deployCmd.Flags().AddFlagSet(providerFlags)
458461
deployCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
459462
deployCmd.Flags().AddFlagSet(roflCommon.WipeFlags)
463+
deployCmd.Flags().AddFlagSet(roflCommon.TermFlags)
460464
}

cmd/rofl/machine/mgmt.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/spf13/cobra"
9-
flag "github.com/spf13/pflag"
109

1110
"github.com/oasisprotocol/oasis-core/go/common/cbor"
1211
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
@@ -22,9 +21,6 @@ import (
2221
)
2322

2423
var (
25-
topUpTerm string
26-
topUpTermCount uint64
27-
2824
showCmd = &cobra.Command{
2925
Use: "show [<machine-name>]",
3026
Short: "Show information about a machine",
@@ -262,13 +258,16 @@ var (
262258
// Resolve provider address.
263259
providerAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, machine.Provider)
264260
if err != nil {
265-
cobra.CheckErr(fmt.Sprintf("Invalid provider address: %s", err))
261+
cobra.CheckErr(fmt.Sprintf("invalid provider address: %s", err))
266262
}
267263

268264
// Parse machine payment term.
269-
term := roflCommon.ParseMachineTerm(topUpTerm)
270-
if topUpTermCount < 1 {
271-
cobra.CheckErr("Number of terms must be at least 1.")
265+
if roflCommon.Term == "" {
266+
cobra.CheckErr("no term period specified. Use --term to specify it")
267+
}
268+
term := roflCommon.ParseMachineTerm(roflCommon.Term)
269+
if roflCommon.TermCount < 1 {
270+
cobra.CheckErr("number of terms must be at least 1.")
272271
}
273272

274273
// When not in offline mode, connect to the given network endpoint.
@@ -281,14 +280,14 @@ var (
281280

282281
fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
283282
fmt.Printf("Top-up machine: %s [%s]\n", machineName, machine.ID)
284-
fmt.Printf("Top-up term: %d x %s\n", topUpTermCount, topUpTerm)
283+
fmt.Printf("Top-up term: %d x %s\n", roflCommon.TermCount, roflCommon.Term)
285284

286285
// Prepare transaction.
287286
tx := roflmarket.NewInstanceTopUpTx(nil, &roflmarket.InstanceTopUp{
288287
Provider: *providerAddr,
289288
ID: machineID,
290289
Term: term,
291-
TermCount: topUpTermCount,
290+
TermCount: roflCommon.TermCount,
292291
})
293292

294293
acc := common.LoadAccount(cliConfig.Global(), npa.AccountName)
@@ -402,10 +401,6 @@ func showCommandArgs[V any](npa *common.NPASelection, raw []byte, args V) {
402401
}
403402

404403
func init() {
405-
topUpFlags := flag.NewFlagSet("", flag.ContinueOnError)
406-
topUpFlags.StringVar(&topUpTerm, "term", roflCommon.TermMonth, "term to pay for in advance")
407-
topUpFlags.Uint64Var(&topUpTermCount, "term-count", 1, "number of terms to pay for in advance")
408-
409404
showCmd.Flags().AddFlagSet(common.SelectorFlags)
410405
showCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
411406

@@ -426,5 +421,5 @@ func init() {
426421
topUpCmd.Flags().AddFlagSet(common.SelectorFlags)
427422
topUpCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
428423
topUpCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
429-
topUpCmd.Flags().AddFlagSet(topUpFlags)
424+
topUpCmd.Flags().AddFlagSet(roflCommon.TermFlags)
430425
}

docs/rofl.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ offer:
194194

195195
[ROFL marketplace]: https://github.com/oasisprotocol/oasis-sdk/blob/main/docs/rofl/features/marketplace.mdx
196196

197+
## Top-up payment for the machine {#top-up}
198+
199+
Run `rofl machine top-up` to extend the rental of the machine obtained from
200+
the [ROFL marketplace]. The rental is extended under the terms of the original
201+
offer. Specify the extension period with [`--term`][term-flags] and
202+
[`--term-count`][term-flags] parameters.
203+
204+
![code shell](../examples/rofl/machine-top-up.in.static)
205+
206+
![code](../examples/rofl/machine-top-up.out.static)
207+
208+
[term-flags]: #deploy
209+
197210
## Advanced
198211

199212
### Upgrade ROFL app dependencies {#upgrade}

examples/rofl/deploy.out.static

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ Transaction hash: bce96976f38485546b5950f8b2a7f9b7c43b9656935cc472a90680187469f4
4141
Execution successful.
4242
Created machine: 0000000000000000
4343
Deployment into machine scheduled.
44-
Use `oasis rofl machine show` to see status.
44+
This machine expires on 2025-08-07 12:35:47 +0200 CEST. Use `oasis rofl machine top-up` to extend it.
45+
Use `oasis rofl machine show` to check status.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oasis rofl machine top-up --term hour --term-count 12
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Using provider: oasis1qp2ens0hsp7gh23wajxa4hpetkdek3swyyulyrmz (oasis1qp2ens0hsp7gh23wajxa4hpetkdek3swyyulyrmz)
2+
Top-up machine: default [000000000000022a]
3+
Top-up term: 12 x hour
4+
Unlock your account.
5+
? Passphrase:
6+
You are about to sign the following transaction:
7+
Format: plain
8+
Method: roflmarket.InstanceTopUp
9+
Body:
10+
{
11+
"provider": "oasis1qp2ens0hsp7gh23wajxa4hpetkdek3swyyulyrmz",
12+
"id": "000000000000022a",
13+
"term": 1,
14+
"term_count": 12
15+
}
16+
Authorized signer(s):
17+
1. AyZKkxNFeyqLI5HGTYqEmCcYxKGo/kueOzSHzdnrSePO (secp256k1eth)
18+
Nonce: 996
19+
Fee:
20+
Amount: 0.0013614 TEST
21+
Gas limit: 13614
22+
(gas price: 0.0000001 TEST per gas unit)
23+
24+
Network: testnet
25+
ParaTime: sapphire
26+
Account: dave
27+
? Sign this transaction? Yes
28+
(In case you are using a hardware-based signer you may need to confirm on device.)
29+
Broadcasting transaction...
30+
Transaction included in block successfully.
31+
Round: 12917124
32+
Transaction hash: 094ddc21c39acd96789153003016bda5d2a0077e7be11635bb755b6c49c287ac
33+
Execution successful.
34+
Machine topped up.

0 commit comments

Comments
 (0)