github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/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/wanchain/go-wanchain/common/math"
    27  	//"github.com/wanchain/go-wanchain/core/types"
    28  	//"github.com/wanchain/go-wanchain/params"
    29  	//"testing"
    30  	//"os"
    31  	//"path/filepath"
    32  )
    33  
    34  type diffTest struct {
    35  	ParentTimestamp    uint64
    36  	ParentDifficulty   *big.Int
    37  	CurrentTimestamp   uint64
    38  	CurrentBlocknumber *big.Int
    39  	CurrentDifficulty  *big.Int
    40  }
    41  
    42  func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
    43  	var ext struct {
    44  		ParentTimestamp    string
    45  		ParentDifficulty   string
    46  		CurrentTimestamp   string
    47  		CurrentBlocknumber string
    48  		CurrentDifficulty  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  
    60  	return nil
    61  }
    62  
    63  // This case is out-of-date due to consensus protocol migrated to Byzantium
    64  //func TestCalcDifficulty(t *testing.T) {
    65  //	file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
    66  //	if err != nil {
    67  //		t.Skip(err)
    68  //	}
    69  //	defer file.Close()
    70  //
    71  //	tests := make(map[string]diffTest)
    72  //	err = json.NewDecoder(file).Decode(&tests)
    73  //	if err != nil {
    74  //		t.Fatal(err)
    75  //	}
    76  //
    77  //	config := &params.ChainConfig{ByzantiumBlock: big.NewInt(1150000)}
    78  //	for name, test := range tests {
    79  //		number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
    80  //		diff := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{
    81  //			Number:     number,
    82  //			Time:       new(big.Int).SetUint64(test.ParentTimestamp),
    83  //			Difficulty: test.ParentDifficulty,
    84  //		})
    85  //		if diff.Cmp(test.CurrentDifficulty) != 0 {
    86  //			t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
    87  //		}
    88  //	}
    89  //}