Merge branch 'master' into alessio/use-go-testing-t-utils

This commit is contained in:
Alessio Treglia
2020-11-28 21:06:58 +00:00
133 changed files with 17979 additions and 2860 deletions
+3
View File
@@ -44,6 +44,9 @@ type Reactor interface {
// copying.
//
// CONTRACT: msgBytes are not nil.
//
// XXX: do not call any methods that can block or incur heavy processing.
// https://github.com/tendermint/tendermint/issues/2888
Receive(chID byte, peer Peer, msgBytes []byte)
}
+2 -3
View File
@@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@@ -21,7 +22,6 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/sr25519"
"github.com/tendermint/tendermint/libs/async"
tmos "github.com/tendermint/tendermint/libs/os"
tmrand "github.com/tendermint/tendermint/libs/rand"
)
@@ -228,8 +228,7 @@ func TestDeriveSecretsAndChallengeGolden(t *testing.T) {
if *update {
t.Logf("Updating golden test vector file %s", goldenFilepath)
data := createGoldenTestVectors(t)
err := tmos.WriteFile(goldenFilepath, []byte(data), 0644)
require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(goldenFilepath, []byte(data), 0644))
}
f, err := os.Open(goldenFilepath)
if err != nil {
-26
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)...)
}
-28
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)
}
}
+2
View File
@@ -236,6 +236,8 @@ func (r *Reactor) logErrAddrBook(err error) {
}
// Receive implements Reactor by handling incoming PEX messages.
// XXX: do not call any methods that can block or incur heavy processing.
// https://github.com/tendermint/tendermint/issues/2888
func (r *Reactor) Receive(chID byte, src Peer, msgBytes []byte) {
msg, err := decodeMsg(msgBytes)
if err != nil {