OSDN Git Service

code need to be little better elegant
[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/protocol/bc"
13         "github.com/bytom/protocol/bc/legacy"
14         "github.com/bytom/protocol/state"
15 )
16
17 func TestNewInitBlock(t *testing.T) {
18         coinbaseTx, err := createCoinbaseTx(0, 0, []byte{})
19         if err != nil {
20                 t.Error(err)
21         }
22         merkleRoot, err := bc.MerkleRoot([]*bc.Tx{coinbaseTx.Tx})
23         if err != nil {
24                 t.Error(err)
25         }
26         snap := state.Empty()
27         if err := snap.ApplyTx(coinbaseTx.Tx); err != nil {
28                 t.Error(err)
29         }
30
31         b := &legacy.Block{
32                 BlockHeader: legacy.BlockHeader{
33                         Version:           1,
34                         Height:            0,
35                         PreviousBlockHash: bc.Hash{},
36                         TimestampMS:       bc.Millis(time.Now()),
37                         BlockCommitment: legacy.BlockCommitment{
38                                 TransactionsMerkleRoot: merkleRoot,
39                                 AssetsMerkleRoot:       snap.Tree.RootHash(),
40                         },
41                         Bits:  uint64(3314649325747331761),
42                         Nonce: 0,
43                 },
44                 Transactions: []*legacy.Tx{coinbaseTx},
45         }
46
47         rawBlock, err := b.MarshalText()
48         if err != nil {
49                 t.Error(err)
50         }
51         fmt.Println(string(rawBlock))
52 }