gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/cmd/aquaminer/main_test.go (about)

     1  // Copyright 2018 The aquachain Authors
     2  // This file is part of aquachain.
     3  //
     4  // aquachain is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU 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  // aquachain 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 General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with aquachain. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/binary"
    21  	"fmt"
    22  	"math/big"
    23  	"testing"
    24  
    25  	"gitlab.com/aquachain/aquachain/common"
    26  	"gitlab.com/aquachain/aquachain/crypto"
    27  )
    28  
    29  func TestMiner(t *testing.T) {
    30  	// dummy work load
    31  	workHash := common.HexToHash("0xd3b5f1b47f52fdc72b1dab0b02ab352442487a1d3a43211bc4f0eb5f092403fc")
    32  	target := new(big.Int).SetBytes(common.HexToHash("0x08637bd05af6c69b5a63f9a49c2c1b10fd7e45803cd141a6937d1fe64f54").Bytes())
    33  
    34  	// good nonce
    35  	nonce := uint64(14649775584697213406)
    36  
    37  	seed := make([]byte, 40)
    38  	copy(seed, workHash.Bytes())
    39  	fmt.Printf("hashing work: %x\nless than target:  %s\nnonce: %v\n", workHash, target, nonce)
    40  
    41  	// debug
    42  	fmt.Printf("seednononc: %x\n", seed)
    43  
    44  	// little endian
    45  	binary.LittleEndian.PutUint64(seed[32:], nonce)
    46  
    47  	// pre hash
    48  	fmt.Printf("beforehash: %x\n", seed)
    49  
    50  	// hash
    51  	result := crypto.VersionHash(2, seed)
    52  
    53  	// difficulty
    54  	out := new(big.Int).SetBytes(result)
    55  	fmt.Printf("result difficulty: %s\n", out)
    56  	fmt.Printf("result difficulty: %x\n", out)
    57  
    58  	// test against target difficulty
    59  	testresult := out.Cmp(target) <= 0
    60  	fmt.Printf("%x: %v\n", out, testresult)
    61  	if !testresult {
    62  		t.FailNow()
    63  	}
    64  }
    65  
    66  func TestZeros(t *testing.T) {
    67  
    68  	seed := make([]byte, 40)
    69  	result := crypto.VersionHash(2, seed)
    70  	fmt.Printf("%02x -> %02x\n", seed, result)
    71  }