Skip to content

Commit

Permalink
[Code Health] refactor: simplify protocol.CountHashDifficultyBits() (
Browse files Browse the repository at this point in the history
…#656)

Co-authored-by: Daniel Olshansky <[email protected]>
  • Loading branch information
bryanchriswhite and Olshansk authored Jul 6, 2024
1 parent 4908ccc commit bdbc365
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 134 deletions.
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 {
// 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

0 comments on commit bdbc365

Please sign in to comment.