tendermint/p2p/key_test.go

51 lines
1.1 KiB
Go
Raw Normal View History

2018-01-01 17:21:42 -08:00
package p2p
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
2018-07-01 19:36:49 -07:00
cmn "github.com/tendermint/tendermint/libs/common"
2018-01-01 17:21:42 -08:00
)
func TestLoadOrGenNodeKey(t *testing.T) {
filePath := filepath.Join(os.TempDir(), cmn.RandStr(12)+"_peer_id.json")
2018-01-13 12:38:40 -08:00
nodeKey, err := LoadOrGenNodeKey(filePath)
2018-01-01 17:21:42 -08:00
assert.Nil(t, err)
2018-01-13 12:38:40 -08:00
nodeKey2, err := LoadOrGenNodeKey(filePath)
2018-01-01 17:21:42 -08:00
assert.Nil(t, err)
assert.Equal(t, nodeKey, nodeKey2)
}
2018-01-13 12:38:40 -08:00
//----------------------------------------------------------
func padBytes(bz []byte, targetBytes int) []byte {
return append(bz, bytes.Repeat([]byte{0xFF}, targetBytes-len(bz))...)
2018-01-01 17:21:42 -08:00
}
func TestPoWTarget(t *testing.T) {
2018-01-13 12:38:40 -08:00
targetBytes := 20
2018-01-01 17:21:42 -08:00
cases := []struct {
2018-01-13 12:38:40 -08:00
difficulty uint
2018-01-01 17:21:42 -08:00
target []byte
}{
2018-01-13 12:38:40 -08:00
{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)},
2018-01-01 17:21:42 -08:00
}
for _, c := range cases {
2018-01-13 12:38:40 -08:00
assert.Equal(t, MakePoWTarget(c.difficulty, 20*8), c.target)
2018-01-01 17:21:42 -08:00
}
}