OSDN Git Service

Merge pull request #15 from Bytom/dev_ci
[bytom/vapor.git] / protocol / validation / block_test.go
1 package validation
2
3 import (
4         "testing"
5
6         "github.com/vapor/consensus"
7         "github.com/vapor/protocol/bc"
8         "github.com/vapor/protocol/bc/types"
9         "github.com/vapor/protocol/state"
10 )
11
12 func TestCheckBlockTime(t *testing.T) {
13         cases := []struct {
14                 blockTime  uint64
15                 parentTime uint64
16                 err        error
17         }{
18                 {
19                         blockTime:  1520000001,
20                         parentTime: 1520000000,
21                         err:        nil,
22                 },
23                 {
24                         blockTime:  1510000000,
25                         parentTime: 1520000000,
26                         err:        errBadTimestamp,
27                 },
28                 {
29                         blockTime:  9999999999,
30                         parentTime: 1520000000,
31                         err:        errBadTimestamp,
32                 },
33         }
34
35         parent := &state.BlockNode{}
36         block := &bc.Block{
37                 BlockHeader: &bc.BlockHeader{},
38         }
39
40         for i, c := range cases {
41                 parent.Timestamp = c.parentTime
42                 block.Timestamp = c.blockTime
43                 if err := checkBlockTime(block, parent); rootErr(err) != c.err {
44                         t.Errorf("case %d got error %s, want %s", i, err, c.err)
45                 }
46         }
47 }
48
49 func TestCheckCoinbaseAmount(t *testing.T) {
50         cases := []struct {
51                 txs    []*types.Tx
52                 amount uint64
53                 err    error
54         }{
55                 {
56                         txs: []*types.Tx{
57                                 types.NewTx(types.TxData{
58                                         Inputs:  []*types.TxInput{types.NewCoinbaseInput(nil)},
59                                         Outputs: []*types.TxOutput{types.NewTxOutput(*consensus.BTMAssetID, 5000, nil)},
60                                 }),
61                         },
62                         amount: 5000,
63                         err:    nil,
64                 },
65                 {
66                         txs: []*types.Tx{
67                                 types.NewTx(types.TxData{
68                                         Inputs:  []*types.TxInput{types.NewCoinbaseInput(nil)},
69                                         Outputs: []*types.TxOutput{types.NewTxOutput(*consensus.BTMAssetID, 5000, nil)},
70                                 }),
71                         },
72                         amount: 6000,
73                         err:    ErrWrongCoinbaseTransaction,
74                 },
75                 {
76                         txs:    []*types.Tx{},
77                         amount: 5000,
78                         err:    ErrWrongCoinbaseTransaction,
79                 },
80         }
81
82         block := new(types.Block)
83         for i, c := range cases {
84                 block.Transactions = c.txs
85                 if err := checkCoinbaseAmount(types.MapBlock(block), c.amount); rootErr(err) != c.err {
86                         t.Errorf("case %d got error %s, want %s", i, err, c.err)
87                 }
88         }
89 }