cosmos-sdk/types/plugin.go

67 lines
1.5 KiB
Go

package types
import (
"fmt"
abci "github.com/tendermint/abci/types"
)
type Plugin interface {
Name() string
SetOption(store KVStore, key string, value string) (log string)
RunTx(store KVStore, ctx CallContext, txBytes []byte) (res abci.Result)
InitChain(store KVStore, vals []*abci.Validator)
BeginBlock(store KVStore, height uint64)
EndBlock(store KVStore, height uint64) []*abci.Validator
}
//----------------------------------------
// CallContext.Caller's coins have been deducted by CallContext.Coins
// Caller's Sequence has been incremented.
type CallContext struct {
CallerAddress []byte
CallerAccount *Account
Coins Coins
}
func NewCallContext(callerAddress []byte, callerAccount *Account, coins Coins) CallContext {
return CallContext{
CallerAddress: callerAddress,
CallerAccount: callerAccount,
Coins: coins,
}
}
//----------------------------------------
type Plugins struct {
byName map[string]Plugin
plist []Plugin
}
func NewPlugins() *Plugins {
return &Plugins{
byName: make(map[string]Plugin),
}
}
func (pgz *Plugins) RegisterPlugin(plugin Plugin) {
name := plugin.Name()
if name == "" {
panic("Plugin name cannot be blank")
}
if _, exists := pgz.byName[name]; exists {
panic(fmt.Sprintf("Plugin already exists by the name of %v", name))
}
pgz.byName[name] = plugin
pgz.plist = append(pgz.plist, plugin)
}
func (pgz *Plugins) GetByName(name string) Plugin {
return pgz.byName[name]
}
func (pgz *Plugins) GetList() []Plugin {
return pgz.plist
}