github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/consensus/ethash/sealer_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:33</date> 10 //</624342612278513664> 11 12 package ethash 13 14 import ( 15 "encoding/json" 16 "io/ioutil" 17 "math/big" 18 "net" 19 "net/http" 20 "testing" 21 "time" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/core/types" 25 ) 26 27 //测试是否正确通知远程HTTP服务器新工作。 28 func TestRemoteNotify(t *testing.T) { 29 //启动简单的Web服务器以捕获通知 30 sink := make(chan [3]string) 31 32 server := &http.Server{ 33 Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 34 blob, err := ioutil.ReadAll(req.Body) 35 if err != nil { 36 t.Fatalf("failed to read miner notification: %v", err) 37 } 38 var work [3]string 39 if err := json.Unmarshal(blob, &work); err != nil { 40 t.Fatalf("failed to unmarshal miner notification: %v", err) 41 } 42 sink <- work 43 }), 44 } 45 //打开自定义侦听器以提取其本地地址 46 listener, err := net.Listen("tcp", "localhost:0") 47 if err != nil { 48 t.Fatalf("failed to open notification server: %v", err) 49 } 50 defer listener.Close() 51 52 go server.Serve(listener) 53 54 //创建自定义ethash引擎 55 ethash := NewTester([]string{"http://“+listener.addr().string()) 56 defer ethash.Close() 57 58 //流式处理工作任务并确保通知冒泡 59 header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)} 60 block := types.NewBlockWithHeader(header) 61 62 ethash.Seal(nil, block, nil) 63 select { 64 case work := <-sink: 65 if want := header.HashNoNonce().Hex(); work[0] != want { 66 t.Errorf("work packet hash mismatch: have %s, want %s", work[0], want) 67 } 68 if want := common.BytesToHash(SeedHash(header.Number.Uint64())).Hex(); work[1] != want { 69 t.Errorf("work packet seed mismatch: have %s, want %s", work[1], want) 70 } 71 target := new(big.Int).Div(new(big.Int).Lsh(big.NewInt(1), 256), header.Difficulty) 72 if want := common.BytesToHash(target.Bytes()).Hex(); work[2] != want { 73 t.Errorf("work packet target mismatch: have %s, want %s", work[2], want) 74 } 75 case <-time.After(time.Second): 76 t.Fatalf("notification timed out") 77 } 78 } 79 80 //将工作包快速推送到矿工身上的测试不会导致任何DAA竞赛 81 //通知中的问题。 82 func TestRemoteMultiNotify(t *testing.T) { 83 //启动简单的Web服务器以捕获通知 84 sink := make(chan [3]string, 64) 85 86 server := &http.Server{ 87 Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 88 blob, err := ioutil.ReadAll(req.Body) 89 if err != nil { 90 t.Fatalf("failed to read miner notification: %v", err) 91 } 92 var work [3]string 93 if err := json.Unmarshal(blob, &work); err != nil { 94 t.Fatalf("failed to unmarshal miner notification: %v", err) 95 } 96 sink <- work 97 }), 98 } 99 //打开自定义侦听器以提取其本地地址 100 listener, err := net.Listen("tcp", "localhost:0") 101 if err != nil { 102 t.Fatalf("failed to open notification server: %v", err) 103 } 104 defer listener.Close() 105 106 go server.Serve(listener) 107 108 //创建自定义ethash引擎 109 ethash := NewTester([]string{"http://“+listener.addr().string()) 110 defer ethash.Close() 111 112 //流式处理大量工作任务并确保所有通知都冒泡出来 113 for i := 0; i < cap(sink); i++ { 114 header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)} 115 block := types.NewBlockWithHeader(header) 116 117 ethash.Seal(nil, block, nil) 118 } 119 for i := 0; i < cap(sink); i++ { 120 select { 121 case <-sink: 122 case <-time.After(250 * time.Millisecond): 123 t.Fatalf("notification %d timed out", i) 124 } 125 } 126 } 127