github.com/evdatsion/aphelion-dpos-bft@v0.32.1/p2p/key_test.go (about) 1 package p2p 2 3 import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 cmn "github.com/evdatsion/aphelion-dpos-bft/libs/common" 11 ) 12 13 func TestLoadOrGenNodeKey(t *testing.T) { 14 filePath := filepath.Join(os.TempDir(), cmn.RandStr(12)+"_peer_id.json") 15 16 nodeKey, err := LoadOrGenNodeKey(filePath) 17 assert.Nil(t, err) 18 19 nodeKey2, err := LoadOrGenNodeKey(filePath) 20 assert.Nil(t, err) 21 22 assert.Equal(t, nodeKey, nodeKey2) 23 } 24 25 //---------------------------------------------------------- 26 27 func padBytes(bz []byte, targetBytes int) []byte { 28 return append(bz, bytes.Repeat([]byte{0xFF}, targetBytes-len(bz))...) 29 } 30 31 func TestPoWTarget(t *testing.T) { 32 33 targetBytes := 20 34 cases := []struct { 35 difficulty uint 36 target []byte 37 }{ 38 {0, padBytes([]byte{}, targetBytes)}, 39 {1, padBytes([]byte{127}, targetBytes)}, 40 {8, padBytes([]byte{0}, targetBytes)}, 41 {9, padBytes([]byte{0, 127}, targetBytes)}, 42 {10, padBytes([]byte{0, 63}, targetBytes)}, 43 {16, padBytes([]byte{0, 0}, targetBytes)}, 44 {17, padBytes([]byte{0, 0, 127}, targetBytes)}, 45 } 46 47 for _, c := range cases { 48 assert.Equal(t, MakePoWTarget(c.difficulty, 20*8), c.target) 49 } 50 }