sealer_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright 2018 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. package ethash
  17. import (
  18. "encoding/json"
  19. "io/ioutil"
  20. "math/big"
  21. "net/http"
  22. "net/http/httptest"
  23. "strconv"
  24. "testing"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/internal/testlog"
  29. "github.com/ethereum/go-ethereum/log"
  30. )
  31. // Tests whether remote HTTP servers are correctly notified of new work.
  32. func TestRemoteNotify(t *testing.T) {
  33. // Start a simple web server to capture notifications.
  34. sink := make(chan [3]string)
  35. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  36. blob, err := ioutil.ReadAll(req.Body)
  37. if err != nil {
  38. t.Errorf("failed to read miner notification: %v", err)
  39. }
  40. var work [3]string
  41. if err := json.Unmarshal(blob, &work); err != nil {
  42. t.Errorf("failed to unmarshal miner notification: %v", err)
  43. }
  44. sink <- work
  45. }))
  46. defer server.Close()
  47. // Create the custom ethash engine.
  48. ethash := NewTester([]string{server.URL}, false)
  49. defer ethash.Close()
  50. // Stream a work task and ensure the notification bubbles out.
  51. header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
  52. block := types.NewBlockWithHeader(header)
  53. ethash.Seal(nil, block, nil, nil)
  54. select {
  55. case work := <-sink:
  56. if want := ethash.SealHash(header).Hex(); work[0] != want {
  57. t.Errorf("work packet hash mismatch: have %s, want %s", work[0], want)
  58. }
  59. if want := common.BytesToHash(SeedHash(header.Number.Uint64())).Hex(); work[1] != want {
  60. t.Errorf("work packet seed mismatch: have %s, want %s", work[1], want)
  61. }
  62. target := new(big.Int).Div(new(big.Int).Lsh(big.NewInt(1), 256), header.Difficulty)
  63. if want := common.BytesToHash(target.Bytes()).Hex(); work[2] != want {
  64. t.Errorf("work packet target mismatch: have %s, want %s", work[2], want)
  65. }
  66. case <-time.After(3 * time.Second):
  67. t.Fatalf("notification timed out")
  68. }
  69. }
  70. // Tests whether remote HTTP servers are correctly notified of new work. (Full pending block body / --miner.notify.full)
  71. func TestRemoteNotifyFull(t *testing.T) {
  72. // Start a simple web server to capture notifications.
  73. sink := make(chan map[string]interface{})
  74. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  75. blob, err := ioutil.ReadAll(req.Body)
  76. if err != nil {
  77. t.Errorf("failed to read miner notification: %v", err)
  78. }
  79. var work map[string]interface{}
  80. if err := json.Unmarshal(blob, &work); err != nil {
  81. t.Errorf("failed to unmarshal miner notification: %v", err)
  82. }
  83. sink <- work
  84. }))
  85. defer server.Close()
  86. // Create the custom ethash engine.
  87. config := Config{
  88. PowMode: ModeTest,
  89. NotifyFull: true,
  90. Log: testlog.Logger(t, log.LvlWarn),
  91. }
  92. ethash := New(config, []string{server.URL}, false)
  93. defer ethash.Close()
  94. // Stream a work task and ensure the notification bubbles out.
  95. header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
  96. block := types.NewBlockWithHeader(header)
  97. ethash.Seal(nil, block, nil, nil)
  98. select {
  99. case work := <-sink:
  100. if want := "0x" + strconv.FormatUint(header.Number.Uint64(), 16); work["number"] != want {
  101. t.Errorf("pending block number mismatch: have %v, want %v", work["number"], want)
  102. }
  103. if want := "0x" + header.Difficulty.Text(16); work["difficulty"] != want {
  104. t.Errorf("pending block difficulty mismatch: have %s, want %s", work["difficulty"], want)
  105. }
  106. case <-time.After(3 * time.Second):
  107. t.Fatalf("notification timed out")
  108. }
  109. }
  110. // Tests that pushing work packages fast to the miner doesn't cause any data race
  111. // issues in the notifications.
  112. func TestRemoteMultiNotify(t *testing.T) {
  113. // Start a simple web server to capture notifications.
  114. sink := make(chan [3]string, 64)
  115. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  116. blob, err := ioutil.ReadAll(req.Body)
  117. if err != nil {
  118. t.Errorf("failed to read miner notification: %v", err)
  119. }
  120. var work [3]string
  121. if err := json.Unmarshal(blob, &work); err != nil {
  122. t.Errorf("failed to unmarshal miner notification: %v", err)
  123. }
  124. sink <- work
  125. }))
  126. defer server.Close()
  127. // Create the custom ethash engine.
  128. ethash := NewTester([]string{server.URL}, false)
  129. ethash.config.Log = testlog.Logger(t, log.LvlWarn)
  130. defer ethash.Close()
  131. // Provide a results reader.
  132. // Otherwise the unread results will be logged asynchronously
  133. // and this can happen after the test is finished, causing a panic.
  134. results := make(chan *types.Block, cap(sink))
  135. // Stream a lot of work task and ensure all the notifications bubble out.
  136. for i := 0; i < cap(sink); i++ {
  137. header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)}
  138. block := types.NewBlockWithHeader(header)
  139. ethash.Seal(nil, block, results, nil)
  140. }
  141. for i := 0; i < cap(sink); i++ {
  142. select {
  143. case <-sink:
  144. <-results
  145. case <-time.After(10 * time.Second):
  146. t.Fatalf("notification %d timed out", i)
  147. }
  148. }
  149. }
  150. // Tests that pushing work packages fast to the miner doesn't cause any data race
  151. // issues in the notifications. Full pending block body / --miner.notify.full)
  152. func TestRemoteMultiNotifyFull(t *testing.T) {
  153. // Start a simple web server to capture notifications.
  154. sink := make(chan map[string]interface{}, 64)
  155. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  156. blob, err := ioutil.ReadAll(req.Body)
  157. if err != nil {
  158. t.Errorf("failed to read miner notification: %v", err)
  159. }
  160. var work map[string]interface{}
  161. if err := json.Unmarshal(blob, &work); err != nil {
  162. t.Errorf("failed to unmarshal miner notification: %v", err)
  163. }
  164. sink <- work
  165. }))
  166. defer server.Close()
  167. // Create the custom ethash engine.
  168. config := Config{
  169. PowMode: ModeTest,
  170. NotifyFull: true,
  171. Log: testlog.Logger(t, log.LvlWarn),
  172. }
  173. ethash := New(config, []string{server.URL}, false)
  174. defer ethash.Close()
  175. // Provide a results reader.
  176. // Otherwise the unread results will be logged asynchronously
  177. // and this can happen after the test is finished, causing a panic.
  178. results := make(chan *types.Block, cap(sink))
  179. // Stream a lot of work task and ensure all the notifications bubble out.
  180. for i := 0; i < cap(sink); i++ {
  181. header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)}
  182. block := types.NewBlockWithHeader(header)
  183. ethash.Seal(nil, block, results, nil)
  184. }
  185. for i := 0; i < cap(sink); i++ {
  186. select {
  187. case <-sink:
  188. <-results
  189. case <-time.After(10 * time.Second):
  190. t.Fatalf("notification %d timed out", i)
  191. }
  192. }
  193. }
  194. // Tests whether stale solutions are correctly processed.
  195. func TestStaleSubmission(t *testing.T) {
  196. ethash := NewTester(nil, true)
  197. defer ethash.Close()
  198. api := &API{ethash}
  199. fakeNonce, fakeDigest := types.BlockNonce{0x01, 0x02, 0x03}, common.HexToHash("deadbeef")
  200. testcases := []struct {
  201. headers []*types.Header
  202. submitIndex int
  203. submitRes bool
  204. }{
  205. // Case1: submit solution for the latest mining package
  206. {
  207. []*types.Header{
  208. {ParentHash: common.BytesToHash([]byte{0xa}), Number: big.NewInt(1), Difficulty: big.NewInt(100000000)},
  209. },
  210. 0,
  211. true,
  212. },
  213. // Case2: submit solution for the previous package but have same parent.
  214. {
  215. []*types.Header{
  216. {ParentHash: common.BytesToHash([]byte{0xb}), Number: big.NewInt(2), Difficulty: big.NewInt(100000000)},
  217. {ParentHash: common.BytesToHash([]byte{0xb}), Number: big.NewInt(2), Difficulty: big.NewInt(100000001)},
  218. },
  219. 0,
  220. true,
  221. },
  222. // Case3: submit stale but acceptable solution
  223. {
  224. []*types.Header{
  225. {ParentHash: common.BytesToHash([]byte{0xc}), Number: big.NewInt(3), Difficulty: big.NewInt(100000000)},
  226. {ParentHash: common.BytesToHash([]byte{0xd}), Number: big.NewInt(9), Difficulty: big.NewInt(100000000)},
  227. },
  228. 0,
  229. true,
  230. },
  231. // Case4: submit very old solution
  232. {
  233. []*types.Header{
  234. {ParentHash: common.BytesToHash([]byte{0xe}), Number: big.NewInt(10), Difficulty: big.NewInt(100000000)},
  235. {ParentHash: common.BytesToHash([]byte{0xf}), Number: big.NewInt(17), Difficulty: big.NewInt(100000000)},
  236. },
  237. 0,
  238. false,
  239. },
  240. }
  241. results := make(chan *types.Block, 16)
  242. for id, c := range testcases {
  243. for _, h := range c.headers {
  244. ethash.Seal(nil, types.NewBlockWithHeader(h), results, nil)
  245. }
  246. if res := api.SubmitWork(fakeNonce, ethash.SealHash(c.headers[c.submitIndex]), fakeDigest); res != c.submitRes {
  247. t.Errorf("case %d submit result mismatch, want %t, get %t", id+1, c.submitRes, res)
  248. }
  249. if !c.submitRes {
  250. continue
  251. }
  252. select {
  253. case res := <-results:
  254. if res.Header().Nonce != fakeNonce {
  255. t.Errorf("case %d block nonce mismatch, want %x, get %x", id+1, fakeNonce, res.Header().Nonce)
  256. }
  257. if res.Header().MixDigest != fakeDigest {
  258. t.Errorf("case %d block digest mismatch, want %x, get %x", id+1, fakeDigest, res.Header().MixDigest)
  259. }
  260. if res.Header().Difficulty.Uint64() != c.headers[c.submitIndex].Difficulty.Uint64() {
  261. t.Errorf("case %d block difficulty mismatch, want %d, get %d", id+1, c.headers[c.submitIndex].Difficulty, res.Header().Difficulty)
  262. }
  263. if res.Header().Number.Uint64() != c.headers[c.submitIndex].Number.Uint64() {
  264. t.Errorf("case %d block number mismatch, want %d, get %d", id+1, c.headers[c.submitIndex].Number.Uint64(), res.Header().Number.Uint64())
  265. }
  266. if res.Header().ParentHash != c.headers[c.submitIndex].ParentHash {
  267. t.Errorf("case %d block parent hash mismatch, want %s, get %s", id+1, c.headers[c.submitIndex].ParentHash.Hex(), res.Header().ParentHash.Hex())
  268. }
  269. case <-time.NewTimer(time.Second).C:
  270. t.Errorf("case %d fetch ethash result timeout", id+1)
  271. }
  272. }
  273. }