Skip to content

Commit

Permalink
wip: refactor:
Browse files Browse the repository at this point in the history
- Isolate TLM processors from the tokenomics keeper
- Restructure TLM processors to avoid non-deterministic execution
- Consolidate and postpone ALL state modification until the end of settlement
  • Loading branch information
bryanchriswhite committed Oct 22, 2024
1 parent c4d8449 commit 2df9645
Show file tree
Hide file tree
Showing 15 changed files with 1,099 additions and 585 deletions.
4 changes: 2 additions & 2 deletions tests/integration/tokenomics/relay_mining_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func TestComputeNewDifficultyHash_RewardsReflectWorkCompleted(t *testing.T) {
// Calling SettlePendingClaims calls ProcessTokenLogicModules behind the scenes
settledResult, expiredResult, err := keepers.Keeper.SettlePendingClaims(sdkCtx)
require.NoError(t, err)
require.Equal(t, 1, int(settledResult.NumClaims))
require.Equal(t, 0, int(expiredResult.NumClaims))
require.Equal(t, 1, int(settledResult.GetNumClaims()))
require.Equal(t, 0, int(expiredResult.GetNumClaims()))

// Update the relay mining difficulty
_, err = keepers.Keeper.UpdateRelayMiningDifficulty(sdkCtx, map[string]uint64{service.Id: claimNumRelays})
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/tokenomics/token_logic_modules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package integration

// TODO_TEST(@bryanchriswhite): All TLM processors are commutative

// TODO_TEST(@bryanchriswhite): Settlement proceeds in the face of errors
// - Does not block settling of other claims in the same session
// - Does not block setting subsequent sessions
7 changes: 7 additions & 0 deletions x/tokenomics/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/pokt-network/poktroll/pkg/client"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
tlm "github.com/pokt-network/poktroll/x/tokenomics/token_logic_module"
"github.com/pokt-network/poktroll/x/tokenomics/types"
)

Expand All @@ -33,6 +34,8 @@ type Keeper struct {
serviceKeeper types.ServiceKeeper

sharedQuerier client.SharedQueryClient

tokenLogicModuleProcessors []tlm.TokenLogicModuleProcessor
}

func NewKeeper(
Expand All @@ -56,6 +59,8 @@ func NewKeeper(

sharedQuerier := prooftypes.NewSharedKeeperQueryClient(sharedKeeper, sessionKeeper)

tlmProcessors := tlm.NewDefaultProcessors(authority)

return Keeper{
cdc: cdc,
storeService: storeService,
Expand All @@ -72,6 +77,8 @@ func NewKeeper(
serviceKeeper: serviceKeeper,

sharedQuerier: sharedQuerier,

tokenLogicModuleProcessors: tlmProcessors,
}
}

Expand Down
68 changes: 34 additions & 34 deletions x/tokenomics/keeper/keeper_settle_pending_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
)

const (
testServiceId = "svc1"
supplierStake = 1000000 // uPOKT
testServiceId = "svc1"
supplierStakeAmt = 1000000 // uPOKT
)

func init() {
Expand Down Expand Up @@ -107,7 +107,7 @@ func (s *TestSuite) SetupTest() {
}
s.keepers.SetService(s.ctx, service)

supplierStake := types.NewCoin("upokt", math.NewInt(supplierStake))
supplierStake := types.NewCoin("upokt", math.NewInt(supplierStakeAmt))
supplier := sharedtypes.Supplier{
OwnerAddress: supplierOwnerAddr,
OperatorAddress: supplierOwnerAddr,
Expand Down Expand Up @@ -215,12 +215,12 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimPendingBeforeSettlement() {
// Expectations: No claims should be settled because the session is still ongoing
blockHeight := s.claim.SessionHeader.SessionEndBlockHeight - 2 // session is still active
sdkCtx := cosmostypes.UnwrapSDKContext(ctx).WithBlockHeight(blockHeight)
settledResult, expiredResult, err := s.keepers.SettlePendingClaims(sdkCtx)
settledResults, expiredResults, err := s.keepers.SettlePendingClaims(sdkCtx)
require.NoError(t, err)

// Check that no claims were settled or expired.
require.Equal(t, uint64(0), settledResult.NumClaims)
require.Equal(t, uint64(0), expiredResult.NumClaims)
require.Equal(t, uint64(0), settledResults.GetNumClaims())
require.Equal(t, uint64(0), expiredResults.GetNumClaims())

// Validate that one claim still remains.
claims := s.keepers.GetAllClaims(ctx)
Expand All @@ -238,12 +238,12 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimPendingBeforeSettlement() {
// 2. Settle pending claims just after the session ended.
// Expectations: Claims should not be settled because the proof window hasn't closed yet.
sdkCtx = sdkCtx.WithBlockHeight(blockHeight)
settledResult, expiredResult, err = s.keepers.SettlePendingClaims(sdkCtx)
settledResults, expiredResults, err = s.keepers.SettlePendingClaims(sdkCtx)
require.NoError(t, err)

// Check that no claims were settled or expired.
require.Equal(t, uint64(0), settledResult.NumClaims)
require.Equal(t, uint64(0), expiredResult.NumClaims)
require.Equal(t, uint64(0), settledResults.GetNumClaims())
require.Equal(t, uint64(0), expiredResults.GetNumClaims())

// Validate that the claim still exists
claims = s.keepers.GetAllClaims(ctx)
Expand All @@ -264,7 +264,7 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimExpired_ProofRequiredAndNotProv

// Set the proof missing penalty to half the supplier's stake so it is not
// unstaked when being slashed.
belowStakeAmountProofMissingPenalty := sdk.NewCoin(volatile.DenomuPOKT, math.NewInt(supplierStake/2))
belowStakeAmountProofMissingPenalty := sdk.NewCoin(volatile.DenomuPOKT, math.NewInt(supplierStakeAmt/2))

// Set the proof parameters such that s.claim requires a proof because:
// - proof_request_probability is 0%
Expand All @@ -285,12 +285,12 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimExpired_ProofRequiredAndNotProv
sessionEndHeight := s.claim.SessionHeader.SessionEndBlockHeight
blockHeight := sharedtypes.GetProofWindowCloseHeight(&sharedParams, sessionEndHeight)
sdkCtx := cosmostypes.UnwrapSDKContext(ctx).WithBlockHeight(blockHeight)
settledResult, expiredResult, err := s.keepers.SettlePendingClaims(sdkCtx)
settledResults, expiredResults, err := s.keepers.SettlePendingClaims(sdkCtx)
require.NoError(t, err)

// Validate claim settlement results
require.Equal(t, uint64(0), settledResult.NumClaims) // 0 claims settled
require.Equal(t, uint64(1), expiredResult.NumClaims) // 1 claim expired
require.Equal(t, uint64(0), settledResults.GetNumClaims()) // 0 claims settled
require.Equal(t, uint64(1), expiredResults.GetNumClaims()) // 1 claim expired

// Validate that no claims remain.
claims := s.keepers.GetAllClaims(ctx)
Expand All @@ -302,7 +302,7 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimExpired_ProofRequiredAndNotProv
// remaining stake that is above the minimum stake (i.e. new_stake == prev_stake / 2).
slashedSupplier, supplierFound := s.keepers.GetSupplier(sdkCtx, s.claim.SupplierOperatorAddress)
require.True(t, supplierFound)
require.Equal(t, math.NewInt(supplierStake/2), slashedSupplier.Stake.Amount)
require.Equal(t, math.NewInt(supplierStakeAmt/2), slashedSupplier.Stake.Amount)
require.Equal(t, uint64(0), slashedSupplier.UnstakeSessionEndHeight)

events := sdkCtx.EventManager().Events()
Expand Down Expand Up @@ -366,8 +366,8 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimSettled_ProofRequiredAndProvide
require.NoError(t, err)

// Validate claim settlement results
require.Equal(t, uint64(1), settledResult.NumClaims) // 1 claim settled
require.Equal(t, uint64(0), expiredResult.NumClaims) // 0 claims expired
require.Equal(t, uint64(1), settledResult.GetNumClaims()) // 1 claim settled
require.Equal(t, uint64(0), expiredResult.GetNumClaims()) // 0 claims expired

// Validate that no claims remain.
claims := s.keepers.GetAllClaims(ctx)
Expand Down Expand Up @@ -399,7 +399,7 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimExpired_ProofRequired_InvalidOn
proofParams.ProofRequestProbability = 1
// Set the proof missing penalty to half the supplier's stake so it is not
// unstaked when being slashed.
belowStakeAmountProofMissingPenalty := sdk.NewCoin(volatile.DenomuPOKT, math.NewInt(supplierStake/2))
belowStakeAmountProofMissingPenalty := sdk.NewCoin(volatile.DenomuPOKT, math.NewInt(supplierStakeAmt/2))
proofParams.ProofMissingPenalty = &belowStakeAmountProofMissingPenalty
err := s.keepers.ProofKeeper.SetParams(ctx, proofParams)
require.NoError(t, err)
Expand All @@ -418,12 +418,12 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimExpired_ProofRequired_InvalidOn
sessionEndHeight := s.claim.SessionHeader.SessionEndBlockHeight
blockHeight := sharedtypes.GetProofWindowCloseHeight(&sharedParams, sessionEndHeight)
sdkCtx := cosmostypes.UnwrapSDKContext(ctx).WithBlockHeight(blockHeight)
settledResult, expiredResult, err := s.keepers.SettlePendingClaims(sdkCtx)
settledResults, expiredResults, err := s.keepers.SettlePendingClaims(sdkCtx)
require.NoError(t, err)

// Validate claim settlement results
require.Equal(t, uint64(0), settledResult.NumClaims) // 0 claims settled
require.Equal(t, uint64(1), expiredResult.NumClaims) // 1 claim expired
require.Equal(t, uint64(0), settledResults.GetNumClaims()) // 0 claims settled
require.Equal(t, uint64(1), expiredResults.GetNumClaims()) // 1 claim expired

// Validate that no claims remain.
claims := s.keepers.GetAllClaims(ctx)
Expand All @@ -436,7 +436,7 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimExpired_ProofRequired_InvalidOn
// Slashing should have occurred without unstaking the supplier.
slashedSupplier, supplierFound := s.keepers.GetSupplier(sdkCtx, s.claim.SupplierOperatorAddress)
require.True(t, supplierFound)
require.Equal(t, math.NewInt(supplierStake/2), slashedSupplier.Stake.Amount)
require.Equal(t, math.NewInt(supplierStakeAmt/2), slashedSupplier.Stake.Amount)
require.Equal(t, uint64(0), slashedSupplier.UnstakeSessionEndHeight)

// Confirm an expiration event was emitted
Expand Down Expand Up @@ -495,12 +495,12 @@ func (s *TestSuite) TestClaimSettlement_ClaimSettled_ProofRequiredAndProvided_Vi
sessionEndHeight := s.claim.SessionHeader.SessionEndBlockHeight
blockHeight := sharedtypes.GetProofWindowCloseHeight(&sharedParams, sessionEndHeight)
sdkCtx := cosmostypes.UnwrapSDKContext(ctx).WithBlockHeight(blockHeight)
settledResult, expiredResult, err := s.keepers.SettlePendingClaims(sdkCtx)
settledResults, expiredResults, err := s.keepers.SettlePendingClaims(sdkCtx)
require.NoError(t, err)

// Validate claim settlement results
require.Equal(t, uint64(1), settledResult.NumClaims) // 1 claim settled
require.Equal(t, uint64(0), expiredResult.NumClaims) // 0 claims expired
require.Equal(t, uint64(1), settledResults.GetNumClaims()) // 1 claim settled
require.Equal(t, uint64(0), expiredResults.GetNumClaims()) // 0 claims expired

// Validate that no claims remain.
claims := s.keepers.GetAllClaims(ctx)
Expand Down Expand Up @@ -550,12 +550,12 @@ func (s *TestSuite) TestSettlePendingClaims_Settles_WhenAProofIsNotRequired() {
sessionEndHeight := s.claim.SessionHeader.SessionEndBlockHeight
blockHeight := sharedtypes.GetProofWindowCloseHeight(&sharedParams, sessionEndHeight)
sdkCtx := cosmostypes.UnwrapSDKContext(ctx).WithBlockHeight(blockHeight)
settledResult, expiredResult, err := s.keepers.SettlePendingClaims(sdkCtx)
settledResults, expiredResults, err := s.keepers.SettlePendingClaims(sdkCtx)
require.NoError(t, err)

// Check that one claim was settled.
require.Equal(t, uint64(1), settledResult.NumClaims) // 1 claim settled
require.Equal(t, uint64(0), expiredResult.NumClaims) // 0 claims expired
require.Equal(t, uint64(1), settledResults.GetNumClaims()) // 1 claim settled
require.Equal(t, uint64(0), expiredResults.GetNumClaims()) // 0 claims expired

// Validate that no claims remain.
claims := s.keepers.GetAllClaims(ctx)
Expand Down Expand Up @@ -647,14 +647,14 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimPendingAfterSettlement() {
// Expectations: No claims should be settled because the session is still ongoing
blockHeight := sharedtypes.GetProofWindowCloseHeight(&sharedParams, sessionOneEndHeight)
sdkCtx = sdkCtx.WithBlockHeight(blockHeight)
settledResult, expiredResult, err := s.keepers.SettlePendingClaims(sdkCtx)
settledResults, expiredResults, err := s.keepers.SettlePendingClaims(sdkCtx)
require.NoError(t, err)

// Check that one claim was settled.
require.Equal(t, uint64(1), settledResult.NumClaims)
require.Equal(t, uint64(1), settledResults.GetNumClaims())

// Validate that no claims expired.
require.Equal(t, uint64(0), expiredResult.NumClaims)
require.Equal(t, uint64(0), expiredResults.GetNumClaims())

// Validate that one claim still remains.
claims = s.keepers.GetAllClaims(ctx)
Expand All @@ -666,12 +666,12 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimPendingAfterSettlement() {
// 2. Settle pending claims just after the session ended.
// Expectations: Claims should not be settled because the proof window hasn't closed yet.
sdkCtx = sdkCtx.WithBlockHeight(blockHeight)
settledResult, expiredResult, err = s.keepers.SettlePendingClaims(sdkCtx)
settledResults, expiredResults, err = s.keepers.SettlePendingClaims(sdkCtx)
require.NoError(t, err)

// Check that no claims were settled or expired.
require.Equal(t, uint64(0), settledResult.NumClaims)
require.Equal(t, uint64(0), expiredResult.NumClaims)
require.Equal(t, uint64(0), settledResults.GetNumClaims())
require.Equal(t, uint64(0), expiredResults.GetNumClaims())

// Validate that the claim still exists
claims = s.keepers.GetAllClaims(ctx)
Expand All @@ -698,7 +698,7 @@ func (s *TestSuite) TestSettlePendingClaims_ClaimExpired_SupplierUnstaked() {
proofParams.ProofRequirementThreshold = &proofRequirementThreshold
// Set the proof missing penalty to be equal to the supplier's stake to make
// its stake below the minimum stake requirement and trigger an unstake.
proofParams.ProofMissingPenalty = &sdk.Coin{Denom: volatile.DenomuPOKT, Amount: math.NewInt(supplierStake)}
proofParams.ProofMissingPenalty = &sdk.Coin{Denom: volatile.DenomuPOKT, Amount: math.NewInt(supplierStakeAmt)}
err = s.keepers.ProofKeeper.SetParams(ctx, proofParams)
require.NoError(t, err)

Expand Down
Loading

0 comments on commit 2df9645

Please sign in to comment.