OSDN Git Service

Fix wallet issue asset tx annotate problem (#199)
[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/crypto/sha3pool"
14         "github.com/bytom/protocol/bc"
15         "github.com/bytom/protocol/bc/legacy"
16         "github.com/bytom/protocol/state"
17 )
18
19 func TestNewInitBlock(t *testing.T) {
20         coinbaseTx, err := createCoinbaseTx(nil, 0, 1)
21         if err != nil {
22                 t.Error(err)
23         }
24         merkleRoot, err := bc.MerkleRoot([]*bc.Tx{coinbaseTx.Tx})
25         if err != nil {
26                 t.Error(err)
27         }
28         snap := state.Empty()
29         if err := snap.ApplyTx(coinbaseTx.Tx); err != nil {
30                 t.Error(err)
31         }
32
33         var seed [32]byte
34         sha3pool.Sum256(seed[:], make([]byte, 32))
35
36         b := &legacy.Block{
37                 BlockHeader: legacy.BlockHeader{
38                         Version:           1,
39                         Height:            1,
40                         PreviousBlockHash: bc.Hash{},
41                         TimestampMS:       bc.Millis(time.Now()),
42                         BlockCommitment: legacy.BlockCommitment{
43                                 TransactionsMerkleRoot: merkleRoot,
44                                 AssetsMerkleRoot:       snap.Tree.RootHash(),
45                         },
46                         Bits: uint64(2161727821138738707),
47                         Seed: bc.NewHash(seed),
48                 },
49                 Transactions: []*legacy.Tx{coinbaseTx},
50         }
51
52         for i := uint64(0); i <= 10000000000000; i++ {
53                 b.Nonce = i
54                 hash := b.Hash()
55
56                 if consensus.CheckProofOfWork(&hash, b.Bits) {
57                         break
58                 }
59         }
60
61         rawBlock, err := b.MarshalText()
62         if err != nil {
63                 t.Error(err)
64         }
65         fmt.Println(string(rawBlock))
66 }