OSDN Git Service

Merge branch 'dev' of git://github.com/Bytom/bytom
[bytom/bytom.git] / mining / mining_test.go
1 // Copyright (c) 2014-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package mining
6
7 import (
8         "fmt"
9         "testing"
10         "time"
11
12         "github.com/bytom/consensus"
13         "github.com/bytom/protocol/bc"
14         "github.com/bytom/protocol/bc/legacy"
15         "github.com/bytom/protocol/state"
16 )
17
18 func TestNewInitBlock(t *testing.T) {
19         coinbaseTx, err := createCoinbaseTx(0, 1, []byte{})
20         if err != nil {
21                 t.Error(err)
22         }
23         merkleRoot, err := bc.MerkleRoot([]*bc.Tx{coinbaseTx.Tx})
24         if err != nil {
25                 t.Error(err)
26         }
27         snap := state.Empty()
28         if err := snap.ApplyTx(coinbaseTx.Tx); err != nil {
29                 t.Error(err)
30         }
31
32         b := &legacy.Block{
33                 BlockHeader: legacy.BlockHeader{
34                         Version:           1,
35                         Height:            1,
36                         PreviousBlockHash: bc.Hash{},
37                         TimestampMS:       bc.Millis(time.Now()),
38                         BlockCommitment: legacy.BlockCommitment{
39                                 TransactionsMerkleRoot: merkleRoot,
40                                 AssetsMerkleRoot:       snap.Tree.RootHash(),
41                         },
42                         Bits: uint64(2161727821138738707),
43                 },
44                 Transactions: []*legacy.Tx{coinbaseTx},
45         }
46
47         for i := uint64(0); i <= 10000000000000; i++ {
48                 b.Nonce = i
49                 hash := b.Hash()
50
51                 if consensus.CheckProofOfWork(&hash, b.Bits) {
52                         break
53                 }
54         }
55
56         rawBlock, err := b.MarshalText()
57         if err != nil {
58                 t.Error(err)
59         }
60         fmt.Println(string(rawBlock))
61 }