github.com/nnlgsakib/mind-dpos@v0.0.0-20230606105614-f3c8ca06f808/consensus/alien/alien_test.go (about) 1 // Copyright 2019 The TTC Authors 2 // This file is part of the TTC library. 3 // 4 // The TTC library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The TTC library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the TTC library. If not, see <http://www.gnu.org/licenses/>. 16 17 package alien 18 19 import ( 20 "testing" 21 22 "github.com/TTCECO/gttc/common" 23 ) 24 25 func TestAlien_PenaltyTrantor(t *testing.T) { 26 tests := []struct { 27 last string 28 current string 29 queue []string 30 lastQ []string 31 result []string // the result of missing 32 }{ 33 { 34 /* Case 0: 35 * simple loop order, miss nothing 36 * A -> B -> C 37 */ 38 last: "A", 39 current: "B", 40 queue: []string{"A", "B", "C"}, 41 lastQ: []string{}, 42 result: []string{}, 43 }, 44 { 45 /* Case 1: 46 * same loop, missing B 47 * A -> B -> C 48 */ 49 last: "A", 50 current: "C", 51 queue: []string{"A", "B", "C"}, 52 lastQ: []string{}, 53 result: []string{"B"}, 54 }, 55 { 56 /* Case 2: 57 * same loop, not start from the first one 58 * C -> A -> B 59 */ 60 last: "C", 61 current: "B", 62 queue: []string{"A", "B", "C"}, 63 lastQ: []string{}, 64 result: []string{"A"}, 65 }, 66 { 67 /* Case 3: 68 * same loop, missing two 69 * A -> B -> C 70 */ 71 last: "C", 72 current: "C", 73 queue: []string{"A", "B", "C"}, 74 lastQ: []string{}, 75 result: []string{"A", "B"}, 76 }, 77 { 78 /* Case 4: 79 * cross loop 80 * B -> A -> B -> C -> A 81 */ 82 last: "B", 83 current: "B", 84 queue: []string{"A", "B", "C"}, 85 lastQ: []string{"C", "A", "B"}, 86 result: []string{"A"}, 87 }, 88 { 89 /* Case 5: 90 * cross loop, nothing missing 91 * A -> C -> A -> B -> C 92 */ 93 last: "A", 94 current: "C", 95 queue: []string{"A", "B", "C"}, 96 lastQ: []string{"C", "A", "B"}, 97 result: []string{}, 98 }, 99 { 100 /* Case 6: 101 * cross loop, two signers missing in last loop 102 * C -> B -> C -> A 103 */ 104 last: "C", 105 current: "A", 106 queue: []string{"A", "B", "C"}, 107 lastQ: []string{"C", "A", "B"}, 108 result: []string{"B", "C"}, 109 }, 110 } 111 112 // Run through the test 113 for i, tt := range tests { 114 // Create the account pool and generate the initial set of all address in addrNames 115 accounts := newTesterAccountPool() 116 addrQueue := make([]common.Address, len(tt.queue)) 117 for j, signer := range tt.queue { 118 addrQueue[j] = accounts.address(signer) 119 } 120 121 extra := HeaderExtra{SignerQueue: addrQueue} 122 var lastExtra HeaderExtra 123 if len(tt.lastQ) > 0 { 124 lastAddrQueue := make([]common.Address, len(tt.lastQ)) 125 for j, signer := range tt.lastQ { 126 lastAddrQueue[j] = accounts.address(signer) 127 } 128 lastExtra = HeaderExtra{SignerQueue: lastAddrQueue} 129 } 130 131 missing := getSignerMissingTrantor(accounts.address(tt.last), accounts.address(tt.current), &extra, &lastExtra) 132 133 signersMissing := make(map[string]bool) 134 for _, signer := range missing { 135 signersMissing[accounts.name(signer)] = true 136 } 137 if len(missing) != len(tt.result) { 138 t.Errorf("test %d: the length of missing not equal to the length of result, Result is %v not %v ", i, signersMissing, tt.result) 139 } 140 141 for j := 0; j < len(missing); j++ { 142 if _, ok := signersMissing[tt.result[j]]; !ok { 143 t.Errorf("test %d: the signersMissing is not equal Result is %v not %v ", i, signersMissing, tt.result) 144 } 145 } 146 147 } 148 } 149 150 func TestAlien_Penalty(t *testing.T) { 151 tests := []struct { 152 last string 153 current string 154 queue []string 155 newLoop bool 156 result []string // the result of current snapshot 157 }{ 158 { 159 /* Case 0: 160 * simple loop order 161 */ 162 last: "A", 163 current: "B", 164 queue: []string{"A", "B", "C"}, 165 newLoop: false, 166 result: []string{}, 167 }, 168 { 169 /* Case 1: 170 * simple loop order, new loop, no matter which one is current signer 171 */ 172 last: "C", 173 current: "A", 174 queue: []string{"A", "B", "C"}, 175 newLoop: true, 176 result: []string{}, 177 }, 178 { 179 /* Case 2: 180 * simple loop order, new loop, no matter which one is current signer 181 */ 182 last: "C", 183 current: "B", 184 queue: []string{"A", "B", "C"}, 185 newLoop: true, 186 result: []string{}, 187 }, 188 { 189 /* Case 3: 190 * simple loop order, new loop, missing in last loop 191 */ 192 last: "B", 193 current: "C", 194 queue: []string{"A", "B", "C"}, 195 newLoop: true, 196 result: []string{"C"}, 197 }, 198 { 199 /* Case 4: 200 * simple loop order, new loop, two signers missing in last loop 201 */ 202 last: "A", 203 current: "C", 204 queue: []string{"A", "B", "C"}, 205 newLoop: true, 206 result: []string{"B", "C"}, 207 }, 208 } 209 210 // Run through the test 211 for i, tt := range tests { 212 // Create the account pool and generate the initial set of all address in addrNames 213 accounts := newTesterAccountPool() 214 addrQueue := make([]common.Address, len(tt.queue)) 215 for j, signer := range tt.queue { 216 addrQueue[j] = accounts.address(signer) 217 } 218 219 extra := HeaderExtra{SignerQueue: addrQueue} 220 missing := getSignerMissing(accounts.address(tt.last), accounts.address(tt.current), extra, tt.newLoop) 221 222 signersMissing := make(map[string]bool) 223 for _, signer := range missing { 224 signersMissing[accounts.name(signer)] = true 225 } 226 if len(missing) != len(tt.result) { 227 t.Errorf("test %d: the length of missing not equal to the length of result, Result is %v not %v ", i, signersMissing, tt.result) 228 } 229 230 for j := 0; j < len(missing); j++ { 231 if _, ok := signersMissing[tt.result[j]]; !ok { 232 t.Errorf("test %d: the signersMissing is not equal Result is %v not %v ", i, signersMissing, tt.result) 233 } 234 } 235 236 } 237 }