Skip to content

Commit

Permalink
[C&P] Fix Unbonding slashed suppliers (#891)
Browse files Browse the repository at this point in the history
## Summary

This PR fixes a few bugs that were dependent to each other:
1. Gracefully unbond suppliers that have 0upokt due to off-stake
slashing.
2. Fix access to an expired session tree on the realy miner
3. Fix proof block hash seed used on chain.

@okdas , One case I didn't manage to reproduce is having the application
module account go to `0`. Please tell me if you encounter it again.

## Issue

- #841 

## Type of change

Select one or more from the following:

- [ ] New feature, functionality or library
- [ ] Consensus breaking; add the `consensus-breaking` label if so. See
#791 for details
- [x] Bug fix
- [ ] Code health or cleanup
- [ ] Documentation
- [ ] Other (specify)

## Testing

- [ ] **Documentation**: `make docusaurus_start`; only needed if you
make doc changes
- [x] **Unit Tests**: `make go_develop_and_test`
- [x] **LocalNet E2E Tests**: `make test_e2e`
- [ ] **DevNet E2E Tests**: Add the `devnet-test-e2e` label to the PR.

## Sanity Checklist

- [x] I have tested my changes using the available tooling
- [x] I have commented my code
- [x] I have performed a self-review of my own code; both comments &
source code
  • Loading branch information
red-0ne authored Oct 23, 2024
1 parent be96b75 commit 0663679
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
15 changes: 13 additions & 2 deletions pkg/relayer/session/sessiontree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package session
import (
"bytes"
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"sync"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/pokt-network/smt/kvstore/pebble"

"github.com/pokt-network/poktroll/pkg/crypto/protocol"
"github.com/pokt-network/poktroll/pkg/polylog"
"github.com/pokt-network/poktroll/pkg/relayer"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
)
Expand All @@ -27,6 +29,8 @@ var _ relayer.SessionTree = (*sessionTree)(nil)
// default value for this should be -1, implying "unlimited".
// Ref discussion: https://github.com/pokt-network/poktroll/pull/755#discussion_r1737287860
type sessionTree struct {
logger polylog.Logger

// sessionMu is a mutex used to protect sessionTree operations from concurrent access.
sessionMu *sync.Mutex

Expand Down Expand Up @@ -273,8 +277,15 @@ func (st *sessionTree) Delete() error {
// This was intentionally removed to lower the IO load.
// When the database is closed, it is deleted it from disk right away.

if err := st.treeStore.Stop(); err != nil {
return err
if st.treeStore != nil {
if err := st.treeStore.Stop(); err != nil {
return err
}
} else {
st.logger.With(
"claim_root", fmt.Sprintf("%x", st.GetClaimRoot()),
"session_id", st.GetSessionHeader().SessionId,
).Info().Msg("KVStore is already stopped")
}

// Delete the KVStore from disk
Expand Down
14 changes: 8 additions & 6 deletions x/proof/keeper/msg_server_submit_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ func (k Keeper) ProofRequirementForClaim(ctx context.Context, claim *types.Claim
}

// Hash of block when proof submission is allowed.
earliestProofCommitBlockHash, err := k.getEarliestSupplierProofCommitBlockHash(ctx, claim)
proofRequirementSeedBlockHash, err := k.getProofRequirementSeedBlockHash(ctx, claim)
if err != nil {
return requirementReason, err
}

// The probability that a proof is required.
proofRequirementSampleValue, err := claim.GetProofRequirementSampleValue(earliestProofCommitBlockHash)
proofRequirementSampleValue, err := claim.GetProofRequirementSampleValue(proofRequirementSeedBlockHash)
if err != nil {
return requirementReason, err
}
Expand Down Expand Up @@ -292,9 +292,9 @@ func (k Keeper) ProofRequirementForClaim(ctx context.Context, claim *types.Claim
return requirementReason, nil
}

// getEarliestSupplierProofCommitBlockHash returns the block hash of the earliest
// block at which a claim may have its proof committed.
func (k Keeper) getEarliestSupplierProofCommitBlockHash(
// getProofRequirementSeedBlockHash returns the block hash of the seed block for
// the proof requirement probabilistic check.
func (k Keeper) getProofRequirementSeedBlockHash(
ctx context.Context,
claim *types.Claim,
) (blockHash []byte, err error) {
Expand All @@ -318,5 +318,7 @@ func (k Keeper) getEarliestSupplierProofCommitBlockHash(
supplierOperatorAddress,
)

return k.sessionKeeper.GetBlockHash(ctx, earliestSupplierProofCommitHeight), nil
// The proof requirement seed block is the last block of the session, and it is
// the block that is before the earliest block at which a proof can be committed.
return k.sessionKeeper.GetBlockHash(ctx, earliestSupplierProofCommitHeight-1), nil
}
23 changes: 14 additions & 9 deletions x/supplier/keeper/unbond_suppliers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,20 @@ func (k Keeper) EndBlockerUnbondSuppliers(ctx context.Context) error {
return err
}

// Send the coins from the supplier pool back to the supplier.
if err = k.bankKeeper.SendCoinsFromModuleToAccount(
ctx, suppliertypes.ModuleName, ownerAddress, []cosmostypes.Coin{*supplier.Stake},
); err != nil {
logger.Error(fmt.Sprintf(
"could not send %s coins from module %s to account %s due to %s",
supplier.Stake.String(), suppliertypes.ModuleName, ownerAddress, err,
))
return err
// If the supplier stake is 0 due to slashing, then do not move 0 coins
// to its account.
// Coin#IsPositive returns false if the coin is 0.
if supplier.Stake.IsPositive() {
// Send the coins from the supplier pool back to the supplier.
if err = k.bankKeeper.SendCoinsFromModuleToAccount(
ctx, suppliertypes.ModuleName, ownerAddress, []cosmostypes.Coin{*supplier.Stake},
); err != nil {
logger.Error(fmt.Sprintf(
"could not send %s coins from module %s to account %s due to %s",
supplier.Stake.String(), suppliertypes.ModuleName, ownerAddress, err,
))
return err
}
}

// Remove the supplier from the store.
Expand Down
1 change: 0 additions & 1 deletion x/tokenomics/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0663679

Please sign in to comment.