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

[TODO] chore: update smt.MerkleRoot#Sum() error handling #672

Merged
merged 18 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion x/proof/types/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (claim *Claim) GetNumComputeUnits() (numComputeUnits uint64, err error) {
}
}()

return smt.MerkleRoot(claim.GetRootHash()).Sum(), nil
return smt.MerkleRoot(claim.GetRootHash()).Sum()
}

// GetNumRelays returns the number of relays for a given claim
Expand Down
31 changes: 19 additions & 12 deletions x/tokenomics/keeper/settle_session_accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"fmt"

math "cosmossdk.io/math"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/pokt-network/smt"

"github.com/pokt-network/poktroll/telemetry"
Expand All @@ -26,7 +27,7 @@ import (
func (k Keeper) SettleSessionAccounting(
ctx context.Context,
claim *prooftypes.Claim,
) error {
) (err error) {
logger := k.Logger().With("method", "SettleSessionAccounting")

settlementAmt := sdk.NewCoin("upokt", math.NewInt(0))
Expand Down Expand Up @@ -68,13 +69,10 @@ func (k Keeper) SettleSessionAccounting(
// Retrieve the sum of the root as a proxy into the amount of work done
root := (smt.MerkleRoot)(claim.GetRootHash())

// TODO_BLOCKER(@Olshansk): This check should be the responsibility of the SMST package
// since it's used to get compute units from the root hash.
if root == nil || len(root) != smt.SmstRootSizeBytes {
logger.Error(fmt.Sprintf("received an invalid root hash of size: %d", len(root)))
return types.ErrTokenomicsRootHashInvalid
claimComputeUnits, err := root.Sum()
if err != nil {
return types.ErrTokenomicsRootHashInvalid.Wrapf("%v", err)
}
claimComputeUnits := root.Sum()

// Helpers for logging the same metadata throughout this function calls
logger = logger.With(
Expand All @@ -96,7 +94,11 @@ func (k Keeper) SettleSessionAccounting(
logger.Info(fmt.Sprintf("About to start settling claim for %d compute units", claimComputeUnits))

// Calculate the amount of tokens to mint & burn
settlementAmt = k.getCoinFromComputeUnits(ctx, root)
settlementAmt, err = k.getCoinFromComputeUnits(ctx, root)
if err != nil {
return err
}

settlementAmtuPOKT := sdk.NewCoins(settlementAmt)

logger.Info(fmt.Sprintf(
Expand Down Expand Up @@ -177,10 +179,15 @@ func (k Keeper) SettleSessionAccounting(
return nil
}

func (k Keeper) getCoinFromComputeUnits(ctx context.Context, root smt.MerkleRoot) sdk.Coin {
func (k Keeper) getCoinFromComputeUnits(ctx context.Context, root smt.MerkleRoot) (sdk.Coin, error) {
// Retrieve the existing tokenomics params
params := k.GetParams(ctx)

upokt := math.NewInt(int64(root.Sum() * params.ComputeUnitsToTokensMultiplier))
return sdk.NewCoin("upokt", upokt)
sum, err := root.Sum()
if err != nil {
return sdk.Coin{}, err
}

upokt := math.NewInt(int64(sum * params.ComputeUnitsToTokensMultiplier))
return sdk.NewCoin("upokt", upokt), nil
}
Loading