cosmos-sdk/types/tx_msg.go

136 lines
3.6 KiB
Go
Raw Normal View History

package types
2018-03-04 00:15:26 -08:00
import "encoding/json"
2018-02-04 16:59:11 -08:00
// Transactions messages must fulfill the Msg
type Msg interface {
// Return the message type.
// Must be alphanumeric or empty.
Type() string
// Get some property of the Msg.
Get(key interface{}) (value interface{})
// Get the canonical byte representation of the Msg.
2018-01-10 20:11:44 -08:00
GetSignBytes() []byte
// ValidateBasic does a simple validation check that
// doesn't require access to any other information.
2018-01-26 04:19:33 -08:00
ValidateBasic() Error
// Signers returns the addrs of signers that must sign.
// CONTRACT: All signatures must be present to be valid.
// CONTRACT: Returns addrs in some deterministic order.
2018-03-01 23:49:07 -08:00
GetSigners() []Address
}
2018-03-02 09:21:49 -08:00
//__________________________________________________________
2018-02-04 16:59:11 -08:00
// Transactions objects must fulfill the Tx
type Tx interface {
2018-01-26 06:22:56 -08:00
// Gets the Msg.
GetMsg() Msg
// Signatures returns the signature of signers who signed the Msg.
// CONTRACT: Length returned is same as length of
// pubkeys returned from MsgKeySigners, and the order
// matches.
// CONTRACT: If the signature is missing (ie the Msg is
// invalid), then the corresponding signature is
// .Empty().
GetSignatures() []StdSignature
}
2018-01-14 19:49:57 -08:00
var _ Tx = (*StdTx)(nil)
2018-02-17 13:19:34 -08:00
// StdTx is a standard way to wrap a Msg with Signatures.
// NOTE: the first signature is the FeePayer (Signatures must not be nil).
type StdTx struct {
Msg `json:"msg"`
2018-03-13 05:50:21 -07:00
Fee StdFee `json:"fee"`
Signatures []StdSignature `json:"signatures"`
}
2018-02-17 13:19:34 -08:00
func NewStdTx(msg Msg, sigs []StdSignature) StdTx {
return StdTx{
Msg: msg,
Signatures: sigs,
}
}
2018-03-13 05:50:21 -07:00
// SetFee sets the StdFee on the transaction.
func (tx StdTx) SetFee(fee StdFee) StdTx {
tx.Fee = fee
return tx
}
//nolint
func (tx StdTx) GetMsg() Msg { return tx.Msg }
func (tx StdTx) GetSignatures() []StdSignature { return tx.Signatures }
2018-03-13 05:46:08 -07:00
// FeePayer returns the address responsible for paying the fees
// for the transactions. It's the first address returned by msg.GetSigners().
// If GetSigners() is empty, this panics.
func FeePayer(tx Tx) Address {
return tx.GetMsg().GetSigners()[0]
}
2018-03-13 05:50:21 -07:00
// StdFee includes the amount of coins paid in fees and the maximum
2018-03-14 10:16:52 -07:00
// gas to be used by the transaction. The ratio yields an effective "gasprice",
2018-03-13 05:50:21 -07:00
// which must be above some miminum to be accepted into the mempool.
type StdFee struct {
Amount Coins `json"amount"`
Gas int64 `json"gas"`
}
func NewStdFee(gas int64, amount ...Coin) StdFee {
return StdFee{
Amount: amount,
Gas: gas,
}
}
2018-01-14 19:49:57 -08:00
2018-03-03 23:36:10 -08:00
// StdSignDoc is replay-prevention structure.
// It includes the result of msg.GetSignBytes(),
// as well as the ChainID (prevent cross chain replay)
// and the Sequence numbers for each signature (prevent
// inchain replay and enforce tx ordering per account).
2018-03-03 23:36:10 -08:00
type StdSignDoc struct {
ChainID string `json:"chain_id"`
Sequences []int64 `json:"sequences"`
MsgBytes []byte `json:"msg_bytes"`
AltBytes []byte `json:"alt_bytes"` // TODO: do we really want this ?
2018-03-03 23:36:10 -08:00
}
2018-03-14 05:11:19 -07:00
// StdSignMsg is a convenience structure for passing along
// a Msg with the other requirements for a StdSignDoc before
// it is signed. For use in the CLI
type StdSignMsg struct {
ChainID string
Sequences []int64
Msg Msg
}
func (msg StdSignMsg) Bytes() []byte {
return StdSignBytes(msg.ChainID, msg.Sequences, msg.Msg)
}
func StdSignBytes(chainID string, sequences []int64, msg Msg) []byte {
2018-03-04 00:15:26 -08:00
bz, err := json.Marshal(StdSignDoc{
ChainID: chainID,
Sequences: sequences,
MsgBytes: msg.GetSignBytes(),
2018-03-04 00:15:26 -08:00
})
if err != nil {
panic(err)
}
return bz
}
2018-03-03 23:36:10 -08:00
//-------------------------------------
2018-02-04 16:59:11 -08:00
// Application function variable used to unmarshal transaction bytes
2018-01-26 04:19:33 -08:00
type TxDecoder func(txBytes []byte) (Tx, Error)