github.com/isti4github/eth-ecc@v0.0.0-20201227085832-c337f2d99319/consensus/eccpow/LDPCDifficulty_test.go (about)

     1  package eccpow
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"math/big"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/Onther-Tech/go-ethereum/core/types"
    11  )
    12  
    13  func TestTablePrint(t *testing.T) {
    14  	for i := range Table {
    15  		fmt.Printf("level : %v, n : %v, wc : %v, wr : %v, decisionFrom : %v, decisionTo : %v, decisionStep : %v, miningProb : %v \n", Table[i].level, Table[i].n, Table[i].wc, Table[i].wr, Table[i].decisionFrom, Table[i].decisionTo, Table[i].decisionStep, Table[i].miningProb)
    16  	}
    17  }
    18  
    19  func TestPrintReciprocal(t *testing.T) {
    20  	for i := range Table {
    21  		val := 1 / Table[i].miningProb
    22  		bigInt := FloatToBigInt(val)
    23  		fmt.Printf("Reciprocal of miningProb : %v \t big Int : %v\n", val, bigInt)
    24  	}
    25  }
    26  
    27  func TestConversionFunc(t *testing.T) {
    28  	for i := range Table {
    29  		difficulty := ProbToDifficulty(Table[i].miningProb)
    30  		miningProb := DifficultyToProb(difficulty)
    31  
    32  		// Consider only integer part.
    33  		fmt.Printf("Difficulty : %v \t MiningProb : %v\t, probability compare : %v \n", difficulty, miningProb, math.Abs(miningProb-Table[i].miningProb) < 1)
    34  	}
    35  }
    36  
    37  func TestDifficultyChange(t *testing.T) {
    38  	var hash []byte
    39  	currentLevel := 0
    40  	currentBlock := new(types.Header)
    41  	// Parent block's timestamp is 0
    42  	// compare elapse time(timestamp) and parent block's timestamp(0)
    43  	currentBlock.Difficulty = big.NewInt(0)
    44  	currentBlock.Time = 0
    45  	currentBlock.UncleHash = types.EmptyUncleHash
    46  	for i := 0; i < 5; i++ {
    47  		fmt.Printf("Current Difficulty : %v\n", currentBlock.Difficulty)
    48  
    49  		startTime := time.Now()
    50  
    51  		RunOptimizedConcurrencyLDPC(currentBlock, hash)
    52  		timeStamp := uint64(time.Since(startTime).Seconds())
    53  		fmt.Printf("Block generation time : %v\n", timeStamp)
    54  
    55  		difficultyCalculator := MakeLDPCDifficultyCalculator()
    56  		nextDifficulty := difficultyCalculator(timeStamp, currentBlock)
    57  		currentBlock.Difficulty = nextDifficulty
    58  		nextLevel := SearchLevel(nextDifficulty)
    59  
    60  		fmt.Printf("Current prob : %v, Next Level : %v,  Next difficulty : %v, Next difficulty from table : %v\n\n", Table[currentLevel].miningProb, Table[nextLevel].level, nextDifficulty, ProbToDifficulty(Table[nextLevel].miningProb))
    61  		// currentBlock.ParentHash = outputWord conversion from []int to [32]byte
    62  		currentLevel = nextLevel
    63  		fmt.Printf("Current Level : %v\n", currentLevel)
    64  	}
    65  }