cosmos-sdk/stack/mock.go

91 lines
1.8 KiB
Go
Raw Normal View History

2017-06-30 11:26:17 -07:00
package stack
import (
"math/rand"
2017-06-30 11:26:17 -07:00
"github.com/tendermint/tmlibs/log"
"github.com/tendermint/basecoin"
)
type naiveContext struct {
id nonce
chain string
height uint64
perms []basecoin.Actor
2017-06-30 11:26:17 -07:00
log.Logger
}
// MockContext returns a simple, non-checking context for test cases.
//
// Always use NewContext() for production code to sandbox malicious code better
func MockContext(chain string, height uint64) basecoin.Context {
return naiveContext{
id: nonce(rand.Int63()),
2017-07-03 08:32:01 -07:00
chain: chain,
height: height,
2017-06-30 11:26:17 -07:00
Logger: log.NewNopLogger(),
}
}
var _ basecoin.Context = naiveContext{}
2017-06-30 11:26:17 -07:00
func (c naiveContext) ChainID() string {
2017-07-03 08:32:01 -07:00
return c.chain
}
func (c naiveContext) BlockHeight() uint64 {
return c.height
}
2017-06-30 11:26:17 -07:00
// WithPermissions will panic if they try to set permission without the proper app
func (c naiveContext) WithPermissions(perms ...basecoin.Actor) basecoin.Context {
return naiveContext{
id: c.id,
chain: c.chain,
height: c.height,
2017-06-30 11:26:17 -07:00
perms: append(c.perms, perms...),
Logger: c.Logger,
}
}
func (c naiveContext) HasPermission(perm basecoin.Actor) bool {
2017-06-30 11:26:17 -07:00
for _, p := range c.perms {
if p.Equals(perm) {
2017-06-30 11:26:17 -07:00
return true
}
}
return false
}
func (c naiveContext) GetPermissions(chain, app string) (res []basecoin.Actor) {
for _, p := range c.perms {
if chain == p.ChainID {
if app == "" || app == p.App {
res = append(res, p)
}
}
}
return res
}
2017-06-30 11:26:17 -07:00
// IsParent ensures that this is derived from the given secureClient
func (c naiveContext) IsParent(other basecoin.Context) bool {
nc, ok := other.(naiveContext)
if !ok {
return false
}
return c.id == nc.id
2017-06-30 11:26:17 -07:00
}
// Reset should clear out all permissions,
// but carry on knowledge that this is a child
func (c naiveContext) Reset() basecoin.Context {
return naiveContext{
id: c.id,
chain: c.chain,
height: c.height,
2017-06-30 11:26:17 -07:00
Logger: c.Logger,
}
}