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

[Code Health] refactor: simplify protocol.CountHashDifficultyBits() #656

Merged
merged 23 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0e54712
chore: add ProofQueryClient interface & implementation
bryanchriswhite Jul 4, 2024
2d2a9b1
chore: supply ProofQueryClient in relayminer via depinject
bryanchriswhite Jul 4, 2024
8f574af
fix: import cycle
bryanchriswhite Jul 4, 2024
1780954
chore: add ProofQueryClient to miner & query for minRelayDifficultyBits
bryanchriswhite Jul 4, 2024
021aba0
refactor: mock proof query client to shared testutil
bryanchriswhite Jul 4, 2024
41c7219
chore: self-review improvements
bryanchriswhite Jul 4, 2024
3e574bd
chore: cleanup todo comments
bryanchriswhite Jul 4, 2024
9b67108
refactor: simplify protocol.CountHashDifficultyBits()
bryanchriswhite Jul 4, 2024
5fd53f8
chore: improve comments
bryanchriswhite Jul 4, 2024
f82cafb
Empty commit
bryanchriswhite Jul 4, 2024
2e9ad57
chore: improve comments
bryanchriswhite Jul 4, 2024
717995e
refactor: promote difficulty calc out of relayer to crypto pkg
bryanchriswhite Jul 4, 2024
85251bf
fixup: refactor: CountHashDifficultyBits()
bryanchriswhite Jul 4, 2024
7ce8558
fixup: refactor: relayminer - fix: dep supply ordering
bryanchriswhite Jul 4, 2024
fb42972
Merge branch 'issues/584/refactor/miner-query-params' into issues/584…
bryanchriswhite Jul 4, 2024
c3ab4bc
fixup: refactor: promote CountHashDifficultyBits() to pkg/crypto/prot…
bryanchriswhite Jul 4, 2024
68427ad
Merge branch 'main' into issues/584/refactor/miner-query-params
bryanchriswhite Jul 5, 2024
c1f5ae6
chore: update comment
bryanchriswhite Jul 5, 2024
b213b8b
Merge branch 'issues/584/refactor/miner-query-params' into issues/584…
bryanchriswhite Jul 5, 2024
e6dfda7
Merge branch 'main' into issues/584/refactor/count-difficulty-bits
bryanchriswhite Jul 5, 2024
00ab72b
chore: update comment
bryanchriswhite Jul 5, 2024
d229378
Merge branch 'main' into issues/584/refactor/count-difficulty-bits
bryanchriswhite Jul 6, 2024
b2fb2bb
Merge branch 'main' into issues/584/refactor/count-difficulty-bits
bryanchriswhite Jul 6, 2024
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
20 changes: 20 additions & 0 deletions pkg/crypto/protocol/difficulty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package protocol

import (
"encoding/binary"
"math/bits"
)

// CountHashDifficultyBits returns the number of leading zero bits in the given byte slice.
// TODO_MAINNET: Consider generalizing difficulty to a target hash. See:
// - https://bitcoin.stackexchange.com/questions/107976/bitcoin-difficulty-why-leading-0s
// - https://bitcoin.stackexchange.com/questions/121920/is-it-always-possible-to-find-a-number-whose-hash-starts-with-a-certain-number-o
// - https://github.com/pokt-network/poktroll/pull/656/files#r1666712528
func CountHashDifficultyBits(bz [32]byte) int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @red-0ne who I believe has similar changes elsewhere.

bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
// Using BigEndian for contiguous bit/byte ordering such leading zeros
// accumulate across adjacent bytes.
// E.g.: []byte{0, 0b00111111, 0x00, 0x00} has 10 leading zero bits. If
// LittleEndian were applied instead, it would have 18 leading zeros because it would
// look like []byte{0, 0, 0b00111111, 0}.
return bits.LeadingZeros64(binary.BigEndian.Uint64(bz[:]))
}
51 changes: 51 additions & 0 deletions pkg/crypto/protocol/difficulty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package protocol_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/pkg/crypto/protocol"
)

func TestCountDifficultyBits(t *testing.T) {
tests := []struct {
bz []byte
difficulty int
}{
{
bz: []byte{0b11111111},
difficulty: 0,
},
{
bz: []byte{0b01111111},
difficulty: 1,
},
{
bz: []byte{0, 255},
difficulty: 8,
},
{
bz: []byte{0, 0b01111111},
difficulty: 9,
},
{
bz: []byte{0, 0b00111111},
difficulty: 10,
},
{
bz: []byte{0, 0, 255},
difficulty: 16,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("difficulty_%d_zero_bits", test.difficulty), func(t *testing.T) {
var bz [32]byte
copy(bz[:], test.bz)
actualDifficulty := protocol.CountHashDifficultyBits(bz)
require.Equal(t, test.difficulty, actualDifficulty)
})
}
}
5 changes: 5 additions & 0 deletions pkg/crypto/protocol/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package protocol

var (
codespace = "crypto/protocol"
)
6 changes: 3 additions & 3 deletions pkg/relayer/miner/gen/gen_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
"sync"
"time"

"github.com/pokt-network/poktroll/pkg/crypto/protocol"
"github.com/pokt-network/poktroll/pkg/observable"
"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/pkg/relayer/protocol"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
)

Expand Down Expand Up @@ -200,13 +200,13 @@ func exitOnError(errCh <-chan error) {
// difficultyGTE returns true if the given hash has a difficulty greater than or
// equal to flagDifficultyBitsThreshold.
func difficultyGTE(hash []byte) bool {
return protocol.MustCountDifficultyBits(hash) >= flagDifficultyBitsThreshold
return protocol.CountDifficultyBits(hash) >= flagDifficultyBitsThreshold
}

// difficultyLT returns true if the given hash has a difficulty less than
// flagDifficultyBitsThreshold.
func difficultyLT(hash []byte) bool {
return protocol.MustCountDifficultyBits(hash) < flagDifficultyBitsThreshold
return protocol.CountDifficultyBits(hash) < flagDifficultyBitsThreshold
}

// getMarshaledRelayFmtLines performs two map operations followed by a collect.
Expand Down
9 changes: 4 additions & 5 deletions pkg/relayer/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"cosmossdk.io/depinject"

"github.com/pokt-network/poktroll/pkg/client"
"github.com/pokt-network/poktroll/pkg/crypto/protocol"
"github.com/pokt-network/poktroll/pkg/either"
"github.com/pokt-network/poktroll/pkg/observable"
"github.com/pokt-network/poktroll/pkg/observable/channel"
"github.com/pokt-network/poktroll/pkg/observable/filter"
"github.com/pokt-network/poktroll/pkg/observable/logging"
"github.com/pokt-network/poktroll/pkg/relayer"
"github.com/pokt-network/poktroll/pkg/relayer/protocol"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
)

Expand Down Expand Up @@ -112,18 +112,17 @@ func (mnr *miner) mapMineRelay(
if err != nil {
return either.Error[*relayer.MinedRelay](err), false
}
relayHashArr := servicetypes.GetHashFromBytes(relayBz)
relayHash := relayHashArr[:]
relayHash := servicetypes.GetHashFromBytes(relayBz)

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

// The relay IS volume / reward applicable
return either.Success(&relayer.MinedRelay{
Relay: *relay,
Bytes: relayBz,
Hash: relayHash,
Hash: relayHash[:],
}), false
}
48 changes: 0 additions & 48 deletions pkg/relayer/protocol/difficulty.go

This file was deleted.

56 changes: 0 additions & 56 deletions pkg/relayer/protocol/difficulty_test.go

This file was deleted.

8 changes: 0 additions & 8 deletions pkg/relayer/protocol/errors.go

This file was deleted.

11 changes: 2 additions & 9 deletions x/proof/keeper/msg_server_submit_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/pokt-network/poktroll/pkg/relayer/protocol"
"github.com/pokt-network/poktroll/pkg/crypto/protocol"
"github.com/pokt-network/poktroll/telemetry"
"github.com/pokt-network/poktroll/x/proof/types"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
Expand Down Expand Up @@ -476,14 +476,7 @@ func verifyClosestProof(
// function that can be used by both the proof and the miner packages.
func validateMiningDifficulty(relayBz []byte, minRelayDifficultyBits uint64) error {
relayHash := servicetypes.GetHashFromBytes(relayBz)

relayDifficultyBits, err := protocol.CountHashDifficultyBits(relayHash[:])
if err != nil {
return types.ErrProofInvalidRelay.Wrapf(
"error counting difficulty bits: %s",
err,
)
}
relayDifficultyBits := protocol.CountHashDifficultyBits(relayHash)

// TODO_MAINNET: Devise a test that tries to attack the network and ensure that there
// is sufficient telemetry.
Expand Down
7 changes: 2 additions & 5 deletions x/proof/keeper/msg_server_submit_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
"google.golang.org/grpc/status"

"github.com/pokt-network/poktroll/pkg/crypto"
"github.com/pokt-network/poktroll/pkg/crypto/protocol"
"github.com/pokt-network/poktroll/pkg/crypto/rings"
"github.com/pokt-network/poktroll/pkg/polylog/polyzero"
"github.com/pokt-network/poktroll/pkg/relayer"
"github.com/pokt-network/poktroll/pkg/relayer/protocol"
"github.com/pokt-network/poktroll/pkg/relayer/session"
testutilevents "github.com/pokt-network/poktroll/testutil/events"
keepertest "github.com/pokt-network/poktroll/testutil/keeper"
Expand Down Expand Up @@ -1394,10 +1394,7 @@ func getClosestRelayDifficultyBits(
require.NoError(t, err)

// Count the number of leading 0s in the relay hash to determine its difficulty.
relayDifficultyBits, err := protocol.CountHashDifficultyBits(relayHash[:])
require.NoError(t, err)

return uint64(relayDifficultyBits)
return uint64(protocol.CountHashDifficultyBits(relayHash))
}

// resetBlockHeightFn returns a function that resets the block height of the
Expand Down
Loading