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.

93 lines
3.4 KiB

  1. package difficulty
  2. import "testing"
  3. import "github.com/deroproject/derosuite/crypto"
  4. func Test_Next_Difficulty(t *testing.T) {
  5. target_seconds := uint64(120)
  6. var cumulative_difficulties []uint64
  7. var timestamps []uint64
  8. calculated := Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  9. expected := uint64(1)
  10. if calculated != expected {
  11. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  12. }
  13. timestamps = append(timestamps, 1)
  14. cumulative_difficulties = append(cumulative_difficulties, 1)
  15. calculated = Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  16. expected = uint64(1)
  17. if calculated != expected {
  18. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  19. }
  20. timestamps = append(timestamps, 1)
  21. cumulative_difficulties = append(cumulative_difficulties, 3)
  22. calculated = Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  23. expected = uint64(3)
  24. if calculated != expected {
  25. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  26. }
  27. timestamps = append(timestamps, 1)
  28. cumulative_difficulties = append(cumulative_difficulties, 10)
  29. calculated = Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  30. expected = uint64(18)
  31. if calculated != expected {
  32. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  33. }
  34. timestamps = append(timestamps, 1)
  35. cumulative_difficulties = append(cumulative_difficulties, 20)
  36. calculated = Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  37. expected = uint64(28)
  38. if calculated != expected {
  39. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  40. }
  41. }
  42. /*
  43. * raw data from daemon
  44. *
  45. * /*
  46. * 2018-01-07 20:18:21.157 [P2P4] INFO global src/cryptonote_core/blockchain.cpp:1436 ----- BLOCK ADDED AS ALTERNATIVE ON HEIGHT 16163
  47. id: <f9a3faa33054a4a1fa349321c546ee5f42cc416f13a991152c64fcbef994518b>
  48. PoW: <28b6fdf6655c45631c28be02bc528342b25fe913696911b788a01e0b0a000000>
  49. difficulty: 77897895
  50. 2018-01-07 21:17:40.182 [P2P9] INFO global src/cryptonote_protocol/cryptonote_protocol_handler.inl:1521 SYNCHRONIZED OK
  51. 2018-01-07 22:04:14.368 [P2P2] INFO global src/p2p/net_node.inl:258 Host 125.161.128.47 blocked.
  52. status
  53. Height: 16294/16294 (100.0%) on mainnet, not mining, net hash 780.13 kH/s, v6, up to date, 0(out)+15(in) connections, uptime 0d 10h 29m 2s
  54. 2018-01-08 03:14:37.490 [P2P1] INFO global src/cryptonote_core/blockchain.cpp:1436 ----- BLOCK ADDED AS ALTERNATIVE ON HEIGHT 13618
  55. id: <a3918ac81a08e8740f99f79ff788d9e147ceb7e530ed590ac1e0f5d1cbba28c5>
  56. PoW: <b34caa51543b82efee0336677dd825e3236220e69d2f090c58df0b3e05000000>
  57. difficulty: 90940906
  58. */
  59. func Test_CheckPowHash(t *testing.T) {
  60. hash := crypto.Hash{0x28, 0xb6, 0xfd, 0xf6, 0x65, 0x5c, 0x45, 0x63, 0x1c, 0x28, 0xbe,
  61. 0x02, 0xbc, 0x52, 0x83, 0x42, 0xb2, 0x5f, 0xe9, 0x13, 0x69, 0x69,
  62. 0x11, 0xb7, 0x88, 0xa0, 0x1e, 0x0b, 0x0a, 0x00, 0x00, 0x00}
  63. difficulty := uint64(77897895)
  64. if !CheckPowHash(hash, difficulty) {
  65. t.Errorf("POW check failedm, severe BUG\n")
  66. }
  67. hash = crypto.Hash{0xb3, 0x4c, 0xaa, 0x51, 0x54, 0x3b, 0x82, 0xef, 0xee, 0x03, 0x36, 0x67,
  68. 0x7d, 0xd8, 0x25, 0xe3, 0x23, 0x62, 0x20, 0xe6, 0x9d, 0x2f, 0x09,
  69. 0x0c, 0x58, 0xdf, 0x0b, 0x3e, 0x05, 0x00, 0x00, 0x00}
  70. difficulty = uint64(77897895)
  71. if !CheckPowHash(hash, difficulty) {
  72. t.Errorf("POW check 2 failed, severe BUG\n")
  73. }
  74. }