github.com/m3shine/gochain@v2.2.26+incompatible/core/block_validator_test.go (about) 1 // Copyright 2015 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 core 18 19 import ( 20 "context" 21 "runtime" 22 "testing" 23 "time" 24 25 "github.com/gochain-io/gochain/common/hexutil" 26 "github.com/gochain-io/gochain/consensus/clique" 27 "github.com/gochain-io/gochain/core/types" 28 "github.com/gochain-io/gochain/core/vm" 29 "github.com/gochain-io/gochain/ethdb" 30 "github.com/gochain-io/gochain/params" 31 ) 32 33 // Tests that simple header verification works, for both good and bad blocks. 34 func TestHeaderVerification(t *testing.T) { 35 ctx := context.Background() 36 37 // Create a simple chain to verify 38 var ( 39 testdb = ethdb.NewMemDatabase() 40 gspec = &Genesis{ 41 Config: params.TestChainConfig, 42 Signer: hexutil.MustDecode("0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), 43 } 44 genesis = gspec.MustCommit(testdb) 45 blocks, _ = GenerateChain(ctx, params.TestChainConfig, genesis, clique.NewFaker(), testdb, 8, nil) 46 ) 47 headers := make([]*types.Header, len(blocks)) 48 for i, block := range blocks { 49 headers[i] = block.Header() 50 } 51 // Run the header checker for blocks one-by-one, checking for both valid and invalid nonces 52 chain, _ := NewBlockChain(ctx, testdb, nil, params.TestChainConfig, clique.NewFaker(), vm.Config{}) 53 defer chain.Stop() 54 55 for i := 0; i < len(blocks); i++ { 56 for j, valid := range []bool{true, false} { 57 var results <-chan error 58 59 if valid { 60 engine := clique.NewFaker() 61 _, results = engine.VerifyHeaders(ctx, chain, []*types.Header{headers[i]}) 62 } else { 63 engine := clique.NewFakeFailer(headers[i].Number.Uint64()) 64 _, results = engine.VerifyHeaders(ctx, chain, []*types.Header{headers[i]}) 65 } 66 // Wait for the verification result 67 select { 68 case err := <-results: 69 if err == nil && !valid { 70 t.Errorf("test %d.%d: expected error", i, j) 71 } else if err != nil && valid { 72 t.Errorf("test %d.%d: unexpected error: %v", i, j, err) 73 } 74 case <-time.After(time.Second): 75 t.Fatalf("test %d.%d: verification timeout", i, j) 76 } 77 // Make sure no more data is returned 78 select { 79 case result, ok := <-results: 80 if ok { 81 t.Fatalf("test %d.%d: unexpected result returned: %v", i, j, result) 82 } 83 case <-time.After(25 * time.Millisecond): 84 } 85 } 86 chain.InsertChain(ctx, blocks[i:i+1]) 87 } 88 } 89 90 // Tests that concurrent header verification works, for both good and bad blocks. 91 func TestHeaderConcurrentVerification2(t *testing.T) { testHeaderConcurrentVerification(t, 2) } 92 func TestHeaderConcurrentVerification8(t *testing.T) { testHeaderConcurrentVerification(t, 8) } 93 func TestHeaderConcurrentVerification32(t *testing.T) { testHeaderConcurrentVerification(t, 32) } 94 95 func testHeaderConcurrentVerification(t *testing.T, threads int) { 96 ctx := context.Background() 97 // Create a simple chain to verify 98 var ( 99 testdb = ethdb.NewMemDatabase() 100 gspec = &Genesis{ 101 Config: params.TestChainConfig, 102 Signer: hexutil.MustDecode("0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), 103 } 104 genesis = gspec.MustCommit(testdb) 105 blocks, _ = GenerateChain(ctx, params.TestChainConfig, genesis, clique.NewFaker(), testdb, 8, nil) 106 ) 107 headers := make([]*types.Header, len(blocks)) 108 seals := make([]bool, len(blocks)) 109 110 for i, block := range blocks { 111 headers[i] = block.Header() 112 seals[i] = true 113 } 114 // Set the number of threads to verify on 115 old := runtime.GOMAXPROCS(threads) 116 defer runtime.GOMAXPROCS(old) 117 118 // Run the header checker for the entire block chain at once both for a valid and 119 // also an invalid chain (enough if one arbitrary block is invalid). 120 for i, valid := range []bool{true, false} { 121 var results <-chan error 122 123 if valid { 124 chain, _ := NewBlockChain(ctx, testdb, nil, params.TestChainConfig, clique.NewFaker(), vm.Config{}) 125 _, results = chain.engine.VerifyHeaders(ctx, chain, headers) 126 chain.Stop() 127 } else { 128 chain, _ := NewBlockChain(ctx, testdb, nil, params.TestChainConfig, clique.NewFakeFailer(uint64(len(headers)-1)), vm.Config{}) 129 _, results = chain.engine.VerifyHeaders(ctx, chain, headers) 130 chain.Stop() 131 } 132 // Wait for all the verification results 133 errs := make(map[int]error) 134 for j := 0; j < len(blocks); j++ { 135 select { 136 case result := <-results: 137 errs[j] = result 138 139 case <-time.After(time.Second): 140 t.Fatalf("test %d.%d: verification timeout", i, j) 141 } 142 } 143 // Check nonce check validity 144 for j := 0; j < len(blocks); j++ { 145 expValid := valid || (j < len(blocks)-2) // We chose the last-but-one nonce in the chain to fail 146 if expValid && errs[j] != nil { 147 t.Errorf("test %d.%d: unexpected error: %v", i, j, errs[j]) 148 } 149 if !expValid { 150 if errs[j] == nil { 151 t.Errorf("test %d.%d: expected error", i, j) 152 } 153 // A few blocks after the first error may pass verification due to concurrent 154 // workers. We don't care about those in this test, just that the correct block 155 // errors out. 156 break 157 } 158 } 159 // Make sure no more data is returned 160 select { 161 case result, ok := <-results: 162 if ok { 163 t.Fatalf("test %d: unexpected result returned: %v", i, result) 164 } 165 case <-time.After(25 * time.Millisecond): 166 } 167 } 168 } 169 170 // Tests that aborting a header validation indeed prevents further checks from being 171 // run, as well as checks that no left-over goroutines are leaked. 172 func TestHeaderConcurrentAbortion2(t *testing.T) { testHeaderConcurrentAbortion(t, 2) } 173 func TestHeaderConcurrentAbortion8(t *testing.T) { testHeaderConcurrentAbortion(t, 8) } 174 func TestHeaderConcurrentAbortion32(t *testing.T) { testHeaderConcurrentAbortion(t, 32) } 175 176 func testHeaderConcurrentAbortion(t *testing.T, threads int) { 177 ctx := context.Background() 178 // Create a simple chain to verify 179 var ( 180 testdb = ethdb.NewMemDatabase() 181 gspec = &Genesis{Config: params.TestChainConfig} 182 genesis = gspec.MustCommit(testdb) 183 blocks, _ = GenerateChain(ctx, params.TestChainConfig, genesis, clique.NewFaker(), testdb, 1024, nil) 184 ) 185 headers := make([]*types.Header, len(blocks)) 186 seals := make([]bool, len(blocks)) 187 188 for i, block := range blocks { 189 headers[i] = block.Header() 190 seals[i] = true 191 } 192 // Set the number of threads to verify on 193 old := runtime.GOMAXPROCS(threads) 194 defer runtime.GOMAXPROCS(old) 195 196 // Start the verifications and immediately abort 197 chain, _ := NewBlockChain(ctx, testdb, nil, params.TestChainConfig, clique.NewFakeDelayer(time.Millisecond), vm.Config{}) 198 defer chain.Stop() 199 200 abort, results := chain.engine.VerifyHeaders(ctx, chain, headers) 201 close(abort) 202 203 // Deplete the results channel 204 verified := 0 205 for depleted := false; !depleted; { 206 select { 207 case result, ok := <-results: 208 if !ok { 209 depleted = true 210 break 211 } 212 if result != nil { 213 t.Errorf("header %d: validation failed: %v", verified, result) 214 } 215 verified++ 216 case <-time.After(50 * time.Millisecond): 217 depleted = true 218 } 219 } 220 // Check that abortion was honored by not processing too many POWs 221 if verified > 2*threads { 222 t.Errorf("verification count too large: have %d, want below %d", verified, 2*threads) 223 } 224 }