-
Notifications
You must be signed in to change notification settings - Fork 8
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
[Mining] refactor: difficulty in terms of target hash #690
Merged
Merged
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ffb6902
refactor: difficulty in terms of target hash
bryanchriswhite ceb283d
fix: e2e test
bryanchriswhite 0ca9b98
fix: e2e test
bryanchriswhite 38d1985
chore: review feedback improvements
bryanchriswhite 1c66b4f
Merge remote-tracking branch 'pokt/main' into refactor/difficulty/tar…
bryanchriswhite 5089b94
Merge branch 'main' into refactor/difficulty/target-hash
bryanchriswhite cc6468d
refactor: protocol.NewRelayHasher, .RelayHashSize
bryanchriswhite 5675f81
fix: ComputeNewDifficultyTargetHash()
bryanchriswhite 440ecc6
chore: review fee
bryanchriswhite ee59e0d
refactor: use big.Floats on-chain & cleanup
bryanchriswhite 69d97d3
chore: review improvements
bryanchriswhite 5a3cfd8
[TODO] chore: update `smt.MerkleRoot#Sum()` error handling (#672)
bryanchriswhite e2802ac
Merge remote-tracking branch 'pokt/main' into refactor/difficulty/tar…
bryanchriswhite 99783a8
Review of #690 by @olshansk (#699)
Olshansk 09cffe2
Update Makefile
Olshansk aa05407
Update Makefile
Olshansk 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
Large diffs are not rendered by default.
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
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 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,20 +1,27 @@ | ||
package protocol | ||
|
||
import ( | ||
"encoding/binary" | ||
"math/bits" | ||
"encoding/hex" | ||
"math/big" | ||
) | ||
|
||
// 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[:])) | ||
var ( | ||
// Difficulty1HashBz is the chosen "highest" (easiest) target hash, which | ||
// corresponds to the lowest possible difficulty. It effectively normalizes | ||
// the difficulty number (which is returned by GetDifficultyFromHash) by defining | ||
// the hash which corresponds to difficulty 1. | ||
// - 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 | ||
Difficulty1HashBz, _ = hex.DecodeString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") | ||
) | ||
|
||
// GetDifficultyFromHash returns the "difficulty" of the given hash, with respect | ||
// to the "highest" (easiest) target hash, Difficulty1Hash. | ||
// The resultant value is not used for any business logic but is simplify there to have a human-readable version of the hash. | ||
func GetDifficultyFromHash(hashBz [RelayHasherSize]byte) int64 { | ||
bryanchriswhite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
difficulty1HashInt := new(big.Int).SetBytes(Difficulty1HashBz) | ||
hashInt := new(big.Int).SetBytes(hashBz[:]) | ||
|
||
// difficulty is the ratio of the highest target hash to the given hash. | ||
return new(big.Int).Div(difficulty1HashInt, hashInt).Int64() | ||
Olshansk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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,51 +1,54 @@ | ||
package protocol_test | ||
package protocol | ||
|
||
import ( | ||
"fmt" | ||
"encoding/hex" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/pokt-network/poktroll/pkg/crypto/protocol" | ||
) | ||
|
||
func TestCountDifficultyBits(t *testing.T) { | ||
func TestGetDifficultyFromHash(t *testing.T) { | ||
tests := []struct { | ||
bz []byte | ||
difficulty int | ||
desc string | ||
hashHex string | ||
expectedDifficulty int64 | ||
}{ | ||
{ | ||
bz: []byte{0b11111111}, | ||
difficulty: 0, | ||
}, | ||
{ | ||
bz: []byte{0b01111111}, | ||
difficulty: 1, | ||
desc: "Difficulty 1", | ||
hashHex: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedDifficulty: 1, | ||
}, | ||
{ | ||
bz: []byte{0, 255}, | ||
difficulty: 8, | ||
desc: "Difficulty 2", | ||
hashHex: "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedDifficulty: 2, | ||
}, | ||
{ | ||
bz: []byte{0, 0b01111111}, | ||
difficulty: 9, | ||
desc: "Difficulty 4", | ||
hashHex: "3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedDifficulty: 4, | ||
}, | ||
{ | ||
bz: []byte{0, 0b00111111}, | ||
difficulty: 10, | ||
}, | ||
{ | ||
bz: []byte{0, 0, 255}, | ||
difficulty: 16, | ||
desc: "Highest difficulty", | ||
hashHex: "0000000000000000000000000000000000000000000000000000000000000001", | ||
expectedDifficulty: new(big.Int).SetBytes(Difficulty1HashBz).Int64(), | ||
}, | ||
} | ||
|
||
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) | ||
t.Run(test.desc, func(t *testing.T) { | ||
hashBytes, err := hex.DecodeString(test.hashHex) | ||
if err != nil { | ||
t.Fatalf("failed to decode hash: %v", err) | ||
} | ||
|
||
var hashBz [RelayHasherSize]byte | ||
copy(hashBz[:], hashBytes) | ||
|
||
difficulty := GetDifficultyFromHash(hashBz) | ||
t.Logf("test: %s, difficulty: %d", test.desc, difficulty) | ||
require.Equal(t, test.expectedDifficulty, difficulty) | ||
}) | ||
} | ||
} |
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,14 @@ | ||
package protocol | ||
|
||
// GetHashFromBytes returns the hash of the relay (full, request or response) bytes. | ||
// It is used as helper in the case that the relay is already marshaled and | ||
// centralizes the hasher used. | ||
func GetHashFromBytes(relayBz []byte) (hash [RelayHasherSize]byte) { | ||
hasher := NewRelayHasher() | ||
// NB: Intentionally ignoring the error, following sha256.Sum256 implementation. | ||
_, _ = hasher.Write(relayBz) | ||
hashBz := hasher.Sum(nil) | ||
copy(hash[:], hashBz) | ||
|
||
return hash | ||
} |
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,11 @@ | ||
package protocol | ||
|
||
import "crypto/sha256" | ||
|
||
const ( | ||
RelayHasherSize = sha256.Size | ||
) | ||
|
||
var ( | ||
NewRelayHasher = sha256.New | ||
) |
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 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 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 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.
#PUC I still don't fully understand what "difficulty 1" is.
Is it the first difficulty?
Is something here equal to / greater than / less than the number 1?
Is the difficulty itself 1?
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.
We have to define a target hash which corresponds to the easiest difficulty so that we have a way to talk about the relative difficulty between it and any other target hash.
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.
Why don't we call it
BaseRelayDifficultyHash
orMinRelayDifficultyHash
?