github.com/cryptoecc/eth-ecc@v0.0.3/consensus/eccpow/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 eccpow
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"math/big"
    24  	"os"
    25  	"path/filepath"
    26  	"testing"
    27  
    28  	"github.com/cryptoecc/ETH-ECC/common"
    29  	"github.com/cryptoecc/ETH-ECC/common/math"
    30  	"github.com/cryptoecc/ETH-ECC/core/types"
    31  	"github.com/cryptoecc/ETH-ECC/params"
    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  func TestCalcDifficulty(t *testing.T) {
    64  	file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
    65  	if err != nil {
    66  		t.Skip(err)
    67  	}
    68  	defer file.Close()
    69  
    70  	tests := make(map[string]diffTest)
    71  	err = json.NewDecoder(file).Decode(&tests)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	config := &params.ChainConfig{HomesteadBlock: big.NewInt(1150000)}
    77  
    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:       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  }
    90  
    91  func TestDecodingVerification(t *testing.T) {
    92  	for i := 0; i < 8; i++ {
    93  		ecc := ECC{}
    94  		header := new(types.Header)
    95  		header.Difficulty = ProbToDifficulty(Table[0].miningProb)
    96  		hash := ecc.SealHash(header).Bytes()
    97  
    98  		hashVector, outputWord, LDPCNonce, digest := RunOptimizedConcurrencyLDPC(header, hash)
    99  
   100  		headerForTest := types.CopyHeader(header)
   101  		headerForTest.MixDigest = common.BytesToHash(digest)
   102  		headerForTest.Nonce = types.EncodeNonce(LDPCNonce)
   103  		hashForTest := ecc.SealHash(headerForTest).Bytes()
   104  
   105  		flag, hashVectorOfVerification, outputWordOfVerification, digestForValidation := VerifyOptimizedDecoding(headerForTest, hashForTest)
   106  
   107  		encodedDigestForValidation := common.BytesToHash(digestForValidation)
   108  
   109  		//fmt.Printf("%+v\n", header)
   110  		//fmt.Printf("Hash : %v\n", hash)
   111  		//fmt.Println()
   112  
   113  		//fmt.Printf("%+v\n", headerForTest)
   114  		//fmt.Printf("headerForTest : %v\n", headerForTest)
   115  		//fmt.Println()
   116  
   117  		// * means padding for compare easily
   118  		if flag && bytes.Equal(headerForTest.MixDigest[:], encodedDigestForValidation[:]) {
   119  			fmt.Printf("Hash vector ** ************ : %v\n", hashVector)
   120  			fmt.Printf("Hash vector of verification : %v\n", hashVectorOfVerification)
   121  
   122  			fmt.Printf("Outputword ** ************ : %v\n", outputWord)
   123  			fmt.Printf("Outputword of verification : %v\n", outputWordOfVerification)
   124  
   125  			fmt.Printf("LDPC Nonce : %v\n", LDPCNonce)
   126  			fmt.Printf("Digest : %v\n", headerForTest.MixDigest[:])
   127  			/*
   128  				t.Logf("Hash vector : %v\n", hashVector)
   129  				t.Logf("Outputword : %v\n", outputWord)
   130  				t.Logf("LDPC Nonce : %v\n", LDPCNonce)
   131  				t.Logf("Digest : %v\n", header.MixDigest[:])
   132  			*/
   133  		} else {
   134  			fmt.Printf("Hash vector ** ************ : %v\n", hashVector)
   135  			fmt.Printf("Hash vector of verification : %v\n", hashVectorOfVerification)
   136  
   137  			fmt.Printf("Outputword ** ************ : %v\n", outputWord)
   138  			fmt.Printf("Outputword of verification : %v\n", outputWordOfVerification)
   139  
   140  			fmt.Printf("flag : %v\n", flag)
   141  			fmt.Printf("Digest compare result : %v\n", bytes.Equal(headerForTest.MixDigest[:], encodedDigestForValidation[:]))
   142  			fmt.Printf("Digest *** ********** : %v\n", headerForTest.MixDigest[:])
   143  			fmt.Printf("Digest for validation : %v\n", encodedDigestForValidation)
   144  
   145  			t.Errorf("Test Fail")
   146  			/*
   147  				t.Errorf("flag : %v\n", flag)
   148  				t.Errorf("Digest compare result : %v", bytes.Equal(header.MixDigest[:], digestForValidation)
   149  				t.Errorf("Digest : %v\n", digest)
   150  				t.Errorf("Digest for validation : %v\n", digestForValidation)
   151  			*/
   152  		}
   153  		//t.Logf("\n")
   154  		fmt.Println()
   155  	}
   156  }