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.

50 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. "github.com/tendermint/tendermint/libs/rand"
  9. )
  10. func TestLoadOrGenNodeKey(t *testing.T) {
  11. filePath := filepath.Join(os.TempDir(), rand.RandStr(12)+"_peer_id.json")
  12. nodeKey, err := LoadOrGenNodeKey(filePath)
  13. assert.Nil(t, err)
  14. nodeKey2, err := LoadOrGenNodeKey(filePath)
  15. assert.Nil(t, err)
  16. assert.Equal(t, nodeKey, nodeKey2)
  17. }
  18. //----------------------------------------------------------
  19. func padBytes(bz []byte, targetBytes int) []byte {
  20. return append(bz, bytes.Repeat([]byte{0xFF}, targetBytes-len(bz))...)
  21. }
  22. func TestPoWTarget(t *testing.T) {
  23. targetBytes := 20
  24. cases := []struct {
  25. difficulty uint
  26. target []byte
  27. }{
  28. {0, padBytes([]byte{}, targetBytes)},
  29. {1, padBytes([]byte{127}, targetBytes)},
  30. {8, padBytes([]byte{0}, targetBytes)},
  31. {9, padBytes([]byte{0, 127}, targetBytes)},
  32. {10, padBytes([]byte{0, 63}, targetBytes)},
  33. {16, padBytes([]byte{0, 0}, targetBytes)},
  34. {17, padBytes([]byte{0, 0, 127}, targetBytes)},
  35. }
  36. for _, c := range cases {
  37. assert.Equal(t, MakePoWTarget(c.difficulty, 20*8), c.target)
  38. }
  39. }