Files
tendermint/p2p/key_test.go
Marko 9bd0f9e634 lint: golint issue fixes (#4258)
* lint: golint issue fixes

- on my local machine golint is a lot stricter than the bot so slowly going through and fixing things.

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* more fixes from golint

* remove isPeerPersistentFn

* add changelog entry
2019-12-17 13:02:45 +01:00

51 lines
1.1 KiB
Go

package p2p
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
tmrand "github.com/tendermint/tendermint/libs/rand"
)
func TestLoadOrGenNodeKey(t *testing.T) {
filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
nodeKey, err := LoadOrGenNodeKey(filePath)
assert.Nil(t, err)
nodeKey2, err := LoadOrGenNodeKey(filePath)
assert.Nil(t, err)
assert.Equal(t, nodeKey, nodeKey2)
}
//----------------------------------------------------------
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)
}
}