consensus_test.go 2.5 KB

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