chain_util_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. package core
  17. import (
  18. "encoding/json"
  19. "math/big"
  20. "os"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/crypto/sha3"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. )
  28. type diffTest struct {
  29. ParentTimestamp uint64
  30. ParentDifficulty *big.Int
  31. CurrentTimestamp uint64
  32. CurrentBlocknumber *big.Int
  33. CurrentDifficulty *big.Int
  34. }
  35. func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
  36. var ext struct {
  37. ParentTimestamp string
  38. ParentDifficulty string
  39. CurrentTimestamp string
  40. CurrentBlocknumber string
  41. CurrentDifficulty string
  42. }
  43. if err := json.Unmarshal(b, &ext); err != nil {
  44. return err
  45. }
  46. d.ParentTimestamp = common.String2Big(ext.ParentTimestamp).Uint64()
  47. d.ParentDifficulty = common.String2Big(ext.ParentDifficulty)
  48. d.CurrentTimestamp = common.String2Big(ext.CurrentTimestamp).Uint64()
  49. d.CurrentBlocknumber = common.String2Big(ext.CurrentBlocknumber)
  50. d.CurrentDifficulty = common.String2Big(ext.CurrentDifficulty)
  51. return nil
  52. }
  53. func TestDifficulty(t *testing.T) {
  54. file, err := os.Open("../tests/files/BasicTests/difficulty.json")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. defer file.Close()
  59. tests := make(map[string]diffTest)
  60. err = json.NewDecoder(file).Decode(&tests)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. for name, test := range tests {
  65. number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
  66. diff := CalcDifficulty(test.CurrentTimestamp, test.ParentTimestamp, number, test.ParentDifficulty)
  67. if diff.Cmp(test.CurrentDifficulty) != 0 {
  68. t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
  69. }
  70. }
  71. }
  72. // Tests block header storage and retrieval operations.
  73. func TestHeaderStorage(t *testing.T) {
  74. db, _ := ethdb.NewMemDatabase()
  75. // Create a test header to move around the database and make sure it's really new
  76. header := &types.Header{Extra: []byte("test header")}
  77. if entry := GetHeader(db, header.Hash()); entry != nil {
  78. t.Fatalf("Non existent header returned: %v", entry)
  79. }
  80. // Write and verify the header in the database
  81. if err := WriteHeader(db, header); err != nil {
  82. t.Fatalf("Failed to write header into database: %v", err)
  83. }
  84. if entry := GetHeader(db, header.Hash()); entry == nil {
  85. t.Fatalf("Stored header not found")
  86. } else if entry.Hash() != header.Hash() {
  87. t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header)
  88. }
  89. if entry := GetHeaderRLP(db, header.Hash()); entry == nil {
  90. t.Fatalf("Stored header RLP not found")
  91. } else {
  92. hasher := sha3.NewKeccak256()
  93. hasher.Write(entry)
  94. if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
  95. t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header)
  96. }
  97. }
  98. // Delete the header and verify the execution
  99. DeleteHeader(db, header.Hash())
  100. if entry := GetHeader(db, header.Hash()); entry != nil {
  101. t.Fatalf("Deleted header returned: %v", entry)
  102. }
  103. }
  104. // Tests block body storage and retrieval operations.
  105. func TestBodyStorage(t *testing.T) {
  106. db, _ := ethdb.NewMemDatabase()
  107. // Create a test body to move around the database and make sure it's really new
  108. body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}
  109. hasher := sha3.NewKeccak256()
  110. rlp.Encode(hasher, body)
  111. hash := common.BytesToHash(hasher.Sum(nil))
  112. if entry := GetBody(db, hash); entry != nil {
  113. t.Fatalf("Non existent body returned: %v", entry)
  114. }
  115. // Write and verify the body in the database
  116. if err := WriteBody(db, hash, body); err != nil {
  117. t.Fatalf("Failed to write body into database: %v", err)
  118. }
  119. if entry := GetBody(db, hash); entry == nil {
  120. t.Fatalf("Stored body not found")
  121. } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
  122. t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
  123. }
  124. if entry := GetBodyRLP(db, hash); entry == nil {
  125. t.Fatalf("Stored body RLP not found")
  126. } else {
  127. hasher := sha3.NewKeccak256()
  128. hasher.Write(entry)
  129. if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
  130. t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
  131. }
  132. }
  133. // Delete the body and verify the execution
  134. DeleteBody(db, hash)
  135. if entry := GetBody(db, hash); entry != nil {
  136. t.Fatalf("Deleted body returned: %v", entry)
  137. }
  138. }
  139. // Tests block storage and retrieval operations.
  140. func TestBlockStorage(t *testing.T) {
  141. db, _ := ethdb.NewMemDatabase()
  142. // Create a test block to move around the database and make sure it's really new
  143. block := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block")})
  144. if entry := GetBlock(db, block.Hash()); entry != nil {
  145. t.Fatalf("Non existent block returned: %v", entry)
  146. }
  147. if entry := GetHeader(db, block.Hash()); entry != nil {
  148. t.Fatalf("Non existent header returned: %v", entry)
  149. }
  150. if entry := GetBody(db, block.Hash()); entry != nil {
  151. t.Fatalf("Non existent body returned: %v", entry)
  152. }
  153. // Write and verify the block in the database
  154. if err := WriteBlock(db, block); err != nil {
  155. t.Fatalf("Failed to write block into database: %v", err)
  156. }
  157. if entry := GetBlock(db, block.Hash()); entry == nil {
  158. t.Fatalf("Stored block not found")
  159. } else if entry.Hash() != block.Hash() {
  160. t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
  161. }
  162. if entry := GetHeader(db, block.Hash()); entry == nil {
  163. t.Fatalf("Stored header not found")
  164. } else if entry.Hash() != block.Header().Hash() {
  165. t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header())
  166. }
  167. if entry := GetBody(db, block.Hash()); entry == nil {
  168. t.Fatalf("Stored body not found")
  169. } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
  170. t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, &types.Body{block.Transactions(), block.Uncles()})
  171. }
  172. // Delete the block and verify the execution
  173. DeleteBlock(db, block.Hash())
  174. if entry := GetBlock(db, block.Hash()); entry != nil {
  175. t.Fatalf("Deleted block returned: %v", entry)
  176. }
  177. if entry := GetHeader(db, block.Hash()); entry != nil {
  178. t.Fatalf("Deleted header returned: %v", entry)
  179. }
  180. if entry := GetBody(db, block.Hash()); entry != nil {
  181. t.Fatalf("Deleted body returned: %v", entry)
  182. }
  183. }
  184. // Tests that partial block contents don't get reassembled into full blocks.
  185. func TestPartialBlockStorage(t *testing.T) {
  186. db, _ := ethdb.NewMemDatabase()
  187. block := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block")})
  188. // Store a header and check that it's not recognized as a block
  189. if err := WriteHeader(db, block.Header()); err != nil {
  190. t.Fatalf("Failed to write header into database: %v", err)
  191. }
  192. if entry := GetBlock(db, block.Hash()); entry != nil {
  193. t.Fatalf("Non existent block returned: %v", entry)
  194. }
  195. DeleteHeader(db, block.Hash())
  196. // Store a body and check that it's not recognized as a block
  197. if err := WriteBody(db, block.Hash(), &types.Body{block.Transactions(), block.Uncles()}); err != nil {
  198. t.Fatalf("Failed to write body into database: %v", err)
  199. }
  200. if entry := GetBlock(db, block.Hash()); entry != nil {
  201. t.Fatalf("Non existent block returned: %v", entry)
  202. }
  203. DeleteBody(db, block.Hash())
  204. // Store a header and a body separately and check reassembly
  205. if err := WriteHeader(db, block.Header()); err != nil {
  206. t.Fatalf("Failed to write header into database: %v", err)
  207. }
  208. if err := WriteBody(db, block.Hash(), &types.Body{block.Transactions(), block.Uncles()}); err != nil {
  209. t.Fatalf("Failed to write body into database: %v", err)
  210. }
  211. if entry := GetBlock(db, block.Hash()); entry == nil {
  212. t.Fatalf("Stored block not found")
  213. } else if entry.Hash() != block.Hash() {
  214. t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
  215. }
  216. }
  217. // Tests block total difficulty storage and retrieval operations.
  218. func TestTdStorage(t *testing.T) {
  219. db, _ := ethdb.NewMemDatabase()
  220. // Create a test TD to move around the database and make sure it's really new
  221. hash, td := common.Hash{}, big.NewInt(314)
  222. if entry := GetTd(db, hash); entry != nil {
  223. t.Fatalf("Non existent TD returned: %v", entry)
  224. }
  225. // Write and verify the TD in the database
  226. if err := WriteTd(db, hash, td); err != nil {
  227. t.Fatalf("Failed to write TD into database: %v", err)
  228. }
  229. if entry := GetTd(db, hash); entry == nil {
  230. t.Fatalf("Stored TD not found")
  231. } else if entry.Cmp(td) != 0 {
  232. t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td)
  233. }
  234. // Delete the TD and verify the execution
  235. DeleteTd(db, hash)
  236. if entry := GetTd(db, hash); entry != nil {
  237. t.Fatalf("Deleted TD returned: %v", entry)
  238. }
  239. }
  240. // Tests that canonical numbers can be mapped to hashes and retrieved.
  241. func TestCanonicalMappingStorage(t *testing.T) {
  242. db, _ := ethdb.NewMemDatabase()
  243. // Create a test canonical number and assinged hash to move around
  244. hash, number := common.Hash{0: 0xff}, uint64(314)
  245. if entry := GetCanonicalHash(db, number); entry != (common.Hash{}) {
  246. t.Fatalf("Non existent canonical mapping returned: %v", entry)
  247. }
  248. // Write and verify the TD in the database
  249. if err := WriteCanonicalHash(db, hash, number); err != nil {
  250. t.Fatalf("Failed to write canonical mapping into database: %v", err)
  251. }
  252. if entry := GetCanonicalHash(db, number); entry == (common.Hash{}) {
  253. t.Fatalf("Stored canonical mapping not found")
  254. } else if entry != hash {
  255. t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash)
  256. }
  257. // Delete the TD and verify the execution
  258. DeleteCanonicalHash(db, number)
  259. if entry := GetCanonicalHash(db, number); entry != (common.Hash{}) {
  260. t.Fatalf("Deleted canonical mapping returned: %v", entry)
  261. }
  262. }
  263. // Tests that head headers and head blocks can be assigned, individually.
  264. func TestHeadStorage(t *testing.T) {
  265. db, _ := ethdb.NewMemDatabase()
  266. blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")})
  267. blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")})
  268. // Check that no head entries are in a pristine database
  269. if entry := GetHeadHeaderHash(db); entry != (common.Hash{}) {
  270. t.Fatalf("Non head header entry returned: %v", entry)
  271. }
  272. if entry := GetHeadBlockHash(db); entry != (common.Hash{}) {
  273. t.Fatalf("Non head block entry returned: %v", entry)
  274. }
  275. // Assign separate entries for the head header and block
  276. if err := WriteHeadHeaderHash(db, blockHead.Hash()); err != nil {
  277. t.Fatalf("Failed to write head header hash: %v", err)
  278. }
  279. if err := WriteHeadBlockHash(db, blockFull.Hash()); err != nil {
  280. t.Fatalf("Failed to write head block hash: %v", err)
  281. }
  282. // Check that both heads are present, and different (i.e. two heads maintained)
  283. if entry := GetHeadHeaderHash(db); entry != blockHead.Hash() {
  284. t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
  285. }
  286. if entry := GetHeadBlockHash(db); entry != blockFull.Hash() {
  287. t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
  288. }
  289. }