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

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"math/big"
     6  )
     7  
     8  // Transactions messages must fulfill the Msg
     9  type Msg interface {
    10  
    11  	// Return the message type.
    12  	// Must be alphanumeric or empty.
    13  	Route() string
    14  
    15  	// Returns a human-readable string for the message, intended for utilization
    16  	// within tags
    17  	Type() string
    18  
    19  	// ValidateBasic does a simple validation check that
    20  	// doesn't require access to any other information.
    21  	ValidateBasic() error
    22  
    23  	// Get the canonical byte representation of the Msg.
    24  	GetSignBytes() []byte
    25  
    26  	// Signers returns the addrs of signers that must sign.
    27  	// CONTRACT: All signatures must be present to be valid.
    28  	// CONTRACT: Returns addrs in some deterministic order.
    29  	GetSigners() []AccAddress
    30  }
    31  
    32  //__________________________________________________________
    33  
    34  // Transactions objects must fulfill the Tx
    35  type Tx interface {
    36  	// Gets the all the transaction's messages.
    37  	GetMsgs() []Msg
    38  
    39  	// ValidateBasic does a simple and lightweight validation check that doesn't
    40  	// require access to any other information.
    41  	ValidateBasic() error
    42  
    43  	// Return tx gas price
    44  	GetGasPrice() *big.Int
    45  
    46  	// Return tx call function signature
    47  	GetTxFnSignatureInfo() ([]byte, int)
    48  
    49  	GetType() TransactionType
    50  
    51  	GetSigners() []AccAddress
    52  
    53  	GetGas() uint64
    54  
    55  	GetRaw() []byte
    56  	GetFrom() string
    57  	GetSender(ctx Context) string
    58  	GetNonce() uint64
    59  	TxHash() []byte
    60  	SetRaw([]byte)
    61  	SetTxHash([]byte)
    62  }
    63  
    64  type HeightSensitive interface {
    65  	ValidWithHeight(h int64) error
    66  }
    67  
    68  type TxAdapter interface {
    69  	Tx
    70  	HeightSensitive
    71  }
    72  
    73  type BaseTx struct {
    74  	Raw   []byte
    75  	Hash  []byte
    76  	From  string
    77  	Nonce uint64
    78  }
    79  
    80  func (tx *BaseTx) GetMsgs() []Msg                      { return nil }
    81  func (tx *BaseTx) ValidateBasic() error                { return nil }
    82  func (tx *BaseTx) GetGasPrice() *big.Int               { return big.NewInt(0) }
    83  func (tx *BaseTx) GetTxFnSignatureInfo() ([]byte, int) { return nil, 0 }
    84  func (tx *BaseTx) GetType() TransactionType            { return UnknownType }
    85  func (tx *BaseTx) GetSigners() []AccAddress            { return nil }
    86  func (tx *BaseTx) GetGas() uint64                      { return 0 }
    87  func (tx *BaseTx) GetNonce() uint64                    { return tx.Nonce }
    88  func (tx *BaseTx) GetFrom() string                     { return tx.From }
    89  func (tx *BaseTx) GetRaw() []byte                      { return tx.Raw }
    90  func (tx *BaseTx) TxHash() []byte                      { return tx.Hash }
    91  func (tx *BaseTx) SetRaw(raw []byte)                   { tx.Raw = raw }
    92  func (tx *BaseTx) SetTxHash(hash []byte)               { tx.Hash = hash }
    93  func (tx *BaseTx) GetSender(_ Context) string          { return tx.From }
    94  
    95  //__________________________________________________________
    96  
    97  type TransactionType int
    98  
    99  const (
   100  	UnknownType TransactionType = iota
   101  	StdTxType
   102  	EvmTxType
   103  )
   104  
   105  func (t TransactionType) String() (res string) {
   106  	switch t {
   107  	case StdTxType:
   108  		res = "StdTx"
   109  	case EvmTxType:
   110  		res = "EvmTx"
   111  	default:
   112  		res = "Unknown"
   113  	}
   114  	return res
   115  }
   116  
   117  // __________________________________________________________
   118  // TxDecoder unmarshals transaction bytes
   119  type TxDecoder func(txBytes []byte, height ...int64) (Tx, error)
   120  
   121  // TxEncoder marshals transaction to bytes
   122  type TxEncoder func(tx Tx) ([]byte, error)
   123  
   124  //__________________________________________________________
   125  
   126  var _ Msg = (*TestMsg)(nil)
   127  
   128  // TestMsg is msg type for testing
   129  type TestMsg struct {
   130  	signers []AccAddress
   131  }
   132  
   133  func NewTestMsg(addrs ...AccAddress) *TestMsg {
   134  	return &TestMsg{
   135  		signers: addrs,
   136  	}
   137  }
   138  
   139  // nolint
   140  func (msg *TestMsg) Route() string { return "TestMsg" }
   141  func (msg *TestMsg) Type() string  { return "Test message" }
   142  func (msg *TestMsg) GetSignBytes() []byte {
   143  	bz, err := json.Marshal(msg.signers)
   144  	if err != nil {
   145  		panic(err)
   146  	}
   147  	return MustSortJSON(bz)
   148  }
   149  func (msg *TestMsg) ValidateBasic() error { return nil }
   150  func (msg *TestMsg) GetSigners() []AccAddress {
   151  	return msg.signers
   152  }
   153  
   154  type TestMsg2 struct {
   155  	Signers []AccAddress
   156  }
   157  
   158  func NewTestMsg2(addrs ...AccAddress) *TestMsg2 {
   159  	return &TestMsg2{
   160  		Signers: addrs,
   161  	}
   162  }
   163  
   164  // nolint
   165  func (msg TestMsg2) Route() string { return "TestMsg" }
   166  func (msg TestMsg2) Type() string  { return "Test message" }
   167  func (msg TestMsg2) GetSignBytes() []byte {
   168  	bz, err := json.Marshal(msg.Signers)
   169  	if err != nil {
   170  		panic(err)
   171  	}
   172  	return MustSortJSON(bz)
   173  }
   174  func (msg TestMsg2) ValidateBasic() error { return nil }
   175  func (msg TestMsg2) GetSigners() []AccAddress {
   176  	return msg.Signers
   177  }