From f1b8aed51cc377504c9e569c4f6d889958371532 Mon Sep 17 00:00:00 2001 From: lbqds Date: Sat, 14 Apr 2018 17:55:42 +0800 Subject: [PATCH] add coinbase tx tests --- mining/mining_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/tx_test.go | 23 +++++++++++---------- 2 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 mining/mining_test.go diff --git a/mining/mining_test.go b/mining/mining_test.go new file mode 100644 index 00000000..205fb962 --- /dev/null +++ b/mining/mining_test.go @@ -0,0 +1,55 @@ +package mining + +import ( + "fmt" + "testing" +) + +func TestCreateCoinbaseTx(t *testing.T) { + reductionInterval := uint64(560640) + baseReward := uint64(41250000000) + cases := []struct { + height uint64 + txFee uint64 + reward uint64 + }{ + { + height: reductionInterval - 1, + txFee: 0, + reward: baseReward, + }, + { + height: reductionInterval, + txFee: 0, + reward: baseReward / 2, + }, + { + height: reductionInterval + 1, + txFee: 0, + reward: baseReward / 2, + }, + { + height: reductionInterval * 2, + txFee: 100000000, + reward: baseReward/4 + 100000000, + }, + { + height: reductionInterval * 10, + txFee: 0, + reward: baseReward / 1024, + }, + } + + for _, c := range cases { + coinbaseTx, err := createCoinbaseTx(nil, c.txFee, c.height) + if err != nil { + t.Fatal(err) + } + + outputAmount := coinbaseTx.Outputs[0].OutputCommitment.Amount + fmt.Println(outputAmount) + if outputAmount != c.reward { + t.Fatalf("coinbase tx reward dismatch, expected: %d, have: %d", c.reward, outputAmount) + } + } +} diff --git a/test/tx_test.go b/test/tx_test.go index b5761a45..cf8f04d0 100644 --- a/test/tx_test.go +++ b/test/tx_test.go @@ -237,17 +237,20 @@ func TestCoinbaseTx(t *testing.T) { t.Fatal(err) } - coinbaseTx, err := CreateCoinbaseTx(defaultCtrlProg, block.Height, 100000) - if err != nil { - t.Fatal(err) - } + txFees := []uint64{100000, 5000000000000} + for _, txFee := range txFees { + coinbaseTx, err := CreateCoinbaseTx(defaultCtrlProg, block.Height, txFee) + if err != nil { + t.Fatal(err) + } - if err := ReplaceCoinbase(block, coinbaseTx); err != nil { - t.Fatal(err) - } + if err := ReplaceCoinbase(block, coinbaseTx); err != nil { + t.Fatal(err) + } - err = SolveAndUpdate(chain, block) - if err == nil { - t.Fatalf("invalid coinbase tx validate success") + err = SolveAndUpdate(chain, block) + if err == nil { + t.Fatalf("invalid coinbase tx validate success") + } } } -- 2.11.0