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

feat: enforce max chains during new session #1582

Merged
merged 28 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
23c2a82
feat: enforce max chains during new session
h5law Oct 18, 2023
6bbf7b3
chore: negate max chains check
h5law Oct 18, 2023
a6eeabb
chore: update comment
h5law Oct 18, 2023
7042817
chore: address comments
h5law Oct 20, 2023
d691298
chore: add mock method
h5law Oct 21, 2023
428a2e6
feat: add feature flag check
h5law Oct 21, 2023
f644791
chore: remove flag height
h5law Oct 21, 2023
e436e8c
chore: use module wide codec
h5law Oct 24, 2023
940a686
feat: expand enforce to apps
h5law Oct 24, 2023
e375b26
chore: use app max chains for app enforce
h5law Oct 25, 2023
c7ebac5
chore: address comments
h5law Oct 26, 2023
27b9781
chore: use session context for param check
h5law Oct 31, 2023
d11da25
chore: add missing method to mock
h5law Nov 6, 2023
998f378
feat: allow nodes to join sessions but dont reward them for work done
h5law Nov 6, 2023
ac8be8e
feat: move node chain check from reward to claim
h5law Nov 13, 2023
236f5e7
chore: remove wrapped function and expose MaxChains in posKeeper expe…
h5law Nov 13, 2023
5fcf934
chore: fix error func call
h5law Nov 13, 2023
9a6dd34
feat: enforce at session level by selecting first n chains
h5law Nov 20, 2023
27a7d55
chore: use older slices package
h5law Nov 20, 2023
d387a7c
chore: revert to for loop
h5law Nov 20, 2023
b4941ea
chore: remove first n selection logic
h5law Nov 22, 2023
bd1198e
chore: remove brackets
h5law Nov 22, 2023
6a42b67
chore: accidental formatting debugging
h5law Nov 26, 2023
b539bb1
chore: revert formatting only changes
h5law Nov 29, 2023
28a1ede
chore: address comments
h5law Dec 1, 2023
d0b904a
feat: add NewSessionNodes test and NewSessionNodesAfterMaxChains test
h5law Dec 1, 2023
d3e970f
chore: combine tests
h5law Dec 5, 2023
d0463b3
chore: address final comments
h5law Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion x/nodes/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ func (k Keeper) ClearSessionCache() {

// IterateAndExecuteOverVals - Goes through the validator set and executes handler
func (k Keeper) IterateAndExecuteOverVals(
ctx sdk.Ctx, fn func(index int64, validator exported.ValidatorI) (stop bool)) {
ctx sdk.Ctx, fn func(index int64, validator exported.ValidatorI) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator, _ := sdk.KVStorePrefixIterator(store, types.AllValidatorsKey)
defer iterator.Close()
Expand Down Expand Up @@ -233,6 +234,11 @@ func (k Keeper) AllValidators(ctx sdk.Ctx) (validators []exported.ValidatorI) {
return validators
}

// ValidatorHasLessOrEqualMaxChains - Check if the validator has less or equal than the maximum chains param
func (k Keeper) ValidatorHasLessOrEqualMaxChains(ctx sdk.Ctx, val exported.ValidatorI) bool {
h5law marked this conversation as resolved.
Show resolved Hide resolved
return int64(len(val.GetChains())) <= k.GetParams(ctx).MaximumChains
}

// map of validator addresses to serialized power
type valPowerMap map[[sdk.AddrLen]byte][]byte

Expand Down
3 changes: 2 additions & 1 deletion x/nodes/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/pokt-network/pocket-core/codec"
"strings"
"time"

"github.com/pokt-network/pocket-core/codec"

"github.com/pokt-network/pocket-core/crypto"

h5law marked this conversation as resolved.
Show resolved Hide resolved
sdk "github.com/pokt-network/pocket-core/types"
Expand Down
1 change: 1 addition & 0 deletions x/pocketcore/types/expectedKeepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type PosKeeper interface {
BlocksPerSession(ctx sdk.Ctx) (res int64)
StakeDenom(ctx sdk.Ctx) (res string)
GetValidatorsByChain(ctx sdk.Ctx, networkID string) (validators []sdk.Address, total int)
ValidatorHasLessOrEqualMaxChains(ctx sdk.Ctx, val nodesexported.ValidatorI) bool
}

type AppsKeeper interface {
Expand Down
23 changes: 14 additions & 9 deletions x/pocketcore/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"log"

sdk "github.com/pokt-network/pocket-core/types"
appexported "github.com/pokt-network/pocket-core/x/apps/exported"
"github.com/pokt-network/pocket-core/x/nodes/exported"
"log"
)

// "Session" - The relationship between an application and the pocket network
Expand Down Expand Up @@ -110,11 +111,11 @@ func NewSessionNodes(sessionCtx, ctx sdk.Ctx, keeper PosKeeper, chain string, se
}
sessionNodes = make(SessionNodes, sessionNodesCount)
var node exported.ValidatorI
//unique address map to avoid re-checking a pseudorandomly selected servicer
// unique address map to avoid re-checking a pseudorandomly selected servicer
m := make(map[string]struct{})
// only select the nodesAddrs if not jailed
for i, numOfNodes := 0, 0; ; i++ {
//if this is true we already checked all nodes we got on getValidatorsBychain
// if this is true we already checked all nodes we got on getValidatorsBychain
if len(m) >= totalNodes {
return nil, NewInsufficientNodesError(ModuleName)
}
Expand All @@ -124,17 +125,21 @@ func NewSessionNodes(sessionCtx, ctx sdk.Ctx, keeper PosKeeper, chain string, se
sessionKey = Hash(sessionKey)
// get the node from the array
n := nodesAddrs[index.Int64()]
//if we already have seen this address we continue as it's either on the list or discarded
// if we already have seen this address we continue as it's either on the list or discarded
if _, ok := m[n.String()]; ok {
continue
}
//add the node address to the map
// add the node address to the map
m[n.String()] = struct{}{}

// cross check the node from the `new` or `end` world state
node = keeper.Validator(ctx, n)
// if not found or jailed, don't add to session and continue
if node == nil || node.IsJailed() || !NodeHasChain(chain, node) || sessionNodes.Contains(node.GetAddress()) {
// if not found or jailed or staked chains is too large, don't add to session and continue
if node == nil ||
node.IsJailed() ||
!NodeHasChain(chain, node) ||
sessionNodes.Contains(node.GetAddress()) ||
!keeper.ValidatorHasLessOrEqualMaxChains(ctx, node) {
h5law marked this conversation as resolved.
Show resolved Hide resolved
continue
}
// else add the node to the session
Expand Down Expand Up @@ -267,8 +272,8 @@ func BlockHash(ctx sdk.Context) string {

// "MaxPossibleRelays" - Returns the maximum possible amount of relays for an App on a sessions
func MaxPossibleRelays(app appexported.ApplicationI, sessionNodeCount int64) sdk.BigInt {
//GetMaxRelays Max value is bound to math.MaxUint64,
//current worse case is 1 chain and 5 nodes per session with a result of 3689348814741910323 which can be used safely as int64
// GetMaxRelays Max value is bound to math.MaxUint64,
// current worse case is 1 chain and 5 nodes per session with a result of 3689348814741910323 which can be used safely as int64
return app.GetMaxRelays().ToDec().Quo(sdk.NewDec(int64(len(app.GetChains())))).Quo(sdk.NewDec(sessionNodeCount)).RoundInt()
}

Expand Down