-
Notifications
You must be signed in to change notification settings - Fork 12
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
bryanchriswhite
merged 23 commits into
main
from
issues/584/refactor/count-difficulty-bits
Jul 6, 2024
Merged
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 2d2a9b1
chore: supply ProofQueryClient in relayminer via depinject
bryanchriswhite 8f574af
fix: import cycle
bryanchriswhite 1780954
chore: add ProofQueryClient to miner & query for minRelayDifficultyBits
bryanchriswhite 021aba0
refactor: mock proof query client to shared testutil
bryanchriswhite 41c7219
chore: self-review improvements
bryanchriswhite 3e574bd
chore: cleanup todo comments
bryanchriswhite 9b67108
refactor: simplify protocol.CountHashDifficultyBits()
bryanchriswhite 5fd53f8
chore: improve comments
bryanchriswhite f82cafb
Empty commit
bryanchriswhite 2e9ad57
chore: improve comments
bryanchriswhite 717995e
refactor: promote difficulty calc out of relayer to crypto pkg
bryanchriswhite 85251bf
fixup: refactor: CountHashDifficultyBits()
bryanchriswhite 7ce8558
fixup: refactor: relayminer - fix: dep supply ordering
bryanchriswhite fb42972
Merge branch 'issues/584/refactor/miner-query-params' into issues/584…
bryanchriswhite c3ab4bc
fixup: refactor: promote CountHashDifficultyBits() to pkg/crypto/prot…
bryanchriswhite 68427ad
Merge branch 'main' into issues/584/refactor/miner-query-params
bryanchriswhite c1f5ae6
chore: update comment
bryanchriswhite b213b8b
Merge branch 'issues/584/refactor/miner-query-params' into issues/584…
bryanchriswhite e6dfda7
Merge branch 'main' into issues/584/refactor/count-difficulty-bits
bryanchriswhite 00ab72b
chore: update comment
bryanchriswhite d229378
Merge branch 'main' into issues/584/refactor/count-difficulty-bits
bryanchriswhite b2fb2bb
Merge branch 'main' into issues/584/refactor/count-difficulty-bits
bryanchriswhite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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[:])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package protocol | ||
|
||
var ( | ||
codespace = "crypto/protocol" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.