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