2018-06-27 10:21:12 -07:00
|
|
|
package oracle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper of the oracle store
|
|
|
|
type Keeper struct {
|
2018-07-26 18:24:18 -07:00
|
|
|
key sdk.StoreKey
|
2018-06-27 10:21:12 -07:00
|
|
|
cdc *wire.Codec
|
|
|
|
|
|
|
|
valset sdk.ValidatorSet
|
|
|
|
|
2018-08-14 17:15:02 -07:00
|
|
|
supermaj sdk.Dec
|
2018-06-27 10:21:12 -07:00
|
|
|
timeout int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeeper constructs a new keeper
|
2018-08-14 17:15:02 -07:00
|
|
|
func NewKeeper(key sdk.StoreKey, cdc *wire.Codec, valset sdk.ValidatorSet, supermaj sdk.Dec, timeout int64) Keeper {
|
2018-06-27 10:21:12 -07:00
|
|
|
if timeout < 0 {
|
|
|
|
panic("Timeout should not be negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
return Keeper{
|
|
|
|
key: key,
|
|
|
|
cdc: cdc,
|
|
|
|
|
|
|
|
valset: valset,
|
|
|
|
|
|
|
|
supermaj: supermaj,
|
|
|
|
timeout: timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InfoStatus - current status of an Info
|
|
|
|
type InfoStatus int8
|
|
|
|
|
|
|
|
// Define InfoStatus
|
|
|
|
const (
|
|
|
|
Pending = InfoStatus(iota)
|
|
|
|
Processed
|
|
|
|
Timeout
|
|
|
|
)
|
|
|
|
|
|
|
|
// Info for each payload
|
|
|
|
type Info struct {
|
2018-08-14 17:15:02 -07:00
|
|
|
Power sdk.Dec
|
2018-06-27 10:21:12 -07:00
|
|
|
Hash []byte
|
|
|
|
LastSigned int64
|
|
|
|
Status InfoStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmptyInfo construct an empty Info
|
|
|
|
func EmptyInfo(ctx sdk.Context) Info {
|
|
|
|
return Info{
|
2018-08-14 17:15:02 -07:00
|
|
|
Power: sdk.ZeroDec(),
|
2018-06-27 10:21:12 -07:00
|
|
|
Hash: ctx.BlockHeader().ValidatorsHash,
|
|
|
|
LastSigned: ctx.BlockHeight(),
|
|
|
|
Status: Pending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info returns the information about a payload
|
|
|
|
func (keeper Keeper) Info(ctx sdk.Context, p Payload) (res Info) {
|
2018-07-26 18:24:18 -07:00
|
|
|
store := ctx.KVStore(keeper.key)
|
2018-06-27 10:21:12 -07:00
|
|
|
|
|
|
|
key := GetInfoKey(p, keeper.cdc)
|
|
|
|
bz := store.Get(key)
|
|
|
|
if bz == nil {
|
|
|
|
return EmptyInfo(ctx)
|
|
|
|
}
|
|
|
|
keeper.cdc.MustUnmarshalBinary(bz, &res)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (keeper Keeper) setInfo(ctx sdk.Context, p Payload, info Info) {
|
2018-07-26 18:24:18 -07:00
|
|
|
store := ctx.KVStore(keeper.key)
|
2018-06-27 10:21:12 -07:00
|
|
|
|
|
|
|
key := GetInfoKey(p, keeper.cdc)
|
|
|
|
bz := keeper.cdc.MustMarshalBinary(info)
|
|
|
|
store.Set(key, bz)
|
|
|
|
}
|
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
func (keeper Keeper) sign(ctx sdk.Context, p Payload, signer sdk.AccAddress) {
|
2018-07-26 18:24:18 -07:00
|
|
|
store := ctx.KVStore(keeper.key)
|
2018-06-27 10:21:12 -07:00
|
|
|
|
|
|
|
key := GetSignKey(p, signer, keeper.cdc)
|
|
|
|
store.Set(key, signer)
|
|
|
|
}
|
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
func (keeper Keeper) signed(ctx sdk.Context, p Payload, signer sdk.AccAddress) bool {
|
2018-07-26 18:24:18 -07:00
|
|
|
store := ctx.KVStore(keeper.key)
|
2018-06-27 10:21:12 -07:00
|
|
|
|
|
|
|
key := GetSignKey(p, signer, keeper.cdc)
|
|
|
|
return store.Has(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (keeper Keeper) clearSigns(ctx sdk.Context, p Payload) {
|
2018-07-26 18:24:18 -07:00
|
|
|
store := ctx.KVStore(keeper.key)
|
2018-06-27 10:21:12 -07:00
|
|
|
|
|
|
|
prefix := GetSignPrefix(p, keeper.cdc)
|
|
|
|
|
|
|
|
iter := sdk.KVStorePrefixIterator(store, prefix)
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
store.Delete(iter.Key())
|
|
|
|
}
|
|
|
|
iter.Close()
|
|
|
|
}
|