Plugin interface methods take store
This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
type AccountCache struct {
|
||||
state AccountGetterSetter
|
||||
accounts map[string]*Account
|
||||
}
|
||||
|
||||
func NewAccountCache(state AccountGetterSetter) *AccountCache {
|
||||
return &AccountCache{
|
||||
state: state,
|
||||
accounts: make(map[string]*Account),
|
||||
}
|
||||
}
|
||||
|
||||
func (cache *AccountCache) GetAccount(addr []byte) *Account {
|
||||
acc, ok := cache.accounts[string(addr)]
|
||||
if !ok {
|
||||
acc = cache.state.GetAccount(addr)
|
||||
cache.accounts[string(addr)] = acc
|
||||
}
|
||||
return acc
|
||||
}
|
||||
|
||||
func (cache *AccountCache) SetAccount(addr []byte, acc *Account) {
|
||||
cache.accounts[string(addr)] = acc
|
||||
}
|
||||
|
||||
func (cache *AccountCache) Sync() {
|
||||
// MUST BE DETERMINISTIC
|
||||
// First, order the addrs.
|
||||
addrs := []string{}
|
||||
for addr := range cache.accounts {
|
||||
addrs = append(addrs, string(addr))
|
||||
}
|
||||
sort.Strings(addrs)
|
||||
|
||||
// Set the accounts in order.
|
||||
for _, addr := range addrs {
|
||||
cache.state.SetAccount([]byte(addr), cache.accounts[addr])
|
||||
}
|
||||
|
||||
// Reset accounts
|
||||
cache.accounts = make(map[string]*Account)
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
// NOT USED
|
||||
type AccountCacher interface {
|
||||
GetAccount(addr []byte) *Account
|
||||
SetAccount(addr []byte, acc *Account)
|
||||
Sync()
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
type KVStore interface {
|
||||
Set(key, value []byte)
|
||||
Get(key []byte) (value []byte)
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
type MemKVStore struct {
|
||||
m map[string][]byte
|
||||
}
|
||||
|
||||
func NewMemKVStore() *MemKVStore {
|
||||
return &MemKVStore{
|
||||
m: make(map[string][]byte, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (mkv *MemKVStore) Set(key []byte, value []byte) {
|
||||
mkv.m[string(key)] = value
|
||||
}
|
||||
|
||||
func (mkv *MemKVStore) Get(key []byte) (value []byte) {
|
||||
return mkv.m[string(key)]
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
// A Cache that enforces deterministic sync order.
|
||||
type KVCache struct {
|
||||
store KVStore
|
||||
cache map[string]kvCacheValue
|
||||
keys *list.List
|
||||
}
|
||||
|
||||
type kvCacheValue struct {
|
||||
v []byte // The value of some key
|
||||
e *list.Element // The KVCache.keys element
|
||||
}
|
||||
|
||||
func NewKVCache(store KVStore) *KVCache {
|
||||
return (&KVCache{
|
||||
store: store,
|
||||
}).Reset()
|
||||
}
|
||||
|
||||
func (kvc *KVCache) Reset() *KVCache {
|
||||
kvc.cache = make(map[string]kvCacheValue)
|
||||
kvc.keys = list.New()
|
||||
return kvc
|
||||
}
|
||||
|
||||
func (kvc *KVCache) Set(key []byte, value []byte) {
|
||||
cacheValue, ok := kvc.cache[string(key)]
|
||||
if ok {
|
||||
kvc.keys.MoveToBack(cacheValue.e)
|
||||
} else {
|
||||
cacheValue.e = kvc.keys.PushBack(key)
|
||||
}
|
||||
cacheValue.v = value
|
||||
kvc.cache[string(key)] = cacheValue
|
||||
}
|
||||
|
||||
func (kvc *KVCache) Get(key []byte) (value []byte) {
|
||||
cacheValue := kvc.cache[string(key)]
|
||||
return cacheValue.v
|
||||
}
|
||||
|
||||
func (kvc *KVCache) Sync() {
|
||||
for e := kvc.keys.Front(); e != nil; e = e.Next() {
|
||||
key := e.Value.([]byte)
|
||||
value := kvc.cache[string(key)]
|
||||
kvc.store.Set(key, value.v)
|
||||
}
|
||||
kvc.Reset()
|
||||
}
|
||||
+7
-9
@@ -4,12 +4,12 @@ import (
|
||||
tmsp "github.com/tendermint/tmsp/types"
|
||||
)
|
||||
|
||||
// Value is any floating value. It must be given to someone.
|
||||
type Plugin interface {
|
||||
SetOption(key string, value string) (log string)
|
||||
RunTx(ctx CallContext, txBytes []byte) (res tmsp.Result)
|
||||
Query(query []byte) (res tmsp.Result)
|
||||
Commit() (res tmsp.Result)
|
||||
SetOption(store KVStore, key string, value string) (log string)
|
||||
RunTx(store KVStore, ctx CallContext, txBytes []byte) (res tmsp.Result)
|
||||
InitChain(store KVStore, vals []*tmsp.Validator)
|
||||
BeginBlock(store KVStore, height uint64)
|
||||
EndBlock(store KVStore, height uint64) []*tmsp.Validator
|
||||
}
|
||||
|
||||
type NamedPlugin struct {
|
||||
@@ -21,14 +21,12 @@ type NamedPlugin struct {
|
||||
//----------------------------------------
|
||||
|
||||
type CallContext struct {
|
||||
Cache AccountCacher
|
||||
Caller *Account
|
||||
Caller []byte
|
||||
Coins Coins
|
||||
}
|
||||
|
||||
func NewCallContext(cache AccountCacher, caller *Account, coins Coins) CallContext {
|
||||
func NewCallContext(caller []byte, coins Coins) CallContext {
|
||||
return CallContext{
|
||||
Cache: cache,
|
||||
Caller: caller,
|
||||
Coins: coins,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user