-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Mining] refactor: difficulty in terms of target hash (#690)
Change the difficulty from leading zero bits to to a target hash. - #584 --- Co-authored-by: Daniel Olshansky <[email protected]> Co-authored-by: Redouane Lakrache <[email protected]>
- Loading branch information
1 parent
1d75111
commit ca6100e
Showing
38 changed files
with
759 additions
and
514 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
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
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,45 @@ | ||
package protocol | ||
|
||
import ( | ||
"encoding/binary" | ||
"math/bits" | ||
"bytes" | ||
"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 ( | ||
// BaseRelayDifficultyHashBz 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 the base difficulty. | ||
// | ||
// When this is the difficulty of a particular service, all relays are reward / volume applicable. | ||
// | ||
// Bitcoin uses a similar concept, where the target hash is defined as the hash: | ||
// - 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 | ||
BaseRelayDifficultyHashBz, _ = hex.DecodeString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") | ||
) | ||
|
||
// GetDifficultyFromHash returns the "difficulty" of the given hash, with respect | ||
// to the "highest" (easiest) target hash, BaseRelayDifficultyHash. | ||
// 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 { | ||
baseRelayDifficultyHashInt := new(big.Int).SetBytes(BaseRelayDifficultyHashBz) | ||
hashInt := new(big.Int).SetBytes(hashBz[:]) | ||
|
||
// difficulty is the ratio of the highest target hash to the given hash. | ||
// TODO_MAINNET: Can this cause an integer overflow? | ||
return new(big.Int).Div(baseRelayDifficultyHashInt, hashInt).Int64() | ||
} | ||
|
||
// IsRelayVolumeApplicable returns true if the relay IS reward / volume applicable. | ||
// A relay is reward / volume applicable IFF its hash is less than the target hash. | ||
// - relayHash is the hash of the relay to be checked. | ||
// - targetHash is the hash of the relay difficulty target for a particular service. | ||
// | ||
// TODO_MAINNET: Devise a test that tries to attack the network and ensure that | ||
// there is sufficient telemetry. | ||
func IsRelayVolumeApplicable(relayHash, targetHash []byte) bool { | ||
return bytes.Compare(relayHash, targetHash) == -1 // True if relayHash < targetHash | ||
} |
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,106 @@ | ||
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, | ||
desc: "Difficulty 1", | ||
hashHex: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedDifficulty: 1, | ||
}, | ||
{ | ||
desc: "Difficulty 2", | ||
hashHex: "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedDifficulty: 2, | ||
}, | ||
{ | ||
desc: "Difficulty 4", | ||
hashHex: "3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedDifficulty: 4, | ||
}, | ||
{ | ||
bz: []byte{0b01111111}, | ||
difficulty: 1, | ||
desc: "Highest difficulty", | ||
hashHex: "0000000000000000000000000000000000000000000000000000000000000001", | ||
expectedDifficulty: new(big.Int).SetBytes(BaseRelayDifficultyHashBz).Int64(), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
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) | ||
}) | ||
} | ||
} | ||
|
||
func TestIsRelayVolumeApplicable(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
relayHashHex string | ||
targetHashHex string | ||
expectedVolumeApplicable bool | ||
}{ | ||
{ | ||
desc: "Applicable: relayHash << targetHash", | ||
relayHashHex: "000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
targetHashHex: "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedVolumeApplicable: true, | ||
}, | ||
{ | ||
bz: []byte{0, 255}, | ||
difficulty: 8, | ||
desc: "Applicable: relayHash < targetHash", | ||
relayHashHex: "00efffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
targetHashHex: "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedVolumeApplicable: true, | ||
}, | ||
{ | ||
bz: []byte{0, 0b01111111}, | ||
difficulty: 9, | ||
desc: "Not Applicable: relayHash = targetHash", | ||
relayHashHex: "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
targetHashHex: "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedVolumeApplicable: false, | ||
}, | ||
{ | ||
bz: []byte{0, 0b00111111}, | ||
difficulty: 10, | ||
desc: "Not applicable: relayHash > targetHash", | ||
relayHashHex: "0effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
targetHashHex: "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedVolumeApplicable: false, | ||
}, | ||
{ | ||
bz: []byte{0, 0, 255}, | ||
difficulty: 16, | ||
desc: "Not applicable: relayHash >> targetHash", | ||
relayHashHex: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
targetHashHex: "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
expectedVolumeApplicable: false, | ||
}, | ||
} | ||
|
||
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) { | ||
relayHash, err := hex.DecodeString(test.relayHashHex) | ||
require.NoError(t, err) | ||
|
||
targetHash, err := hex.DecodeString(test.targetHashHex) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, test.expectedVolumeApplicable, IsRelayVolumeApplicable(relayHash, targetHash)) | ||
}) | ||
} | ||
} |
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,15 @@ | ||
package protocol | ||
|
||
// GetRelayHashFromBytes 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 GetRelayHashFromBytes(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
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.