Skip to content

Commit

Permalink
chore: add ProofQueryClient to miner & query for minRelayDifficultyBits
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jul 4, 2024
1 parent 8f574af commit 1780954
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/relayer/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,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
39 changes: 31 additions & 8 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 @@ -25,29 +28,42 @@ var (
// 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 ...
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 +93,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 +125,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
17 changes: 15 additions & 2 deletions pkg/relayer/miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import (
"testing"
"time"

"cosmossdk.io/depinject"
"github.com/golang/mock/gomock"
"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/mockclient"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
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 +42,16 @@ func TestMiner_MinedRelays(t *testing.T) {
expectedMinedRelays = unmarshalHexMinedRelays(t, marshaledMinableRelaysHex)
)

mnr, err := miner.NewMiner(miner.WithDifficulty(testDifficulty))
// TODO_IN_THIS_COMMIT: move to shared testutil.
ctrl := gomock.NewController(t)
defaultProofParams := prooftypes.DefaultParams()
proofQueryClientMock := mockclient.NewMockProofQueryClient(ctrl)
proofQueryClientMock.EXPECT().
GetParams(gomock.Any()).
Return(&defaultProofParams, nil).
AnyTimes()
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

0 comments on commit 1780954

Please sign in to comment.