github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/consensus/podc/core/handler_test.go (about) 1 // Copyright 2017 AMIS Technologies 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "github.com/ethereum/go-ethereum/common" 21 "math/big" 22 "testing" 23 "github.com/ethereum/go-ethereum/consensus/podc" 24 ) 25 26 // notice: the normal case have been tested in integration tests. 27 func TestHandleMsg(t *testing.T) { 28 N := uint64(28) 29 F := uint64(1) 30 sys := NewTestSystemWithBackend(N, F) 31 32 closer := sys.Run(true) 33 defer closer() 34 35 v0 := sys.backends[0] 36 r0 := v0.engine.(*core) 37 38 m, _ := Encode(&podc.Subject{ 39 View: &podc.View{ 40 Sequence: big.NewInt(0), 41 Round: big.NewInt(0), 42 }, 43 Digest: common.StringToHash("1234567890"), 44 }) 45 // with a matched payload. msgPreprepare should match with *istanbul.Preprepare in normal case. 46 msg := &message{ 47 Code: msgPreprepare, 48 Msg: m, 49 Address: v0.Address(), 50 Signature: []byte{}, 51 CommittedSeal: []byte{}, 52 } 53 54 _, val := v0.Validators(nil).GetByAddress(v0.Address()) 55 if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodePreprepare { 56 t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodePreprepare) 57 } 58 59 60 //m, _ = Encode(&podc.Preprepare{ 61 // View: &podc.View{ 62 // Sequence: big.NewInt(0), 63 // Round: big.NewInt(0), 64 // }, 65 // Digest: common.StringToHash("1234567890"), 66 //}) 67 //// with a unmatched payload. msgDSelect should match with *istanbul.Subject in normal case. 68 //msg = &message{ 69 // Code: msgDSelect, 70 // Msg: m, 71 // Address: v0.Address(), 72 // Signature: []byte{}, 73 // CommittedSeal: []byte{}, 74 //} 75 // 76 //_, val := v0.Validators(nil).GetByAddress(v0.Address()) 77 //if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodePreprepare { 78 // t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodePreprepare) 79 //} 80 81 m, _ := Encode(&podc.Proposal{ 82 View: &podc.View{ 83 Sequence: big.NewInt(1), 84 Round: big.NewInt(1), 85 }, 86 Proposal: makeBlock(1), 87 }) 88 // with a unmatched payload. msgPrepare should match with *istanbul.Subject in normal case. 89 msg = &message{ 90 Code: msgPreprepare, 91 Msg: m, 92 Address: v0.Address(), 93 Signature: []byte{}, 94 CommittedSeal: []byte{}, 95 } 96 97 _, val = v0.Validators(nil).GetByAddress(v0.Address()) 98 if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodePrepare { 99 t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodePreprepare) 100 } 101 102 //m, _ = Encode(&podc.Preprepare{ 103 // View: &podc.View{ 104 // Sequence: big.NewInt(0), 105 // Round: big.NewInt(0), 106 // }, 107 // Proposal: makeBlock(2), 108 //}) 109 //// with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case. 110 //msg = &message{ 111 // Code: msgCommit, 112 // Msg: m, 113 // Address: v0.Address(), 114 // Signature: []byte{}, 115 // CommittedSeal: []byte{}, 116 //} 117 // 118 //_, val = v0.Validators(nil).GetByAddress(v0.Address()) 119 //if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodeCommit { 120 // t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodeCommit) 121 //} 122 // 123 //m, _ = Encode(&podc.Preprepare{ 124 // View: &podc.View{ 125 // Sequence: big.NewInt(0), 126 // Round: big.NewInt(0), 127 // }, 128 // Proposal: makeBlock(3), 129 //}) 130 //// invalid message code. message code is not exists in list 131 //msg = &message{ 132 // Code: uint64(99), 133 // Msg: m, 134 // Address: v0.Address(), 135 // Signature: []byte{}, 136 // CommittedSeal: []byte{}, 137 //} 138 // 139 //_, val = v0.Validators(nil).GetByAddress(v0.Address()) 140 //if err := r0.handleCheckedMsg(msg, val); err == nil { 141 // t.Errorf("error mismatch: have %v, want nil", err) 142 //} 143 // 144 //// with malicious payload 145 //if err := r0.handleMsg([]byte{1}); err == nil { 146 // t.Errorf("error mismatch: have %v, want nil", err) 147 //} 148 ////--------------------- yichoi ------------------------ 149 // 150 //m, _ = Encode(&podc.Preprepare{ 151 // View: &podc.View{ 152 // Sequence: big.NewInt(0), 153 // Round: big.NewInt(0), 154 // }, 155 // Proposal: makeBlock(2), 156 //}) 157 //// with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case. 158 //msg = &message{ 159 // Code: msgDSelect, 160 // Msg: m, 161 // Address: v0.Address(), 162 // Signature: []byte{}, 163 // CommittedSeal: []byte{}, 164 //} 165 // 166 //_, val = v0.Validators(nil).GetByAddress(v0.Address()) 167 //if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodeCommit { 168 // t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodeCommit) 169 //} 170 ////------------------------------------- 171 //m, _ = Encode(&podc.Preprepare{ 172 // View: &podc.View{ 173 // Sequence: big.NewInt(0), 174 // Round: big.NewInt(0), 175 // }, 176 // Proposal: makeBlock(2), 177 //}) 178 //// with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case. 179 //msg = &message{ 180 // Code: msgCoordinatorDecide, 181 // Msg: m, 182 // Address: v0.Address(), 183 // Signature: []byte{}, 184 // CommittedSeal: []byte{}, 185 //} 186 // 187 //_, val = v0.Validators(nil).GetByAddress(v0.Address()) 188 //if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodeCommit { 189 // t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodeCommit) 190 //} 191 ////------------------------------------- 192 //m, _ = Encode(&podc.Preprepare{ 193 // View: &podc.View{ 194 // Sequence: big.NewInt(0), 195 // Round: big.NewInt(0), 196 // }, 197 // Proposal: makeBlock(2), 198 //}) 199 //// with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case. 200 //msg = &message{ 201 // Code: msgRacing, 202 // Msg: m, 203 // Address: v0.Address(), 204 // Signature: []byte{}, 205 // CommittedSeal: []byte{}, 206 //} 207 // 208 //_, val = v0.Validators(nil).GetByAddress(v0.Address()) 209 //if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodeCommit { 210 // t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodeCommit) 211 //} 212 ////------------------------------------- 213 //m, _ = Encode(&podc.Preprepare{ 214 // View: &podc.View{ 215 // Sequence: big.NewInt(0), 216 // Round: big.NewInt(0), 217 // }, 218 // Proposal: makeBlock(2), 219 //}) 220 //// with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case. 221 //msg = &message{ 222 // Code: msgCandidateDecide, 223 // Msg: m, 224 // Address: v0.Address(), 225 // Signature: []byte{}, 226 // CommittedSeal: []byte{}, 227 //} 228 // 229 //_, val = v0.Validators(nil).GetByAddress(v0.Address()) 230 //if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodeCommit { 231 // t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodeCommit) 232 //} 233 // 234 //m, _ = Encode(&podc.Preprepare{ 235 // View: &podc.View{ 236 // Sequence: big.NewInt(0), 237 // Round: big.NewInt(0), 238 // }, 239 // Proposal: makeBlock(2), 240 //}) 241 //// with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case. 242 //msg = &message{ 243 // Code: msgRoundChange, 244 // Msg: m, 245 // Address: v0.Address(), 246 // Signature: []byte{}, 247 // CommittedSeal: []byte{}, 248 //} 249 // 250 //_, val = v0.Validators(nil).GetByAddress(v0.Address()) 251 //if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodeCommit { 252 // t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodeCommit) 253 //} 254 }