github.com/aquanetwork/aquachain@v1.7.8/cmd/aquaminer/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  	"math/big"
     7  	"testing"
     8  
     9  	"gitlab.com/aquachain/aquachain/common"
    10  	"gitlab.com/aquachain/aquachain/crypto"
    11  )
    12  
    13  func TestMiner(t *testing.T) {
    14  	// dummy work load
    15  	workHash := common.HexToHash("0xd3b5f1b47f52fdc72b1dab0b02ab352442487a1d3a43211bc4f0eb5f092403fc")
    16  	target := new(big.Int).SetBytes(common.HexToHash("0x08637bd05af6c69b5a63f9a49c2c1b10fd7e45803cd141a6937d1fe64f54").Bytes())
    17  
    18  	// good nonce
    19  	nonce := uint64(14649775584697213406)
    20  
    21  	seed := make([]byte, 40)
    22  	copy(seed, workHash.Bytes())
    23  	fmt.Printf("hashing work: %x\nless than target:  %s\nnonce: %v\n", workHash, target, nonce)
    24  
    25  	// debug
    26  	fmt.Printf("seednononc: %x\n", seed)
    27  
    28  	// little endian
    29  	binary.LittleEndian.PutUint64(seed[32:], nonce)
    30  
    31  	// pre hash
    32  	fmt.Printf("beforehash: %x\n", seed)
    33  
    34  	// hash
    35  	result := crypto.VersionHash(2, seed)
    36  
    37  	// difficulty
    38  	out := new(big.Int).SetBytes(result)
    39  	fmt.Printf("result difficulty: %s\n", out)
    40  	fmt.Printf("result difficulty: %x\n", out)
    41  
    42  	// test against target difficulty
    43  	testresult := out.Cmp(target) <= 0
    44  	fmt.Printf("%x: %v\n", out, testresult)
    45  	if !testresult {
    46  		t.FailNow()
    47  	}
    48  }