From 4cb71ceb6ed22152f5984c05c9cfe141348d3ba2 Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Mon, 10 Apr 2023 13:55:06 -0400 Subject: [PATCH] style!: lint fixes for x/tx (#15748) --- go.mod | 2 +- go.sum | 4 +-- simapp/go.mod | 2 +- simapp/go.sum | 4 +-- tests/go.mod | 2 +- tests/integration/aminojson/aminojson_test.go | 8 +++--- x/auth/tx/textual.go | 2 +- x/tx/CHANGELOG.md | 1 + x/tx/decode/unknown_test.go | 4 --- x/tx/signing/aminojson/aminojson.go | 2 +- x/tx/signing/aminojson/aminojson_test.go | 2 +- x/tx/signing/aminojson/json_marshal.go | 9 ++++--- x/tx/signing/direct/direct.go | 2 +- x/tx/signing/direct/direct_test.go | 6 ++--- x/tx/signing/directaux/direct_aux.go | 25 ++++++++++--------- x/tx/signing/directaux/direct_aux_test.go | 4 +-- x/tx/signing/get_signers.go | 2 +- x/tx/signing/signer_data.go | 4 +-- x/tx/signing/testutil/util.go | 4 +-- x/tx/signing/textual/any_test.go | 6 ++--- x/tx/signing/textual/bytes_test.go | 2 +- x/tx/signing/textual/coin_test.go | 6 ++--- x/tx/signing/textual/coins_test.go | 8 +++--- x/tx/signing/textual/dec_test.go | 2 +- x/tx/signing/textual/duration.go | 24 +++++++++--------- x/tx/signing/textual/e2e_test.go | 10 ++++---- x/tx/signing/textual/encode_test.go | 4 +-- x/tx/signing/textual/enum_test.go | 19 ++++++++------ x/tx/signing/textual/fuzz_test.go | 2 +- x/tx/signing/textual/handler.go | 4 +-- x/tx/signing/textual/int_test.go | 2 +- x/tx/signing/textual/message_test.go | 8 +++--- x/tx/signing/textual/repeated_test.go | 6 ++--- x/tx/signing/textual/string_test.go | 6 ++--- x/tx/signing/textual/timestamp_test.go | 14 +++++------ x/tx/signing/textual/tx_test.go | 20 ++++++++------- 36 files changed, 120 insertions(+), 112 deletions(-) diff --git a/go.mod b/go.mod index 982d5cf1a1..a988b423c8 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( cosmossdk.io/log v1.0.0 cosmossdk.io/math v1.0.0 cosmossdk.io/store v0.1.0-alpha.1.0.20230328185921-37ba88872dbc - cosmossdk.io/x/tx v0.5.0 + cosmossdk.io/x/tx v0.5.1-0.20230407182919-057d2e09bd63 github.com/99designs/keyring v1.2.1 github.com/armon/go-metrics v0.4.1 github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 diff --git a/go.sum b/go.sum index ef456d53fb..af9f6a33b6 100644 --- a/go.sum +++ b/go.sum @@ -51,8 +51,8 @@ cosmossdk.io/math v1.0.0 h1:ro9w7eKx23om2tZz/VM2Pf+z2WAbGX1yDQQOJ6iGeJw= cosmossdk.io/math v1.0.0/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k= cosmossdk.io/store v0.1.0-alpha.1.0.20230328185921-37ba88872dbc h1:9piuA+NYmhe+SyMPtMoboLw/djgDbrI3dD5TG020Tnk= cosmossdk.io/store v0.1.0-alpha.1.0.20230328185921-37ba88872dbc/go.mod h1:UFF5rmjN7WYVfxo6ArdY/l1+yyWMURBWOmSJypGqFHQ= -cosmossdk.io/x/tx v0.5.0 h1:01wPSoiYDHlfudV0fn867SBXI3uI/8tpatBgVVSnFzI= -cosmossdk.io/x/tx v0.5.0/go.mod h1:kDcwrN6QbCj+9NXVHL8qipMzA9YlMr9NpZa08SHCdLA= +cosmossdk.io/x/tx v0.5.1-0.20230407182919-057d2e09bd63 h1:zHqj2VwZ/MStFmR7SUe/7gErOFhL9v2rkjmWPB/st34= +cosmossdk.io/x/tx v0.5.1-0.20230407182919-057d2e09bd63/go.mod h1:Oh3Kh+IPOfMEILNxVd2e8SLqRrIjYHpdGBfDg4ghU/k= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek= filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= diff --git a/simapp/go.mod b/simapp/go.mod index cf677a34f1..5dc8cbb001 100644 --- a/simapp/go.mod +++ b/simapp/go.mod @@ -38,7 +38,7 @@ require ( cloud.google.com/go/storage v1.30.0 // indirect cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba // indirect cosmossdk.io/errors v1.0.0-beta.7 // indirect - cosmossdk.io/x/tx v0.5.0 // indirect + cosmossdk.io/x/tx v0.5.1-0.20230407182919-057d2e09bd63 // indirect filippo.io/edwards25519 v1.0.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect github.com/99designs/keyring v1.2.1 // indirect diff --git a/simapp/go.sum b/simapp/go.sum index 4edde3a89e..2b4629c5fa 100644 --- a/simapp/go.sum +++ b/simapp/go.sum @@ -202,8 +202,8 @@ cosmossdk.io/log v1.0.0 h1:NGKZ/A5rd4PduDfoscgABklX557PWjQINbosZy/m3Jk= cosmossdk.io/log v1.0.0/go.mod h1:CwX9BLiBruZb7lzLlRr3R231d/fVPUXk8gAdV4LQap0= cosmossdk.io/math v1.0.0 h1:ro9w7eKx23om2tZz/VM2Pf+z2WAbGX1yDQQOJ6iGeJw= cosmossdk.io/math v1.0.0/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k= -cosmossdk.io/x/tx v0.5.0 h1:01wPSoiYDHlfudV0fn867SBXI3uI/8tpatBgVVSnFzI= -cosmossdk.io/x/tx v0.5.0/go.mod h1:kDcwrN6QbCj+9NXVHL8qipMzA9YlMr9NpZa08SHCdLA= +cosmossdk.io/x/tx v0.5.1-0.20230407182919-057d2e09bd63 h1:zHqj2VwZ/MStFmR7SUe/7gErOFhL9v2rkjmWPB/st34= +cosmossdk.io/x/tx v0.5.1-0.20230407182919-057d2e09bd63/go.mod h1:Oh3Kh+IPOfMEILNxVd2e8SLqRrIjYHpdGBfDg4ghU/k= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek= filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= diff --git a/tests/go.mod b/tests/go.mod index 8a2cf63778..9a853a69a8 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -13,7 +13,7 @@ require ( cosmossdk.io/x/evidence v0.1.0 cosmossdk.io/x/feegrant v0.0.0-20230117113717-50e7c4a4ceff cosmossdk.io/x/nft v0.0.0-20230113085233-fae3332d62fc - cosmossdk.io/x/tx v0.5.0 + cosmossdk.io/x/tx v0.5.1-0.20230407182919-057d2e09bd63 cosmossdk.io/x/upgrade v0.0.0-20230127052425-54c8e1568335 github.com/cometbft/cometbft v0.37.0 github.com/cosmos/cosmos-db v1.0.0-rc.1 diff --git a/tests/integration/aminojson/aminojson_test.go b/tests/integration/aminojson/aminojson_test.go index 796f440828..bae33a8bdd 100644 --- a/tests/integration/aminojson/aminojson_test.go +++ b/tests/integration/aminojson/aminojson_test.go @@ -382,7 +382,7 @@ func TestAminoJSON_Equivalence(t *testing.T) { } handlerOptions := signing_testutil.HandlerArgumentOptions{ - ChainId: "test-chain", + ChainID: "test-chain", Memo: "sometestmemo", Msg: tt.pulsar, AccNum: 1, @@ -408,7 +408,7 @@ func TestAminoJSON_Equivalence(t *testing.T) { theTx := txBuilder.GetTx() legacySigningData := signing.SignerData{ - ChainID: handlerOptions.ChainId, + ChainID: handlerOptions.ChainID, Address: handlerOptions.SignerAddress, AccountNumber: handlerOptions.AccNum, Sequence: handlerOptions.AccSeq, @@ -683,7 +683,7 @@ func TestAminoJSON_LegacyParity(t *testing.T) { } handlerOptions := signing_testutil.HandlerArgumentOptions{ - ChainId: "test-chain", + ChainID: "test-chain", Memo: "sometestmemo", Msg: tc.pulsar, AccNum: 1, @@ -709,7 +709,7 @@ func TestAminoJSON_LegacyParity(t *testing.T) { theTx := txBuilder.GetTx() legacySigningData := signing.SignerData{ - ChainID: handlerOptions.ChainId, + ChainID: handlerOptions.ChainID, Address: handlerOptions.SignerAddress, AccountNumber: handlerOptions.AccNum, Sequence: handlerOptions.AccSeq, diff --git a/x/auth/tx/textual.go b/x/auth/tx/textual.go index 7553f03596..a804280adf 100644 --- a/x/auth/tx/textual.go +++ b/x/auth/tx/textual.go @@ -79,7 +79,7 @@ func (h signModeTextualHandler) GetSignBytesWithContext(ctx context.Context, mod return h.t.GetSignBytes(ctx, txsigning.SignerData{ Address: data.Address, - ChainId: data.ChainID, + ChainID: data.ChainID, AccountNumber: data.AccountNumber, Sequence: data.Sequence, PubKey: &anypb.Any{ diff --git a/x/tx/CHANGELOG.md b/x/tx/CHANGELOG.md index 1b314b76b0..dc3ac1bd12 100644 --- a/x/tx/CHANGELOG.md +++ b/x/tx/CHANGELOG.md @@ -36,6 +36,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#15581](https://github.com/cosmos/cosmos-sdk/pull/15581) `GetSignersOptions` and `directaux.SignModeHandlerOptions` now require a `signing.ProtoFileResolver` interface instead of `protodesc.Resolver`. * [#15742](https://github.com/cosmos/cosmos-sdk/pull/15742) The `direct_aux` package has been renamed to `directaux` in line with Go conventions. No other types were changed during the package rename. +* [#15748](https://github.com/cosmos/cosmos-sdk/pull/15748) Rename signing.SignerData.ChainId to .ChainID, in line with Go conventions. ### Bug Fixes diff --git a/x/tx/decode/unknown_test.go b/x/tx/decode/unknown_test.go index f47fca8a53..9ed18b6c4f 100644 --- a/x/tx/decode/unknown_test.go +++ b/x/tx/decode/unknown_test.go @@ -22,10 +22,6 @@ func errUnknownField(typ string, tagNum int, wireType protowire.Type) error { return decode.ErrUnknownField.Wrapf("%s: {TagNum: %d, WireType:%q}", typ, tagNum, wt) } -func errMismatchedField(typ string, wireType protowire.Type) error { - return fmt.Errorf("invalid wire type %s for field %s", decode.WireTypeToString(wireType), typ) -} - var ProtoResolver = protoregistry.GlobalFiles func TestRejectUnknownFieldsRepeated(t *testing.T) { diff --git a/x/tx/signing/aminojson/aminojson.go b/x/tx/signing/aminojson/aminojson.go index 7b1625f33c..8fa486f887 100644 --- a/x/tx/signing/aminojson/aminojson.go +++ b/x/tx/signing/aminojson/aminojson.go @@ -101,7 +101,7 @@ func (h SignModeHandler) GetSignBytes(_ context.Context, signerData signing.Sign signDoc := &aminojsonpb.AminoSignDoc{ AccountNumber: signerData.AccountNumber, TimeoutHeight: body.TimeoutHeight, - ChainId: signerData.ChainId, + ChainId: signerData.ChainID, Sequence: signerData.Sequence, Memo: body.Memo, Msgs: txData.Body.Messages, diff --git a/x/tx/signing/aminojson/aminojson_test.go b/x/tx/signing/aminojson/aminojson_test.go index 511ac8a654..10de8d1c43 100644 --- a/x/tx/signing/aminojson/aminojson_test.go +++ b/x/tx/signing/aminojson/aminojson_test.go @@ -19,7 +19,7 @@ func TestAminoJsonSignMode(t *testing.T) { Amount: []*basev1beta1.Coin{{Denom: "uatom", Amount: "1000"}}, } handlerOptions := testutil.HandlerArgumentOptions{ - ChainId: "test-chain", + ChainID: "test-chain", Memo: "sometestmemo", Tip: &txv1beta1.Tip{Tipper: "tipper", Amount: []*basev1beta1.Coin{{Denom: "Tip-token", Amount: "10"}}}, Msg: &bankv1beta1.MsgSend{ diff --git a/x/tx/signing/aminojson/json_marshal.go b/x/tx/signing/aminojson/json_marshal.go index 49c2e7f08c..01a91c7a2e 100644 --- a/x/tx/signing/aminojson/json_marshal.go +++ b/x/tx/signing/aminojson/json_marshal.go @@ -186,15 +186,16 @@ func (enc Encoder) marshalMessage(msg protoreflect.Message, writer io.Writer) er if !msg.Has(f) { // msg.WhichOneof(oneof) == nil: no field of the oneof has been set // !emptyOneOfWritten: we haven't written a null for this oneof yet (only write one null per empty oneof) - if isOneOf && msg.WhichOneof(oneof) == nil && !emptyOneOfWritten[oneofFieldName] { + switch { + case isOneOf && msg.WhichOneof(oneof) == nil && !emptyOneOfWritten[oneofFieldName]: name = oneofFieldName writeNil = true emptyOneOfWritten[oneofFieldName] = true - } else if omitEmpty(f) { + case omitEmpty(f): continue - } else if f.Kind() == protoreflect.MessageKind && + case f.Kind() == protoreflect.MessageKind && f.Cardinality() != protoreflect.Repeated && - !v.Message().IsValid() { + !v.Message().IsValid(): return errors.Errorf("not supported: dont_omit_empty=true on invalid (nil?) message field: %s", name) } } diff --git a/x/tx/signing/direct/direct.go b/x/tx/signing/direct/direct.go index 450f0af4e3..6cacb63aaf 100644 --- a/x/tx/signing/direct/direct.go +++ b/x/tx/signing/direct/direct.go @@ -23,7 +23,7 @@ func (SignModeHandler) GetSignBytes(_ context.Context, signerData signing.Signer return proto.Marshal(&txv1beta1.SignDoc{ BodyBytes: txData.BodyBytes, AuthInfoBytes: txData.AuthInfoBytes, - ChainId: signerData.ChainId, + ChainId: signerData.ChainID, AccountNumber: signerData.AccountNumber, }) } diff --git a/x/tx/signing/direct/direct_test.go b/x/tx/signing/direct/direct_test.go index d28d25e6a7..d75c396527 100644 --- a/x/tx/signing/direct/direct_test.go +++ b/x/tx/signing/direct/direct_test.go @@ -58,12 +58,12 @@ func TestDirectModeHandler(t *testing.T) { directHandler := direct.SignModeHandler{} - chainId := "test-chain" + chainID := "test-chain" accNum := uint64(1) signingData := signing.SignerData{ Address: "", - ChainId: chainId, + ChainID: chainID, AccountNumber: accNum, PubKey: pk, } @@ -89,7 +89,7 @@ func TestDirectModeHandler(t *testing.T) { signBytes2, err := proto.Marshal(&txv1beta1.SignDoc{ BodyBytes: txData.BodyBytes, AuthInfoBytes: txData.AuthInfoBytes, - ChainId: chainId, + ChainId: chainID, AccountNumber: accNum, }) require.NoError(t, err) diff --git a/x/tx/signing/directaux/direct_aux.go b/x/tx/signing/directaux/direct_aux.go index 791418d992..8738834906 100644 --- a/x/tx/signing/directaux/direct_aux.go +++ b/x/tx/signing/directaux/direct_aux.go @@ -72,18 +72,19 @@ func (h SignModeHandler) Mode() signingv1beta1.SignMode { // getFirstSigner returns the first signer from the first message in the tx. It replicates behavior in // https://github.com/cosmos/cosmos-sdk/blob/4a6a1e3cb8de459891cb0495052589673d14ef51/x/auth/tx/builder.go#L142 func (h SignModeHandler) getFirstSigner(txData signing.TxData) (string, error) { - for _, anyMsg := range txData.Body.Messages { - msg, err := anyutil.Unpack(anyMsg, h.fileResolver, h.typeResolver) - if err != nil { - return "", err - } - signer, err := h.signersContext.GetSigners(msg) - if err != nil { - return "", err - } - return signer[0], nil + if len(txData.Body.Messages) == 0 { + return "", fmt.Errorf("no signer found") } - return "", fmt.Errorf("no signer found") + + msg, err := anyutil.Unpack(txData.Body.Messages[0], h.fileResolver, h.typeResolver) + if err != nil { + return "", err + } + signer, err := h.signersContext.GetSigners(msg) + if err != nil { + return "", err + } + return signer[0], nil } // GetSignBytes implements signing.SignModeHandler.GetSignBytes. @@ -106,7 +107,7 @@ func (h SignModeHandler) GetSignBytes( signDocDirectAux := &txv1beta1.SignDocDirectAux{ BodyBytes: txData.BodyBytes, PublicKey: signerData.PubKey, - ChainId: signerData.ChainId, + ChainId: signerData.ChainID, AccountNumber: signerData.AccountNumber, Sequence: signerData.Sequence, Tip: txData.AuthInfo.Tip, diff --git a/x/tx/signing/directaux/direct_aux_test.go b/x/tx/signing/directaux/direct_aux_test.go index 697e0abaff..2f7ca5440b 100644 --- a/x/tx/signing/directaux/direct_aux_test.go +++ b/x/tx/signing/directaux/direct_aux_test.go @@ -66,7 +66,7 @@ func TestDirectAuxHandler(t *testing.T) { } signingData := signing.SignerData{ - ChainId: chainID, + ChainID: chainID, AccountNumber: accNum, Sequence: accSeq, Address: "", @@ -88,7 +88,7 @@ func TestDirectAuxHandler(t *testing.T) { t.Log("verify fee payer cannot use SIGN_MODE_DIRECT_AUX") feePayerSigningData := signing.SignerData{ - ChainId: chainID, + ChainID: chainID, AccountNumber: accNum, Address: feePayerAddr, PubKey: anyPk, diff --git a/x/tx/signing/get_signers.go b/x/tx/signing/get_signers.go index 77a632c336..eabd70ed03 100644 --- a/x/tx/signing/get_signers.go +++ b/x/tx/signing/get_signers.go @@ -54,7 +54,7 @@ type getSignersFunc func(proto.Message) []string func getSignersFieldNames(descriptor protoreflect.MessageDescriptor) ([]string, error) { signersFields := proto.GetExtension(descriptor.Options(), msgv1.E_Signer).([]string) - if signersFields == nil || len(signersFields) == 0 { + if len(signersFields) == 0 { return nil, fmt.Errorf("no cosmos.msg.v1.signer option found for message %s", descriptor.FullName()) } diff --git a/x/tx/signing/signer_data.go b/x/tx/signing/signer_data.go index a4ac86eb7d..3b252be630 100644 --- a/x/tx/signing/signer_data.go +++ b/x/tx/signing/signer_data.go @@ -10,8 +10,8 @@ type SignerData struct { // In case of multisigs, this should be the multisig's address. Address string - // ChainId is the chain that this transaction is targeted - ChainId string + // ChainID is the chain that this transaction is targeting. + ChainID string // AccountNumber is the account number of the signer. // diff --git a/x/tx/signing/testutil/util.go b/x/tx/signing/testutil/util.go index 5d9a0ffd60..ef689ec1e7 100644 --- a/x/tx/signing/testutil/util.go +++ b/x/tx/signing/testutil/util.go @@ -12,7 +12,7 @@ import ( ) type HandlerArgumentOptions struct { - ChainId string + ChainID string Memo string Msg proto.Message AccNum uint64 @@ -79,7 +79,7 @@ func MakeHandlerArguments(options HandlerArgumentOptions) (signing.SignerData, s signerAddress := options.SignerAddress signerData := signing.SignerData{ - ChainId: options.ChainId, + ChainID: options.ChainID, AccountNumber: options.AccNum, Sequence: options.AccSeq, Address: signerAddress, diff --git a/x/tx/signing/textual/any_test.go b/x/tx/signing/textual/any_test.go index b15d63355e..da64acf0df 100644 --- a/x/tx/signing/textual/any_test.go +++ b/x/tx/signing/textual/any_test.go @@ -26,7 +26,7 @@ import ( "cosmossdk.io/x/tx/signing/textual" ) -type anyJsonTest struct { +type anyJSONTest struct { Proto json.RawMessage Screens []textual.Screen } @@ -35,7 +35,7 @@ func TestAny(t *testing.T) { raw, err := os.ReadFile("./internal/testdata/any.json") require.NoError(t, err) - var testcases []anyJsonTest + var testcases []anyJSONTest err = json.Unmarshal(raw, &testcases) require.NoError(t, err) @@ -59,7 +59,7 @@ func TestAny(t *testing.T) { parsedMsg := val.Message().Interface() require.IsType(t, &anypb.Any{}, parsedMsg) parsedAny := parsedMsg.(*anypb.Any) - diff := cmp.Diff(anyMsg, parsedAny, protocmp.Transform()) + diff := cmp.Diff(&anyMsg, parsedAny, protocmp.Transform()) require.Empty(t, diff) }) } diff --git a/x/tx/signing/textual/bytes_test.go b/x/tx/signing/textual/bytes_test.go index 1f9fae13a8..02fea06cf3 100644 --- a/x/tx/signing/textual/bytes_test.go +++ b/x/tx/signing/textual/bytes_test.go @@ -12,7 +12,7 @@ import ( "cosmossdk.io/x/tx/signing/textual" ) -func TestBytesJsonTestCases(t *testing.T) { +func TestBytesJSONTestCases(t *testing.T) { var testcases []bytesTest // Bytes.json contains bytes that are represented in base64 format, and // their expected results in hex. diff --git a/x/tx/signing/textual/coin_test.go b/x/tx/signing/textual/coin_test.go index 492e5d2705..a9ed0295b4 100644 --- a/x/tx/signing/textual/coin_test.go +++ b/x/tx/signing/textual/coin_test.go @@ -71,8 +71,8 @@ func TestMetadataQuerier(t *testing.T) { require.ErrorIs(t, err, expErr) } -func TestCoinJsonTestcases(t *testing.T) { - var testcases []coinJsonTest +func TestCoinJSONTestcases(t *testing.T) { + var testcases []coinJSONTest raw, err := os.ReadFile("./internal/testdata/coin.json") require.NoError(t, err) err = json.Unmarshal(raw, &testcases) @@ -121,7 +121,7 @@ func TestCoinJsonTestcases(t *testing.T) { // an error, otherwise match Text, then Parse() the text and expect it to // match (via proto.Equals()) the original Proto. If the test case has no // Proto, try to Parse() the Text and expect an error if Error is set. -type coinJsonTest struct { +type coinJSONTest struct { Proto *basev1beta1.Coin Metadata *bankv1beta1.Metadata Error bool diff --git a/x/tx/signing/textual/coins_test.go b/x/tx/signing/textual/coins_test.go index 55ff78482c..e7bd921e10 100644 --- a/x/tx/signing/textual/coins_test.go +++ b/x/tx/signing/textual/coins_test.go @@ -15,8 +15,8 @@ import ( "cosmossdk.io/x/tx/signing/textual" ) -func TestCoinsJsonTestcases(t *testing.T) { - var testcases []coinsJsonTest +func TestCoinsJSONTestcases(t *testing.T) { + var testcases []coinsJSONTest raw, err := os.ReadFile("./internal/testdata/coins.json") require.NoError(t, err) err = json.Unmarshal(raw, &testcases) @@ -93,12 +93,12 @@ func checkCoinEqual(t *testing.T, coin, coin1 *basev1beta1.Coin) { require.True(t, v.Equal(v1)) } -// coinsJsonTest is the type of test cases in the testdata file. +// coinsJSONTest is the type of test cases in the testdata file. // If the test case has a Proto, try to Format() it. If Error is set, expect // an error, otherwise match Text, then Parse() the text and expect it to // match (via proto.Equals()) the original Proto. If the test case has no // Proto, try to Parse() the Text and expect an error if Error is set. -type coinsJsonTest struct { +type coinsJSONTest struct { Proto []*basev1beta1.Coin Metadata map[string]*bankv1beta1.Metadata Text string diff --git a/x/tx/signing/textual/dec_test.go b/x/tx/signing/textual/dec_test.go index 1ee1615a7b..defb6d9cc5 100644 --- a/x/tx/signing/textual/dec_test.go +++ b/x/tx/signing/textual/dec_test.go @@ -15,7 +15,7 @@ import ( "cosmossdk.io/x/tx/signing/textual" ) -func TestDecJsonTestcases(t *testing.T) { +func TestDecJSONTestcases(t *testing.T) { type decimalTest []string var testcases []decimalTest raw, err := os.ReadFile("./internal/testdata/decimals.json") diff --git a/x/tx/signing/textual/duration.go b/x/tx/signing/textual/duration.go index 7afe077571..f45ae95d9b 100644 --- a/x/tx/signing/textual/duration.go +++ b/x/tx/signing/textual/duration.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" dpb "google.golang.org/protobuf/types/known/durationpb" ) @@ -23,9 +24,9 @@ func NewDurationValueRenderer() ValueRenderer { } const ( - min_sec = 60 - hour_sec = 60 * min_sec - day_sec = 24 * hour_sec + minSec = 60 + hourSec = 60 * minSec + daySec = 24 * hourSec ) type factors struct { @@ -34,12 +35,12 @@ type factors struct { func factorSeconds(x int64) factors { var f factors - f.days = x / day_sec - x -= f.days * day_sec - f.hours = x / hour_sec - x -= f.hours * hour_sec - f.minutes = x / min_sec - x -= f.minutes * min_sec + f.days = x / daySec + x -= f.days * daySec + f.hours = x / hourSec + x -= f.hours * hourSec + f.minutes = x / minSec + x -= f.minutes * minSec f.seconds = x return f } @@ -84,8 +85,7 @@ func (dr durationValueRenderer) Format(_ context.Context, v protoreflect.Value) if duration.Seconds < 0 || duration.Nanos < 0 { negative = true // copy to avoid side-effecting our input - d := *duration - duration = &d + duration = proto.Clone(duration).(*dpb.Duration) duration.Seconds *= -1 duration.Nanos *= -1 } @@ -172,7 +172,7 @@ func (dr durationValueRenderer) Parse(_ context.Context, screens []Screen) (prot } dur := &dpb.Duration{} - dur.Seconds = days*day_sec + hours*hour_sec + minutes*min_sec + seconds + dur.Seconds = days*daySec + hours*hourSec + minutes*minSec + seconds // #nosec G701 // Since there are 9 digits or fewer, this conversion is safe. dur.Nanos = int32(nanos) diff --git a/x/tx/signing/textual/e2e_test.go b/x/tx/signing/textual/e2e_test.go index e2260b21db..d6e8e26b4d 100644 --- a/x/tx/signing/textual/e2e_test.go +++ b/x/tx/signing/textual/e2e_test.go @@ -22,16 +22,16 @@ import ( "cosmossdk.io/x/tx/signing/textual/internal/textualpb" ) -type e2eJsonTest struct { - txJsonTest +type e2eJSONTest struct { + txJSONTest Cbor string } -func TestE2EJsonTestcases(t *testing.T) { +func TestE2EJSONTestcases(t *testing.T) { raw, err := os.ReadFile("./internal/testdata/e2e.json") require.NoError(t, err) - var testcases []e2eJsonTest + var testcases []e2eJSONTest err = json.Unmarshal(raw, &testcases) require.NoError(t, err) @@ -49,7 +49,7 @@ func TestE2EJsonTestcases(t *testing.T) { AuthInfoBytes: authInfoBz, SignerData: &textualpb.SignerData{ Address: signerData.Address, - ChainId: signerData.ChainId, + ChainId: signerData.ChainID, AccountNumber: signerData.AccountNumber, Sequence: signerData.Sequence, PubKey: signerData.PubKey, diff --git a/x/tx/signing/textual/encode_test.go b/x/tx/signing/textual/encode_test.go index fbfa016379..4221e23084 100644 --- a/x/tx/signing/textual/encode_test.go +++ b/x/tx/signing/textual/encode_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/require" ) -type encodingJsonTest struct { +type encodingJSONTest struct { Screens []Screen Encoding string } @@ -20,7 +20,7 @@ func TestEncodingJson(t *testing.T) { raw, err := os.ReadFile("./internal/testdata/encode.json") require.NoError(t, err) - var testcases []encodingJsonTest + var testcases []encodingJSONTest err = json.Unmarshal(raw, &testcases) require.NoError(t, err) diff --git a/x/tx/signing/textual/enum_test.go b/x/tx/signing/textual/enum_test.go index 21daf7541a..a1ae24854d 100644 --- a/x/tx/signing/textual/enum_test.go +++ b/x/tx/signing/textual/enum_test.go @@ -22,7 +22,7 @@ type enumTest struct { Text string } -func TestEnumJsonTestcases(t *testing.T) { +func TestEnumJSONTestcases(t *testing.T) { var testcases []enumTest raw, err := os.ReadFile("./internal/testdata/enum.json") require.NoError(t, err) @@ -61,11 +61,16 @@ func TestEnumJsonTestcases(t *testing.T) { // treats empty and default values as the same, we actually parse the protojson // encoded string to retrieve which field is set. func getFd(proto json.RawMessage, m *testpb.Baz) protoreflect.FieldDescriptor { - if strings.Contains(string(proto), `"ee"`) { - return m.ProtoReflect().Descriptor().Fields().ByNumber(1) - } else if strings.Contains(string(proto), `"ie"`) { - return m.ProtoReflect().Descriptor().Fields().ByNumber(2) - } else { - return m.ProtoReflect().Descriptor().Fields().ByNumber(3) + var fnum protoreflect.FieldNumber + + switch { + case strings.Contains(string(proto), `"ee"`): + fnum = 1 + case strings.Contains(string(proto), `"ie"`): + fnum = 2 + default: + fnum = 3 } + + return m.ProtoReflect().Descriptor().Fields().ByNumber(fnum) } diff --git a/x/tx/signing/textual/fuzz_test.go b/x/tx/signing/textual/fuzz_test.go index c2e0908a44..81e2fbb867 100644 --- a/x/tx/signing/textual/fuzz_test.go +++ b/x/tx/signing/textual/fuzz_test.go @@ -62,7 +62,7 @@ func FuzzTimestampJSONParseToParseRoundTrip(f *testing.F) { f.Add(seed) f.Fuzz(func(t *testing.T, input []byte) { - var testCases []timestampJsonTest + var testCases []timestampJSONTest if err := json.Unmarshal(input, &testCases); err != nil { return } diff --git a/x/tx/signing/textual/handler.go b/x/tx/signing/textual/handler.go index 45682e2c6d..9d12e96cb1 100644 --- a/x/tx/signing/textual/handler.go +++ b/x/tx/signing/textual/handler.go @@ -163,7 +163,7 @@ func (r *SignModeHandler) GetMessageValueRenderer(md protoreflect.MessageDescrip func (r *SignModeHandler) init() { if r.scalars == nil { r.scalars = map[string]ValueRendererCreator{} - r.scalars["cosmos.Int"] = func(fd protoreflect.FieldDescriptor) ValueRenderer { return NewIntValueRenderer(fd) } + r.scalars["cosmos.Int"] = NewIntValueRenderer r.scalars["cosmos.Dec"] = func(_ protoreflect.FieldDescriptor) ValueRenderer { return NewDecValueRenderer() } } if r.messages == nil { @@ -195,7 +195,7 @@ func (r *SignModeHandler) GetSignBytes(ctx context.Context, signerData signing.S AuthInfoBytes: txData.AuthInfoBytes, SignerData: &textualpb.SignerData{ Address: signerData.Address, - ChainId: signerData.ChainId, + ChainId: signerData.ChainID, AccountNumber: signerData.AccountNumber, Sequence: signerData.Sequence, PubKey: signerData.PubKey, diff --git a/x/tx/signing/textual/int_test.go b/x/tx/signing/textual/int_test.go index 33dbdb4fec..a5e265e3f3 100644 --- a/x/tx/signing/textual/int_test.go +++ b/x/tx/signing/textual/int_test.go @@ -15,7 +15,7 @@ import ( "cosmossdk.io/x/tx/signing/textual" ) -func TestIntJsonTestcases(t *testing.T) { +func TestIntJSONTestcases(t *testing.T) { type integerTest []string var testcases []integerTest raw, err := os.ReadFile("./internal/testdata/integers.json") diff --git a/x/tx/signing/textual/message_test.go b/x/tx/signing/textual/message_test.go index b4d04b27f9..da6f4f99a1 100644 --- a/x/tx/signing/textual/message_test.go +++ b/x/tx/signing/textual/message_test.go @@ -23,20 +23,22 @@ func EmptyCoinMetadataQuerier(ctx context.Context, denom string) (*bankv1beta1.M return nil, nil } -type messageJsonTest struct { +type messageJSONTest struct { Proto *testpb.Foo Screens []textual.Screen } -func TestMessageJsonTestcases(t *testing.T) { +func TestMessageJSONTestcases(t *testing.T) { raw, err := os.ReadFile("./internal/testdata/message.json") require.NoError(t, err) - var testcases []messageJsonTest + var testcases []messageJSONTest err = json.Unmarshal(raw, &testcases) require.NoError(t, err) tr, err := textual.NewSignModeHandler(textual.SignModeOptions{CoinMetadataQuerier: EmptyCoinMetadataQuerier}) + require.NoError(t, err) + for i, tc := range testcases { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { rend := textual.NewMessageValueRenderer(tr, (&testpb.Foo{}).ProtoReflect().Descriptor()) diff --git a/x/tx/signing/textual/repeated_test.go b/x/tx/signing/textual/repeated_test.go index b2a6a98318..3eece902e4 100644 --- a/x/tx/signing/textual/repeated_test.go +++ b/x/tx/signing/textual/repeated_test.go @@ -16,16 +16,16 @@ import ( "cosmossdk.io/x/tx/signing/textual" ) -type repeatedJsonTest struct { +type repeatedJSONTest struct { Proto *testpb.Qux Screens []textual.Screen } -func TestRepeatedJsonTestcases(t *testing.T) { +func TestRepeatedJSONTestcases(t *testing.T) { raw, err := os.ReadFile("./internal/testdata/repeated.json") require.NoError(t, err) - var testcases []repeatedJsonTest + var testcases []repeatedJSONTest err = json.Unmarshal(raw, &testcases) require.NoError(t, err) diff --git a/x/tx/signing/textual/string_test.go b/x/tx/signing/textual/string_test.go index 142fae5c0d..4f215b8d6d 100644 --- a/x/tx/signing/textual/string_test.go +++ b/x/tx/signing/textual/string_test.go @@ -12,15 +12,15 @@ import ( "google.golang.org/protobuf/reflect/protoreflect" ) -type stringJsonTest struct { +type stringJSONTest struct { Text string } -func TestStringJsonTestcases(t *testing.T) { +func TestStringJSONTestcases(t *testing.T) { raw, err := os.ReadFile("./internal/testdata/string.json") require.NoError(t, err) - var testcases []stringJsonTest + var testcases []stringJSONTest err = json.Unmarshal(raw, &testcases) require.NoError(t, err) diff --git a/x/tx/signing/textual/timestamp_test.go b/x/tx/signing/textual/timestamp_test.go index c94448b433..d9ca6bbc08 100644 --- a/x/tx/signing/textual/timestamp_test.go +++ b/x/tx/signing/textual/timestamp_test.go @@ -16,7 +16,7 @@ import ( tspb "google.golang.org/protobuf/types/known/timestamppb" ) -// timestampJsonTest is the type of test cases in the testdata file. +// timestampJSONTest is the type of test cases in the testdata file. // If the test case has a Proto, try to Format() it. If Error is set, expect // an error, otherwise match Text, then Parse() the text and expect it to // match (via proto.Equals()) the original Proto. If the test case has no @@ -24,26 +24,26 @@ import ( // // The Timestamp proto seconds field is int64, but restricted in range // by convention and will fit within a JSON number. -type timestampJsonTest struct { +type timestampJSONTest struct { Proto *tspb.Timestamp Error bool Text string } -func TestTimestampJsonTestcasesTestData(t *testing.T) { +func TestTimestampJSONTestcasesTestData(t *testing.T) { raw, err := os.ReadFile("./internal/testdata/timestamp.json") require.NoError(t, err) - testTimestampJsonTestcases(t, raw) + testTimestampJSONTestcases(t, raw) } // Tests to ensure that we compare standardized forms of the final timestamppb.Timestamp. // Please see issue https://github.com/cosmos/cosmos-sdk/issues/15761 func TestTimestampJsonTestcasesExtraneousNanos(t *testing.T) { - testTimestampJsonTestcases(t, []byte(`[{"proto":{"nAnos":1000000000},"text":"1970-01-01T00:00:01Z"}]`)) + testTimestampJSONTestcases(t, []byte(`[{"proto":{"nAnos":1000000000},"text":"1970-01-01T00:00:01Z"}]`)) } -func testTimestampJsonTestcases(t *testing.T, raw []byte) { - var testcases []timestampJsonTest +func testTimestampJSONTestcases(t *testing.T, raw []byte) { + var testcases []timestampJSONTest err := json.Unmarshal(raw, &testcases) require.NoError(t, err) diff --git a/x/tx/signing/textual/tx_test.go b/x/tx/signing/textual/tx_test.go index d990366087..1b63b8c8c9 100644 --- a/x/tx/signing/textual/tx_test.go +++ b/x/tx/signing/textual/tx_test.go @@ -26,29 +26,29 @@ import ( "cosmossdk.io/x/tx/signing/textual/internal/textualpb" ) -// txJsonTestTx represents the type that in the JSON test +// txJSONTestTx represents the type that in the JSON test // cases `proto` field. The inner contents are protojson // encoded, so we represent them as []byte here, and decode // them inside the test. -type txJsonTestTx struct { +type txJSONTestTx struct { Body json.RawMessage AuthInfo json.RawMessage `json:"auth_info"` } -type txJsonTest struct { +type txJSONTest struct { Name string - Proto txJsonTestTx + Proto txJSONTestTx SignerData json.RawMessage `json:"signer_data"` Metadata *bankv1beta1.Metadata Error bool Screens []textual.Screen } -func TestTxJsonTestcases(t *testing.T) { +func TestTxJSONTestcases(t *testing.T) { raw, err := os.ReadFile("./internal/testdata/tx.json") require.NoError(t, err) - var testcases []txJsonTest + var testcases []txJSONTest err = json.Unmarshal(raw, &testcases) require.NoError(t, err) @@ -57,6 +57,8 @@ func TestTxJsonTestcases(t *testing.T) { txBody, bodyBz, txAuthInfo, authInfoBz, signerData := createTextualData(t, tc.Proto, tc.SignerData) tr, err := textual.NewSignModeHandler(textual.SignModeOptions{CoinMetadataQuerier: mockCoinMetadataQuerier}) + require.NoError(t, err) + rend := textual.NewTxValueRenderer(tr) ctx := addMetadataToContext(context.Background(), tc.Metadata) @@ -65,7 +67,7 @@ func TestTxJsonTestcases(t *testing.T) { AuthInfoBytes: authInfoBz, SignerData: &textualpb.SignerData{ Address: signerData.Address, - ChainId: signerData.ChainId, + ChainId: signerData.ChainID, AccountNumber: signerData.AccountNumber, Sequence: signerData.Sequence, PubKey: signerData.PubKey, @@ -115,7 +117,7 @@ func TestTxJsonTestcases(t *testing.T) { // createTextualData creates a Textual data give then JSON // test case. -func createTextualData(t *testing.T, jsonTx txJsonTestTx, jsonSignerData json.RawMessage) (*txv1beta1.TxBody, []byte, *txv1beta1.AuthInfo, []byte, signing.SignerData) { +func createTextualData(t *testing.T, jsonTx txJSONTestTx, jsonSignerData json.RawMessage) (*txv1beta1.TxBody, []byte, *txv1beta1.AuthInfo, []byte, signing.SignerData) { body := &txv1beta1.TxBody{} authInfo := &txv1beta1.AuthInfo{} protoSignerData := &textualpb.SignerData{} @@ -142,7 +144,7 @@ func createTextualData(t *testing.T, jsonTx txJsonTestTx, jsonSignerData json.Ra func signerDataFromProto(d *textualpb.SignerData) signing.SignerData { return signing.SignerData{ Address: d.Address, - ChainId: d.ChainId, + ChainID: d.ChainId, AccountNumber: d.AccountNumber, Sequence: d.Sequence, PubKey: d.PubKey,