cosmos-sdk/modules/base/chain.go

69 lines
1.7 KiB
Go
Raw Normal View History

package base
import (
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/stack"
"github.com/cosmos/cosmos-sdk/state"
)
//nolint
const (
NameChain = "chain"
)
// Chain enforces that this tx was bound to the named chain
type Chain struct {
2017-07-30 14:26:25 -07:00
stack.PassInitState
stack.PassInitValidate
}
// Name of the module - fulfills Middleware interface
func (Chain) Name() string {
return NameChain
}
var _ stack.Middleware = Chain{}
// CheckTx makes sure we are on the proper chain - fulfills Middlware interface
func (c Chain) CheckTx(ctx sdk.Context, store state.SimpleDB, tx sdk.Tx, next sdk.Checker) (res sdk.CheckResult, err error) {
2017-07-11 04:44:44 -07:00
stx, err := c.checkChainTx(ctx.ChainID(), ctx.BlockHeight(), tx)
if err != nil {
return res, err
}
return next.CheckTx(ctx, store, stx)
}
// DeliverTx makes sure we are on the proper chain - fulfills Middlware interface
func (c Chain) DeliverTx(ctx sdk.Context, store state.SimpleDB, tx sdk.Tx, next sdk.Deliver) (res sdk.DeliverResult, err error) {
2017-07-11 04:44:44 -07:00
stx, err := c.checkChainTx(ctx.ChainID(), ctx.BlockHeight(), tx)
if err != nil {
return res, err
}
return next.DeliverTx(ctx, store, stx)
}
2017-07-11 04:44:44 -07:00
// checkChainTx makes sure the tx is a Chain Tx, it is on the proper chain,
// and it has not expired.
func (c Chain) checkChainTx(chainID string, height uint64, tx sdk.Tx) (sdk.Tx, error) {
// make sure it is a chaintx
ctx, ok := tx.Unwrap().(ChainTx)
if !ok {
2017-07-18 22:23:13 -07:00
return tx, ErrNoChain()
}
// basic validation
err := ctx.ValidateBasic()
if err != nil {
return tx, err
}
// compare against state
2017-07-03 08:32:01 -07:00
if ctx.ChainID != chainID {
2017-07-18 22:23:13 -07:00
return tx, ErrWrongChain(ctx.ChainID)
}
if ctx.ExpiresAt != 0 && ctx.ExpiresAt <= height {
2017-07-18 22:23:13 -07:00
return tx, ErrExpired()
}
return ctx.Tx, nil
}