Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CNS-7: Enable consumer operation upon staking #142

Merged
merged 11 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app

import (
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ignite-hq/cli/ignite/pkg/cosmoscmd"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
)

// Setup a new App for testing purposes
func TestSetup() (cosmoscmd.App, sdk.Context) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test functions should be in a file ending with _test, go knows not to compile them, with the current name, it will be included in the binary

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just seting up for a test, not a test function in itself

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't matter what is in the file, if it doesn't end with _test.go its compiled into the final binary and we dont need test helper functions in our binary

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if i do this it makes the file unusable outside of the package, since it becomes a test file

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
encoding := cosmoscmd.MakeEncodingConfig(ModuleBasics)
app := New(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, encoding, simapp.EmptyAppOptions{})
return app, ctx
}
423 changes: 383 additions & 40 deletions docs/static/openapi.yml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion testutil/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func StakeAccount(t *testing.T, ctx context.Context, keepers testkeeper.Keepers,
vrfPk.Unmarshal(pk)
_, err := servers.PairingServer.StakeClient(ctx, &types.MsgStakeClient{Creator: acc.Addr.String(), ChainID: spec.Name, Amount: sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(stake)), Geolocation: 1, Vrfpk: vrfPk.String()})
require.Nil(t, err)

}
}

Expand Down
4 changes: 2 additions & 2 deletions testutil/keeper/keepers_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ func NewBlock(ctx context.Context, ks *Keepers) {
ks.Epochstorage.FixateParams(unwrapedCtx, block)
//begin block
ks.Epochstorage.SetEpochDetailsStart(unwrapedCtx, block)
ks.Epochstorage.StoreEpochStakeStorage(unwrapedCtx, block, epochstoragetypes.ProviderKey)
ks.Epochstorage.StoreEpochStakeStorage(unwrapedCtx, block, epochstoragetypes.ClientKey)
ks.Epochstorage.StoreCurrentEpochStakeStorage(unwrapedCtx, block, epochstoragetypes.ProviderKey)
ks.Epochstorage.StoreCurrentEpochStakeStorage(unwrapedCtx, block, epochstoragetypes.ClientKey)

ks.Pairing.RemoveOldEpochPayment(unwrapedCtx)
ks.Pairing.CheckUnstakingForCommit(unwrapedCtx)
Expand Down
5 changes: 3 additions & 2 deletions x/conflict/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type EpochstorageKeeper interface {
GetStakeEntryForClientEpoch(ctx sdk.Context, chainID string, selectedClient sdk.AccAddress, epoch uint64) (entry *epochstoragetypes.StakeEntry, err error)
GetStakeEntryForProviderEpoch(ctx sdk.Context, chainID string, selectedProvider sdk.AccAddress, epoch uint64) (entry *epochstoragetypes.StakeEntry, err error)
GetStakeEntryForAllProvidersEpoch(ctx sdk.Context, chainID string, epoch uint64) (entrys *[]epochstoragetypes.StakeEntry, err error)
ModifyStakeEntry(ctx sdk.Context, storageType string, chainID string, stakeEntry epochstoragetypes.StakeEntry, removeIndex uint64)
StakeEntryByAddress(ctx sdk.Context, storageType string, chainID string, address sdk.AccAddress) (value epochstoragetypes.StakeEntry, found bool, index uint64)
ModifyStakeEntryCurrent(ctx sdk.Context, storageType string, chainID string, stakeEntry epochstoragetypes.StakeEntry, removeIndex uint64)
GetStakeEntryByAddressCurrent(ctx sdk.Context, storageType string, chainID string, address sdk.AccAddress) (value epochstoragetypes.StakeEntry, found bool, index uint64)
BypassCurrentAndAppendNewEpochStakeEntry(ctx sdk.Context, storageType string, chainID string, stakeEntry epochstoragetypes.StakeEntry) (added bool, err error)
PushFixatedParams(ctx sdk.Context, block uint64, limit uint64)
}

Expand Down
55 changes: 50 additions & 5 deletions x/epochstorage/keeper/stake_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (k Keeper) GetStakeEntryByAddressFromStorage(ctx sdk.Context, stakeStorage
return
}

func (k Keeper) StakeEntryByAddress(ctx sdk.Context, storageType string, chainID string, address sdk.AccAddress) (value types.StakeEntry, found bool, index uint64) {
func (k Keeper) GetStakeEntryByAddressCurrent(ctx sdk.Context, storageType string, chainID string, address sdk.AccAddress) (value types.StakeEntry, found bool, index uint64) {
stakeStorage, found := k.GetStakeStorageCurrent(ctx, storageType, chainID)
if !found {
return types.StakeEntry{}, false, 0
Expand All @@ -226,7 +226,7 @@ func (k Keeper) StakeEntryByAddress(ctx sdk.Context, storageType string, chainID
return
}

func (k Keeper) RemoveStakeEntry(ctx sdk.Context, storageType string, chainID string, idx uint64) {
func (k Keeper) RemoveStakeEntryCurrent(ctx sdk.Context, storageType string, chainID string, idx uint64) {
stakeStorage, found := k.GetStakeStorageCurrent(ctx, storageType, chainID)
if !found {
return
Expand All @@ -235,7 +235,7 @@ func (k Keeper) RemoveStakeEntry(ctx sdk.Context, storageType string, chainID st
k.SetStakeStorageCurrent(ctx, storageType, chainID, stakeStorage)
}

func (k Keeper) AppendStakeEntry(ctx sdk.Context, storageType string, chainID string, stakeEntry types.StakeEntry) {
func (k Keeper) AppendStakeEntryCurrent(ctx sdk.Context, storageType string, chainID string, stakeEntry types.StakeEntry) {
//this stake storage entries are sorted by stake amount
stakeStorage, found := k.GetStakeStorageCurrent(ctx, storageType, chainID)
var entries = []types.StakeEntry{}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (k Keeper) AppendStakeEntry(ctx sdk.Context, storageType string, chainID st
k.SetStakeStorageCurrent(ctx, storageType, chainID, stakeStorage)
}

func (k Keeper) ModifyStakeEntry(ctx sdk.Context, storageType string, chainID string, stakeEntry types.StakeEntry, removeIndex uint64) {
func (k Keeper) ModifyStakeEntryCurrent(ctx sdk.Context, storageType string, chainID string, stakeEntry types.StakeEntry, removeIndex uint64) {
//this stake storage entries are sorted by stake amount
stakeStorage, found := k.GetStakeStorageCurrent(ctx, storageType, chainID)
if !found {
Expand Down Expand Up @@ -423,7 +423,7 @@ func (k Keeper) PopUnstakeEntries(ctx sdk.Context, storageType string, block uin
// ------------------------------------------------

// takes the current stake storage and puts it in epoch storage
func (k Keeper) StoreEpochStakeStorage(ctx sdk.Context, block uint64, storageType string) {
func (k Keeper) StoreCurrentEpochStakeStorage(ctx sdk.Context, block uint64, storageType string) {
allChainIDs := k.specKeeper.GetAllChainIDs(ctx)
for _, chainID := range allChainIDs {
tmpStorage, found := k.GetStakeStorageCurrent(ctx, storageType, chainID)
Expand Down Expand Up @@ -487,3 +487,48 @@ func (k Keeper) GetEpochStakeEntries(ctx sdk.Context, block uint64, storageType
}
return stakeStorage.StakeEntries, true
}

//append to epoch stake entries ONLY if it doesn't exist
func (k Keeper) BypassCurrentAndAppendNewEpochStakeEntry(ctx sdk.Context, storageType string, chainID string, stakeEntry types.StakeEntry) (added bool, err error) {
epoch := k.GetEpochStart(ctx)
stakeEntry.Deadline = epoch
storage, found := k.getStakeStorageEpoch(ctx, epoch, storageType, chainID)
if !found {
entries := []types.StakeEntry{}
//create a new one
storage = types.StakeStorage{Index: k.StakeStorageKey(storageType, epoch, chainID), StakeEntries: entries}
}
entryAddr, err := sdk.AccAddressFromBech32(stakeEntry.Address)
if err != nil {
return false, err
}

for _, clientStakeEntry := range storage.StakeEntries {
clientAddr, err := sdk.AccAddressFromBech32(clientStakeEntry.Address)
if err != nil {
panic(fmt.Sprintf("invalid user address saved in keeper %s, err: %s", clientStakeEntry.Address, err))
}
if clientAddr.Equals(entryAddr) {
return false, nil //stake already exists in this epoch
}
}

//put it in the right place
entries := storage.StakeEntries
sortFunc := func(i int) bool {
return stakeEntry.Stake.Amount.LT(entries[i].Stake.Amount)
}
//returns the smallest index in which the sort func is true
index := sort.Search(len(entries), sortFunc)
if index < len(entries) {
Yaroms marked this conversation as resolved.
Show resolved Hide resolved
entries = append(entries[:index+1], entries[index:]...)
entries[index] = stakeEntry
} else {
//put in the end
entries = append(entries, stakeEntry)
}

storage.StakeEntries = entries
k.SetStakeStorage(ctx, storage)
return true, nil
}
4 changes: 2 additions & 2 deletions x/epochstorage/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {

am.keeper.SetEpochDetailsStart(ctx, block)

am.keeper.StoreEpochStakeStorage(ctx, block, types.ProviderKey)
am.keeper.StoreCurrentEpochStakeStorage(ctx, block, types.ProviderKey)

am.keeper.StoreEpochStakeStorage(ctx, block, types.ClientKey)
am.keeper.StoreCurrentEpochStakeStorage(ctx, block, types.ClientKey)
// Notify world we have a new session
details := map[string]string{"height": fmt.Sprintf("%d", ctx.BlockHeight()), "description": "New Block Epoch Started"}
logger := am.keeper.Logger(ctx)
Expand Down
4 changes: 2 additions & 2 deletions x/pairing/keeper/msg_server_relay_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (k msgServer) dealWithUnresponsiveProviders(ctx sdk.Context, unresponsiveDa
utils.LavaFormatError("unable to sdk.AccAddressFromBech32(unresponsive_provider)", err, &map[string]string{"unresponsive_provider_address": unresponsiveProvider})
continue
}
existingEntry, entryExists, indexInStakeStorage := k.epochStorageKeeper.StakeEntryByAddress(ctx, epochstoragetypes.ProviderKey, chainID, sdkUnresponsiveProviderAddress)
existingEntry, entryExists, indexInStakeStorage := k.epochStorageKeeper.GetStakeEntryByAddressCurrent(ctx, epochstoragetypes.ProviderKey, chainID, sdkUnresponsiveProviderAddress)
// if !entryExists provider is alraedy unstaked
if !entryExists {
continue // if provider is not staked, nothing to do.
Expand Down Expand Up @@ -362,6 +362,6 @@ func (k msgServer) getTotalPaymentsForPreviousEpochs(ctx sdk.Context, numberOfEp
}

func (k msgServer) unSafeUnstakeProviderEntry(ctx sdk.Context, providerKey string, chainID string, indexInStakeStorage uint64, existingEntry epochstoragetypes.StakeEntry) {
k.epochStorageKeeper.RemoveStakeEntry(ctx, epochstoragetypes.ProviderKey, chainID, indexInStakeStorage)
k.epochStorageKeeper.RemoveStakeEntryCurrent(ctx, epochstoragetypes.ProviderKey, chainID, indexInStakeStorage)
k.epochStorageKeeper.AppendUnstakeEntry(ctx, epochstoragetypes.ProviderKey, existingEntry)
}
Loading