cosmos-sdk/types/plugin.go

72 lines
1.7 KiB
Go
Raw Normal View History

2016-03-24 12:17:26 -07:00
package types
import (
"fmt"
2017-01-14 20:42:47 -08:00
abci "github.com/tendermint/abci/types"
2016-03-24 12:17:26 -07:00
)
type Plugin interface {
// Name of this plugin, should be short.
Name() string
// Run a transaction from ABCI DeliverTx
2017-01-14 20:42:47 -08:00
RunTx(store KVStore, ctx CallContext, txBytes []byte) (res abci.Result)
// Other ABCI message handlers
SetOption(store KVStore, key, value string) (log string)
2017-01-14 20:42:47 -08:00
InitChain(store KVStore, vals []*abci.Validator)
BeginBlock(store KVStore, hash []byte, header *abci.Header)
EndBlock(store KVStore, height uint64) abci.ResponseEndBlock
}
2016-03-27 12:47:50 -07:00
//----------------------------------------
2016-03-24 12:17:26 -07:00
type CallContext struct {
CallerAddress []byte // Caller's Address (hash of PubKey)
CallerAccount *Account // Caller's Account, w/ fee & TxInputs deducted
2017-01-27 10:46:01 -08:00
Coins Coins // The coins that the caller wishes to spend, excluding fees
2016-03-24 12:17:26 -07:00
}
func NewCallContext(callerAddress []byte, callerAccount *Account, coins Coins) CallContext {
2016-03-24 12:17:26 -07:00
return CallContext{
CallerAddress: callerAddress,
CallerAccount: callerAccount,
Coins: coins,
2016-03-24 12:17:26 -07:00
}
}
2016-03-27 12:47:50 -07:00
//----------------------------------------
type Plugins struct {
byName map[string]Plugin
plist []Plugin
2016-03-27 12:47:50 -07:00
}
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))
}
2016-03-27 12:47:50 -07:00
pgz.byName[name] = plugin
pgz.plist = append(pgz.plist, plugin)
2016-03-27 12:47:50 -07:00
}
func (pgz *Plugins) GetByName(name string) Plugin {
return pgz.byName[name]
}
func (pgz *Plugins) GetList() []Plugin {
2016-03-27 12:47:50 -07:00
return pgz.plist
}