OSDN Git Service

add unit test and modify file name (#247)
[bytom/vapor.git] / proposal / proposal_test.go
index 02777db..bfc8fa3 100644 (file)
@@ -1,12 +1,17 @@
 package proposal
 
 import (
+       "encoding/hex"
        "testing"
 
        "github.com/vapor/consensus"
+       "github.com/vapor/protocol/bc/types"
+       "github.com/vapor/protocol/state"
+       "github.com/vapor/protocol/vm/vmutil"
+       "github.com/vapor/testutil"
 )
 
-func TestCreateCoinbaseTx(t *testing.T) {
+func TestCalCoinbaseTxReward(t *testing.T) {
        consensus.ActiveNetParams = consensus.Params{
                ProducerSubsidys: []consensus.ProducerSubsidy{
                        {BeginBlock: 0, EndBlock: 0, Subsidy: 24},
@@ -16,42 +21,251 @@ func TestCreateCoinbaseTx(t *testing.T) {
                },
        }
        reductionInterval := uint64(840000)
+
        cases := []struct {
-               height  uint64
-               txFee   uint64
-               subsidy uint64
+               desc            string
+               block           *types.Block
+               consensusResult *state.ConsensusResult
+               wantReward      state.CoinbaseReward
        }{
                {
-                       height:  reductionInterval - 1,
-                       txFee:   100000000,
-                       subsidy: 24 + 100000000,
+                       desc: "the block height is reductionInterval - 1",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: reductionInterval - 1,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{},
+                       },
+                       wantReward: state.CoinbaseReward{
+                               Amount:         24,
+                               ControlProgram: []byte{0x51},
+                       },
+               },
+               {
+                       desc: "the block height is reductionInterval",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: reductionInterval,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{},
+                       },
+                       wantReward: state.CoinbaseReward{
+                               Amount:         24,
+                               ControlProgram: []byte{0x51},
+                       },
                },
                {
-                       height:  reductionInterval,
-                       txFee:   2000000000,
-                       subsidy: 24 + 2000000000,
+                       desc: "the block height is reductionInterval + 1",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: reductionInterval + 1,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{},
+                       },
+                       wantReward: state.CoinbaseReward{
+                               Amount:         12,
+                               ControlProgram: []byte{0x51},
+                       },
                },
                {
-                       height:  reductionInterval + 1,
-                       txFee:   0,
-                       subsidy: 12,
+                       desc: "the block height is reductionInterval * 2",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: reductionInterval * 2,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{},
+                       },
+                       wantReward: state.CoinbaseReward{
+                               Amount:         12,
+                               ControlProgram: []byte{0x51},
+                       },
                },
                {
-                       height:  reductionInterval * 2,
-                       txFee:   100000000,
-                       subsidy: 12 + 100000000,
+                       desc: "the block height is 2*reductionInterval + 1",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: 2*reductionInterval + 1,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{},
+                       },
+                       wantReward: state.CoinbaseReward{
+                               Amount:         6,
+                               ControlProgram: []byte{0x51},
+                       },
                },
        }
 
-       for index, c := range cases {
-               coinbaseTx, err := createCoinbaseTx(nil, c.txFee, c.height)
+       var err error
+       for i, c := range cases {
+               c.block.Transactions[0], err = createCoinbaseTx(nil, c.block.Height)
                if err != nil {
                        t.Fatal(err)
                }
 
-               outputAmount := coinbaseTx.Outputs[0].OutputCommitment().Amount
-               if outputAmount != c.subsidy {
-                       t.Fatalf("index:%d,coinbase tx reward dismatch, expected: %d, have: %d", index, c.subsidy, outputAmount)
+               if err := c.consensusResult.AttachCoinbaseReward(c.block); err != nil {
+                       t.Fatal(err)
+               }
+
+               defaultProgram, _ := vmutil.DefaultCoinbaseProgram()
+               gotReward := state.CoinbaseReward{
+                       Amount:         c.consensusResult.CoinbaseReward[hex.EncodeToString(defaultProgram)],
+                       ControlProgram: defaultProgram,
+               }
+
+               if !testutil.DeepEqual(gotReward, c.wantReward) {
+                       t.Fatalf("test case %d: %s, the coinbase reward got: %v, want: %v", i, c.desc, gotReward, c.wantReward)
+               }
+       }
+}
+
+func TestCountCoinbaseTxRewards(t *testing.T) {
+       consensus.ActiveNetParams = consensus.Params{
+               ProducerSubsidys: []consensus.ProducerSubsidy{
+                       {BeginBlock: 0, EndBlock: 0, Subsidy: 24},
+                       {BeginBlock: 1, EndBlock: 840000, Subsidy: 24},
+                       {BeginBlock: 840001, EndBlock: 1680000, Subsidy: 12},
+                       {BeginBlock: 1680001, EndBlock: 3360000, Subsidy: 6},
+               },
+       }
+
+       cases := []struct {
+               desc            string
+               block           *types.Block
+               consensusResult *state.ConsensusResult
+               wantRewards     []state.CoinbaseReward
+       }{
+               {
+                       desc: "the block height is RoundVoteBlockNums - 1",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: consensus.RoundVoteBlockNums - 1,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 10,
+                               },
+                       },
+                       wantRewards: []state.CoinbaseReward{},
+               },
+               {
+                       desc: "the block height is RoundVoteBlockNums",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: consensus.RoundVoteBlockNums,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 10,
+                                       "52": 20,
+                               },
+                       },
+                       wantRewards: []state.CoinbaseReward{
+                               state.CoinbaseReward{
+                                       Amount:         20,
+                                       ControlProgram: []byte{0x52},
+                               },
+                               state.CoinbaseReward{
+                                       Amount:         34,
+                                       ControlProgram: []byte{0x51},
+                               },
+                       },
+               },
+               {
+                       desc: "the block height is RoundVoteBlockNums + 1",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: consensus.RoundVoteBlockNums + 1,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 10,
+                               },
+                       },
+                       wantRewards: []state.CoinbaseReward{},
+               },
+               {
+                       desc: "the block height is RoundVoteBlockNums * 2",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: consensus.RoundVoteBlockNums * 2,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 10,
+                                       "52": 20,
+                                       "53": 30,
+                               },
+                       },
+                       wantRewards: []state.CoinbaseReward{
+                               state.CoinbaseReward{
+                                       Amount:         20,
+                                       ControlProgram: []byte{0x52},
+                               },
+                               state.CoinbaseReward{
+                                       Amount:         30,
+                                       ControlProgram: []byte{0x53},
+                               },
+                               state.CoinbaseReward{
+                                       Amount:         34,
+                                       ControlProgram: []byte{0x51},
+                               },
+                       },
+               },
+               {
+                       desc: "the block height is 2*RoundVoteBlockNums + 1",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: 2*consensus.RoundVoteBlockNums + 1,
+                               },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{},
+                       },
+                       wantRewards: []state.CoinbaseReward{},
+               },
+       }
+
+       var err error
+       for i, c := range cases {
+               c.block.Transactions[0], err = createCoinbaseTx(nil, c.block.Height)
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               if err := c.consensusResult.AttachCoinbaseReward(c.block); err != nil {
+                       t.Fatal(err)
+               }
+
+               rewards, err := c.consensusResult.GetCoinbaseRewards(c.block.Height)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if !testutil.DeepEqual(rewards, c.wantRewards) {
+                       t.Fatalf("test case %d: %s, the coinbase reward got: %v, want: %v", i, c.desc, rewards, c.wantRewards)
                }
        }
 }