github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/tests/difficulty_test_util.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package tests
    26  
    27  import (
    28  	"fmt"
    29  	"math/big"
    30  
    31  	"github.com/ethereum/go-ethereum/common"
    32  	"github.com/ethereum/go-ethereum/common/math"
    33  	"github.com/ethereum/go-ethereum/consensus/ethash"
    34  	"github.com/ethereum/go-ethereum/core/types"
    35  	"github.com/ethereum/go-ethereum/params"
    36  )
    37  
    38  //
    39  
    40  type DifficultyTest struct {
    41  	ParentTimestamp    *big.Int    `json:"parentTimestamp"`
    42  	ParentDifficulty   *big.Int    `json:"parentDifficulty"`
    43  	UncleHash          common.Hash `json:"parentUncles"`
    44  	CurrentTimestamp   *big.Int    `json:"currentTimestamp"`
    45  	CurrentBlockNumber uint64      `json:"currentBlockNumber"`
    46  	CurrentDifficulty  *big.Int    `json:"currentDifficulty"`
    47  }
    48  
    49  type difficultyTestMarshaling struct {
    50  	ParentTimestamp    *math.HexOrDecimal256
    51  	ParentDifficulty   *math.HexOrDecimal256
    52  	CurrentTimestamp   *math.HexOrDecimal256
    53  	CurrentDifficulty  *math.HexOrDecimal256
    54  	UncleHash          common.Hash
    55  	CurrentBlockNumber math.HexOrDecimal64
    56  }
    57  
    58  func (test *DifficultyTest) Run(config *params.ChainConfig) error {
    59  	parentNumber := big.NewInt(int64(test.CurrentBlockNumber - 1))
    60  	parent := &types.Header{
    61  		Difficulty: test.ParentDifficulty,
    62  		Time:       test.ParentTimestamp,
    63  		Number:     parentNumber,
    64  		UncleHash:  test.UncleHash,
    65  	}
    66  
    67  	actual := ethash.CalcDifficulty(config, test.CurrentTimestamp.Uint64(), parent)
    68  	exp := test.CurrentDifficulty
    69  
    70  	if actual.Cmp(exp) != 0 {
    71  		return fmt.Errorf("parent[time %v diff %v unclehash:%x] child[time %v number %v] diff %v != expected %v",
    72  			test.ParentTimestamp, test.ParentDifficulty, test.UncleHash,
    73  			test.CurrentTimestamp, test.CurrentBlockNumber, actual, exp)
    74  	}
    75  	return nil
    76  
    77  }