You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.1 KiB

  1. package p2p
  2. import (
  3. "bytes"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. cmn "github.com/tendermint/tmlibs/common"
  9. )
  10. func TestLoadOrGenNodeKey(t *testing.T) {
  11. filePath := filepath.Join(os.TempDir(), cmn.RandStr(12)+"_peer_id.json")
  12. target := MakePoWTarget(2)
  13. nodeKey, err := LoadOrGenNodeKey(filePath, target)
  14. assert.Nil(t, err)
  15. nodeKey2, err := LoadOrGenNodeKey(filePath, target)
  16. assert.Nil(t, err)
  17. assert.Equal(t, nodeKey, nodeKey2)
  18. }
  19. func repeatBytes(val byte, n int) []byte {
  20. return bytes.Repeat([]byte{val}, n)
  21. }
  22. func TestPoWTarget(t *testing.T) {
  23. cases := []struct {
  24. difficulty uint8
  25. target []byte
  26. }{
  27. {0, bytes.Repeat([]byte{255}, 20)},
  28. {1, append([]byte{128}, repeatBytes(255, 19)...)},
  29. {8, append([]byte{0}, repeatBytes(255, 19)...)},
  30. {9, append([]byte{0, 128}, repeatBytes(255, 18)...)},
  31. {10, append([]byte{0, 64}, repeatBytes(255, 18)...)},
  32. {16, append([]byte{0, 0}, repeatBytes(255, 18)...)},
  33. {17, append([]byte{0, 0, 128}, repeatBytes(255, 17)...)},
  34. }
  35. for _, c := range cases {
  36. assert.Equal(t, MakePoWTarget(c.difficulty), c.target)
  37. }
  38. }