-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Code Health] refactor: simplify
protocol.CountHashDifficultyBits()
(…
…#656) Co-authored-by: Daniel Olshansky <[email protected]>
- Loading branch information
1 parent
4908ccc
commit bdbc365
Showing
10 changed files
with
87 additions
and
134 deletions.
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 { | ||
// 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