Browse Source

p2p: remove unused MakePoWTarget() (#5684)

pull/5689/head
Erik Grinaker 4 years ago
committed by GitHub
parent
commit
e3728e7709
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 1 additions and 54 deletions
  1. +1
    -0
      CHANGELOG_PENDING.md
  2. +0
    -26
      p2p/key.go
  3. +0
    -28
      p2p/key_test.go

+ 1
- 0
CHANGELOG_PENDING.md View File

@ -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)


+ 0
- 26
p2p/key.go View File

@ -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)...)
}

+ 0
- 28
p2p/key_test.go View File

@ -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)
}
}

Loading…
Cancel
Save