github.com/core-coin/go-core/v2@v2.1.9/consensus/cryptore/cryptore_test.go (about) 1 // Copyright 2023 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package cryptore 18 19 import ( 20 "math/big" 21 "testing" 22 "time" 23 24 "github.com/core-coin/go-core/v2/common" 25 "github.com/core-coin/go-core/v2/common/hexutil" 26 "github.com/core-coin/go-core/v2/core/types" 27 ) 28 29 // Tests that cryptore works correctly in test mode. 30 func TestTestMode(t *testing.T) { 31 header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(2)} 32 33 cryptore := NewTester(nil, false) 34 defer cryptore.Close() 35 36 results := make(chan *types.Block) 37 err := cryptore.Seal(nil, types.NewBlockWithHeader(header), results, nil) 38 if err != nil { 39 t.Fatalf("failed to seal block: %v", err) 40 } 41 select { 42 case block := <-results: 43 header.Nonce = types.EncodeNonce(block.Nonce()) 44 if err := cryptore.VerifySeal(nil, header); err != nil { 45 t.Fatalf("unexpected verification error: %v", err) 46 } 47 case <-time.NewTimer(2 * time.Second).C: 48 t.Error("sealing result timeout") 49 } 50 } 51 52 func TestRemoteSealer(t *testing.T) { 53 cryptore := NewTester(nil, false) 54 defer cryptore.Close() 55 56 api := &API{cryptore} 57 if _, err := api.GetWork(); err != errNoMiningWork { 58 t.Error("expect to return an error indicate there is no mining work") 59 } 60 header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)} 61 block := types.NewBlockWithHeader(header) 62 sealhash := cryptore.SealHash(header) 63 64 // Push new work. 65 results := make(chan *types.Block) 66 cryptore.Seal(nil, block, results, nil) 67 68 var ( 69 work [4]string 70 err error 71 ) 72 if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() { 73 t.Error("expect to return a mining work has same hash") 74 } 75 76 if res := api.SubmitWork(types.BlockNonce{}, sealhash); res { 77 t.Error("expect to return false when submit a fake solution") 78 } 79 // Push new block with same block number to replace the original one. 80 header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)} 81 block = types.NewBlockWithHeader(header) 82 sealhash = cryptore.SealHash(header) 83 cryptore.Seal(nil, block, results, nil) 84 85 if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() { 86 t.Error("expect to return the latest pushed work") 87 } 88 } 89 90 func TestHashRate(t *testing.T) { 91 var ( 92 hashrate = []hexutil.Uint64{100, 200, 300} 93 expect uint64 94 ids = []common.Hash{common.HexToHash("a"), common.HexToHash("b"), common.HexToHash("c")} 95 ) 96 cryptore := NewTester(nil, false) 97 defer cryptore.Close() 98 99 if tot := cryptore.Hashrate(); tot != 0 { 100 t.Error("expect the result should be zero") 101 } 102 103 api := &API{cryptore} 104 for i := 0; i < len(hashrate); i += 1 { 105 if res := api.SubmitHashRate(hashrate[i], ids[i]); !res { 106 t.Error("remote miner submit hashrate failed") 107 } 108 expect += uint64(hashrate[i]) 109 } 110 if tot := cryptore.Hashrate(); tot != float64(expect) { 111 t.Error("expect total hashrate should be same") 112 } 113 } 114 115 func TestClosedRemoteSealer(t *testing.T) { 116 cryptore := NewTester(nil, false) 117 time.Sleep(1 * time.Second) // ensure exit channel is listening 118 cryptore.Close() 119 120 api := &API{cryptore} 121 if _, err := api.GetWork(); err != errCryptoreStopped { 122 t.Error("expect to return an error to indicate cryptore is stopped") 123 } 124 125 if res := api.SubmitHashRate(hexutil.Uint64(100), common.HexToHash("a")); res { 126 t.Error("expect to return false when submit hashrate to a stopped cryptore") 127 } 128 }