github.com/klaytn/klaytn@v1.12.1/consensus/gxhash/consensus_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from consensus/ethash/consensus_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package gxhash
    22  
    23  import (
    24  	"encoding/json"
    25  	"math/big"
    26  
    27  	// Enable below packages when enabling TestCalcDiffulty
    28  	// "os"
    29  	// "path/filepath"
    30  	// "testing"
    31  
    32  	"github.com/klaytn/klaytn/common/math"
    33  	// "github.com/klaytn/klaytn/blockchain/types"
    34  	// "github.com/klaytn/klaytn/params"
    35  )
    36  
    37  type diffTest struct {
    38  	ParentTimestamp    uint64
    39  	ParentBlockScore   *big.Int
    40  	CurrentTimestamp   uint64
    41  	CurrentBlocknumber *big.Int
    42  	CurrentBlockScore  *big.Int
    43  }
    44  
    45  func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
    46  	var ext struct {
    47  		ParentTimestamp    string
    48  		ParentBlockScore   string
    49  		CurrentTimestamp   string
    50  		CurrentBlocknumber string
    51  		CurrentBlockScore  string
    52  	}
    53  	if err := json.Unmarshal(b, &ext); err != nil {
    54  		return err
    55  	}
    56  
    57  	d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp)
    58  	d.ParentBlockScore = math.MustParseBig256(ext.ParentBlockScore)
    59  	d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp)
    60  	d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber)
    61  	d.CurrentBlockScore = math.MustParseBig256(ext.CurrentBlockScore)
    62  
    63  	return nil
    64  }
    65  
    66  // TODO-Klaytn-FailedTest Enable this test later
    67  /*
    68  func TestCalcBlockScore(t *testing.T) {
    69  	file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
    70  	if err != nil {
    71  		t.Skip(err)
    72  	}
    73  	defer file.Close()
    74  
    75  	tests := make(map[string]diffTest)
    76  	err = json.NewDecoder(file).Decode(&tests)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	config := &params.ChainConfig{}
    82  
    83  	for name, test := range tests {
    84  		number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
    85  		diff := CalcBlockScore(config, test.CurrentTimestamp, &types.Header{
    86  			Number:     number,
    87  			Time:       new(big.Int).SetUint64(test.ParentTimestamp),
    88  			BlockScore: test.ParentBlockScore,
    89  		})
    90  		if diff.Cmp(test.CurrentBlockScore) != 0 {
    91  			t.Error(name, "failed. Expected", test.CurrentBlockScore, "and calculated", diff)
    92  		}
    93  	}
    94  }
    95  */