From c7cb557d18749db2b34576d50281a2ee7a953022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Jane=C5=BE?= Date: Fri, 31 Jul 2020 14:29:17 +0200 Subject: [PATCH 1/3] go/consensus/api/transaction: Add PrettyPrintBody() to Transaction type Extract a transaction's body pretty-printing functionality to a separate method to allow easier extension of the PrettyPrint() method. --- .changelog/3157.feature.md | 1 + go/consensus/api/transaction/transaction.go | 39 ++++++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 .changelog/3157.feature.md diff --git a/.changelog/3157.feature.md b/.changelog/3157.feature.md new file mode 100644 index 00000000000..4e7b7ec6e41 --- /dev/null +++ b/.changelog/3157.feature.md @@ -0,0 +1 @@ +go/consensus/api/transaction: Add `PrettyPrintBody()` to `Transaction` type diff --git a/go/consensus/api/transaction/transaction.go b/go/consensus/api/transaction/transaction.go index 22fd3db30a1..482a7a01444 100644 --- a/go/consensus/api/transaction/transaction.go +++ b/go/consensus/api/transaction/transaction.go @@ -46,45 +46,50 @@ 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 \n", prefix, base64.StdEncoding.EncodeToString(t.Body)) + fmt.Fprintf(w, "%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 \n", prefix, err) - fmt.Fprintf(w, "%s \n", prefix, base64.StdEncoding.EncodeToString(t.Body)) + fmt.Fprintf(w, "%s\n", prefix, err) + fmt.Fprintf(w, "%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 \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) } // PrettyType returns a representation of the type that can be used for pretty printing. From 974955116d0486cc34704b8e8c5d95663c7cd4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Jane=C5=BE?= Date: Fri, 31 Jul 2020 14:33:56 +0200 Subject: [PATCH 2/3] go/consensus/api/transaction: PrettyPrint Genesis' hash for txns If it is provided in the given context under ContextKeyGenesisHash key. --- go/common/prettyprint/context.go | 7 +++++++ go/consensus/api/transaction/transaction.go | 5 +++++ 2 files changed, 12 insertions(+) create mode 100644 go/common/prettyprint/context.go diff --git a/go/common/prettyprint/context.go b/go/common/prettyprint/context.go new file mode 100644 index 00000000000..2fff2c43376 --- /dev/null +++ b/go/common/prettyprint/context.go @@ -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 diff --git a/go/consensus/api/transaction/transaction.go b/go/consensus/api/transaction/transaction.go index 482a7a01444..2dcf5c4952b 100644 --- a/go/consensus/api/transaction/transaction.go +++ b/go/consensus/api/transaction/transaction.go @@ -90,6 +90,11 @@ func (t Transaction) PrettyPrint(ctx context.Context, prefix string, w io.Writer 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. From 71e3a8ef755999070ba4781c3f62e899fc0f98ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Jane=C5=BE?= Date: Fri, 31 Jul 2020 14:41:10 +0200 Subject: [PATCH 3/3] go/oasis-node/cmd: Show Genesis document's hash when displaying txns 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. --- .changelog/2871.feature.md | 5 +++++ go/oasis-node/cmd/consensus/consensus.go | 2 ++ go/oasis-node/cmd/stake/account.go | 18 ++++++++++-------- 3 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 .changelog/2871.feature.md diff --git a/.changelog/2871.feature.md b/.changelog/2871.feature.md new file mode 100644 index 00000000000..46de8a58c34 --- /dev/null +++ b/.changelog/2871.feature.md @@ -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. diff --git a/go/oasis-node/cmd/consensus/consensus.go b/go/oasis-node/cmd/consensus/consensus.go index bacff2fd215..5b7e89af5fa 100644 --- a/go/oasis-node/cmd/consensus/consensus.go +++ b/go/oasis-node/cmd/consensus/consensus.go @@ -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" @@ -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) diff --git a/go/oasis-node/cmd/stake/account.go b/go/oasis-node/cmd/stake/account.go index 89695d67a06..5b7e8c2cbc3 100644 --- a/go/oasis-node/cmd/stake/account.go +++ b/go/oasis-node/cmd/stake/account.go @@ -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" @@ -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 } @@ -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) { @@ -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) { @@ -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) { @@ -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 { @@ -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() {