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.

109 lines
4.4 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package difficulty
  17. import "testing"
  18. import "github.com/deroproject/derosuite/crypto"
  19. func Test_Next_Difficulty(t *testing.T) {
  20. target_seconds := uint64(120)
  21. var cumulative_difficulties []uint64
  22. var timestamps []uint64
  23. calculated := Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  24. expected := uint64(1)
  25. if calculated != expected {
  26. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  27. }
  28. timestamps = append(timestamps, 1)
  29. cumulative_difficulties = append(cumulative_difficulties, 1)
  30. calculated = Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  31. expected = uint64(1)
  32. if calculated != expected {
  33. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  34. }
  35. timestamps = append(timestamps, 1)
  36. cumulative_difficulties = append(cumulative_difficulties, 3)
  37. calculated = Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  38. expected = uint64(3)
  39. if calculated != expected {
  40. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  41. }
  42. timestamps = append(timestamps, 1)
  43. cumulative_difficulties = append(cumulative_difficulties, 10)
  44. calculated = Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  45. expected = uint64(18)
  46. if calculated != expected {
  47. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  48. }
  49. timestamps = append(timestamps, 1)
  50. cumulative_difficulties = append(cumulative_difficulties, 20)
  51. calculated = Next_Difficulty(timestamps, cumulative_difficulties, target_seconds)
  52. expected = uint64(28)
  53. if calculated != expected {
  54. t.Errorf("Difficulty should be %d found %d\n", expected, calculated)
  55. }
  56. }
  57. /*
  58. * raw data from daemon
  59. *
  60. * /*
  61. * 2018-01-07 20:18:21.157 [P2P4] INFO global src/cryptonote_core/blockchain.cpp:1436 ----- BLOCK ADDED AS ALTERNATIVE ON HEIGHT 16163
  62. id: <f9a3faa33054a4a1fa349321c546ee5f42cc416f13a991152c64fcbef994518b>
  63. PoW: <28b6fdf6655c45631c28be02bc528342b25fe913696911b788a01e0b0a000000>
  64. difficulty: 77897895
  65. 2018-01-07 21:17:40.182 [P2P9] INFO global src/cryptonote_protocol/cryptonote_protocol_handler.inl:1521 SYNCHRONIZED OK
  66. 2018-01-07 22:04:14.368 [P2P2] INFO global src/p2p/net_node.inl:258 Host 125.161.128.47 blocked.
  67. status
  68. 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
  69. 2018-01-08 03:14:37.490 [P2P1] INFO global src/cryptonote_core/blockchain.cpp:1436 ----- BLOCK ADDED AS ALTERNATIVE ON HEIGHT 13618
  70. id: <a3918ac81a08e8740f99f79ff788d9e147ceb7e530ed590ac1e0f5d1cbba28c5>
  71. PoW: <b34caa51543b82efee0336677dd825e3236220e69d2f090c58df0b3e05000000>
  72. difficulty: 90940906
  73. */
  74. func Test_CheckPowHash(t *testing.T) {
  75. hash := crypto.Hash{0x28, 0xb6, 0xfd, 0xf6, 0x65, 0x5c, 0x45, 0x63, 0x1c, 0x28, 0xbe,
  76. 0x02, 0xbc, 0x52, 0x83, 0x42, 0xb2, 0x5f, 0xe9, 0x13, 0x69, 0x69,
  77. 0x11, 0xb7, 0x88, 0xa0, 0x1e, 0x0b, 0x0a, 0x00, 0x00, 0x00}
  78. difficulty := uint64(77897895)
  79. if !CheckPowHash(hash, difficulty) {
  80. t.Errorf("POW check failedm, severe BUG\n")
  81. }
  82. hash = crypto.Hash{0xb3, 0x4c, 0xaa, 0x51, 0x54, 0x3b, 0x82, 0xef, 0xee, 0x03, 0x36, 0x67,
  83. 0x7d, 0xd8, 0x25, 0xe3, 0x23, 0x62, 0x20, 0xe6, 0x9d, 0x2f, 0x09,
  84. 0x0c, 0x58, 0xdf, 0x0b, 0x3e, 0x05, 0x00, 0x00, 0x00}
  85. difficulty = uint64(77897895)
  86. if !CheckPowHash(hash, difficulty) {
  87. t.Errorf("POW check 2 failed, severe BUG\n")
  88. }
  89. }