github.com/marconiprotocol/go-methereum-lite@v0.0.0-20190918214227-3cd8b06fcf99/consensus/ethash/consensus_test.go (about) 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 17 package ethash 18 19 import ( 20 "encoding/json" 21 "math/big" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "github.com/MarconiProtocol/go-methereum-lite/common" 27 "github.com/MarconiProtocol/go-methereum-lite/common/math" 28 "github.com/MarconiProtocol/go-methereum-lite/core/types" 29 "github.com/MarconiProtocol/go-methereum-lite/params" 30 ) 31 32 type difficultyTest struct { 33 ParentTimestamp uint64 34 ParentDifficulty *big.Int 35 CurrentTimestamp uint64 36 CurrentBlocknumber *big.Int 37 CurrentDifficulty *big.Int 38 ParentUncles common.Hash 39 } 40 41 func (d *difficultyTest) UnmarshalJSON(b []byte) (err error) { 42 var ext struct { 43 ParentTimestamp string 44 ParentDifficulty string 45 CurrentTimestamp string 46 CurrentBlocknumber string 47 CurrentDifficulty string 48 ParentUncles string 49 } 50 if err := json.Unmarshal(b, &ext); err != nil { 51 return err 52 } 53 54 d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp) 55 d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty) 56 d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp) 57 d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber) 58 d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty) 59 d.ParentUncles = common.HexToHash(ext.ParentUncles) 60 61 return nil 62 } 63 64 func TestCalcDifficulty(t *testing.T) { 65 file, err := os.Open(filepath.Join("..", "..", "tests", "testdata_marconi", "BasicTests", "difficulty.json")) 66 if err != nil { 67 t.Fatal(err) 68 } 69 defer file.Close() 70 71 tests := make(map[string]difficultyTest) 72 err = json.NewDecoder(file).Decode(&tests) 73 if err != nil { 74 t.Fatal(err) 75 } 76 77 config := ¶ms.ChainConfig{HomesteadBlock: big.NewInt(1150000)} 78 79 for name, test := range tests { 80 number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1)) 81 difficulty := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{ 82 Number: number, 83 Time: new(big.Int).SetUint64(test.ParentTimestamp), 84 Difficulty: test.ParentDifficulty, 85 UncleHash: test.ParentUncles, 86 }) 87 if difficulty.Cmp(test.CurrentDifficulty) != 0 { 88 t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", difficulty) 89 } 90 } 91 }