-// 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/protocol/bc"
- "github.com/bytom/protocol/bc/legacy"
- "github.com/bytom/protocol/state"
-)
-
-func TestNewInitBlock(t *testing.T) {
- coinbaseTx, err := createCoinbaseTx(0, 0, []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)
- }
+import "testing"
- b := &legacy.Block{
- BlockHeader: legacy.BlockHeader{
- Version: 1,
- Height: 0,
- PreviousBlockHash: bc.Hash{},
- TimestampMS: bc.Millis(time.Now()),
- BlockCommitment: legacy.BlockCommitment{
- TransactionsMerkleRoot: merkleRoot,
- AssetsMerkleRoot: snap.Tree.RootHash(),
- },
- Bits: uint64(3314649325747331761),
- Nonce: 0,
+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},
}
- rawBlock, err := b.MarshalText()
- if err != nil {
- t.Error(err)
+ for _, c := range cases {
+ coinbaseTx, err := createCoinbaseTx(nil, c.txFee, c.height)
+ if err != nil {
+ t.Fatal(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))
}