Skip to content

Commit bdbc365

Browse files
[Code Health] refactor: simplify protocol.CountHashDifficultyBits() (#656)
Co-authored-by: Daniel Olshansky <olshansky.daniel@gmail.com>
1 parent 4908ccc commit bdbc365

10 files changed

Lines changed: 87 additions & 134 deletions

File tree

pkg/crypto/protocol/difficulty.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package protocol
2+
3+
import (
4+
"encoding/binary"
5+
"math/bits"
6+
)
7+
8+
// CountHashDifficultyBits returns the number of leading zero bits in the given byte slice.
9+
// TODO_MAINNET: Consider generalizing difficulty to a target hash. See:
10+
// - https://bitcoin.stackexchange.com/questions/107976/bitcoin-difficulty-why-leading-0s
11+
// - https://bitcoin.stackexchange.com/questions/121920/is-it-always-possible-to-find-a-number-whose-hash-starts-with-a-certain-number-o
12+
// - https://github.com/pokt-network/poktroll/pull/656/files#r1666712528
13+
func CountHashDifficultyBits(bz [32]byte) int {
14+
// Using BigEndian for contiguous bit/byte ordering such leading zeros
15+
// accumulate across adjacent bytes.
16+
// E.g.: []byte{0, 0b00111111, 0x00, 0x00} has 10 leading zero bits. If
17+
// LittleEndian were applied instead, it would have 18 leading zeros because it would
18+
// look like []byte{0, 0, 0b00111111, 0}.
19+
return bits.LeadingZeros64(binary.BigEndian.Uint64(bz[:]))
20+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package protocol_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/pokt-network/poktroll/pkg/crypto/protocol"
10+
)
11+
12+
func TestCountDifficultyBits(t *testing.T) {
13+
tests := []struct {
14+
bz []byte
15+
difficulty int
16+
}{
17+
{
18+
bz: []byte{0b11111111},
19+
difficulty: 0,
20+
},
21+
{
22+
bz: []byte{0b01111111},
23+
difficulty: 1,
24+
},
25+
{
26+
bz: []byte{0, 255},
27+
difficulty: 8,
28+
},
29+
{
30+
bz: []byte{0, 0b01111111},
31+
difficulty: 9,
32+
},
33+
{
34+
bz: []byte{0, 0b00111111},
35+
difficulty: 10,
36+
},
37+
{
38+
bz: []byte{0, 0, 255},
39+
difficulty: 16,
40+
},
41+
}
42+
43+
for _, test := range tests {
44+
t.Run(fmt.Sprintf("difficulty_%d_zero_bits", test.difficulty), func(t *testing.T) {
45+
var bz [32]byte
46+
copy(bz[:], test.bz)
47+
actualDifficulty := protocol.CountHashDifficultyBits(bz)
48+
require.Equal(t, test.difficulty, actualDifficulty)
49+
})
50+
}
51+
}

pkg/crypto/protocol/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package protocol
2+
3+
var (
4+
codespace = "crypto/protocol"
5+
)

pkg/relayer/miner/gen/gen_fixtures.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import (
1717
"sync"
1818
"time"
1919

20+
"github.com/pokt-network/poktroll/pkg/crypto/protocol"
2021
"github.com/pokt-network/poktroll/pkg/observable"
2122
"github.com/pokt-network/poktroll/pkg/observable/channel"
2223
"github.com/pokt-network/poktroll/pkg/relayer"
2324
"github.com/pokt-network/poktroll/pkg/relayer/miner"
24-
"github.com/pokt-network/poktroll/pkg/relayer/protocol"
2525
servicetypes "github.com/pokt-network/poktroll/x/service/types"
2626
)
2727

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

206206
// difficultyLT returns true if the given hash has a difficulty less than
207207
// flagDifficultyBitsThreshold.
208208
func difficultyLT(hash []byte) bool {
209-
return protocol.MustCountDifficultyBits(hash) < flagDifficultyBitsThreshold
209+
return protocol.CountDifficultyBits(hash) < flagDifficultyBitsThreshold
210210
}
211211

212212
// getMarshaledRelayFmtLines performs two map operations followed by a collect.

pkg/relayer/miner/miner.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"cosmossdk.io/depinject"
77

88
"github.com/pokt-network/poktroll/pkg/client"
9+
"github.com/pokt-network/poktroll/pkg/crypto/protocol"
910
"github.com/pokt-network/poktroll/pkg/either"
1011
"github.com/pokt-network/poktroll/pkg/observable"
1112
"github.com/pokt-network/poktroll/pkg/observable/channel"
1213
"github.com/pokt-network/poktroll/pkg/observable/filter"
1314
"github.com/pokt-network/poktroll/pkg/observable/logging"
1415
"github.com/pokt-network/poktroll/pkg/relayer"
15-
"github.com/pokt-network/poktroll/pkg/relayer/protocol"
1616
servicetypes "github.com/pokt-network/poktroll/x/service/types"
1717
)
1818

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

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

123122
// The relay IS volume / reward applicable
124123
return either.Success(&relayer.MinedRelay{
125124
Relay: *relay,
126125
Bytes: relayBz,
127-
Hash: relayHash,
126+
Hash: relayHash[:],
128127
}), false
129128
}

pkg/relayer/protocol/difficulty.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

pkg/relayer/protocol/difficulty_test.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

pkg/relayer/protocol/errors.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

x/proof/keeper/msg_server_submit_proof.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"google.golang.org/grpc/codes"
1919
"google.golang.org/grpc/status"
2020

21-
"github.com/pokt-network/poktroll/pkg/relayer/protocol"
21+
"github.com/pokt-network/poktroll/pkg/crypto/protocol"
2222
"github.com/pokt-network/poktroll/telemetry"
2323
"github.com/pokt-network/poktroll/x/proof/types"
2424
servicetypes "github.com/pokt-network/poktroll/x/service/types"
@@ -476,14 +476,7 @@ func verifyClosestProof(
476476
// function that can be used by both the proof and the miner packages.
477477
func validateMiningDifficulty(relayBz []byte, minRelayDifficultyBits uint64) error {
478478
relayHash := servicetypes.GetHashFromBytes(relayBz)
479-
480-
relayDifficultyBits, err := protocol.CountHashDifficultyBits(relayHash[:])
481-
if err != nil {
482-
return types.ErrProofInvalidRelay.Wrapf(
483-
"error counting difficulty bits: %s",
484-
err,
485-
)
486-
}
479+
relayDifficultyBits := protocol.CountHashDifficultyBits(relayHash)
487480

488481
// TODO_MAINNET: Devise a test that tries to attack the network and ensure that there
489482
// is sufficient telemetry.

x/proof/keeper/msg_server_submit_proof_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import (
1616
"google.golang.org/grpc/status"
1717

1818
"github.com/pokt-network/poktroll/pkg/crypto"
19+
"github.com/pokt-network/poktroll/pkg/crypto/protocol"
1920
"github.com/pokt-network/poktroll/pkg/crypto/rings"
2021
"github.com/pokt-network/poktroll/pkg/polylog/polyzero"
2122
"github.com/pokt-network/poktroll/pkg/relayer"
22-
"github.com/pokt-network/poktroll/pkg/relayer/protocol"
2323
"github.com/pokt-network/poktroll/pkg/relayer/session"
2424
testutilevents "github.com/pokt-network/poktroll/testutil/events"
2525
keepertest "github.com/pokt-network/poktroll/testutil/keeper"
@@ -1394,10 +1394,7 @@ func getClosestRelayDifficultyBits(
13941394
require.NoError(t, err)
13951395

13961396
// Count the number of leading 0s in the relay hash to determine its difficulty.
1397-
relayDifficultyBits, err := protocol.CountHashDifficultyBits(relayHash[:])
1398-
require.NoError(t, err)
1399-
1400-
return uint64(relayDifficultyBits)
1397+
return uint64(protocol.CountHashDifficultyBits(relayHash))
14011398
}
14021399

14031400
// resetBlockHeightFn returns a function that resets the block height of the

0 commit comments

Comments
 (0)