mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-13 05:11:21 +00:00
persistent node key and ID
This commit is contained in:
49
p2p/key_test.go
Normal file
49
p2p/key_test.go
Normal file
@ -0,0 +1,49 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
)
|
||||
|
||||
func TestLoadOrGenNodeKey(t *testing.T) {
|
||||
filePath := filepath.Join(os.TempDir(), cmn.RandStr(12)+"_peer_id.json")
|
||||
|
||||
target := MakePoWTarget(2)
|
||||
nodeKey, err := LoadOrGenNodeKey(filePath, target)
|
||||
assert.Nil(t, err)
|
||||
|
||||
nodeKey2, err := LoadOrGenNodeKey(filePath, target)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, nodeKey, nodeKey2)
|
||||
}
|
||||
|
||||
func repeatBytes(val byte, n int) []byte {
|
||||
return bytes.Repeat([]byte{val}, n)
|
||||
}
|
||||
|
||||
func TestPoWTarget(t *testing.T) {
|
||||
|
||||
cases := []struct {
|
||||
difficulty uint8
|
||||
target []byte
|
||||
}{
|
||||
{0, bytes.Repeat([]byte{255}, 20)},
|
||||
{1, append([]byte{128}, repeatBytes(255, 19)...)},
|
||||
{8, append([]byte{0}, repeatBytes(255, 19)...)},
|
||||
{9, append([]byte{0, 128}, repeatBytes(255, 18)...)},
|
||||
{10, append([]byte{0, 64}, repeatBytes(255, 18)...)},
|
||||
{16, append([]byte{0, 0}, repeatBytes(255, 18)...)},
|
||||
{17, append([]byte{0, 0, 128}, repeatBytes(255, 17)...)},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
assert.Equal(t, MakePoWTarget(c.difficulty), c.target)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user