Skip to content

Commit

Permalink
chore: started refactoring pool accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Dec 23, 2024
1 parent 9425f41 commit 8a76d53
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 242 deletions.
2 changes: 1 addition & 1 deletion proto/kyve/stakers/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ message GenesisState {
// staker_list ...
repeated Staker staker_list = 2 [(gogoproto.nullable) = false];
// valaccount_list ...
repeated Valaccount valaccount_list = 3 [(gogoproto.nullable) = false];
repeated PoolAccount pool_account_list = 3 [(gogoproto.nullable) = false];
// commission_change_entries ...
repeated CommissionChangeEntry commission_change_entries = 4 [(gogoproto.nullable) = false];
// queue_state_commission ...
Expand Down
16 changes: 10 additions & 6 deletions proto/kyve/stakers/v1beta1/stakers.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ message Staker {
];
}

// Valaccount gets authorized by a staker to
// vote in a given pool by favor of the staker.
message Valaccount {
// PoolAccount gets authorized by a validator to
// vote in a given pool by favor of the validator.
// The pool account basically acts like an operator
// here so the validator private key can be stored
// securely and not on a remote server where
// the pool account will operate
message PoolAccount {
// pool_id defines the pool in which the address
// is allowed to vote in.
uint64 pool_id = 1;
// staker is the address the valaccount is voting for.
// staker is the address validator
string staker = 2;
// valaddress is the account stored on the protocol
// pool_address is the account stored on the protocol
// node which votes for the staker in the given pool
string valaddress = 3;
string pool_address = 3;
// When a node is inactive (does not vote at all)
// A point is added, after a certain amount of points
// is reached the node gets kicked out.
Expand Down
4 changes: 2 additions & 2 deletions x/bundles/keeper/logic_bundles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ var _ = Describe("logic_bundles.go", Ordered, func() {
err := s.App().BundlesKeeper.AssertCanVote(s.Ctx(), 0, i.STAKER_2, i.VALADDRESS_2_A, "y62A3tfbSNcNYDGoL-eXwzyV-Zc9Q0OVtDvR1biJmNI")

// ASSERT
Expect(err).To(Equal(stakertypes.ErrValaccountUnauthorized))
Expect(err).To(Equal(stakertypes.ErrPoolAccountUnauthorized))
})

It("Assert can vote if bundle is dropped", func() {
Expand Down Expand Up @@ -899,7 +899,7 @@ var _ = Describe("logic_bundles.go", Ordered, func() {
err := s.App().BundlesKeeper.AssertCanPropose(s.Ctx(), 0, i.STAKER_2, i.VALADDRESS_2_A, 0)

// ASSERT
Expect(err).To(Equal(stakertypes.ErrValaccountUnauthorized))
Expect(err).To(Equal(stakertypes.ErrPoolAccountUnauthorized))
})

It("Assert can propose if sender is not next uploader", func() {
Expand Down
2 changes: 1 addition & 1 deletion x/bundles/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type StakerKeeper interface {
GetAllStakerAddressesOfPool(ctx sdk.Context, poolId uint64) (stakers []string)
AssertValaccountAuthorized(ctx sdk.Context, poolId uint64, stakerAddress string, valaddress string) error

GetValaccount(ctx sdk.Context, poolId uint64, stakerAddress string) (valaccount stakersTypes.Valaccount, active bool)
GetValaccount(ctx sdk.Context, poolId uint64, stakerAddress string) (valaccount stakersTypes.PoolAccount, active bool)

LeavePool(ctx sdk.Context, staker string, poolId uint64)

Expand Down
2 changes: 1 addition & 1 deletion x/delegation/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type UpgradeKeeper interface {
type StakersKeeper interface {
DoesStakerExist(ctx sdk.Context, staker string) bool
GetAllStakerAddressesOfPool(ctx sdk.Context, poolId uint64) (stakers []string)
GetValaccountsFromStaker(ctx sdk.Context, stakerAddress string) (val []*stakerstypes.Valaccount)
GetValaccountsFromStaker(ctx sdk.Context, stakerAddress string) (val []*stakerstypes.PoolAccount)
GetPoolCount(ctx sdk.Context, stakerAddress string) (poolCount uint64)
GetActiveStakers(ctx sdk.Context) []string
}
2 changes: 1 addition & 1 deletion x/query/keeper/grpc_query_can_propose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ var _ = Describe("grpc_query_can_propose.go", Ordered, func() {
Expect(err).To(BeNil())

Expect(canPropose.Possible).To(BeFalse())
Expect(canPropose.Reason).To(Equal(stakertypes.ErrValaccountUnauthorized.Error()))
Expect(canPropose.Reason).To(Equal(stakertypes.ErrPoolAccountUnauthorized.Error()))

_, txErr := s.RunTx(&bundletypes.MsgSubmitBundleProposal{
Creator: i.VALADDRESS_1_A,
Expand Down
2 changes: 1 addition & 1 deletion x/query/keeper/grpc_query_can_vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ var _ = Describe("grpc_query_can_vote.go", Ordered, func() {
Expect(err).To(BeNil())

Expect(canVote.Possible).To(BeFalse())
Expect(canVote.Reason).To(Equal(stakertypes.ErrValaccountUnauthorized.Error()))
Expect(canVote.Reason).To(Equal(stakertypes.ErrPoolAccountUnauthorized.Error()))

_, txErr := s.RunTx(&bundletypes.MsgVoteBundleProposal{
Creator: i.VALADDRESS_1_A,
Expand Down
53 changes: 27 additions & 26 deletions x/stakers/keeper/exported_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,22 @@ import (
// LeavePool removes a staker from a pool and emits the corresponding event.
// The staker is no longer able to participate in the given pool.
// All points the staker had in that pool are deleted.
func (k Keeper) LeavePool(ctx sdk.Context, staker string, poolId uint64) {
k.RemoveValaccountFromPool(ctx, poolId, staker)
func (k Keeper) LeavePool(ctx sdk.Context, stakerAddress string, poolId uint64) {
k.RemovePoolAccountFromPool(ctx, stakerAddress, poolId)

_ = ctx.EventManager().EmitTypedEvent(&stakertypes.EventLeavePool{
PoolId: poolId,
Staker: staker,
Staker: stakerAddress,
})
}

// GetAllStakerAddressesOfPool returns a list of all stakers
// which have currently a valaccount registered for the given pool
// which have currently a pool account registered for the given pool
// and are therefore allowed to participate in that pool.
func (k Keeper) GetAllStakerAddressesOfPool(ctx sdk.Context, poolId uint64) (stakers []string) {
for _, valaccount := range k.GetAllValaccountsOfPool(ctx, poolId) {
stakers = append(stakers, valaccount.Staker)
poolAccounts := k.GetAllPoolAccountsOfPool(ctx, poolId)
for _, poolAccount := range poolAccounts {
stakers = append(stakers, poolAccount.Staker)
}

return stakers
Expand All @@ -58,11 +59,11 @@ func (k Keeper) GetPaginatedStakersByPoolStake(ctx sdk.Context, pagination *quer
for _, validator := range validators {
address := util.MustAccountAddressFromValAddress(validator.OperatorAddress)

if stakerStatus == querytypes.STAKER_STATUS_PROTOCOL_ACTIVE && len(k.GetValaccountsFromStaker(ctx, address)) == 0 {
if stakerStatus == querytypes.STAKER_STATUS_PROTOCOL_ACTIVE && len(k.GetPoolAccountsFromStaker(ctx, address)) == 0 {
continue
}

if stakerStatus == querytypes.STAKER_STATUS_PROTOCOL_INACTIVE && len(k.GetValaccountsFromStaker(ctx, address)) > 0 {
if stakerStatus == querytypes.STAKER_STATUS_PROTOCOL_INACTIVE && len(k.GetPoolAccountsFromStaker(ctx, address)) > 0 {
continue
}

Expand Down Expand Up @@ -105,7 +106,7 @@ func (k Keeper) GetPaginatedStakersByPoolCount(ctx sdk.Context, pagination *quer
}

sort.Slice(addresses, func(i, j int) bool {
return len(k.GetValaccountsFromStaker(ctx, addresses[i])) > len(k.GetValaccountsFromStaker(ctx, addresses[j]))
return len(k.GetPoolAccountsFromStaker(ctx, addresses[i])) > len(k.GetPoolAccountsFromStaker(ctx, addresses[j]))
})

pageRes, err := arrayPaginationAccumulator(addresses, pagination, accumulator)
Expand All @@ -116,18 +117,18 @@ func (k Keeper) GetPaginatedStakersByPoolCount(ctx sdk.Context, pagination *quer
return pageRes, nil
}

// AssertValaccountAuthorized checks if the given `valaddress` is allowed to vote in pool
// AssertPoolAccountAuthorized checks if the given `pool address` is allowed to vote in pool
// with id `poolId` to vote in favor of `stakerAddress`.
// If the valaddress is not authorized the appropriate error is returned.
// If the pool address is not authorized the appropriate error is returned.
// Otherwise, it returns `nil`
func (k Keeper) AssertValaccountAuthorized(ctx sdk.Context, poolId uint64, stakerAddress string, valaddress string) error {
valaccount, active := k.GetValaccount(ctx, poolId, stakerAddress)
func (k Keeper) AssertPoolAccountAuthorized(ctx sdk.Context, stakerAddress string, poolId uint64, poolAddress string) error {
poolAccount, active := k.GetPoolAccount(ctx, stakerAddress, poolId)
if !active {
return stakertypes.ErrValaccountUnauthorized
return stakertypes.ErrPoolAccountUnauthorized
}

if valaccount.Valaddress != valaddress {
return stakertypes.ErrValaccountUnauthorized
if poolAccount.PoolAddress != poolAddress {
return stakertypes.ErrPoolAccountUnauthorized
}

return nil
Expand Down Expand Up @@ -177,21 +178,21 @@ func (k Keeper) GetValidator(ctx sdk.Context, staker string) (stakingTypes.Valid
}

// GetValidatorPoolCommission returns the commission a validator has inside the pool
func (k Keeper) GetValidatorPoolCommission(ctx sdk.Context, staker string, poolId uint64) math.LegacyDec {
valaccount, _ := k.GetValaccount(ctx, poolId, staker)
return valaccount.Commission
func (k Keeper) GetValidatorPoolCommission(ctx sdk.Context, stakerAddress string, poolId uint64) math.LegacyDec {
poolAccount, _ := k.GetPoolAccount(ctx, stakerAddress, poolId)
return poolAccount.Commission
}

// GetValidatorPoolStake returns stake a validator has actively and at risk inside the pool
func (k Keeper) GetValidatorPoolStake(ctx sdk.Context, staker string, poolId uint64) uint64 {
return k.GetValidatorPoolStakes(ctx, poolId, staker)[staker]
func (k Keeper) GetValidatorPoolStake(ctx sdk.Context, stakerAddress string, poolId uint64) uint64 {
return k.GetValidatorPoolStakes(ctx, poolId, stakerAddress)[stakerAddress]
}

// GetValidatorTotalPoolStake returns the total stake the validator has combined in every pool
func (k Keeper) GetValidatorTotalPoolStake(ctx sdk.Context, staker string) (totalStake uint64) {
valaccounts := k.GetValaccountsFromStaker(ctx, staker)
for _, valaccount := range valaccounts {
totalStake += k.GetValidatorPoolStake(ctx, valaccount.Staker, valaccount.PoolId)
poolAccounts := k.GetPoolAccountsFromStaker(ctx, staker)
for _, poolAccount := range poolAccounts {
totalStake += k.GetValidatorPoolStake(ctx, poolAccount.Staker, poolAccount.PoolId)
}

return
Expand Down Expand Up @@ -244,11 +245,11 @@ func (k Keeper) GetValidatorPoolStakes(ctx sdk.Context, poolId uint64, mustInclu

for _, address := range addresses {
validator, _ := k.GetValidator(ctx, address)
valaccount, _ := k.GetValaccount(ctx, poolId, address)
poolAccount, _ := k.GetPoolAccount(ctx, address, poolId)

// calculate the stake the validator has specifically chosen for this pool
// with his stake fraction
stake := uint64(valaccount.StakeFraction.MulInt(validator.GetBondedTokens()).TruncateInt64())
stake := uint64(poolAccount.StakeFraction.MulInt(validator.GetBondedTokens()).TruncateInt64())

stakes[address] = stake
validators = append(validators, ValidatorStake{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,61 @@ import (

// IncrementPoints increments to Points for a staker in a given pool.
// Returns the amount of the current points (including the current incrementation)
func (k Keeper) IncrementPoints(ctx sdk.Context, poolId uint64, stakerAddress string) uint64 {
valaccount, found := k.GetValaccount(ctx, poolId, stakerAddress)
func (k Keeper) IncrementPoints(ctx sdk.Context, stakerAddress string, poolId uint64) uint64 {
poolAccount, found := k.GetPoolAccount(ctx, stakerAddress, poolId)
if found {
valaccount.Points += 1
k.SetValaccount(ctx, valaccount)
poolAccount.Points += 1
k.SetPoolAccount(ctx, poolAccount)
}
return valaccount.Points
return poolAccount.Points
}

// ResetPoints sets the point count for the staker in the given pool back to zero.
// Returns the amount of points the staker had before the reset.
func (k Keeper) ResetPoints(ctx sdk.Context, poolId uint64, stakerAddress string) (previousPoints uint64) {
valaccount, found := k.GetValaccount(ctx, poolId, stakerAddress)
func (k Keeper) ResetPoints(ctx sdk.Context, stakerAddress string, poolId uint64) (previousPoints uint64) {
poolAccount, found := k.GetPoolAccount(ctx, stakerAddress, poolId)
if found {
previousPoints = valaccount.Points
valaccount.Points = 0
k.SetValaccount(ctx, valaccount)
previousPoints = poolAccount.Points
poolAccount.Points = 0
k.SetPoolAccount(ctx, poolAccount)
}
return
}

// GetAllValaccountsOfPool returns a list of all valaccount
func (k Keeper) GetAllValaccountsOfPool(ctx sdk.Context, poolId uint64) (val []*types.Valaccount) {
// GetAllPoolAccountsOfPool returns a list of all pool accounts
func (k Keeper) GetAllPoolAccountsOfPool(ctx sdk.Context, poolId uint64) (val []*types.PoolAccount) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.ValaccountPrefix)
store := prefix.NewStore(storeAdapter, types.PoolAccountPrefix)

iterator := storeTypes.KVStorePrefixIterator(store, util.GetByteKey(poolId))
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
valaccount := types.Valaccount{}
k.cdc.MustUnmarshal(iterator.Value(), &valaccount)
poolAccount := types.PoolAccount{}
k.cdc.MustUnmarshal(iterator.Value(), &poolAccount)

if valaccount.Valaddress != "" {
val = append(val, &valaccount)
if poolAccount.PoolAddress != "" {
val = append(val, &poolAccount)
}
}

return
}

// GetValaccountsFromStaker returns all pools the staker has valaccounts in
func (k Keeper) GetValaccountsFromStaker(ctx sdk.Context, stakerAddress string) (val []*types.Valaccount) {
// GetPoolAccountsFromStaker returns all pools the staker has pool accounts in
func (k Keeper) GetPoolAccountsFromStaker(ctx sdk.Context, stakerAddress string) (val []*types.PoolAccount) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
storeIndex2 := prefix.NewStore(storeAdapter, types.ValaccountPrefixIndex2)
storeIndex2 := prefix.NewStore(storeAdapter, types.PoolAccountPrefixIndex2)

iterator := storeTypes.KVStorePrefixIterator(storeIndex2, util.GetByteKey(stakerAddress))
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
poolId := binary.BigEndian.Uint64(iterator.Key()[len(stakerAddress) : len(stakerAddress)+8])
valaccount, active := k.GetValaccount(ctx, poolId, stakerAddress)
poolAccount, active := k.GetPoolAccount(ctx, stakerAddress, poolId)

if active {
val = append(val, &valaccount)
val = append(val, &poolAccount)
}
}

Expand All @@ -82,7 +82,7 @@ func (k Keeper) GetValaccountsFromStaker(ctx sdk.Context, stakerAddress string)
// currently participating.
func (k Keeper) GetPoolCount(ctx sdk.Context, stakerAddress string) (poolCount uint64) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
storeIndex2 := prefix.NewStore(storeAdapter, types.ValaccountPrefixIndex2)
storeIndex2 := prefix.NewStore(storeAdapter, types.PoolAccountPrefixIndex2)
iterator := storeTypes.KVStorePrefixIterator(storeIndex2, util.GetByteKey(stakerAddress))
defer iterator.Close()

Expand All @@ -96,29 +96,29 @@ func (k Keeper) GetPoolCount(ctx sdk.Context, stakerAddress string) (poolCount u
// # Raw KV-Store operations #
// #############################

// SetValaccount set a specific Valaccount in the store from its index
func (k Keeper) SetValaccount(ctx sdk.Context, valaccount types.Valaccount) {
// SetPoolAccount set a specific pool account in the store from its index
func (k Keeper) SetPoolAccount(ctx sdk.Context, poolAccount types.PoolAccount) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.ValaccountPrefix)
b := k.cdc.MustMarshal(&valaccount)
store.Set(types.ValaccountKey(
valaccount.PoolId,
valaccount.Staker,
store := prefix.NewStore(storeAdapter, types.PoolAccountPrefix)
b := k.cdc.MustMarshal(&poolAccount)
store.Set(types.PoolAccountKey(
poolAccount.PoolId,
poolAccount.Staker,
), b)

storeIndex2 := prefix.NewStore(storeAdapter, types.ValaccountPrefixIndex2)
storeIndex2.Set(types.ValaccountKeyIndex2(
valaccount.Staker,
valaccount.PoolId,
storeIndex2 := prefix.NewStore(storeAdapter, types.PoolAccountPrefixIndex2)
storeIndex2.Set(types.PoolAccountKeyIndex2(
poolAccount.Staker,
poolAccount.PoolId,
), []byte{})
}

// GetValaccount returns a Valaccount from its index
func (k Keeper) GetValaccount(ctx sdk.Context, poolId uint64, stakerAddress string) (val types.Valaccount, active bool) {
// GetPoolAccount returns a pool account from its index
func (k Keeper) GetPoolAccount(ctx sdk.Context, stakerAddress string, poolId uint64) (val types.PoolAccount, active bool) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.ValaccountPrefix)
store := prefix.NewStore(storeAdapter, types.PoolAccountPrefix)

b := store.Get(types.ValaccountKey(
b := store.Get(types.PoolAccountKey(
poolId,
stakerAddress,
))
Expand All @@ -129,21 +129,21 @@ func (k Keeper) GetValaccount(ctx sdk.Context, poolId uint64, stakerAddress stri
}

k.cdc.MustUnmarshal(b, &val)
return val, val.Valaddress != ""
return val, val.PoolAddress != ""
}

// GetAllValaccounts returns all active valaccounts
func (k Keeper) GetAllValaccounts(ctx sdk.Context) (list []types.Valaccount) {
// GetAllPoolAccounts returns all active pool accounts
func (k Keeper) GetAllPoolAccounts(ctx sdk.Context) (list []types.PoolAccount) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.ValaccountPrefix)
store := prefix.NewStore(storeAdapter, types.PoolAccountPrefix)

iterator := storeTypes.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.Valaccount
var val types.PoolAccount
k.cdc.MustUnmarshal(iterator.Value(), &val)
if val.Valaddress != "" {
if val.PoolAddress != "" {
list = append(list, val)
}
}
Expand Down
Loading

0 comments on commit 8a76d53

Please sign in to comment.