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.

54 lines
998 B

5 years ago
  1. package core
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestHashBytes(t *testing.T) {
  9. m := []byte("test")
  10. h := HashBytes(m)
  11. assert.Equal(t, hex.EncodeToString(h[:]), "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
  12. assert.True(t, !h.IsZero())
  13. z := Hash{}
  14. assert.True(t, z.IsZero())
  15. }
  16. type testData struct {
  17. Data []byte
  18. Nonce uint64
  19. }
  20. func (d *testData) Bytes() []byte {
  21. b, _ := json.Marshal(d)
  22. return b
  23. }
  24. func (d *testData) GetNonce() uint64 {
  25. return d.Nonce
  26. }
  27. func (d *testData) IncrementNonce() {
  28. d.Nonce++
  29. }
  30. func TestPoW(t *testing.T) {
  31. difficulty := 2
  32. data := &testData{
  33. Data: []byte("test"),
  34. Nonce: 0,
  35. }
  36. nonce, err := CalculatePoW(data, difficulty)
  37. assert.Nil(t, err)
  38. data.Nonce = nonce
  39. h := HashBytes(data.Bytes())
  40. assert.Equal(t, hex.EncodeToString(h[:]), "0000020881c02f5171b978e74bb710242e95cc67b36416e382118a7ab2a69321")
  41. // CheckPoW
  42. assert.True(t, CheckPoW(h, difficulty))
  43. }