OSDN Git Service

fix the bug (#372)
[bytom/vapor.git] / proposal / proposal_test.go
index bfc8fa3..e211b14 100644 (file)
@@ -1,32 +1,28 @@
 package proposal
 
 import (
-       "encoding/hex"
        "testing"
 
        "github.com/vapor/consensus"
+       "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
        "github.com/vapor/protocol/state"
-       "github.com/vapor/protocol/vm/vmutil"
        "github.com/vapor/testutil"
 )
 
 func TestCalCoinbaseTxReward(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},
-               },
+       consensus.ActiveNetParams.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},
        }
        reductionInterval := uint64(840000)
 
        cases := []struct {
-               desc            string
-               block           *types.Block
-               consensusResult *state.ConsensusResult
-               wantReward      state.CoinbaseReward
+               desc       string
+               block      *types.Block
+               wantReward state.CoinbaseReward
        }{
                {
                        desc: "the block height is reductionInterval - 1",
@@ -34,10 +30,14 @@ func TestCalCoinbaseTxReward(t *testing.T) {
                                BlockHeader: types.BlockHeader{
                                        Height: reductionInterval - 1,
                                },
-                               Transactions: []*types.Tx{nil},
-                       },
-                       consensusResult: &state.ConsensusResult{
-                               CoinbaseReward: map[string]uint64{},
+                               Transactions: []*types.Tx{
+                                       &types.Tx{
+                                               TxData: types.TxData{
+                                                       Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
+                                                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
+                                               },
+                                       },
+                               },
                        },
                        wantReward: state.CoinbaseReward{
                                Amount:         24,
@@ -50,10 +50,14 @@ func TestCalCoinbaseTxReward(t *testing.T) {
                                BlockHeader: types.BlockHeader{
                                        Height: reductionInterval,
                                },
-                               Transactions: []*types.Tx{nil},
-                       },
-                       consensusResult: &state.ConsensusResult{
-                               CoinbaseReward: map[string]uint64{},
+                               Transactions: []*types.Tx{
+                                       &types.Tx{
+                                               TxData: types.TxData{
+                                                       Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
+                                                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
+                                               },
+                                       },
+                               },
                        },
                        wantReward: state.CoinbaseReward{
                                Amount:         24,
@@ -66,10 +70,14 @@ func TestCalCoinbaseTxReward(t *testing.T) {
                                BlockHeader: types.BlockHeader{
                                        Height: reductionInterval + 1,
                                },
-                               Transactions: []*types.Tx{nil},
-                       },
-                       consensusResult: &state.ConsensusResult{
-                               CoinbaseReward: map[string]uint64{},
+                               Transactions: []*types.Tx{
+                                       &types.Tx{
+                                               TxData: types.TxData{
+                                                       Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
+                                                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
+                                               },
+                                       },
+                               },
                        },
                        wantReward: state.CoinbaseReward{
                                Amount:         12,
@@ -82,10 +90,14 @@ func TestCalCoinbaseTxReward(t *testing.T) {
                                BlockHeader: types.BlockHeader{
                                        Height: reductionInterval * 2,
                                },
-                               Transactions: []*types.Tx{nil},
-                       },
-                       consensusResult: &state.ConsensusResult{
-                               CoinbaseReward: map[string]uint64{},
+                               Transactions: []*types.Tx{
+                                       &types.Tx{
+                                               TxData: types.TxData{
+                                                       Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
+                                                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
+                                               },
+                                       },
+                               },
                        },
                        wantReward: state.CoinbaseReward{
                                Amount:         12,
@@ -98,10 +110,14 @@ func TestCalCoinbaseTxReward(t *testing.T) {
                                BlockHeader: types.BlockHeader{
                                        Height: 2*reductionInterval + 1,
                                },
-                               Transactions: []*types.Tx{nil},
-                       },
-                       consensusResult: &state.ConsensusResult{
-                               CoinbaseReward: map[string]uint64{},
+                               Transactions: []*types.Tx{
+                                       &types.Tx{
+                                               TxData: types.TxData{
+                                                       Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
+                                                       Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
+                                               },
+                                       },
+                               },
                        },
                        wantReward: state.CoinbaseReward{
                                Amount:         6,
@@ -110,50 +126,38 @@ func TestCalCoinbaseTxReward(t *testing.T) {
                },
        }
 
-       var err error
        for i, c := range cases {
-               c.block.Transactions[0], err = createCoinbaseTx(nil, c.block.Height)
+               gotReward, err := state.CalCoinbaseReward(c.block)
                if err != nil {
                        t.Fatal(err)
                }
 
-               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)
+               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},
-               },
+       consensus.ActiveNetParams.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                string
+               block               *types.Block
+               consensusResult     *state.ConsensusResult
+               wantRewards         []state.CoinbaseReward
+               wantConsensusResult *state.ConsensusResult
        }{
                {
                        desc: "the block height is RoundVoteBlockNums - 1",
                        block: &types.Block{
                                BlockHeader: types.BlockHeader{
-                                       Height: consensus.RoundVoteBlockNums - 1,
+                                       Height: consensus.ActiveNetParams.RoundVoteBlockNums - 1,
                                },
                                Transactions: []*types.Tx{nil},
                        },
@@ -163,12 +167,17 @@ func TestCountCoinbaseTxRewards(t *testing.T) {
                                },
                        },
                        wantRewards: []state.CoinbaseReward{},
+                       wantConsensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 34,
+                               },
+                       },
                },
                {
                        desc: "the block height is RoundVoteBlockNums",
                        block: &types.Block{
                                BlockHeader: types.BlockHeader{
-                                       Height: consensus.RoundVoteBlockNums,
+                                       Height: consensus.ActiveNetParams.RoundVoteBlockNums,
                                },
                                Transactions: []*types.Tx{nil},
                        },
@@ -178,22 +187,44 @@ func TestCountCoinbaseTxRewards(t *testing.T) {
                                        "52": 20,
                                },
                        },
-                       wantRewards: []state.CoinbaseReward{
-                               state.CoinbaseReward{
-                                       Amount:         20,
-                                       ControlProgram: []byte{0x52},
+                       wantRewards: []state.CoinbaseReward{},
+                       wantConsensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 34,
+                                       "52": 20,
+                               },
+                       },
+               },
+               {
+                       desc: "the block height is RoundVoteBlockNums + 1",
+                       block: &types.Block{
+                               BlockHeader: types.BlockHeader{
+                                       Height: consensus.ActiveNetParams.RoundVoteBlockNums + 1,
                                },
+                               Transactions: []*types.Tx{nil},
+                       },
+                       consensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 10,
+                               },
+                       },
+                       wantRewards: []state.CoinbaseReward{
                                state.CoinbaseReward{
-                                       Amount:         34,
+                                       Amount:         10,
                                        ControlProgram: []byte{0x51},
                                },
                        },
+                       wantConsensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 24,
+                               },
+                       },
                },
                {
-                       desc: "the block height is RoundVoteBlockNums + 1",
+                       desc: "the block height is RoundVoteBlockNums * 2",
                        block: &types.Block{
                                BlockHeader: types.BlockHeader{
-                                       Height: consensus.RoundVoteBlockNums + 1,
+                                       Height: consensus.ActiveNetParams.RoundVoteBlockNums * 2,
                                },
                                Transactions: []*types.Tx{nil},
                        },
@@ -203,12 +234,17 @@ func TestCountCoinbaseTxRewards(t *testing.T) {
                                },
                        },
                        wantRewards: []state.CoinbaseReward{},
+                       wantConsensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 34,
+                               },
+                       },
                },
                {
-                       desc: "the block height is RoundVoteBlockNums * 2",
+                       desc: "the block height is 2*RoundVoteBlockNums + 1",
                        block: &types.Block{
                                BlockHeader: types.BlockHeader{
-                                       Height: consensus.RoundVoteBlockNums * 2,
+                                       Height: 2*consensus.ActiveNetParams.RoundVoteBlockNums + 1,
                                },
                                Transactions: []*types.Tx{nil},
                        },
@@ -216,7 +252,6 @@ func TestCountCoinbaseTxRewards(t *testing.T) {
                                CoinbaseReward: map[string]uint64{
                                        "51": 10,
                                        "52": 20,
-                                       "53": 30,
                                },
                        },
                        wantRewards: []state.CoinbaseReward{
@@ -225,47 +260,40 @@ func TestCountCoinbaseTxRewards(t *testing.T) {
                                        ControlProgram: []byte{0x52},
                                },
                                state.CoinbaseReward{
-                                       Amount:         30,
-                                       ControlProgram: []byte{0x53},
-                               },
-                               state.CoinbaseReward{
-                                       Amount:         34,
+                                       Amount:         10,
                                        ControlProgram: []byte{0x51},
                                },
                        },
-               },
-               {
-                       desc: "the block height is 2*RoundVoteBlockNums + 1",
-                       block: &types.Block{
-                               BlockHeader: types.BlockHeader{
-                                       Height: 2*consensus.RoundVoteBlockNums + 1,
+                       wantConsensusResult: &state.ConsensusResult{
+                               CoinbaseReward: map[string]uint64{
+                                       "51": 24,
                                },
-                               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)
+               rewards, err := c.consensusResult.GetCoinbaseRewards(c.block.Height - 1)
                if err != nil {
                        t.Fatal(err)
                }
 
-               if err := c.consensusResult.AttachCoinbaseReward(c.block); 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)
                }
 
-               rewards, err := c.consensusResult.GetCoinbaseRewards(c.block.Height)
+               // create coinbase transaction
+               c.block.Transactions[0], err = createCoinbaseTx(nil, c.block.Height, rewards)
                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)
+
+               if err := c.consensusResult.AttachCoinbaseReward(c.block); err != nil {
+                       t.Fatal(err)
+               }
+
+               if !testutil.DeepEqual(c.consensusResult, c.wantConsensusResult) {
+                       t.Fatalf("test case %d: %s, the consensusResult got: %v, want: %v", i, c.desc, c.consensusResult, c.wantConsensusResult)
                }
        }
 }