github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/consensus/ethash/consensus_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package ethash
    19  
    20  import (
    21  	"encoding/json"
    22  	"math/big"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	"github.com/AigarNetwork/aigar/common/math"
    28  	"github.com/AigarNetwork/aigar/core/types"
    29  	"github.com/AigarNetwork/aigar/params"
    30  )
    31  
    32  type diffTest struct {
    33  	ParentTimestamp    uint64
    34  	ParentDifficulty   *big.Int
    35  	CurrentTimestamp   uint64
    36  	CurrentBlocknumber *big.Int
    37  	CurrentDifficulty  *big.Int
    38  }
    39  
    40  func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
    41  	var ext struct {
    42  		ParentTimestamp    string
    43  		ParentDifficulty   string
    44  		CurrentTimestamp   string
    45  		CurrentBlocknumber string
    46  		CurrentDifficulty  string
    47  	}
    48  	if err := json.Unmarshal(b, &ext); err != nil {
    49  		return err
    50  	}
    51  
    52  	d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp)
    53  	d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty)
    54  	d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp)
    55  	d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber)
    56  	d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty)
    57  
    58  	return nil
    59  }
    60  
    61  func TestCalcDifficulty(t *testing.T) {
    62  	file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
    63  	if err != nil {
    64  		t.Skip(err)
    65  	}
    66  	defer file.Close()
    67  
    68  	tests := make(map[string]diffTest)
    69  	err = json.NewDecoder(file).Decode(&tests)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	config := &params.ChainConfig{HomesteadBlock: big.NewInt(1150000)}
    75  
    76  	for name, test := range tests {
    77  		number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
    78  		diff := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{
    79  			Number:     number,
    80  			Time:       test.ParentTimestamp,
    81  			Difficulty: test.ParentDifficulty,
    82  		})
    83  		if diff.Cmp(test.CurrentDifficulty) != 0 {
    84  			t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
    85  		}
    86  	}
    87  }