github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/core/chain_util_test.go (about)

     1  // Copyright 2015 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 core
    18  
    19  import (
    20  	"encoding/json"
    21  	"math/big"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  )
    27  
    28  type diffTest struct {
    29  	ParentTimestamp    uint64
    30  	ParentDifficulty   *big.Int
    31  	CurrentTimestamp   uint64
    32  	CurrentBlocknumber *big.Int
    33  	CurrentDifficulty  *big.Int
    34  }
    35  
    36  func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
    37  	var ext struct {
    38  		ParentTimestamp    string
    39  		ParentDifficulty   string
    40  		CurrentTimestamp   string
    41  		CurrentBlocknumber string
    42  		CurrentDifficulty  string
    43  	}
    44  	if err := json.Unmarshal(b, &ext); err != nil {
    45  		return err
    46  	}
    47  
    48  	d.ParentTimestamp = common.String2Big(ext.ParentTimestamp).Uint64()
    49  	d.ParentDifficulty = common.String2Big(ext.ParentDifficulty)
    50  	d.CurrentTimestamp = common.String2Big(ext.CurrentTimestamp).Uint64()
    51  	d.CurrentBlocknumber = common.String2Big(ext.CurrentBlocknumber)
    52  	d.CurrentDifficulty = common.String2Big(ext.CurrentDifficulty)
    53  
    54  	return nil
    55  }
    56  
    57  func TestDifficulty(t *testing.T) {
    58  	file, err := os.Open("../tests/files/BasicTests/difficulty.json")
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	defer file.Close()
    63  
    64  	tests := make(map[string]diffTest)
    65  	err = json.NewDecoder(file).Decode(&tests)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	for name, test := range tests {
    71  		number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
    72  		diff := CalcDifficulty(test.CurrentTimestamp, test.ParentTimestamp, number, test.ParentDifficulty)
    73  		if diff.Cmp(test.CurrentDifficulty) != 0 {
    74  			t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
    75  		}
    76  	}
    77  }