github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/types/tx/types.go (about)

     1  package tx
     2  
     3  import (
     4  	"fmt"
     5  
     6  	ibcmsg "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/ibc-adapter"
     7  
     8  	codectypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types"
     9  	cryptotypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/crypto/types"
    10  	ibckeys "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/crypto/types"
    11  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    12  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
    13  )
    14  
    15  // MaxGasWanted defines the max gas allowed.
    16  const MaxGasWanted = uint64((1 << 63) - 1)
    17  
    18  // Interface implementation checks.
    19  var _, _, _, _ codectypes.UnpackInterfacesMessage = &Tx{}, &TxBody{}, &AuthInfo{}, &SignerInfo{}
    20  
    21  // var _ sdk.Tx = &Tx{}
    22  var _ *Tx = &Tx{}
    23  
    24  // GetMsgs implements the GetMsgs method on sdk.Tx.
    25  func (t *Tx) GetMsgs() []ibcmsg.Msg {
    26  	if t == nil || t.Body == nil {
    27  		return nil
    28  	}
    29  
    30  	anys := t.Body.Messages
    31  	res := make([]ibcmsg.Msg, len(anys))
    32  	for i, any := range anys {
    33  		cached := any.GetCachedValue()
    34  		if cached == nil {
    35  			panic("Any cached value is nil. Transaction messages must be correctly packed Any values.")
    36  		}
    37  		res[i] = cached.(ibcmsg.Msg)
    38  	}
    39  	return res
    40  }
    41  
    42  // ValidateBasic implements the ValidateBasic method on sdk.Tx.
    43  func (t *Tx) ValidateBasic() error {
    44  	if t == nil {
    45  		return fmt.Errorf("bad Tx")
    46  	}
    47  
    48  	body := t.Body
    49  	if body == nil {
    50  		return fmt.Errorf("missing TxBody")
    51  	}
    52  
    53  	authInfo := t.AuthInfo
    54  	if authInfo == nil {
    55  		return fmt.Errorf("missing AuthInfo")
    56  	}
    57  
    58  	fee := authInfo.Fee
    59  	if fee == nil {
    60  		return fmt.Errorf("missing fee")
    61  	}
    62  
    63  	if fee.GasLimit > MaxGasWanted {
    64  		return sdkerrors.Wrapf(
    65  			sdkerrors.ErrInvalidRequest,
    66  			"invalid gas supplied; %d > %d", fee.GasLimit, MaxGasWanted,
    67  		)
    68  	}
    69  
    70  	if fee.Amount.IsAnyNil() {
    71  		return sdkerrors.Wrapf(
    72  			sdkerrors.ErrInsufficientFee,
    73  			"invalid fee provided: null",
    74  		)
    75  	}
    76  
    77  	if fee.Amount.IsAnyNegative() {
    78  		return sdkerrors.Wrapf(
    79  			sdkerrors.ErrInsufficientFee,
    80  			"invalid fee provided: %s", fee.Amount,
    81  		)
    82  	}
    83  
    84  	if fee.Payer != "" {
    85  		_, err := sdk.AccAddressFromBech32(fee.Payer)
    86  		if err != nil {
    87  			return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid fee payer address (%s)", err)
    88  		}
    89  	}
    90  
    91  	sigs := t.Signatures
    92  
    93  	if len(sigs) == 0 {
    94  		return sdkerrors.ErrNoSignatures
    95  	}
    96  
    97  	if len(sigs) != len(t.GetSigners()) {
    98  		return sdkerrors.Wrapf(
    99  			sdkerrors.ErrUnauthorized,
   100  			"wrong number of signers; expected %d, got %d", len(t.GetSigners()), len(sigs),
   101  		)
   102  	}
   103  
   104  	return nil
   105  }
   106  
   107  // GetSigners retrieves all the signers of a tx.
   108  // This includes all unique signers of the messages (in order),
   109  // as well as the FeePayer (if specified and not already included).
   110  func (t *Tx) GetSigners() []sdk.AccAddress {
   111  	var signers []sdk.AccAddress
   112  	seen := map[string]bool{}
   113  
   114  	for _, msg := range t.GetMsgs() {
   115  		for _, addr := range msg.GetSigners() {
   116  			if !seen[addr.String()] {
   117  				signers = append(signers, addr)
   118  				seen[addr.String()] = true
   119  			}
   120  		}
   121  	}
   122  
   123  	// ensure any specified fee payer is included in the required signers (at the end)
   124  	feePayer := t.AuthInfo.Fee.Payer
   125  	if feePayer != "" && !seen[feePayer] {
   126  		payerAddr, err := sdk.AccAddressFromBech32(feePayer)
   127  		if err != nil {
   128  			panic(err)
   129  		}
   130  		signers = append(signers, payerAddr)
   131  		seen[feePayer] = true
   132  	}
   133  
   134  	return signers
   135  }
   136  
   137  func (t *Tx) GetGas() uint64 {
   138  	return t.AuthInfo.Fee.GasLimit
   139  }
   140  
   141  // func (t *Tx) GetFee() sdk.Coins {
   142  func (t *Tx) GetFee() sdk.CoinAdapters {
   143  	return t.AuthInfo.Fee.Amount
   144  }
   145  func (t *Tx) FeePayer() sdk.AccAddress {
   146  	feePayer := t.AuthInfo.Fee.Payer
   147  	if feePayer != "" {
   148  		payerAddr, err := sdk.AccAddressFromBech32(feePayer)
   149  		if err != nil {
   150  			panic(err)
   151  		}
   152  		return payerAddr
   153  	}
   154  	// use first signer as default if no payer specified
   155  	return t.GetSigners()[0]
   156  }
   157  
   158  func (t *Tx) FeeGranter() sdk.AccAddress {
   159  	feePayer := t.AuthInfo.Fee.Granter
   160  	if feePayer != "" {
   161  		granterAddr, err := sdk.AccAddressFromBech32(feePayer)
   162  		if err != nil {
   163  			panic(err)
   164  		}
   165  		return granterAddr
   166  	}
   167  	return nil
   168  }
   169  
   170  // UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
   171  func (t *Tx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
   172  	if t.Body != nil {
   173  		if err := t.Body.UnpackInterfaces(unpacker); err != nil {
   174  			return err
   175  		}
   176  	}
   177  
   178  	if t.AuthInfo != nil {
   179  		return t.AuthInfo.UnpackInterfaces(unpacker)
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  // UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
   186  func (m *TxBody) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
   187  	for _, any := range m.Messages {
   188  		var msg ibcmsg.Msg
   189  		err := unpacker.UnpackAny(any, &msg)
   190  		if err != nil {
   191  			return err
   192  		}
   193  	}
   194  
   195  	return nil
   196  }
   197  
   198  // UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
   199  func (m *AuthInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
   200  	for _, signerInfo := range m.SignerInfos {
   201  		err := signerInfo.UnpackInterfaces(unpacker)
   202  		if err != nil {
   203  			return err
   204  		}
   205  	}
   206  	return nil
   207  }
   208  
   209  // UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
   210  func (m *SignerInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
   211  	return unpacker.UnpackAny(m.PublicKey, new(cryptotypes.PubKey))
   212  }
   213  
   214  // RegisterInterfaces registers the sdk.Tx interface.
   215  func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
   216  	registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil))
   217  	//registry.RegisterInterface("cosmos.tx.v1beta1.Coin", (*sdk.Coin)(nil))
   218  	registry.RegisterInterface("cosmos.crypto.secp256k1.PubKey", (*ibckeys.PubKey)(nil))
   219  	registry.RegisterImplementations((*sdk.Tx)(nil), &Tx{})
   220  }