git.gammaspectra.live/P2Pool/consensus/v3@v3.8.0/types/pow_test.go (about)

     1  package types
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  )
     7  
     8  var (
     9  	powHash             = MustHashFromString("abcf2c2ee4a64a683f24bedb2099dd16ae08c03a1ecc1208bf93a90200000000")
    10  	sidechainDifficulty = DifficultyFrom64(2062136440)
    11  	powDifficulty       = DifficultyFrom64(412975968250)
    12  	moneroDifficulty    = DifficultyFrom64(229654626174)
    13  )
    14  
    15  func TestDifficultyFromPoW(t *testing.T) {
    16  	diff := DifficultyFromPoW(powHash)
    17  
    18  	if !diff.Equals(powDifficulty) {
    19  		t.Errorf("%s does not equal %s", diff, powDifficulty)
    20  	}
    21  }
    22  
    23  func TestDifficulty_CheckPoW(t *testing.T) {
    24  
    25  	if !moneroDifficulty.CheckPoW(powHash) {
    26  		t.Errorf("%s does not pass PoW %s", powHash, moneroDifficulty)
    27  	}
    28  
    29  	if !sidechainDifficulty.CheckPoW(powHash) {
    30  		t.Errorf("%s does not pass PoW %s", powHash, sidechainDifficulty)
    31  	}
    32  
    33  	if !powDifficulty.CheckPoW(powHash) {
    34  		t.Errorf("%s does not pass PoW %s", powHash, powDifficulty)
    35  	}
    36  
    37  	powHash2 := powHash
    38  	powHash2[len(powHash2)-1]++
    39  
    40  	if moneroDifficulty.CheckPoW(powHash2) {
    41  		t.Errorf("%s does pass PoW %s incorrectly", powHash2, moneroDifficulty)
    42  	}
    43  
    44  	if sidechainDifficulty.CheckPoW(powHash2) {
    45  		t.Errorf("%s does pass PoW %s incorrectly", powHash2, sidechainDifficulty)
    46  	}
    47  
    48  	powHash3 := powHash
    49  	powHash3[len(powHash2)-9]++
    50  
    51  	if powDifficulty.CheckPoW(powHash3) {
    52  		t.Errorf("%s does pass PoW %s incorrectly", powHash3, powDifficulty)
    53  	}
    54  }
    55  
    56  func BenchmarkDifficulty_CheckPoW(b *testing.B) {
    57  	b.ReportAllocs()
    58  
    59  	b.RunParallel(func(pb *testing.PB) {
    60  		var result bool
    61  		for pb.Next() {
    62  			result = moneroDifficulty.CheckPoW(powHash)
    63  		}
    64  		runtime.KeepAlive(result)
    65  	})
    66  }
    67  
    68  func BenchmarkDifficulty_CheckPoW_Native(b *testing.B) {
    69  	b.ReportAllocs()
    70  
    71  	b.RunParallel(func(pb *testing.PB) {
    72  		var result bool
    73  		for pb.Next() {
    74  			result = moneroDifficulty.CheckPoW_Native(powHash)
    75  		}
    76  		runtime.KeepAlive(result)
    77  	})
    78  }