OSDN Git Service

01df93c4dbdf508bc251703af4ce4836292c2e52
[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/mining/tensority"
8         "github.com/vapor/protocol/bc"
9         "github.com/vapor/protocol/bc/types"
10         "github.com/vapor/protocol/state"
11         "github.com/vapor/testutil"
12 )
13
14 func TestCheckBlockTime(t *testing.T) {
15         cases := []struct {
16                 blockTime  uint64
17                 parentTime uint64
18                 err        error
19         }{
20                 {
21                         blockTime:  1520000001,
22                         parentTime: 1520000000,
23                         err:        nil,
24                 },
25                 {
26                         blockTime:  1510000000,
27                         parentTime: 1520000000,
28                         err:        errBadTimestamp,
29                 },
30                 {
31                         blockTime:  9999999999,
32                         parentTime: 1520000000,
33                         err:        errBadTimestamp,
34                 },
35         }
36
37         parent := &state.BlockNode{}
38         block := &bc.Block{
39                 BlockHeader: &bc.BlockHeader{},
40         }
41
42         for i, c := range cases {
43                 parent.Timestamp = c.parentTime
44                 block.Timestamp = c.blockTime
45                 if err := checkBlockTime(block, parent); rootErr(err) != c.err {
46                         t.Errorf("case %d got error %s, want %s", i, err, c.err)
47                 }
48         }
49 }
50
51 func TestCheckCoinbaseAmount(t *testing.T) {
52         cases := []struct {
53                 txs    []*types.Tx
54                 amount uint64
55                 err    error
56         }{
57                 {
58                         txs: []*types.Tx{
59                                 types.NewTx(types.TxData{
60                                         Inputs:  []*types.TxInput{types.NewCoinbaseInput(nil)},
61                                         Outputs: []*types.TxOutput{types.NewTxOutput(*consensus.BTMAssetID, 5000, nil)},
62                                 }),
63                         },
64                         amount: 5000,
65                         err:    nil,
66                 },
67                 {
68                         txs: []*types.Tx{
69                                 types.NewTx(types.TxData{
70                                         Inputs:  []*types.TxInput{types.NewCoinbaseInput(nil)},
71                                         Outputs: []*types.TxOutput{types.NewTxOutput(*consensus.BTMAssetID, 5000, nil)},
72                                 }),
73                         },
74                         amount: 6000,
75                         err:    ErrWrongCoinbaseTransaction,
76                 },
77                 {
78                         txs:    []*types.Tx{},
79                         amount: 5000,
80                         err:    ErrWrongCoinbaseTransaction,
81                 },
82         }
83
84         block := new(types.Block)
85         for i, c := range cases {
86                 block.Transactions = c.txs
87                 if err := checkCoinbaseAmount(types.MapBlock(block), c.amount); rootErr(err) != c.err {
88                         t.Errorf("case %d got error %s, want %s", i, err, c.err)
89                 }
90         }
91 }
92
93 func TestValidateBlockHeader(t *testing.T) {
94         // add (hash, seed) --> (tensority hash) to the  tensority cache for avoid
95         // real matrix calculate cost.
96         tensority.AIHash.AddCache(&bc.Hash{V0: 0}, &bc.Hash{}, testutil.MaxHash)
97         tensority.AIHash.AddCache(&bc.Hash{V0: 1}, &bc.Hash{}, testutil.MinHash)
98         tensority.AIHash.AddCache(&bc.Hash{V0: 1}, consensus.InitialSeed, testutil.MinHash)
99
100         cases := []struct {
101                 block  *bc.Block
102                 parent *state.BlockNode
103                 err    error
104         }{
105                 {
106                         block: &bc.Block{BlockHeader: &bc.BlockHeader{
107                                 Version: 1,
108                         }},
109                         parent: &state.BlockNode{
110                                 Version: 2,
111                         },
112                         err: errVersionRegression,
113                 },
114                 {
115                         block: &bc.Block{BlockHeader: &bc.BlockHeader{
116                                 Height: 20,
117                         }},
118                         parent: &state.BlockNode{
119                                 Height: 18,
120                         },
121                         err: errMisorderedBlockHeight,
122                 },
123                 {
124                         block: &bc.Block{BlockHeader: &bc.BlockHeader{
125                                 Height: 20,
126                                 Bits:   0,
127                         }},
128                         parent: &state.BlockNode{
129                                 Height: 19,
130                                 Bits:   2305843009214532812,
131                         },
132                         err: errBadBits,
133                 },
134                 {
135                         block: &bc.Block{BlockHeader: &bc.BlockHeader{
136                                 Height:          20,
137                                 PreviousBlockId: &bc.Hash{V0: 18},
138                         }},
139                         parent: &state.BlockNode{
140                                 Height: 19,
141                                 Hash:   bc.Hash{V0: 19},
142                         },
143                         err: errMismatchedBlock,
144                 },
145                 {
146                         block: &bc.Block{
147                                 ID: bc.Hash{V0: 0},
148                                 BlockHeader: &bc.BlockHeader{
149                                         Height:          1,
150                                         Timestamp:       1523352601,
151                                         PreviousBlockId: &bc.Hash{V0: 0},
152                                         Bits:            2305843009214532812,
153                                 },
154                         },
155                         parent: &state.BlockNode{
156                                 Height:    0,
157                                 Timestamp: 1523352600,
158                                 Hash:      bc.Hash{V0: 0},
159                                 Seed:      &bc.Hash{V1: 1},
160                                 Bits:      2305843009214532812,
161                         },
162                         err: errWorkProof,
163                 },
164                 {
165                         block: &bc.Block{
166                                 ID: bc.Hash{V0: 1},
167                                 BlockHeader: &bc.BlockHeader{
168                                         Height:          1,
169                                         Timestamp:       1523352601,
170                                         PreviousBlockId: &bc.Hash{V0: 0},
171                                         Bits:            2305843009214532812,
172                                 },
173                         },
174                         parent: &state.BlockNode{
175                                 Height:    0,
176                                 Timestamp: 1523352600,
177                                 Hash:      bc.Hash{V0: 0},
178                                 Seed:      &bc.Hash{V1: 1},
179                                 Bits:      2305843009214532812,
180                         },
181                         err: nil,
182                 },
183         }
184
185         for i, c := range cases {
186                 if err := ValidateBlockHeader(c.block, c.parent); rootErr(err) != c.err {
187                         t.Errorf("case %d got error %s, want %s", i, err, c.err)
188                 }
189         }
190 }