2017-07-25 19:19:31 -07:00
|
|
|
package stack
|
2017-07-25 15:31:05 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/tendermint/basecoin"
|
|
|
|
"github.com/tendermint/basecoin/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
//nolint
|
|
|
|
const (
|
|
|
|
NameCheckpoint = "check"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Checkpoint isolates all data store below this
|
|
|
|
type Checkpoint struct {
|
2017-07-26 14:04:40 -07:00
|
|
|
OnCheck bool
|
|
|
|
OnDeliver bool
|
2017-07-25 19:19:31 -07:00
|
|
|
PassOption
|
2017-07-25 15:31:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name of the module - fulfills Middleware interface
|
|
|
|
func (Checkpoint) Name() string {
|
|
|
|
return NameCheckpoint
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:19:31 -07:00
|
|
|
var _ Middleware = Checkpoint{}
|
2017-07-25 15:31:05 -07:00
|
|
|
|
|
|
|
// CheckTx reverts all data changes if there was an error
|
|
|
|
func (c Checkpoint) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
|
2017-07-26 14:04:40 -07:00
|
|
|
if !c.OnCheck {
|
|
|
|
return next.CheckTx(ctx, store, tx)
|
|
|
|
}
|
2017-07-25 19:19:31 -07:00
|
|
|
ps := state.NewKVCache(unwrap(store))
|
2017-07-25 15:31:05 -07:00
|
|
|
res, err = next.CheckTx(ctx, ps, tx)
|
|
|
|
if err == nil {
|
|
|
|
ps.Sync()
|
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeliverTx reverts all data changes if there was an error
|
|
|
|
func (c Checkpoint) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
|
2017-07-26 14:04:40 -07:00
|
|
|
if !c.OnDeliver {
|
|
|
|
return next.DeliverTx(ctx, store, tx)
|
|
|
|
}
|
2017-07-25 19:19:31 -07:00
|
|
|
ps := state.NewKVCache(unwrap(store))
|
2017-07-25 15:31:05 -07:00
|
|
|
res, err = next.DeliverTx(ctx, ps, tx)
|
|
|
|
if err == nil {
|
|
|
|
ps.Sync()
|
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|