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.

144 lines
4.6 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 inputmaturity
  17. import "testing"
  18. import "github.com/arnaucode/derosuite/config"
  19. //this file implements the logic to detect whether an input is mature or still locked
  20. //This function is crucial as any bugs can have catastrophic effects
  21. //this function is used both by the core blockchain and wallet
  22. //TODO we need to check the edge cases
  23. func Test_Input_Maturity(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. current_chain_height uint64
  27. input_block_height uint64
  28. locked_to_height uint64
  29. sigtype uint64
  30. expected bool
  31. }{
  32. {
  33. name: "simple test",
  34. current_chain_height: 0,
  35. input_block_height: 1,
  36. locked_to_height: 0,
  37. sigtype: 0,
  38. expected: false,
  39. },
  40. // miner tx blocks mature in 60 blocks
  41. {
  42. name: "miner test 59",
  43. current_chain_height: 59,
  44. input_block_height: 0,
  45. locked_to_height: 0,
  46. sigtype: 0,
  47. expected: false,
  48. },
  49. {
  50. name: "miner test 60",
  51. current_chain_height: 60,
  52. input_block_height: 0,
  53. locked_to_height: 0,
  54. sigtype: 0,
  55. expected: true,
  56. },
  57. {
  58. name: "miner test 60", // genesis block reward should mature at block 61
  59. current_chain_height: 61,
  60. input_block_height: 0,
  61. locked_to_height: 0,
  62. sigtype: 0,
  63. expected: true,
  64. },
  65. // normal tx output matures in 10 blocks
  66. {
  67. name: "normal test 9", // reward should mature at block 11
  68. current_chain_height: 9,
  69. input_block_height: 0,
  70. locked_to_height: 0,
  71. sigtype: 1,
  72. expected: false,
  73. },
  74. {
  75. name: "normal test 10", //reward should mature at block 10
  76. current_chain_height: 10,
  77. input_block_height: 0,
  78. locked_to_height: 0,
  79. sigtype: 1,
  80. expected: true,
  81. },
  82. {
  83. name: "normal test 11", // reward should mature at block 11
  84. current_chain_height: 11,
  85. input_block_height: 0,
  86. locked_to_height: 0,
  87. sigtype: 1,
  88. expected: true,
  89. },
  90. // height based lock
  91. {
  92. name: "locked_to_height ", // reward should mature at specific block
  93. current_chain_height: config.CRYPTONOTE_MAX_BLOCK_NUMBER - 1,
  94. input_block_height: 0,
  95. locked_to_height: config.CRYPTONOTE_MAX_BLOCK_NUMBER - 1,
  96. sigtype: 1,
  97. expected: true,
  98. },
  99. {
  100. name: "locked_to_height false", // reward should mature at block 11
  101. current_chain_height: config.CRYPTONOTE_MAX_BLOCK_NUMBER - 2,
  102. input_block_height: 0,
  103. locked_to_height: config.CRYPTONOTE_MAX_BLOCK_NUMBER - 1,
  104. sigtype: 1,
  105. expected: false,
  106. },
  107. // time based locked
  108. {
  109. name: "locked_to_time false",
  110. current_chain_height: config.CRYPTONOTE_MAX_BLOCK_NUMBER,
  111. input_block_height: 0,
  112. locked_to_height: 15174219710,
  113. sigtype: 1,
  114. expected: false,
  115. },
  116. {
  117. name: "locked_to_time true",
  118. current_chain_height: config.CRYPTONOTE_MAX_BLOCK_NUMBER,
  119. input_block_height: 0,
  120. locked_to_height: 1517421971,
  121. sigtype: 1,
  122. expected: true,
  123. },
  124. }
  125. for _, test := range tests {
  126. actual := Is_Input_Mature(test.current_chain_height, test.input_block_height, test.locked_to_height, test.sigtype)
  127. if actual != test.expected {
  128. t.Fatalf("Input Maturity testing failed name %s actual %v expected %v", test.name, actual, test.expected)
  129. }
  130. }
  131. }