Skip to content

Commit

Permalink
[Cleanup] Wrap errors consistently via error types. (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
h5law authored Mar 12, 2024
1 parent f7b2341 commit 9329a75
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
20 changes: 6 additions & 14 deletions pkg/relayer/proxy/relay_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package proxy
import (
"context"

sdkerrors "cosmossdk.io/errors"
ring_secp256k1 "github.com/athanorlabs/go-dleq/secp256k1"
ring "github.com/noot/ring-go"

Expand Down Expand Up @@ -34,23 +33,20 @@ func (rp *relayerProxy) VerifyRelayRequest(
}
signature := relayRequest.Meta.Signature
if signature == nil {
return sdkerrors.Wrapf(
ErrRelayerProxyInvalidRelayRequest,
return ErrRelayerProxyInvalidRelayRequest.Wrapf(
"missing signature from relay request: %v", relayRequest,
)
}

ringSig := new(ring.RingSig)
if err := ringSig.Deserialize(ring_secp256k1.NewCurve(), signature); err != nil {
return sdkerrors.Wrapf(
ErrRelayerProxyInvalidRelayRequestSignature,
return ErrRelayerProxyInvalidRelayRequestSignature.Wrapf(
"error deserializing ring signature: %v", err,
)
}

if relayRequest.Meta.SessionHeader.ApplicationAddress == "" {
return sdkerrors.Wrap(
ErrRelayerProxyInvalidRelayRequest,
return ErrRelayerProxyInvalidRelayRequest.Wrap(
"missing application address from relay request",
)
}
Expand All @@ -59,16 +55,14 @@ func (rp *relayerProxy) VerifyRelayRequest(
appAddress := relayRequest.Meta.SessionHeader.ApplicationAddress
appRing, err := rp.ringCache.GetRingForAddress(ctx, appAddress)
if err != nil {
return sdkerrors.Wrapf(
ErrRelayerProxyInvalidRelayRequest,
return ErrRelayerProxyInvalidRelayRequest.Wrapf(
"error getting ring for application address %s: %v", appAddress, err,
)
}

// verify the ring signature against the ring
if !ringSig.Ring().Equals(appRing) {
return sdkerrors.Wrapf(
ErrRelayerProxyInvalidRelayRequestSignature,
return ErrRelayerProxyInvalidRelayRequestSignature.Wrapf(
"ring signature does not match ring for application address %s", appAddress,
)
}
Expand All @@ -81,8 +75,7 @@ func (rp *relayerProxy) VerifyRelayRequest(

// verify the relay request's signature
if valid := ringSig.Verify(requestSignableBz); !valid {
return sdkerrors.Wrapf(
ErrRelayerProxyInvalidRelayRequestSignature,
return ErrRelayerProxyInvalidRelayRequestSignature.Wrapf(
"invalid ring signature",
)
}
Expand All @@ -107,7 +100,6 @@ func (rp *relayerProxy) VerifyRelayRequest(
service.Id,
sessionBlockHeight,
)

if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/relayer/proxy/synchronous.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/url"
"time"

sdkerrors "cosmossdk.io/errors"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"

"github.com/pokt-network/poktroll/pkg/polylog"
Expand Down Expand Up @@ -177,8 +176,7 @@ func (sync *synchronousRPCServer) ServeHTTP(writer http.ResponseWriter, request
}

if relayRequest.Meta == nil {
err = sdkerrors.Wrapf(
ErrRelayerProxyInvalidRelayRequest,
err = ErrRelayerProxyInvalidRelayRequest.Wrapf(
"missing meta from relay request: %v", relayRequest,
)
sync.replyWithError(ctx, relayRequest.Payload, writer, sync.proxyConfig.ProxyName, supplierService.Id, err)
Expand Down
4 changes: 1 addition & 3 deletions x/service/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
"fmt"

sdkerrors "cosmossdk.io/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

Expand Down Expand Up @@ -48,8 +47,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
func (p Params) Validate() error {
// TODO(@h5law): Look into better validation
if p.AddServiceFee < DefaultAddServiceFee {
return sdkerrors.Wrapf(
ErrServiceInvalidServiceFee,
return ErrServiceInvalidServiceFee.Wrapf(
"AddServiceFee param %d uPOKT: got %d",
DefaultAddServiceFee,
p.AddServiceFee,
Expand Down
12 changes: 7 additions & 5 deletions x/session/keeper/session_hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
"fmt"
"math/rand"

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
_ "golang.org/x/crypto/sha3"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pokt-network/poktroll/x/session/types"
sharedhelpers "github.com/pokt-network/poktroll/x/shared/helpers"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
Expand Down Expand Up @@ -98,8 +97,7 @@ func (k Keeper) hydrateSessionMetadata(ctx context.Context, sh *sessionHydrator)

lastCommittedBlockHeight := sdk.UnwrapSDKContext(ctx).BlockHeight()
if sh.blockHeight > lastCommittedBlockHeight {
return sdkerrors.Wrapf(
types.ErrSessionHydration,
return types.ErrSessionHydration.Wrapf(
"block height %d is ahead of the last committed block height %d",
sh.blockHeight, lastCommittedBlockHeight,
)
Expand Down Expand Up @@ -212,7 +210,11 @@ func (k Keeper) hydrateSessionSuppliers(ctx context.Context, sh *sessionHydrator
// TODO_INVESTIGATE: We are using a `Go` native implementation for a pseudo-random number generator. In order
// for it to be language agnostic, a general purpose algorithm MUST be used.
// pseudoRandomSelection returns a random subset of the candidates.
func pseudoRandomSelection(candidates []*sharedtypes.Supplier, numTarget int, sessionIDBz []byte) []*sharedtypes.Supplier {
func pseudoRandomSelection(
candidates []*sharedtypes.Supplier,
numTarget int,
sessionIDBz []byte,
) []*sharedtypes.Supplier {
// Take the first 8 bytes of sessionId to use as the seed
// NB: There is specific reason why `BigEndian` was chosen over `LittleEndian` in this specific context.
seed := int64(binary.BigEndian.Uint64(sha3Hash(sessionIDBz)[:8]))
Expand Down

0 comments on commit 9329a75

Please sign in to comment.