github.com/aquanetwork/aquachain@v1.7.8/consensus/aquahash/consensus_test.go (about)

     1  // Copyright 2017 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package aquahash
    18  
    19  import (
    20  	"encoding/json"
    21  	"math/big"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"gitlab.com/aquachain/aquachain/common/math"
    27  	"gitlab.com/aquachain/aquachain/core/types"
    28  	"gitlab.com/aquachain/aquachain/params"
    29  )
    30  
    31  type diffTest struct {
    32  	ParentTimestamp    uint64
    33  	ParentDifficulty   *big.Int
    34  	CurrentTimestamp   uint64
    35  	CurrentBlocknumber *big.Int
    36  	CurrentDifficulty  *big.Int
    37  }
    38  
    39  func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
    40  	var ext struct {
    41  		ParentTimestamp    string
    42  		ParentDifficulty   string
    43  		CurrentTimestamp   string
    44  		CurrentBlocknumber string
    45  		CurrentDifficulty  string
    46  	}
    47  	if err := json.Unmarshal(b, &ext); err != nil {
    48  		return err
    49  	}
    50  
    51  	d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp)
    52  	d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty)
    53  	d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp)
    54  	d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber)
    55  	d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty)
    56  
    57  	return nil
    58  }
    59  
    60  func TestCalcDifficulty(t *testing.T) {
    61  	file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
    62  	if err != nil {
    63  		t.Skip(err)
    64  	}
    65  	defer file.Close()
    66  
    67  	tests := make(map[string]diffTest)
    68  	err = json.NewDecoder(file).Decode(&tests)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	config := &params.ChainConfig{HomesteadBlock: big.NewInt(1150000)}
    74  
    75  	for name, test := range tests {
    76  		number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
    77  		diff := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{
    78  			Number:     number,
    79  			Time:       new(big.Int).SetUint64(test.ParentTimestamp),
    80  			Difficulty: test.ParentDifficulty,
    81  		}, nil)
    82  		if diff.Cmp(test.CurrentDifficulty) != 0 {
    83  			t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
    84  		}
    85  	}
    86  }