github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/consensus/ethash/ethash_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 ethash
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/wanchain/go-wanchain/accounts"
    24  	"github.com/wanchain/go-wanchain/common"
    25  	"github.com/wanchain/go-wanchain/core/types"
    26  	"github.com/wanchain/go-wanchain/crypto"
    27  )
    28  
    29  // func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) {
    30  // 	d, err := ioutil.TempDir("", "wanchain-keystore-test")
    31  // 	if err != nil {
    32  // 		t.Fatal(err)
    33  // 	}
    34  // 	new := NewPlaintextKeyStore
    35  // 	if encrypted {
    36  // 		new = func(kd string) *KeyStore { return NewKeyStore(kd, veryLightScryptN, veryLightScryptP) }
    37  // 	}
    38  // 	return d, new(d)
    39  // }
    40  
    41  // Tests that ethash works correctly in test mode.
    42  var fakedAddr = common.HexToAddress("0xf9b32578b4420a36f132db32b56f3831a7cc1804")
    43  var fakedAccountPrivateKey, _ = crypto.HexToECDSA("f1572f76b75b40a7da72d6f2ee7fda3d1189c2d28f0a2f096347055abe344d7f")
    44  
    45  func fakeSignerFn(signer accounts.Account, hash []byte) ([]byte, error) {
    46  	return crypto.Sign(hash, fakedAccountPrivateKey)
    47  }
    48  
    49  func TestTestMode(t *testing.T) {
    50  	head := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
    51  	head.Coinbase = fakedAddr
    52  
    53  	head.Extra = make([]byte, extraSeal+extraVanity)
    54  	sighash4Extra, err := fakeSignerFn(accounts.Account{}, sigHash(head).Bytes())
    55  	copy(head.Extra[len(head.Extra)-extraSeal:], sighash4Extra)
    56  
    57  	ethash := NewTester(nil)
    58  	ethash.signer = fakedAddr
    59  	ethash.signFn = fakeSignerFn
    60  	block, err := ethash.Seal(nil, types.NewBlockWithHeader(head), nil)
    61  
    62  	if err != nil {
    63  		t.Fatalf("failed to seal block: %v", err)
    64  	}
    65  	head.Nonce = types.EncodeNonce(block.Nonce())
    66  	head.MixDigest = block.MixDigest()
    67  	if err := ethash.VerifySeal(nil, head); err != nil {
    68  		t.Fatalf("unexpected verification error: %v", err)
    69  	}
    70  
    71  }