-
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.
refactor: count difficulty; bit resolution
- Loading branch information
1 parent
4e66081
commit a1d18ce
Showing
3 changed files
with
41 additions
and
10 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
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 |
---|---|---|
@@ -1,17 +1,40 @@ | ||
package protocol | ||
|
||
import ( | ||
"encoding/hex" | ||
"strings" | ||
"math/bits" | ||
) | ||
|
||
// TODO_BLOCKER: Revisit this part of the algorithm after initial TestNet Launch. | ||
// TODO_TEST: Add extensive tests for the core relay mining business logic. | ||
// BytesDifficultyGreaterThan determines if the bytes exceed a certain difficulty, and it | ||
// is used to determine if a relay is volume applicable. See the spec for more details: https://github.com/pokt-network/pocket-network-protocol | ||
func BytesDifficultyGreaterThan(bz []byte, compDifficultyBytes int) bool { | ||
hexZerosPrefix := strings.Repeat("0", compDifficultyBytes*2) // 2 hex chars per byte. | ||
hexBz := hex.EncodeToString(bz) | ||
|
||
return strings.HasPrefix(hexBz, hexZerosPrefix) | ||
// MustCountDifficultyBits returns the number of leading zero bits in the given | ||
// byte slice. It panics if an error is encountered. | ||
func MustCountDifficultyBits(bz []byte) int { | ||
diff, err := CountDifficultyBits(bz) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return diff | ||
} | ||
|
||
// CountDifficultyBits returns the number of leading zero bits in the given byte | ||
// slice. It returns an error if the byte slice is all zero bits. | ||
func CountDifficultyBits(bz []byte) (int, error) { | ||
bzLen := len(bz) | ||
|
||
var zeroBits int | ||
for i, b := range bz { | ||
if b != 0 { | ||
zeroBits = bits.LeadingZeros8(b) | ||
if zeroBits == 8 { | ||
// we already checked that b != 0. | ||
return 0, ErrDifficulty.Wrap("impossible code path") | ||
} | ||
|
||
return (i)*8 + zeroBits, nil | ||
} | ||
} | ||
|
||
return 0, ErrDifficulty.Wrapf("difficulty matches bytes length: %d; bytes (hex): % x", bzLen, 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,8 @@ | ||
package protocol | ||
|
||
import errorsmod "cosmossdk.io/errors" | ||
|
||
var ( | ||
ErrDifficulty = errorsmod.New(codespace, 1, "difficulty error") | ||
codespace = "relayer/protocol" | ||
) |