cosmos-sdk/examples/democoin/x/oracle/keeper.go
Rigel d9dc061b4f
Merge pull request #1819: rational -> decimal
* changelog

* ...

* decimal func working

* decimal complete, untested

* fixing tests

* decimal compile errors resolved

* test compile errors

* precision multiplier test

* 1% laptop battery

* fixed TestNewDecFromStr

* equalities working

* fix bankers round chop

* ...

* working, some decimal issues resolved

* fix rounding error

* rounding works

* decimal works

* ...

* deleted rational

* rational conversion working

* revert changelog

* code compiles (not tests)

* went through all NewDec, made sure they were converted from NewRat properly

* test debugging

* all testing bugs besides the json marshalling fixed

* json unmarshal

* lint

* document update

* fix lcd test

* cli test fix

* mostly undo Dece -> Rate

* val comments

* Efficiency improvements

This now caches all of the precision multipliers (as they were all
used in non-mutative functions), and caches the precisionInt calculation.
(Now it just copies the already calculated value)

* Cache another precisionInt() call.

* Improve banker rounding efficiency

* remove defer, make negation in-place.

* chris val comments

* bez comments

* Aditya comments

* ...

* val comments

* rebasing start

* ...

* compiling

* tests pass

* cli fix

* anton, cwgoes, val comments

* val and jae comments

* type

* undo reuse quo
2018-08-14 20:15:02 -04:00

112 lines
2.2 KiB
Go

package oracle
import (
"github.com/cosmos/cosmos-sdk/wire"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Keeper of the oracle store
type Keeper struct {
key sdk.StoreKey
cdc *wire.Codec
valset sdk.ValidatorSet
supermaj sdk.Dec
timeout int64
}
// NewKeeper constructs a new keeper
func NewKeeper(key sdk.StoreKey, cdc *wire.Codec, valset sdk.ValidatorSet, supermaj sdk.Dec, timeout int64) Keeper {
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 {
Power sdk.Dec
Hash []byte
LastSigned int64
Status InfoStatus
}
// EmptyInfo construct an empty Info
func EmptyInfo(ctx sdk.Context) Info {
return Info{
Power: sdk.ZeroDec(),
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) {
store := ctx.KVStore(keeper.key)
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) {
store := ctx.KVStore(keeper.key)
key := GetInfoKey(p, keeper.cdc)
bz := keeper.cdc.MustMarshalBinary(info)
store.Set(key, bz)
}
func (keeper Keeper) sign(ctx sdk.Context, p Payload, signer sdk.AccAddress) {
store := ctx.KVStore(keeper.key)
key := GetSignKey(p, signer, keeper.cdc)
store.Set(key, signer)
}
func (keeper Keeper) signed(ctx sdk.Context, p Payload, signer sdk.AccAddress) bool {
store := ctx.KVStore(keeper.key)
key := GetSignKey(p, signer, keeper.cdc)
return store.Has(key)
}
func (keeper Keeper) clearSigns(ctx sdk.Context, p Payload) {
store := ctx.KVStore(keeper.key)
prefix := GetSignPrefix(p, keeper.cdc)
iter := sdk.KVStorePrefixIterator(store, prefix)
for ; iter.Valid(); iter.Next() {
store.Delete(iter.Key())
}
iter.Close()
}