Skip to content

Commit

Permalink
Merge pull request #3792 from oasisprotocol/tjanez/cmd-context-with-g…
Browse files Browse the repository at this point in the history
…enesis-info

go/oasis-node/cmd: Improve transaction preview when generating txns
  • Loading branch information
tjanez authored Mar 17, 2021
2 parents 3a80506 + a91700d commit a39d1e1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .changelog/3792.feature.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go/oasis-node/cmd: Improve transaction preview when generating transactions

Display amounts in tokens and display genesis document's hash when previewing
transactions with various `oasis-node * gen_*` CLI commands.
1 change: 1 addition & 0 deletions .changelog/3792.feature.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/oasis-node/cmd/common/context: Add `GetCtxWithGenesisInfo()` helper
20 changes: 20 additions & 0 deletions go/oasis-node/cmd/common/context/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Package context implements common context helpers.
package context

import (
"context"

"github.com/oasisprotocol/oasis-core/go/common/prettyprint"
genesisAPI "github.com/oasisprotocol/oasis-core/go/genesis/api"
)

// GetCtxWithGenesisInfo returns a new context with values that contain
// additional from the given genesis file (e.g. token's symbol and token value's
// base-10 exponent, genesis document's hash).
func GetCtxWithGenesisInfo(genesis *genesisAPI.Document) context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, prettyprint.ContextKeyTokenSymbol, genesis.Staking.TokenSymbol)
ctx = context.WithValue(ctx, prettyprint.ContextKeyTokenValueExponent, genesis.Staking.TokenValueExponent)
ctx = context.WithValue(ctx, prettyprint.ContextKeyGenesisHash, genesis.Hash())
return ctx
}
9 changes: 5 additions & 4 deletions go/oasis-node/cmd/governance/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
governance "github.com/oasisprotocol/oasis-core/go/governance/api"
cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common"
cmdConsensus "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/consensus"
cmdContext "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/context"
cmdFlags "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
cmdGrpc "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/grpc"
cmdSigner "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/signer"
Expand Down Expand Up @@ -100,7 +101,7 @@ func doGenSubmitProposal(cmd *cobra.Command, args []string) {
cmdCommon.EarlyLogAndExit(err)
}

cmdConsensus.InitGenesis()
genesis := cmdConsensus.InitGenesis()
cmdConsensus.AssertTxFileOK()

nonce, fee := cmdConsensus.GetTxNonceAndFee()
Expand Down Expand Up @@ -149,15 +150,15 @@ func doGenSubmitProposal(cmd *cobra.Command, args []string) {
os.Exit(1)
}

cmdConsensus.SignAndSaveTx(context.Background(), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func doGenCastVote(cmd *cobra.Command, args []string) {
if err := cmdCommon.Init(); err != nil {
cmdCommon.EarlyLogAndExit(err)
}

cmdConsensus.InitGenesis()
genesis := cmdConsensus.InitGenesis()
cmdConsensus.AssertTxFileOK()

id := viper.GetUint64(cfgVoteProposalID)
Expand All @@ -179,7 +180,7 @@ func doGenCastVote(cmd *cobra.Command, args []string) {
ID: id,
Vote: vote,
})
cmdConsensus.SignAndSaveTx(context.Background(), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func doProposalInfo(cmd *cobra.Command, args []string) {
Expand Down
6 changes: 3 additions & 3 deletions go/oasis-node/cmd/keymanager/keymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keymanager

import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -25,6 +24,7 @@ import (
kmApi "github.com/oasisprotocol/oasis-core/go/keymanager/api"
cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common"
cmdConsensus "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/consensus"
cmdContext "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/context"
cmdFlags "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
)

Expand Down Expand Up @@ -391,7 +391,7 @@ func doGenUpdate(cmd *cobra.Command, args []string) {
cmdCommon.EarlyLogAndExit(err)
}

cmdConsensus.InitGenesis()
genesis := cmdConsensus.InitGenesis()
cmdConsensus.AssertTxFileOK()

// Assemble the SignedPolicySGX from the policy document and detached
Expand Down Expand Up @@ -444,7 +444,7 @@ func doGenUpdate(cmd *cobra.Command, args []string) {
// Build, sign, and write the UpdatePolicy transaction.
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := kmApi.NewUpdatePolicyTx(nonce, fee, &signedPolicy)
cmdConsensus.SignAndSaveTx(context.Background(), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func statusFromFlags() (*kmApi.Status, error) {
Expand Down
9 changes: 5 additions & 4 deletions go/oasis-node/cmd/registry/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common"
cmdConsensus "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/consensus"
cmdContext "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/context"
cmdFlags "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
cmdGrpc "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/grpc"
cmdSigner "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/signer"
Expand Down Expand Up @@ -279,7 +280,7 @@ func doGenRegister(cmd *cobra.Command, args []string) {
cmdCommon.EarlyLogAndExit(err)
}

cmdConsensus.InitGenesis()
genesis := cmdConsensus.InitGenesis()
cmdConsensus.AssertTxFileOK()

ent, signer, err := cmdCommon.LoadEntitySigner()
Expand All @@ -302,21 +303,21 @@ func doGenRegister(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := registry.NewRegisterEntityTx(nonce, fee, signed)

cmdConsensus.SignAndSaveTx(context.Background(), tx, signer)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, signer)
}

func doGenDeregister(cmd *cobra.Command, args []string) {
if err := cmdCommon.Init(); err != nil {
cmdCommon.EarlyLogAndExit(err)
}

cmdConsensus.InitGenesis()
genesis := cmdConsensus.InitGenesis()
cmdConsensus.AssertTxFileOK()

nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := registry.NewDeregisterEntityTx(nonce, fee)

cmdConsensus.SignAndSaveTx(context.Background(), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func doList(cmd *cobra.Command, args []string) {
Expand Down
5 changes: 3 additions & 2 deletions go/oasis-node/cmd/registry/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common"
cmdConsensus "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/consensus"
cmdContext "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/context"
cmdFlags "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
cmdGrpc "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/grpc"
cmdSigner "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/signer"
Expand Down Expand Up @@ -206,7 +207,7 @@ func doGenRegister(cmd *cobra.Command, args []string) {
cmdCommon.EarlyLogAndExit(err)
}

cmdConsensus.InitGenesis()
genesis := cmdConsensus.InitGenesis()
cmdConsensus.AssertTxFileOK()

rt, err := runtimeFromFlags()
Expand All @@ -220,7 +221,7 @@ func doGenRegister(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := registry.NewRegisterRuntimeTx(nonce, fee, rt)

cmdConsensus.SignAndSaveTx(context.Background(), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func doList(cmd *cobra.Command, args []string) {
Expand Down
26 changes: 8 additions & 18 deletions go/oasis-node/cmd/stake/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/spf13/viper"

"github.com/oasisprotocol/oasis-core/go/common/prettyprint"
genesisAPI "github.com/oasisprotocol/oasis-core/go/genesis/api"
cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common"
cmdConsensus "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/consensus"
cmdContext "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/context"
cmdFlags "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
cmdGrpc "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/grpc"
"github.com/oasisprotocol/oasis-core/go/staking/api"
Expand Down Expand Up @@ -128,16 +128,6 @@ var (
}
)

// getCtxWithInfo returns a new context with values that contain additional
// information (ticker symbol, value base-10 exponent, genesis document's hash).
func getCtxWithInfo(genesis *genesisAPI.Document) context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, prettyprint.ContextKeyTokenSymbol, genesis.Staking.TokenSymbol)
ctx = context.WithValue(ctx, prettyprint.ContextKeyTokenValueExponent, genesis.Staking.TokenValueExponent)
ctx = context.WithValue(ctx, prettyprint.ContextKeyGenesisHash, genesis.Hash())
return ctx
}

func doAccountInfo(cmd *cobra.Command, args []string) {
if err := cmdCommon.Init(); err != nil {
cmdCommon.EarlyLogAndExit(err)
Expand Down Expand Up @@ -273,7 +263,7 @@ func doAccountTransfer(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := api.NewTransferTx(nonce, fee, &xfer)

cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func doAccountBurn(cmd *cobra.Command, args []string) {
Expand All @@ -295,7 +285,7 @@ func doAccountBurn(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := api.NewBurnTx(nonce, fee, &burn)

cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func doAccountEscrow(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -323,7 +313,7 @@ func doAccountEscrow(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := api.NewAddEscrowTx(nonce, fee, &escrow)

cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func doAccountReclaimEscrow(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -351,7 +341,7 @@ func doAccountReclaimEscrow(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := api.NewReclaimEscrowTx(nonce, fee, &reclaim)

cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func scanRateStep(dst *api.CommissionRateStep, raw string) error {
Expand Down Expand Up @@ -429,7 +419,7 @@ func doAccountAmendCommissionSchedule(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := api.NewAmendCommissionScheduleTx(nonce, fee, &amendCommissionSchedule)

cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func doAccountAllow(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -466,7 +456,7 @@ func doAccountAllow(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := api.NewAllowTx(nonce, fee, &allow)

cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func doAccountWithdraw(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -494,7 +484,7 @@ func doAccountWithdraw(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := api.NewWithdrawTx(nonce, fee, &withdraw)

cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx, nil)
cmdConsensus.SignAndSaveTx(cmdContext.GetCtxWithGenesisInfo(genesis), tx, nil)
}

func registerAccountCmd() {
Expand Down

0 comments on commit a39d1e1

Please sign in to comment.