OSDN Git Service

add coinbase tx tests
authorlbqds <lbqds@outlook.com>
Sat, 14 Apr 2018 09:55:42 +0000 (17:55 +0800)
committerlbqds <lbqds@outlook.com>
Mon, 16 Apr 2018 02:55:06 +0000 (10:55 +0800)
mining/mining_test.go [new file with mode: 0644]
test/tx_test.go

diff --git a/mining/mining_test.go b/mining/mining_test.go
new file mode 100644 (file)
index 0000000..205fb96
--- /dev/null
@@ -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)
+               }
+       }
+}
index b5761a4..cf8f04d 100644 (file)
@@ -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")
+               }
        }
 }