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