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