github.com/xfers/quorum@v21.1.0+incompatible/params/config_test.go (about) 1 // Copyright 2017 The go-ethereum Authors 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 params 18 19 import ( 20 "math/big" 21 "reflect" 22 "testing" 23 ) 24 25 // Quorum - test code size and transaction size limit in chain config 26 func TestMaxCodeSizeAndTransactionSizeLimit(t *testing.T) { 27 type testData struct { 28 size uint64 29 valid bool 30 err string 31 } 32 type testDataType struct { 33 isCodeSize bool 34 data []testData 35 } 36 37 const codeSizeErr = "Genesis max code size must be between 24 and 128" 38 const txSizeErr = "Genesis transaction size limit must be between 32 and 128" 39 var codeSizeData = []testData{ 40 {23, false, codeSizeErr}, 41 {24, true, ""}, 42 {50, true, ""}, 43 {128, true, ""}, 44 {129, false, codeSizeErr}, 45 } 46 47 var txSizeData = []testData{ 48 {31, false, txSizeErr}, 49 {32, true, ""}, 50 {50, true, ""}, 51 {128, true, ""}, 52 {129, false, txSizeErr}, 53 } 54 55 var testDataArr = []testDataType{ 56 {true, codeSizeData}, 57 {false, txSizeData}, 58 } 59 60 for _, td := range testDataArr { 61 var ccfg *ChainConfig 62 for _, d := range td.data { 63 var msgPrefix string 64 if td.isCodeSize { 65 ccfg = &ChainConfig{MaxCodeSize: d.size, TransactionSizeLimit: 50} 66 msgPrefix = "max code size" 67 } else { 68 ccfg = &ChainConfig{MaxCodeSize: 50, TransactionSizeLimit: d.size} 69 msgPrefix = "transaction size limit" 70 } 71 err := ccfg.IsValid() 72 if d.valid { 73 if err != nil { 74 t.Errorf(msgPrefix+" %d, expected no error but got %v", d.size, err) 75 } 76 } else { 77 if err == nil { 78 t.Errorf(msgPrefix+" %d, expected error but got none", d.size) 79 } else { 80 if err.Error() != d.err { 81 t.Errorf(msgPrefix+" %d, expected error but got %v", d.size, err.Error()) 82 } 83 } 84 } 85 } 86 } 87 } 88 89 func TestCheckCompatible(t *testing.T) { 90 type test struct { 91 stored, new *ChainConfig 92 head uint64 93 wantErr *ConfigCompatError 94 } 95 var storedMaxCodeConfig0, storedMaxCodeConfig1, storedMaxCodeConfig2 []MaxCodeConfigStruct 96 defaultRec := MaxCodeConfigStruct{big.NewInt(0), 24} 97 rec1 := MaxCodeConfigStruct{big.NewInt(5), 32} 98 rec2 := MaxCodeConfigStruct{big.NewInt(10), 40} 99 rec3 := MaxCodeConfigStruct{big.NewInt(8), 40} 100 101 storedMaxCodeConfig0 = append(storedMaxCodeConfig0, defaultRec) 102 103 storedMaxCodeConfig1 = append(storedMaxCodeConfig1, defaultRec) 104 storedMaxCodeConfig1 = append(storedMaxCodeConfig1, rec1) 105 storedMaxCodeConfig1 = append(storedMaxCodeConfig1, rec2) 106 107 storedMaxCodeConfig2 = append(storedMaxCodeConfig2, rec1) 108 storedMaxCodeConfig2 = append(storedMaxCodeConfig2, rec2) 109 110 var passedValidMaxConfig0 []MaxCodeConfigStruct 111 passedValidMaxConfig0 = append(passedValidMaxConfig0, defaultRec) 112 passedValidMaxConfig0 = append(passedValidMaxConfig0, rec1) 113 114 var passedValidMaxConfig1 []MaxCodeConfigStruct 115 passedValidMaxConfig1 = append(passedValidMaxConfig1, defaultRec) 116 passedValidMaxConfig1 = append(passedValidMaxConfig1, rec1) 117 passedValidMaxConfig1 = append(passedValidMaxConfig1, rec3) 118 119 tests := []test{ 120 {stored: AllEthashProtocolChanges, new: AllEthashProtocolChanges, head: 0, wantErr: nil}, 121 {stored: AllEthashProtocolChanges, new: AllEthashProtocolChanges, head: 100, wantErr: nil}, 122 { 123 stored: &ChainConfig{EIP150Block: big.NewInt(10)}, 124 new: &ChainConfig{EIP150Block: big.NewInt(20)}, 125 head: 9, 126 wantErr: nil, 127 }, 128 { 129 stored: AllEthashProtocolChanges, 130 new: &ChainConfig{HomesteadBlock: nil}, 131 head: 3, 132 wantErr: &ConfigCompatError{ 133 What: "Homestead fork block", 134 StoredConfig: big.NewInt(0), 135 NewConfig: nil, 136 RewindTo: 0, 137 }, 138 }, 139 { 140 stored: AllEthashProtocolChanges, 141 new: &ChainConfig{HomesteadBlock: big.NewInt(1)}, 142 head: 3, 143 wantErr: &ConfigCompatError{ 144 What: "Homestead fork block", 145 StoredConfig: big.NewInt(0), 146 NewConfig: big.NewInt(1), 147 RewindTo: 0, 148 }, 149 }, 150 { 151 stored: &ChainConfig{HomesteadBlock: big.NewInt(30), EIP150Block: big.NewInt(10)}, 152 new: &ChainConfig{HomesteadBlock: big.NewInt(25), EIP150Block: big.NewInt(20)}, 153 head: 25, 154 wantErr: &ConfigCompatError{ 155 What: "EIP150 fork block", 156 StoredConfig: big.NewInt(10), 157 NewConfig: big.NewInt(20), 158 RewindTo: 9, 159 }, 160 }, 161 { 162 stored: &ChainConfig{Istanbul: &IstanbulConfig{Ceil2Nby3Block: big.NewInt(10)}}, 163 new: &ChainConfig{Istanbul: &IstanbulConfig{Ceil2Nby3Block: big.NewInt(20)}}, 164 head: 4, 165 wantErr: nil, 166 }, 167 { 168 stored: &ChainConfig{Istanbul: &IstanbulConfig{Ceil2Nby3Block: big.NewInt(10)}}, 169 new: &ChainConfig{Istanbul: &IstanbulConfig{Ceil2Nby3Block: big.NewInt(20)}}, 170 head: 30, 171 wantErr: &ConfigCompatError{ 172 What: "Ceil 2N/3 fork block", 173 StoredConfig: big.NewInt(10), 174 NewConfig: big.NewInt(20), 175 RewindTo: 9, 176 }, 177 }, 178 { 179 stored: &ChainConfig{MaxCodeSizeChangeBlock: big.NewInt(10)}, 180 new: &ChainConfig{MaxCodeSizeChangeBlock: big.NewInt(20)}, 181 head: 30, 182 wantErr: &ConfigCompatError{ 183 What: "max code size change fork block", 184 StoredConfig: big.NewInt(10), 185 NewConfig: big.NewInt(20), 186 RewindTo: 9, 187 }, 188 }, 189 { 190 stored: &ChainConfig{MaxCodeSizeChangeBlock: big.NewInt(10)}, 191 new: &ChainConfig{MaxCodeSizeChangeBlock: big.NewInt(20)}, 192 head: 4, 193 wantErr: nil, 194 }, 195 { 196 stored: &ChainConfig{QIP714Block: big.NewInt(10)}, 197 new: &ChainConfig{QIP714Block: big.NewInt(20)}, 198 head: 30, 199 wantErr: &ConfigCompatError{ 200 What: "permissions fork block", 201 StoredConfig: big.NewInt(10), 202 NewConfig: big.NewInt(20), 203 RewindTo: 9, 204 }, 205 }, 206 { 207 stored: &ChainConfig{QIP714Block: big.NewInt(10)}, 208 new: &ChainConfig{QIP714Block: big.NewInt(20)}, 209 head: 4, 210 wantErr: nil, 211 }, 212 { 213 stored: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig0}, 214 new: &ChainConfig{MaxCodeSizeConfig: nil}, 215 head: 4, 216 wantErr: &ConfigCompatError{ 217 What: "genesis file missing max code size information", 218 StoredConfig: big.NewInt(4), 219 NewConfig: big.NewInt(4), 220 RewindTo: 3, 221 }, 222 }, 223 { 224 stored: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig0}, 225 new: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig0}, 226 head: 4, 227 wantErr: nil, 228 }, 229 { 230 stored: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig0}, 231 new: &ChainConfig{MaxCodeSizeConfig: passedValidMaxConfig0}, 232 head: 10, 233 wantErr: &ConfigCompatError{ 234 What: "maxCodeSizeConfig data incompatible. updating maxCodeSize for past", 235 StoredConfig: big.NewInt(10), 236 NewConfig: big.NewInt(10), 237 RewindTo: 9, 238 }, 239 }, 240 { 241 stored: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig0}, 242 new: &ChainConfig{MaxCodeSizeConfig: passedValidMaxConfig0}, 243 head: 4, 244 wantErr: nil, 245 }, 246 { 247 stored: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig1}, 248 new: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig1}, 249 head: 12, 250 wantErr: nil, 251 }, 252 { 253 stored: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig1}, 254 new: &ChainConfig{MaxCodeSizeConfig: passedValidMaxConfig1}, 255 head: 12, 256 wantErr: &ConfigCompatError{ 257 What: "maxCodeSizeConfig data incompatible. maxCodeSize historical data does not match", 258 StoredConfig: big.NewInt(12), 259 NewConfig: big.NewInt(12), 260 RewindTo: 11, 261 }, 262 }, 263 { 264 stored: &ChainConfig{MaxCodeSize: 32}, 265 new: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig2}, 266 head: 8, 267 wantErr: nil, 268 }, 269 { 270 stored: &ChainConfig{MaxCodeSize: 32}, 271 new: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig2}, 272 head: 15, 273 wantErr: nil, 274 }, 275 { 276 stored: &ChainConfig{MaxCodeSize: 32, MaxCodeSizeChangeBlock: big.NewInt(10)}, 277 new: &ChainConfig{MaxCodeSizeConfig: storedMaxCodeConfig1}, 278 head: 15, 279 wantErr: nil, 280 }, 281 } 282 283 for _, test := range tests { 284 err := test.stored.CheckCompatible(test.new, test.head, false) 285 if !reflect.DeepEqual(err, test.wantErr) { 286 t.Errorf("error mismatch:\nstored: %v\nnew: %v\nhead: %v\nerr: %v\nwant: %v", test.stored, test.new, test.head, err, test.wantErr) 287 } 288 } 289 }