mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-21 14:46:15 +00:00
p2p: remove unused MakePoWTarget() (#5684)
This commit is contained in:
@@ -16,6 +16,7 @@ Friendly reminder, we have a [bug bounty program](https://hackerone.com/tendermi
|
||||
- P2P Protocol
|
||||
|
||||
- Go API
|
||||
- [p2p] Removed unused function `MakePoWTarget`. (@erikgrinaker)
|
||||
|
||||
- [libs/os] Kill() and {Must,}{Read,Write}File() functions have been removed. (@alessio)
|
||||
|
||||
|
||||
-26
@@ -1,9 +1,7 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
@@ -94,27 +92,3 @@ func (nodeKey *NodeKey) SaveAs(filePath string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// MakePoWTarget returns the big-endian encoding of 2^(targetBits - difficulty) - 1.
|
||||
// It can be used as a Proof of Work target.
|
||||
// NOTE: targetBits must be a multiple of 8 and difficulty must be less than targetBits.
|
||||
func MakePoWTarget(difficulty, targetBits uint) []byte {
|
||||
if targetBits%8 != 0 {
|
||||
panic(fmt.Sprintf("targetBits (%d) not a multiple of 8", targetBits))
|
||||
}
|
||||
if difficulty >= targetBits {
|
||||
panic(fmt.Sprintf("difficulty (%d) >= targetBits (%d)", difficulty, targetBits))
|
||||
}
|
||||
targetBytes := targetBits / 8
|
||||
zeroPrefixLen := (int(difficulty) / 8)
|
||||
prefix := bytes.Repeat([]byte{0}, zeroPrefixLen)
|
||||
mod := (difficulty % 8)
|
||||
if mod > 0 {
|
||||
nonZeroPrefix := byte(1<<(8-mod) - 1)
|
||||
prefix = append(prefix, nonZeroPrefix)
|
||||
}
|
||||
tailLen := int(targetBytes) - len(prefix)
|
||||
return append(prefix, bytes.Repeat([]byte{0xFF}, tailLen)...)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -52,30 +51,3 @@ func TestNodeKeySaveAs(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.FileExists(t, filePath)
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
|
||||
func padBytes(bz []byte, targetBytes int) []byte {
|
||||
return append(bz, bytes.Repeat([]byte{0xFF}, targetBytes-len(bz))...)
|
||||
}
|
||||
|
||||
func TestPoWTarget(t *testing.T) {
|
||||
|
||||
targetBytes := 20
|
||||
cases := []struct {
|
||||
difficulty uint
|
||||
target []byte
|
||||
}{
|
||||
{0, padBytes([]byte{}, targetBytes)},
|
||||
{1, padBytes([]byte{127}, targetBytes)},
|
||||
{8, padBytes([]byte{0}, targetBytes)},
|
||||
{9, padBytes([]byte{0, 127}, targetBytes)},
|
||||
{10, padBytes([]byte{0, 63}, targetBytes)},
|
||||
{16, padBytes([]byte{0, 0}, targetBytes)},
|
||||
{17, padBytes([]byte{0, 0, 127}, targetBytes)},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
assert.Equal(t, MakePoWTarget(c.difficulty, 20*8), c.target)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user