github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/scrypt/consensus_test.go (about) 1 // Copyright (c) 2019 Simplechain 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Lesser General Public License as published by 5 // the Free Software Foundation, either version 3 of the License, or 6 // (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Lesser General Public License for more details. 12 // 13 // You should have received a copy of the GNU Lesser General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package scrypt 17 18 import ( 19 "encoding/json" 20 "math/big" 21 "strings" 22 "testing" 23 24 "github.com/bigzoro/my_simplechain/common/math" 25 "github.com/bigzoro/my_simplechain/core/types" 26 "github.com/bigzoro/my_simplechain/params" 27 ) 28 29 type diffTest struct { 30 ParentTimestamp uint64 31 ParentDifficulty *big.Int 32 CurrentTimestamp uint64 33 CurrentBlocknumber *big.Int 34 CurrentDifficulty *big.Int 35 } 36 37 func (d *diffTest) UnmarshalJSON(b []byte) (err error) { 38 var ext struct { 39 ParentTimestamp string 40 ParentDifficulty string 41 CurrentTimestamp string 42 CurrentBlocknumber string 43 CurrentDifficulty string 44 } 45 if err := json.Unmarshal(b, &ext); err != nil { 46 return err 47 } 48 49 d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp) 50 d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty) 51 d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp) 52 d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber) 53 d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty) 54 55 return nil 56 } 57 58 var testData string = ` 59 { 60 "preExpDiffIncrease" : { 61 "parentTimestamp" : "42", 62 "parentDifficulty" : "1000000", 63 "currentTimestamp" : "43", 64 "currentBlockNumber" : "42", 65 "currentDifficulty" : "1001920", 66 "parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" 67 }, 68 "preExpDiffDecrease" : { 69 "parentTimestamp" : "42", 70 "parentDifficulty" : "1000000", 71 "currentTimestamp" : "60", 72 "currentBlockNumber" : "42", 73 "currentDifficulty" : "1000569", 74 "parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" 75 } 76 }` 77 78 func TestCalcDifficulty(t *testing.T) { 79 //TODO add tests file 80 81 //file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json")) 82 //if err != nil { 83 // t.Skip(err) 84 //} 85 //defer file.Close() 86 // 87 //tests := make(map[string]diffTest) 88 //err = json.NewDecoder(file).Decode(&tests) 89 //if err != nil { 90 // t.Fatal(err) 91 //} 92 93 tests := make(map[string]diffTest) 94 strRead := strings.NewReader(testData) 95 err := json.NewDecoder(strRead).Decode(&tests) 96 if err != nil { 97 t.Fatal(err) 98 } 99 100 config := ¶ms.ChainConfig{HomesteadBlock: big.NewInt(1150000)} 101 for name, test := range tests { 102 number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1)) 103 diff := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{ 104 Number: number, 105 Time: test.ParentTimestamp, 106 Difficulty: test.ParentDifficulty, 107 }) 108 if diff.Cmp(test.CurrentDifficulty) != 0 { 109 t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff) 110 } 111 } 112 }