difficulty_test_util.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 tests
  17. import (
  18. "fmt"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/math"
  22. "github.com/ethereum/go-ethereum/consensus/ethash"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/params"
  25. )
  26. //go:generate gencodec -type DifficultyTest -field-override difficultyTestMarshaling -out gen_difficultytest.go
  27. type DifficultyTest struct {
  28. ParentTimestamp uint64 `json:"parentTimestamp"`
  29. ParentDifficulty *big.Int `json:"parentDifficulty"`
  30. UncleHash common.Hash `json:"parentUncles"`
  31. CurrentTimestamp uint64 `json:"currentTimestamp"`
  32. CurrentBlockNumber uint64 `json:"currentBlockNumber"`
  33. CurrentDifficulty *big.Int `json:"currentDifficulty"`
  34. }
  35. type difficultyTestMarshaling struct {
  36. ParentTimestamp math.HexOrDecimal64
  37. ParentDifficulty *math.HexOrDecimal256
  38. CurrentTimestamp math.HexOrDecimal64
  39. CurrentDifficulty *math.HexOrDecimal256
  40. UncleHash common.Hash
  41. CurrentBlockNumber math.HexOrDecimal64
  42. }
  43. func (test *DifficultyTest) Run(config *params.ChainConfig) error {
  44. parentNumber := big.NewInt(int64(test.CurrentBlockNumber - 1))
  45. parent := &types.Header{
  46. Difficulty: test.ParentDifficulty,
  47. Time: test.ParentTimestamp,
  48. Number: parentNumber,
  49. UncleHash: test.UncleHash,
  50. }
  51. actual := ethash.CalcDifficulty(config, test.CurrentTimestamp, parent)
  52. exp := test.CurrentDifficulty
  53. if actual.Cmp(exp) != 0 {
  54. return fmt.Errorf("parent[time %v diff %v unclehash:%x] child[time %v number %v] diff %v != expected %v",
  55. test.ParentTimestamp, test.ParentDifficulty, test.UncleHash,
  56. test.CurrentTimestamp, test.CurrentBlockNumber, actual, exp)
  57. }
  58. return nil
  59. }