forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
70 lines (56 loc) · 1.64 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package types
import (
"fmt"
abci "github.com/tendermint/abci/types"
)
type Plugin interface {
// Name of this plugin, should be short.
Name() string
// Run a transaction from ABCI DeliverTx
RunTx(store KVStore, ctx CallContext, txBytes []byte) (res abci.Result)
// Other ABCI message handlers
SetOption(store KVStore, key string, value string) (log string)
InitChain(store KVStore, vals []*abci.Validator)
BeginBlock(store KVStore, height uint64)
EndBlock(store KVStore, height uint64) []*abci.Validator
}
//----------------------------------------
type CallContext struct {
CallerAddress []byte // Caller's Address (hash of PubKey)
CallerAccount *Account // Caller's Account, w/ fee & TxInputs deducted
TxInput Coins // The coins that the caller wishes to spend, excluding fees
}
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
}