Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed May 31, 2024
1 parent 3919d0e commit 86ba8b6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 101 deletions.
8 changes: 4 additions & 4 deletions x/auth/tx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func newBuilder(addressCodec address.Codec, decoder *decode.Decoder, codec codec
}

func newBuilderFromDecodedTx(addrCodec address.Codec, decoder *decode.Decoder, codec codec.BinaryCodec, decoded *gogoTxWrapper) (*builder, error) {
signatures := make([][]byte, len(decoded.decodedTx.Tx.Signatures))
copy(signatures, decoded.decodedTx.Tx.Signatures)
signatures := make([][]byte, len(decoded.Tx.Signatures))
copy(signatures, decoded.Tx.Signatures)

sigInfos := make([]*tx.SignerInfo, len(decoded.decodedTx.Tx.AuthInfo.SignerInfos))
for i, sigInfo := range decoded.decodedTx.Tx.AuthInfo.SignerInfos {
sigInfos := make([]*tx.SignerInfo, len(decoded.Tx.AuthInfo.SignerInfos))
for i, sigInfo := range decoded.Tx.AuthInfo.SignerInfos {
modeInfoV1 := new(tx.ModeInfo)
fromV2ModeInfo(sigInfo.ModeInfo, modeInfoV1)
sigInfos[i] = &tx.SignerInfo{
Expand Down
4 changes: 2 additions & 2 deletions x/auth/tx/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func DefaultTxEncoder() sdk.TxEncoder {
if !ok {
return nil, fmt.Errorf("unexpected tx type: %T", tx)
}
return marshalOption.Marshal(gogoWrapper.decodedTx.TxRaw)
return marshalOption.Marshal(gogoWrapper.TxRaw)
}
}

Expand All @@ -32,6 +32,6 @@ func DefaultJSONTxEncoder(cdc codec.Codec) sdk.TxEncoder {
if !ok {
return nil, fmt.Errorf("unexpected tx type: %T", tx)
}
return jsonMarshaler.Marshal(gogoWrapper.decodedTx.Tx)
return jsonMarshaler.Marshal(gogoWrapper.Tx)
}
}
120 changes: 30 additions & 90 deletions x/auth/tx/gogotx.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package tx

import (
"crypto/sha256"
"errors"
"fmt"
"reflect"
"strings"

"github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/protoadapt"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/anypb"

"cosmossdk.io/core/address"
"cosmossdk.io/core/transaction"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
"cosmossdk.io/x/auth/ante"
Expand All @@ -31,7 +27,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)

func newWrapperFromDecodedTx(addrCodec address.Codec, cdc codec.BinaryCodec, decodedTx *decode.DecodedTx) (w *gogoTxWrapper, err error) {
func newWrapperFromDecodedTx(addrCodec address.Codec, cdc codec.BinaryCodec, decodedTx *decode.DecodedTx) (*gogoTxWrapper, error) {
// set msgsv1
msgs, err := decodeMsgsV1(cdc, decodedTx.Tx.Body.Messages)
if err != nil {
Expand Down Expand Up @@ -83,8 +79,8 @@ func newWrapperFromDecodedTx(addrCodec address.Codec, cdc codec.BinaryCodec, dec
}

return &gogoTxWrapper{
DecodedTx: decodedTx,
cdc: cdc,
decodedTx: decodedTx,
reflectMsgs: reflectMsgs,
msgs: msgs,
fees: fees,
Expand All @@ -96,74 +92,18 @@ func newWrapperFromDecodedTx(addrCodec address.Codec, cdc codec.BinaryCodec, dec
// gogoTxWrapper is a gogoTxWrapper around the tx.Tx proto.Message which retain the raw
// body and auth_info bytes.
type gogoTxWrapper struct {
decodedTx *decode.DecodedTx
cdc codec.BinaryCodec
*decode.DecodedTx

cdc codec.BinaryCodec

msgs []proto.Message
reflectMsgs []protoreflect.Message
fees sdk.Coins
feePayer []byte
feeGranter []byte

// Cache for hash and full bytes
cachedHash [32]byte
cachedBytes []byte
cachedHashed bool
}

func (w *gogoTxWrapper) String() string { return w.decodedTx.Tx.String() }

func (w *gogoTxWrapper) Bytes() []byte {
if !w.cachedHashed {
w.computeHashAndBytes()
}
return w.cachedBytes
}

func (w *gogoTxWrapper) Hash() [32]byte {
if !w.cachedHashed {
w.computeHashAndBytes()
}
return w.cachedHash
}

func (w *gogoTxWrapper) computeHashAndBytes() {
bz, err := proto.Marshal(w.decodedTx.TxRaw)
if err != nil {
panic(err)
}

w.cachedBytes = bz
w.cachedHash = sha256.Sum256(bz)
w.cachedHashed = true
}

func (w *gogoTxWrapper) GetGasLimit() (uint64, error) {
if w.decodedTx == nil || w.decodedTx.Tx == nil || w.decodedTx.Tx.AuthInfo == nil || w.decodedTx.Tx.AuthInfo.Fee == nil {
return 0, errors.New("gas limit not available, one or more required fields are nil")
}
return w.decodedTx.Tx.AuthInfo.Fee.GasLimit, nil
}

func (w *gogoTxWrapper) GetMessages() ([]transaction.Msg, error) {
if w.decodedTx == nil || w.decodedTx.Messages == nil {
return nil, errors.New("messages not available or are nil")
}

msgs := make([]transaction.Msg, len(w.decodedTx.Messages))
for i, msg := range w.decodedTx.Messages {
msgs[i] = protoadapt.MessageV1Of(msg)
}

return msgs, nil
}

func (w *gogoTxWrapper) GetSenders() ([][]byte, error) {
if w.decodedTx == nil || w.decodedTx.Signers == nil {
return nil, errors.New("senders not available or are nil")
}
return w.decodedTx.Signers, nil
}
func (w *gogoTxWrapper) String() string { return w.Tx.String() }

var (
_ authsigning.Tx = &gogoTxWrapper{}

Check failure on line 109 in x/auth/tx/gogotx.go

View workflow job for this annotation

GitHub Actions / dependency-review

cannot use &gogoTxWrapper{} (value of type *gogoTxWrapper) as "cosmossdk.io/x/auth/signing".Tx value in variable declaration: *gogoTxWrapper does not implement "cosmossdk.io/x/auth/signing".Tx (missing method Bytes)

Check failure on line 109 in x/auth/tx/gogotx.go

View workflow job for this annotation

GitHub Actions / tests (03)

cannot use &gogoTxWrapper{} (value of type *gogoTxWrapper) as "cosmossdk.io/x/auth/signing".Tx value in variable declaration: *gogoTxWrapper does not implement "cosmossdk.io/x/auth/signing".Tx (missing method Bytes)

Check failure on line 109 in x/auth/tx/gogotx.go

View workflow job for this annotation

GitHub Actions / tests (03)

cannot use &gogoTxWrapper{} (value of type *gogoTxWrapper) as "cosmossdk.io/x/auth/signing".Tx value in variable declaration: *gogoTxWrapper does not implement "cosmossdk.io/x/auth/signing".Tx (missing method Bytes)

Check failure on line 109 in x/auth/tx/gogotx.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use &gogoTxWrapper{} (value of type *gogoTxWrapper) as "cosmossdk.io/x/auth/signing".Tx value in variable declaration: *gogoTxWrapper does not implement "cosmossdk.io/x/auth/signing".Tx (missing method Bytes)

Check failure on line 109 in x/auth/tx/gogotx.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use &gogoTxWrapper{} (value of type *gogoTxWrapper) as "cosmossdk.io/x/auth/signing".Tx value in variable declaration: *gogoTxWrapper does not implement "cosmossdk.io/x/auth/signing".Tx (missing method Bytes)

Check failure on line 109 in x/auth/tx/gogotx.go

View workflow job for this annotation

GitHub Actions / tests (00)

cannot use &gogoTxWrapper{} (value of type *gogoTxWrapper) as "cosmossdk.io/x/auth/signing".Tx value in variable declaration: *gogoTxWrapper does not implement "cosmossdk.io/x/auth/signing".Tx (missing method Bytes)

Check failure on line 109 in x/auth/tx/gogotx.go

View workflow job for this annotation

GitHub Actions / tests (00)

cannot use &gogoTxWrapper{} (value of type *gogoTxWrapper) as "cosmossdk.io/x/auth/signing".Tx value in variable declaration: *gogoTxWrapper does not implement "cosmossdk.io/x/auth/signing".Tx (missing method Bytes)

Check failure on line 109 in x/auth/tx/gogotx.go

View workflow job for this annotation

GitHub Actions / tests (02)

cannot use &gogoTxWrapper{} (value of type *gogoTxWrapper) as "cosmossdk.io/x/auth/signing".Tx value in variable declaration: *gogoTxWrapper does not implement "cosmossdk.io/x/auth/signing".Tx (missing method Bytes)

Check failure on line 109 in x/auth/tx/gogotx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use &gogoTxWrapper{} (value of type *gogoTxWrapper) as "cosmossdk.io/x/auth/signing".Tx value in variable declaration: *gogoTxWrapper does not implement "cosmossdk.io/x/auth/signing".Tx (missing method Bytes)
Expand All @@ -190,21 +130,21 @@ func (w *gogoTxWrapper) GetReflectMessages() ([]protoreflect.Message, error) {
}

func (w *gogoTxWrapper) ValidateBasic() error {
if len(w.decodedTx.Tx.Signatures) == 0 {
if len(w.Tx.Signatures) == 0 {
return sdkerrors.ErrNoSignatures.Wrapf("empty signatures")
}
if len(w.decodedTx.Signers) != len(w.decodedTx.Tx.Signatures) {
return sdkerrors.ErrUnauthorized.Wrapf("invalid number of signatures: got %d signatures and %d signers", len(w.decodedTx.Tx.Signatures), len(w.decodedTx.Signers))
if len(w.Signers) != len(w.Tx.Signatures) {
return sdkerrors.ErrUnauthorized.Wrapf("invalid number of signatures: got %d signatures and %d signers", len(w.Tx.Signatures), len(w.Signers))
}
return nil
}

func (w *gogoTxWrapper) GetSigners() ([][]byte, error) {
return w.decodedTx.Signers, nil
return w.Signers, nil
}

func (w *gogoTxWrapper) GetPubKeys() ([]cryptotypes.PubKey, error) {
signerInfos := w.decodedTx.Tx.AuthInfo.SignerInfos
signerInfos := w.Tx.AuthInfo.SignerInfos
pks := make([]cryptotypes.PubKey, len(signerInfos))

for i, si := range signerInfos {
Expand All @@ -229,7 +169,7 @@ func (w *gogoTxWrapper) GetPubKeys() ([]cryptotypes.PubKey, error) {
}

func (w *gogoTxWrapper) GetGas() uint64 {
return w.decodedTx.Tx.AuthInfo.Fee.GasLimit
return w.Tx.AuthInfo.Fee.GasLimit
}

func (w *gogoTxWrapper) GetFee() sdk.Coins { return w.fees }
Expand All @@ -238,18 +178,18 @@ func (w *gogoTxWrapper) FeePayer() []byte { return w.feePayer }

func (w *gogoTxWrapper) FeeGranter() []byte { return w.feeGranter }

func (w *gogoTxWrapper) GetMemo() string { return w.decodedTx.Tx.Body.Memo }
func (w *gogoTxWrapper) GetMemo() string { return w.Tx.Body.Memo }

// GetTimeoutHeight returns the transaction's timeout height (if set).
func (w *gogoTxWrapper) GetTimeoutHeight() uint64 { return w.decodedTx.Tx.Body.TimeoutHeight }
func (w *gogoTxWrapper) GetTimeoutHeight() uint64 { return w.Tx.Body.TimeoutHeight }

// GetUnordered returns the transaction's unordered field (if set).
func (w *gogoTxWrapper) GetUnordered() bool { return w.decodedTx.Tx.Body.Unordered }
func (w *gogoTxWrapper) GetUnordered() bool { return w.Tx.Body.Unordered }

// GetSignaturesV2 returns the signatures of the Tx.
func (w *gogoTxWrapper) GetSignaturesV2() ([]signing.SignatureV2, error) {
signerInfos := w.decodedTx.Tx.AuthInfo.SignerInfos
sigs := w.decodedTx.Tx.Signatures
signerInfos := w.Tx.AuthInfo.SignerInfos
sigs := w.Tx.Signatures
pubKeys, err := w.GetPubKeys()
if err != nil {
return nil, err
Expand Down Expand Up @@ -286,46 +226,46 @@ func (w *gogoTxWrapper) GetSignaturesV2() ([]signing.SignatureV2, error) {
// TODO: evaluate if this is even needed considering we have decoded tx.
func (w *gogoTxWrapper) GetSigningTxData() txsigning.TxData {
return txsigning.TxData{
Body: w.decodedTx.Tx.Body,
AuthInfo: w.decodedTx.Tx.AuthInfo,
BodyBytes: w.decodedTx.TxRaw.BodyBytes,
AuthInfoBytes: w.decodedTx.TxRaw.AuthInfoBytes,
BodyHasUnknownNonCriticals: w.decodedTx.TxBodyHasUnknownNonCriticals,
Body: w.Tx.Body,
AuthInfo: w.Tx.AuthInfo,
BodyBytes: w.TxRaw.BodyBytes,
AuthInfoBytes: w.TxRaw.AuthInfoBytes,
BodyHasUnknownNonCriticals: w.TxBodyHasUnknownNonCriticals,
}
}

func (w *gogoTxWrapper) GetExtensionOptions() []*codectypes.Any {
return intoAnyV1(w.decodedTx.Tx.Body.ExtensionOptions)
return intoAnyV1(w.Tx.Body.ExtensionOptions)
}

func (w *gogoTxWrapper) GetNonCriticalExtensionOptions() []*codectypes.Any {
return intoAnyV1(w.decodedTx.Tx.Body.NonCriticalExtensionOptions)
return intoAnyV1(w.Tx.Body.NonCriticalExtensionOptions)
}

func (w *gogoTxWrapper) AsTx() (*txtypes.Tx, error) {
body := new(txtypes.TxBody)
authInfo := new(txtypes.AuthInfo)

err := w.cdc.Unmarshal(w.decodedTx.TxRaw.BodyBytes, body)
err := w.cdc.Unmarshal(w.TxRaw.BodyBytes, body)
if err != nil {
return nil, err
}
err = w.cdc.Unmarshal(w.decodedTx.TxRaw.AuthInfoBytes, authInfo)
err = w.cdc.Unmarshal(w.TxRaw.AuthInfoBytes, authInfo)
if err != nil {
return nil, err
}
return &txtypes.Tx{
Body: body,
AuthInfo: authInfo,
Signatures: w.decodedTx.TxRaw.Signatures,
Signatures: w.TxRaw.Signatures,
}, nil
}

func (w *gogoTxWrapper) AsTxRaw() (*txtypes.TxRaw, error) {
return &txtypes.TxRaw{
BodyBytes: w.decodedTx.TxRaw.BodyBytes,
AuthInfoBytes: w.decodedTx.TxRaw.AuthInfoBytes,
Signatures: w.decodedTx.TxRaw.Signatures,
BodyBytes: w.TxRaw.BodyBytes,
AuthInfoBytes: w.TxRaw.AuthInfoBytes,
Signatures: w.TxRaw.Signatures,
}, nil
}

Expand Down
1 change: 0 additions & 1 deletion x/auth/tx/gogotx_test.go

This file was deleted.

8 changes: 4 additions & 4 deletions x/auth/tx/legacy_amino_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func (s signModeLegacyAminoJSONHandler) GetSignBytes(mode signingtypes.SignMode,
return nil, fmt.Errorf("can only handle a protobuf Tx, got %T", tx)
}

if protoTx.decodedTx.TxBodyHasUnknownNonCriticals {
if protoTx.TxBodyHasUnknownNonCriticals {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, aminoNonCriticalFieldsError)
}

body := protoTx.decodedTx.Tx.Body
body := protoTx.Tx.Body

if len(body.ExtensionOptions) != 0 || len(body.NonCriticalExtensionOptions) != 0 {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "%s does not support protobuf extension options", signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON)
Expand All @@ -68,8 +68,8 @@ func (s signModeLegacyAminoJSONHandler) GetSignBytes(mode signingtypes.SignMode,
legacytx.StdFee{
Amount: protoTx.GetFee(),
Gas: protoTx.GetGas(),
Payer: protoTx.decodedTx.Tx.AuthInfo.Fee.Payer,
Granter: protoTx.decodedTx.Tx.AuthInfo.Fee.Granter,
Payer: protoTx.Tx.AuthInfo.Fee.Payer,
Granter: protoTx.Tx.AuthInfo.Fee.Granter,
},
tx.GetMsgs(), protoTx.GetMemo(),
), nil
Expand Down

0 comments on commit 86ba8b6

Please sign in to comment.