github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/tests/difficulty_test_util.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:50</date>
    10  //</624342685259403264>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package tests
    29  
    30  import (
    31  	"fmt"
    32  	"math/big"
    33  
    34  	"github.com/ethereum/go-ethereum/common"
    35  	"github.com/ethereum/go-ethereum/common/math"
    36  	"github.com/ethereum/go-ethereum/consensus/ethash"
    37  	"github.com/ethereum/go-ethereum/core/types"
    38  	"github.com/ethereum/go-ethereum/params"
    39  )
    40  
    41  //
    42  
    43  type DifficultyTest struct {
    44  	ParentTimestamp    *big.Int    `json:"parentTimestamp"`
    45  	ParentDifficulty   *big.Int    `json:"parentDifficulty"`
    46  	UncleHash          common.Hash `json:"parentUncles"`
    47  	CurrentTimestamp   *big.Int    `json:"currentTimestamp"`
    48  	CurrentBlockNumber uint64      `json:"currentBlockNumber"`
    49  	CurrentDifficulty  *big.Int    `json:"currentDifficulty"`
    50  }
    51  
    52  type difficultyTestMarshaling struct {
    53  	ParentTimestamp    *math.HexOrDecimal256
    54  	ParentDifficulty   *math.HexOrDecimal256
    55  	CurrentTimestamp   *math.HexOrDecimal256
    56  	CurrentDifficulty  *math.HexOrDecimal256
    57  	UncleHash          common.Hash
    58  	CurrentBlockNumber math.HexOrDecimal64
    59  }
    60  
    61  func (test *DifficultyTest) Run(config *params.ChainConfig) error {
    62  	parentNumber := big.NewInt(int64(test.CurrentBlockNumber - 1))
    63  	parent := &types.Header{
    64  		Difficulty: test.ParentDifficulty,
    65  		Time:       test.ParentTimestamp,
    66  		Number:     parentNumber,
    67  		UncleHash:  test.UncleHash,
    68  	}
    69  
    70  	actual := ethash.CalcDifficulty(config, test.CurrentTimestamp.Uint64(), parent)
    71  	exp := test.CurrentDifficulty
    72  
    73  	if actual.Cmp(exp) != 0 {
    74  		return fmt.Errorf("parent[time %v diff %v unclehash:%x] child[time %v number %v] diff %v != expected %v",
    75  			test.ParentTimestamp, test.ParentDifficulty, test.UncleHash,
    76  			test.CurrentTimestamp, test.CurrentBlockNumber, actual, exp)
    77  	}
    78  	return nil
    79  
    80  }
    81