consensus_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2017 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. "math/big"
  20. "os"
  21. "path/filepath"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common/math"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. type diffTest struct {
  28. ParentTimestamp uint64
  29. ParentDifficulty *big.Int
  30. CurrentTimestamp uint64
  31. CurrentBlocknumber *big.Int
  32. CurrentDifficulty *big.Int
  33. }
  34. func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
  35. var ext struct {
  36. ParentTimestamp string
  37. ParentDifficulty string
  38. CurrentTimestamp string
  39. CurrentBlocknumber string
  40. CurrentDifficulty string
  41. }
  42. if err := json.Unmarshal(b, &ext); err != nil {
  43. return err
  44. }
  45. d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp)
  46. d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty)
  47. d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp)
  48. d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber)
  49. d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty)
  50. return nil
  51. }
  52. func TestCalcDifficulty(t *testing.T) {
  53. file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
  54. if err != nil {
  55. t.Skip(err)
  56. }
  57. defer file.Close()
  58. tests := make(map[string]diffTest)
  59. err = json.NewDecoder(file).Decode(&tests)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. config := &params.ChainConfig{HomesteadBlock: big.NewInt(1150000)}
  64. for name, test := range tests {
  65. number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
  66. diff := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{
  67. Number: number,
  68. Time: new(big.Int).SetUint64(test.ParentTimestamp),
  69. Difficulty: test.ParentDifficulty,
  70. })
  71. if diff.Cmp(test.CurrentDifficulty) != 0 {
  72. t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
  73. }
  74. }
  75. }