Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/oasis-node/cmd: Show Genesis document's hash when displaying transactions #3157

Merged
merged 3 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changelog/2871.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go/oasis-node/cmd: Show Genesis document's hash when displaying transactions

Show Genesis document's hash when generating staking transactions with
`oasis-node stake account gen_*` CLI commands and when showing transactions'
pretty prints with the `oasis-node consensus show_tx` CLI command.
1 change: 1 addition & 0 deletions .changelog/3157.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/consensus/api/transaction: Add `PrettyPrintBody()` to `Transaction` type
7 changes: 7 additions & 0 deletions go/common/prettyprint/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package prettyprint

// ContextKeyGenesisHash is the key to retrieve the Genesis document's hash
// value from a context.
var ContextKeyGenesisHash = contextKey("genesis/hash")

type contextKey string
44 changes: 27 additions & 17 deletions go/consensus/api/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,55 @@ type Transaction struct {
Body cbor.RawMessage `json:"body,omitempty"`
}

// PrettyPrint writes a pretty-printed representation of the type
// PrettyPrintBody writes a pretty-printed representation of transaction's body
// to the given writer.
func (t Transaction) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
fmt.Fprintf(w, "%sNonce: %d\n", prefix, t.Nonce)
if t.Fee != nil {
fmt.Fprintf(w, "%sFee: %s (gas limit: %d, gas price: %s)\n", prefix, t.Fee.Amount, t.Fee.Gas, t.Fee.GasPrice())
} else {
fmt.Fprintf(w, "%sFee: none\n", prefix)
}
fmt.Fprintf(w, "%sMethod: %s\n", prefix, t.Method)
fmt.Fprintf(w, "%sBody:\n", prefix)

func (t Transaction) PrettyPrintBody(ctx context.Context, prefix string, w io.Writer) {
bodyType := t.Method.BodyType()
if bodyType == nil {
fmt.Fprintf(w, "%s <unknown method body: %s>\n", prefix, base64.StdEncoding.EncodeToString(t.Body))
fmt.Fprintf(w, "%s<unknown method body: %s>\n", prefix, base64.StdEncoding.EncodeToString(t.Body))
return
}

// Deserialize into correct type.
v := reflect.New(reflect.TypeOf(bodyType)).Interface()
if err := cbor.Unmarshal(t.Body, v); err != nil {
fmt.Fprintf(w, "%s <error: %s>\n", prefix, err)
fmt.Fprintf(w, "%s <malformed: %s>\n", prefix, base64.StdEncoding.EncodeToString(t.Body))
fmt.Fprintf(w, "%s<error: %s>\n", prefix, err)
fmt.Fprintf(w, "%s<malformed: %s>\n", prefix, base64.StdEncoding.EncodeToString(t.Body))
return
}

// If the body type supports pretty printing, use that.
if pp, ok := v.(prettyprint.PrettyPrinter); ok {
pp.PrettyPrint(ctx, prefix+" ", w)
pp.PrettyPrint(ctx, prefix, w)
return
}

// Otherwise, just serialize into JSON and display that.
data, err := json.MarshalIndent(v, prefix+" ", " ")
data, err := json.MarshalIndent(v, prefix, " ")
if err != nil {
fmt.Fprintf(w, "%s <raw: %s>\n", prefix, base64.StdEncoding.EncodeToString(t.Body))
return
}
fmt.Fprintf(w, "%s %s\n", prefix, data)
fmt.Fprintf(w, "%s%s\n", prefix, data)
}

// PrettyPrint writes a pretty-printed representation of the transaction to the
// given writer.
func (t Transaction) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
fmt.Fprintf(w, "%sNonce: %d\n", prefix, t.Nonce)
if t.Fee != nil {
fmt.Fprintf(w, "%sFee: %s (gas limit: %d, gas price: %s)\n", prefix, t.Fee.Amount, t.Fee.Gas, t.Fee.GasPrice())
} else {
fmt.Fprintf(w, "%sFee: none\n", prefix)
}
fmt.Fprintf(w, "%sMethod: %s\n", prefix, t.Method)
fmt.Fprintf(w, "%sBody:\n", prefix)
t.PrettyPrintBody(ctx, prefix+" ", w)

if genesisHash, ok := ctx.Value(prettyprint.ContextKeyGenesisHash).(hash.Hash); ok {
fmt.Println("Other info:")
fmt.Printf(" Genesis document's hash: %s\n", genesisHash)
}
}

// PrettyType returns a representation of the type that can be used for pretty printing.
Expand Down
2 changes: 2 additions & 0 deletions go/oasis-node/cmd/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/common/prettyprint"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common"
Expand Down Expand Up @@ -138,6 +139,7 @@ func doShowTx(cmd *cobra.Command, args []string) {
ctx := context.Background()
ctx = context.WithValue(ctx, staking.PrettyPrinterContextKeyTokenSymbol, genesis.Staking.TokenSymbol)
ctx = context.WithValue(ctx, staking.PrettyPrinterContextKeyTokenValueExponent, genesis.Staking.TokenValueExponent)
ctx = context.WithValue(ctx, prettyprint.ContextKeyGenesisHash, genesis.Hash())

sigTx := loadTx()
sigTx.PrettyPrint(ctx, "", os.Stdout)
Expand Down
18 changes: 10 additions & 8 deletions go/oasis-node/cmd/stake/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
flag "github.com/spf13/pflag"
"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"
Expand Down Expand Up @@ -93,12 +94,13 @@ var (
}
)

// getCtxWithTokenInfo returns a new context with values that contain token
// information (ticker symbol, value base-10 exponent).
func getCtxWithTokenInfo(genesis *genesisAPI.Document) context.Context {
// 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, staking.PrettyPrinterContextKeyTokenSymbol, genesis.Staking.TokenSymbol)
ctx = context.WithValue(ctx, staking.PrettyPrinterContextKeyTokenValueExponent, genesis.Staking.TokenValueExponent)
ctx = context.WithValue(ctx, prettyprint.ContextKeyGenesisHash, genesis.Hash())
return ctx
}

Expand Down Expand Up @@ -152,7 +154,7 @@ func doAccountTransfer(cmd *cobra.Command, args []string) {
nonce, fee := cmdConsensus.GetTxNonceAndFee()
tx := staking.NewTransferTx(nonce, fee, &xfer)

cmdConsensus.SignAndSaveTx(getCtxWithTokenInfo(genesis), tx)
cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx)
}

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

cmdConsensus.SignAndSaveTx(getCtxWithTokenInfo(genesis), tx)
cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx)
}

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

cmdConsensus.SignAndSaveTx(getCtxWithTokenInfo(genesis), tx)
cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx)
}

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

cmdConsensus.SignAndSaveTx(getCtxWithTokenInfo(genesis), tx)
cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx)
}

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

cmdConsensus.SignAndSaveTx(getCtxWithTokenInfo(genesis), tx)
cmdConsensus.SignAndSaveTx(getCtxWithInfo(genesis), tx)
}

func registerAccountCmd() {
Expand Down