pool.go to be functions on pool

This commit is contained in:
rigelrozanski 2018-03-28 22:37:42 +02:00
parent 797aec2e71
commit d0db8b45ae
4 changed files with 63 additions and 79 deletions

View File

@ -298,3 +298,33 @@ func (k Keeper) setParams(ctx sdk.Context, params Params) {
store.Set(ParamKey, b)
k.params = Params{} // clear the cache
}
//_______________________________________________________________________
// load/save the pool
func (k Keeper) GetPool(ctx sdk.Context) (gs Pool) {
// check if cached before anything
if k.gs != (Pool{}) {
return k.gs
}
store := ctx.KVStore(k.storeKey)
b := store.Get(PoolKey)
if b == nil {
return initialPool()
}
err := k.cdc.UnmarshalBinary(b, &gs)
if err != nil {
panic(err) // This error should never occur big problem if does
}
return
}
func (k Keeper) setPool(ctx sdk.Context, p Pool) {
store := ctx.KVStore(k.storeKey)
b, err := k.cdc.MarshalBinary(p)
if err != nil {
panic(err)
}
store.Set(PoolKey, b)
k.gs = Pool{} // clear the cache
}

View File

@ -305,3 +305,18 @@ func TestParams(t *testing.T) {
resParams = keeper.GetParams(ctx)
assert.Equal(t, expParams, resParams)
}
func TestPool(t *testing.T) {
ctx, _, keeper := createTestInput(t, nil, false, 0)
expPool := initialPool()
//check that the empty keeper loads the default
resPool := keeper.GetPool(ctx)
assert.Equal(t, expPool, resPool)
//modify a params, save, and retrieve
expPool.TotalSupply = 777
keeper.setPool(ctx, expPool)
resPool = keeper.GetPool(ctx)
assert.Equal(t, expPool, resPool)
}

View File

@ -4,102 +4,64 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// load/save the global staking state
func (k Keeper) GetPool(ctx sdk.Context) (gs Pool) {
// check if cached before anything
if k.gs != (Pool{}) {
return k.gs
}
store := ctx.KVStore(k.storeKey)
b := store.Get(PoolKey)
if b == nil {
return initialPool()
}
err := k.cdc.UnmarshalBinary(b, &gs)
if err != nil {
panic(err) // This error should never occur big problem if does
}
return
}
func (k Keeper) setPool(ctx sdk.Context, p Pool) {
store := ctx.KVStore(k.storeKey)
b, err := k.cdc.MarshalBinary(p)
if err != nil {
panic(err)
}
store.Set(PoolKey, b)
k.gs = Pool{} // clear the cache
}
//_______________________________________________________________________
//TODO make these next two functions more efficient should be reading and writting to state ye know
// move a candidates asset pool from bonded to unbonded pool
func (k Keeper) bondedToUnbondedPool(ctx sdk.Context, candidate Candidate) {
func (p Pool) bondedToUnbondedPool(candidate Candidate) (Pool, Candidate) {
// replace bonded shares with unbonded shares
tokens := k.removeSharesBonded(ctx, candidate.Assets)
candidate.Assets = k.addTokensUnbonded(ctx, tokens)
candidate.Status = Unbonded
k.setCandidate(ctx, candidate)
return p, candidate
}
// move a candidates asset pool from unbonded to bonded pool
func (k Keeper) unbondedToBondedPool(ctx sdk.Context, candidate Candidate) {
func (p Pool) unbondedToBondedPool(candidate Candidate) (Pool, Candidate) {
// replace unbonded shares with bonded shares
tokens := k.removeSharesUnbonded(ctx, candidate.Assets)
candidate.Assets = k.addTokensBonded(ctx, tokens)
candidate.Status = Bonded
k.setCandidate(ctx, candidate)
return p, candidate
}
//_______________________________________________________________________
func (k Keeper) addTokensBonded(ctx sdk.Context, amount int64) (issuedShares sdk.Rat) {
p := k.GetPool(ctx)
func (p Pool) addTokensBonded(ctx sdk.Context, amount int64) (p Pool, issuedShares sdk.Rat) {
issuedShares = p.bondedShareExRate().Inv().Mul(sdk.NewRat(amount)) // (tokens/shares)^-1 * tokens
p.BondedPool += amount
p.BondedShares = p.BondedShares.Add(issuedShares)
k.setPool(ctx, p)
return
return p, issuedShares
}
func (k Keeper) removeSharesBonded(ctx sdk.Context, shares sdk.Rat) (removedTokens int64) {
p := k.GetPool(ctx)
func (p Pool) removeSharesBonded(ctx sdk.Context, shares sdk.Rat) (p Pool, removedTokens int64) {
removedTokens = p.bondedShareExRate().Mul(shares).Evaluate() // (tokens/shares) * shares
p.BondedShares = p.BondedShares.Sub(shares)
p.BondedPool -= removedTokens
k.setPool(ctx, p)
return
return p, removedTokens
}
func (k Keeper) addTokensUnbonded(ctx sdk.Context, amount int64) (issuedShares sdk.Rat) {
p := k.GetPool(ctx)
func (p Pool) addTokensUnbonded(ctx sdk.Context, amount int64) (p Pool, issuedShares sdk.Rat) {
issuedShares = p.unbondedShareExRate().Inv().Mul(sdk.NewRat(amount)) // (tokens/shares)^-1 * tokens
p.UnbondedShares = p.UnbondedShares.Add(issuedShares)
p.UnbondedPool += amount
k.setPool(ctx, p)
return
return p, issuedShares
}
func (k Keeper) removeSharesUnbonded(ctx sdk.Context, shares sdk.Rat) (removedTokens int64) {
p := k.GetPool(ctx)
func (p Pool) removeSharesUnbonded(ctx sdk.Context, shares sdk.Rat) (p Pool, removedTokens int64) {
removedTokens = p.unbondedShareExRate().Mul(shares).Evaluate() // (tokens/shares) * shares
p.UnbondedShares = p.UnbondedShares.Sub(shares)
p.UnbondedPool -= removedTokens
k.setPool(ctx, p)
return
return p, removedTokens
}
//_______________________________________________________________________
// add tokens to a candidate
func (k Keeper) candidateAddTokens(ctx sdk.Context, candidate Candidate, amount int64) (issuedDelegatorShares sdk.Rat) {
func (p Pool) candidateAddTokens(ctx sdk.Context, candidate Candidate,
amount int64) (p Pool, candidate Candidate, issuedDelegatorShares sdk.Rat) {
p := k.GetPool(ctx)
exRate := candidate.delegatorShareExRate()
var receivedGlobalShares sdk.Rat
@ -112,14 +74,13 @@ func (k Keeper) candidateAddTokens(ctx sdk.Context, candidate Candidate, amount
issuedDelegatorShares = exRate.Mul(receivedGlobalShares)
candidate.Liabilities = candidate.Liabilities.Add(issuedDelegatorShares)
k.setPool(ctx, p) // TODO cache Pool?
return
return p, candidate, issuedDelegatorShares
}
// remove shares from a candidate
func (k Keeper) candidateRemoveShares(ctx sdk.Context, candidate Candidate, shares sdk.Rat) (createdCoins int64) {
func (p Pool) candidateRemoveShares(ctx sdk.Context, candidate Candidate,
shares sdk.Rat) (p Pool, candidate Candidate, createdCoins int64) {
p := k.GetPool(ctx)
//exRate := candidate.delegatorShareExRate() //XXX make sure not used
globalPoolSharesToRemove := candidate.delegatorShareExRate().Mul(shares)
@ -130,6 +91,5 @@ func (k Keeper) candidateRemoveShares(ctx sdk.Context, candidate Candidate, shar
}
candidate.Assets = candidate.Assets.Sub(globalPoolSharesToRemove)
candidate.Liabilities = candidate.Liabilities.Sub(shares)
k.setPool(ctx, p) // TODO cache Pool?
return
return p, candidate, createdCoins
}

View File

@ -1,22 +1 @@
package stake
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPool(t *testing.T) {
ctx, _, keeper := createTestInput(t, nil, false, 0)
expPool := initialPool()
//check that the empty keeper loads the default
resPool := keeper.GetPool(ctx)
assert.Equal(t, expPool, resPool)
//modify a params, save, and retrieve
expPool.TotalSupply = 777
keeper.setPool(ctx, expPool)
resPool = keeper.GetPool(ctx)
assert.Equal(t, expPool, resPool)
}