OSDN Git Service

Merge pull request #935 from Bytom/dev
[bytom/bytom.git] / mining / mining_test.go
index d2cb11a..d558e1f 100644 (file)
@@ -1,61 +1,46 @@
-// Copyright (c) 2014-2016 The btcsuite developers
-// Use of this source code is governed by an ISC
-// license that can be found in the LICENSE file.
-
 package mining
 
-import (
-       "fmt"
-       "testing"
-       "time"
-
-       "github.com/bytom/consensus"
-       "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/legacy"
-       "github.com/bytom/protocol/state"
-)
-
-func TestNewInitBlock(t *testing.T) {
-       coinbaseTx, err := createCoinbaseTx(0, 1, []byte{})
-       if err != nil {
-               t.Error(err)
-       }
-       merkleRoot, err := bc.MerkleRoot([]*bc.Tx{coinbaseTx.Tx})
-       if err != nil {
-               t.Error(err)
-       }
-       snap := state.Empty()
-       if err := snap.ApplyTx(coinbaseTx.Tx); err != nil {
-               t.Error(err)
-       }
-
-       b := &legacy.Block{
-               BlockHeader: legacy.BlockHeader{
-                       Version:           1,
-                       Height:            1,
-                       PreviousBlockHash: bc.Hash{},
-                       TimestampMS:       bc.Millis(time.Now()),
-                       BlockCommitment: legacy.BlockCommitment{
-                               TransactionsMerkleRoot: merkleRoot,
-                               AssetsMerkleRoot:       snap.Tree.RootHash(),
-                       },
-                       Bits: uint64(2161727821138738707),
+import "testing"
+
+func TestCreateCoinbaseTx(t *testing.T) {
+       reductionInterval := uint64(840000)
+       baseSubsidy := uint64(41250000000)
+       cases := []struct {
+               height  uint64
+               txFee   uint64
+               subsidy uint64
+       }{
+               {
+                       height:  reductionInterval - 1,
+                       txFee:   100000000,
+                       subsidy: baseSubsidy + 100000000,
+               },
+               {
+                       height:  reductionInterval,
+                       txFee:   2000000000,
+                       subsidy: baseSubsidy/2 + 2000000000,
+               },
+               {
+                       height:  reductionInterval + 1,
+                       txFee:   0,
+                       subsidy: baseSubsidy / 2,
+               },
+               {
+                       height:  reductionInterval * 2,
+                       txFee:   100000000,
+                       subsidy: baseSubsidy/4 + 100000000,
                },
-               Transactions: []*legacy.Tx{coinbaseTx},
        }
 
-       for i := uint64(0); i <= 10000000000000; i++ {
-               b.Nonce = i
-               hash := b.Hash()
-
-               if consensus.CheckProofOfWork(&hash, b.Bits) {
-                       break
+       for _, c := range cases {
+               coinbaseTx, err := createCoinbaseTx(nil, c.txFee, c.height)
+               if err != nil {
+                       t.Fatal(err)
                }
-       }
 
-       rawBlock, err := b.MarshalText()
-       if err != nil {
-               t.Error(err)
+               outputAmount := coinbaseTx.Outputs[0].OutputCommitment.Amount
+               if outputAmount != c.subsidy {
+                       t.Fatalf("coinbase tx reward dismatch, expected: %d, have: %d", c.subsidy, outputAmount)
+               }
        }
-       fmt.Println(string(rawBlock))
 }