Skip to content

Commit

Permalink
[Relayminer] refactor: query min_relay_difficulty_bits param (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite authored Jul 5, 2024
1 parent 6c3dd65 commit e119984
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 28 deletions.
20 changes: 19 additions & 1 deletion pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//go:generate mockgen -destination=../../testutil/mockclient/supplier_query_client_mock.go -package=mockclient . SupplierQueryClient
//go:generate mockgen -destination=../../testutil/mockclient/session_query_client_mock.go -package=mockclient . SessionQueryClient
//go:generate mockgen -destination=../../testutil/mockclient/shared_query_client_mock.go -package=mockclient . SharedQueryClient
//go:generate mockgen -destination=../../testutil/mockclient/proof_query_client_mock.go -package=mockclient . ProofQueryClient
//go:generate mockgen -destination=../../testutil/mockclient/cosmos_tx_builder_mock.go -package=mockclient github.com/cosmos/cosmos-sdk/client TxBuilder
//go:generate mockgen -destination=../../testutil/mockclient/cosmos_keyring_mock.go -package=mockclient github.com/cosmos/cosmos-sdk/crypto/keyring Keyring
//go:generate mockgen -destination=../../testutil/mockclient/cosmos_client_mock.go -package=mockclient github.com/cosmos/cosmos-sdk/client AccountRetriever
Expand Down Expand Up @@ -284,7 +285,7 @@ type SessionQueryClient interface {
}

// SharedQueryClient defines an interface that enables the querying of the
// on-chain shared module information.
// on-chain shared module params.
type SharedQueryClient interface {
// GetParams queries the chain for the current shared module parameters.
GetParams(ctx context.Context) (*sharedtypes.Params, error)
Expand Down Expand Up @@ -313,3 +314,20 @@ type SharedQueryClient interface {
type BlockQueryClient interface {
Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error)
}

// ProofParams is a go interface type which corresponds to the poktroll.proof.Params
// protobuf message. Since the generated go types don't include interface types, this
// is necessary to prevent dependency cycles.
type ProofParams interface {
GetMinRelayDifficultyBits() uint64
GetProofRequestProbability() float32
GetProofRequirementThreshold() uint64
GetProofMissingPenalty() *cosmostypes.Coin
}

// ProofQueryClient defines an interface that enables the querying of the
// on-chain proof module params.
type ProofQueryClient interface {
// GetParams queries the chain for the current shared module parameters.
GetParams(ctx context.Context) (ProofParams, error)
}
50 changes: 50 additions & 0 deletions pkg/client/query/proofquerier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package query

import (
"context"

"cosmossdk.io/depinject"
"github.com/cosmos/gogoproto/grpc"

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

// proofQuerier is a wrapper around the prooftypes.QueryClient that enables the
// querying of on-chain proof module params.
type proofQuerier struct {
clientConn grpc.ClientConn
proofQuerier prooftypes.QueryClient
}

// NewProofQuerier returns a new instance of a client.ProofQueryClient by
// injecting the dependecies provided by the depinject.Config.
//
// Required dependencies:
// - grpc.ClientConn
func NewProofQuerier(deps depinject.Config) (client.ProofQueryClient, error) {
querier := &proofQuerier{}

if err := depinject.Inject(
deps,
&querier.clientConn,
); err != nil {
return nil, err
}

querier.proofQuerier = prooftypes.NewQueryClient(querier.clientConn)

return querier, nil
}

// GetParams queries the chain for the current proof module parameters.
func (pq *proofQuerier) GetParams(
ctx context.Context,
) (client.ProofParams, error) {
req := &prooftypes.QueryParamsRequest{}
res, err := pq.proofQuerier.Params(ctx, req)
if err != nil {
return nil, err
}
return &res.Params, nil
}
18 changes: 18 additions & 0 deletions pkg/deps/config/suppliers.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,24 @@ func NewSupplySharedQueryClientFn() SupplierFn {
}
}

// NewSupplyProofQueryClientFn returns a function which constructs a
// ProofQueryClient instance and returns a new depinject.Config which
// is supplied with the given deps and the new ProofQueryClient.
func NewSupplyProofQueryClientFn() SupplierFn {
return func(
_ context.Context,
deps depinject.Config,
_ *cobra.Command,
) (depinject.Config, error) {
proofQuerier, err := query.NewProofQuerier(deps)
if err != nil {
return nil, err
}

return depinject.Configs(deps, depinject.Supply(proofQuerier)), nil
}
}

// newSupplyTxClientFn returns a new depinject.Config which is supplied with
// the given deps and the new TxClient.
func newSupplyTxClientsFn(ctx context.Context, deps depinject.Config, signingKeyName string) (depinject.Config, error) {
Expand Down
13 changes: 7 additions & 6 deletions pkg/relayer/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ func setupRelayerDependencies(

supplierFuncs := []config.SupplierFn{
config.NewSupplyLoggerFromCtx(ctx),
config.NewSupplyEventsQueryClientFn(queryNodeRPCUrl), // leaf
config.NewSupplyBlockQueryClientFn(queryNodeRPCUrl), // leaf
config.NewSupplyBlockClientFn(queryNodeRPCUrl), // leaf
config.NewSupplyQueryClientContextFn(queryNodeGRPCUrl), // leaf
supplyMiner, // leaf
config.NewSupplyEventsQueryClientFn(queryNodeRPCUrl), // leaf
config.NewSupplyBlockQueryClientFn(queryNodeRPCUrl), // leaf
config.NewSupplyBlockClientFn(queryNodeRPCUrl), // leaf
config.NewSupplyQueryClientContextFn(queryNodeGRPCUrl), // leaf
config.NewSupplyTxClientContextFn(queryNodeGRPCUrl, txNodeRPCUrl), // leaf
config.NewSupplyDelegationClientFn(), // leaf
config.NewSupplySharedQueryClientFn(), // leaf
config.NewSupplyProofQueryClientFn(),
supplyMiner,
config.NewSupplyAccountQuerierFn(),
config.NewSupplyApplicationQuerierFn(),
config.NewSupplySupplierQuerierFn(),
Expand All @@ -217,7 +218,7 @@ func supplyMiner(
deps depinject.Config,
_ *cobra.Command,
) (depinject.Config, error) {
mnr, err := miner.NewMiner()
mnr, err := miner.NewMiner(deps)
if err != nil {
return nil, err
}
Expand Down
50 changes: 32 additions & 18 deletions pkg/relayer/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package miner
import (
"context"

"cosmossdk.io/depinject"

"github.com/pokt-network/poktroll/pkg/client"
"github.com/pokt-network/poktroll/pkg/either"
"github.com/pokt-network/poktroll/pkg/observable"
"github.com/pokt-network/poktroll/pkg/observable/channel"
Expand All @@ -13,41 +16,45 @@ import (
servicetypes "github.com/pokt-network/poktroll/x/service/types"
)

var (
_ relayer.Miner = (*miner)(nil)
// TODO_BLOCKER(@Olshansk): query on-chain governance params once available.
// Setting this to 0 to effectively disables mining for now.
// I.e., all relays are added to the tree.
defaultRelayDifficultyBits = 0
)
var _ relayer.Miner = (*miner)(nil)

// Miner is responsible for observing servedRelayObs, hashing and checking the
// difficulty of each, finally publishing those with sufficient difficulty to
// minedRelayObs as they are applicable for relay volume.
//
// Available options:
// - WithDifficulty
//
// TODO_BLOCKER(@Olshansk): The relay hashing and relay difficulty mechanisms & values must come
// from on-chain.
type miner struct {
// proofQueryClient is used to query for the minimum relay difficulty.
proofQueryClient client.ProofQueryClient

// relayDifficultyBits is the minimum difficulty that a relay must have to be
// volume / reward applicable.
relayDifficultyBits int
relayDifficultyBits uint64
}

// NewMiner creates a new miner from the given dependencies and options. It
// returns an error if it has not been sufficiently configured or supplied.
//
// Required Dependencies:
// - ProofQueryClient
//
// Available options:
// - WithDifficulty
func NewMiner(
deps depinject.Config,
opts ...relayer.MinerOption,
) (*miner, error) {
mnr := &miner{}

if err := depinject.Inject(deps, &mnr.proofQueryClient); err != nil {
return nil, err
}

for _, opt := range opts {
opt(mnr)
}

mnr.setDefaults()
if err := mnr.setDefaults(); err != nil {
return nil, err
}

return mnr, nil
}
Expand Down Expand Up @@ -77,10 +84,17 @@ func (mnr *miner) MinedRelays(

// setDefaults ensures that the miner has been configured with a hasherConstructor and uses
// the default hasherConstructor if not.
func (mnr *miner) setDefaults() {
func (mnr *miner) setDefaults() error {
ctx := context.TODO()
params, err := mnr.proofQueryClient.GetParams(ctx)
if err != nil {
return err
}

if mnr.relayDifficultyBits == 0 {
mnr.relayDifficultyBits = defaultRelayDifficultyBits
mnr.relayDifficultyBits = params.GetMinRelayDifficultyBits()
}
return nil
}

// mapMineRelay is intended to be used as a MapFn.
Expand All @@ -102,7 +116,7 @@ func (mnr *miner) mapMineRelay(
relayHash := relayHashArr[:]

// The relay IS NOT volume / reward applicable
if protocol.MustCountDifficultyBits(relayHash) < mnr.relayDifficultyBits {
if uint64(protocol.MustCountDifficultyBits(relayHash)) < mnr.relayDifficultyBits {
return either.Success[*relayer.MinedRelay](nil), true
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/relayer/miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import (
"testing"
"time"

"cosmossdk.io/depinject"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/pkg/observable/channel"
"github.com/pokt-network/poktroll/pkg/relayer"
"github.com/pokt-network/poktroll/pkg/relayer/miner"
"github.com/pokt-network/poktroll/testutil/testclient/testqueryclients"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
)

const testDifficulty = 16
const testDifficulty = uint64(16)

// TestMiner_MinedRelays constructs an observable of mined relays, through which
// it pipes pre-mined relay fixtures. It asserts that the observable only emits
Expand All @@ -38,7 +40,9 @@ func TestMiner_MinedRelays(t *testing.T) {
expectedMinedRelays = unmarshalHexMinedRelays(t, marshaledMinableRelaysHex)
)

mnr, err := miner.NewMiner(miner.WithDifficulty(testDifficulty))
proofQueryClientMock := testqueryclients.NewTestProofQueryClient(t)
deps := depinject.Supply(proofQueryClientMock)
mnr, err := miner.NewMiner(deps, miner.WithDifficulty(testDifficulty))
require.NoError(t, err)

minedRelays := mnr.MinedRelays(ctx, mockRelaysObs)
Expand Down
2 changes: 1 addition & 1 deletion pkg/relayer/miner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/pokt-network/poktroll/pkg/relayer"

// WithDifficulty sets the difficulty of the miner, where difficultyBytes is the
// minimum number of leading zero bytes.
func WithDifficulty(difficultyBits int) relayer.MinerOption {
func WithDifficulty(difficultyBits uint64) relayer.MinerOption {
return func(mnr relayer.Miner) {
mnr.(*miner).relayDifficultyBits = difficultyBits
}
Expand Down
24 changes: 24 additions & 0 deletions testutil/testclient/testqueryclients/proofquerier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package testqueryclients

import (
"testing"

"github.com/golang/mock/gomock"

"github.com/pokt-network/poktroll/testutil/mockclient"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
)

// NewTestProofQueryClient creates a mock of the ProofQueryClient which uses the
// default proof module params for its GetParams() method implementation.
func NewTestProofQueryClient(t *testing.T) *mockclient.MockProofQueryClient {
ctrl := gomock.NewController(t)
defaultProofParams := prooftypes.DefaultParams()
proofQueryClientMock := mockclient.NewMockProofQueryClient(ctrl)
proofQueryClientMock.EXPECT().
GetParams(gomock.Any()).
Return(&defaultProofParams, nil).
AnyTimes()

return proofQueryClientMock
}
2 changes: 2 additions & 0 deletions x/proof/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/pokt-network/poktroll/app/volatile"
"github.com/pokt-network/poktroll/pkg/client"
)

var (
_ client.ProofParams = (*Params)(nil)
_ paramtypes.ParamSet = (*Params)(nil)

KeyMinRelayDifficultyBits = []byte("MinRelayDifficultyBits")
Expand Down

0 comments on commit e119984

Please sign in to comment.