8 "github.com/bytom/vapor/consensus"
9 "github.com/bytom/vapor/protocol/bc"
10 "github.com/bytom/vapor/protocol/bc/types"
11 "github.com/bytom/vapor/protocol/state"
12 "github.com/bytom/vapor/protocol/vm"
13 "github.com/bytom/vapor/protocol/vm/vmutil"
14 "github.com/bytom/vapor/testutil"
17 func TestCheckBlockTime(t *testing.T) {
25 blockTime: 1520000500,
26 parentTime: []uint64{1520000000},
30 desc: "timestamp less than past median time",
31 blockTime: 1520005500,
32 parentTime: []uint64{1520000000, 1520000500, 1520001000, 1520001500, 1520002000, 1520002500, 1520003000, 1520003500, 1520004000, 1520004500, 1520005000},
36 desc: "timestamp greater than max limit",
37 blockTime: 99999999990000,
38 parentTime: []uint64{15200000000000},
42 desc: "timestamp of the block and the parent block are both greater than max limit",
43 blockTime: uint64(time.Now().UnixNano()/int64(time.Millisecond)) + consensus.ActiveNetParams.MaxTimeOffsetMs + 2000,
44 parentTime: []uint64{uint64(time.Now().UnixNano()/int64(time.Millisecond)) + consensus.ActiveNetParams.MaxTimeOffsetMs + 1000},
49 parent := &types.BlockHeader{Version: 1}
51 BlockHeader: &bc.BlockHeader{Version: 1},
54 for i, c := range cases {
55 parent.Timestamp = c.parentTime[0]
56 parentSuccessor := parent
57 for i := 1; i < len(c.parentTime); i++ {
58 Previous := &types.BlockHeader{Version: 1, Timestamp: c.parentTime[i]}
59 parentSuccessor.PreviousBlockHash = Previous.Hash()
60 parentSuccessor = Previous
63 block.Timestamp = c.blockTime
64 if err := checkBlockTime(block, parent); rootErr(err) != c.err {
65 t.Errorf("case %d got error %s, want %s", i, err, c.err)
70 func TestCheckCoinbaseTx(t *testing.T) {
74 rewards []state.CoinbaseReward
78 desc: "zero coinbase amount",
80 types.NewTx(types.TxData{
81 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
82 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 0, []byte{0x51})},
85 rewards: []state.CoinbaseReward{},
89 desc: "zero coinbase amount and aggregate rewards",
91 types.NewTx(types.TxData{
92 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
93 Outputs: []*types.TxOutput{
94 types.NewIntraChainOutput(*consensus.BTMAssetID, 0, []byte{0x51}),
95 types.NewIntraChainOutput(*consensus.BTMAssetID, 20000, []byte{0x51}),
96 types.NewIntraChainOutput(*consensus.BTMAssetID, 10000, []byte{0x52}),
100 rewards: []state.CoinbaseReward{
101 state.CoinbaseReward{
103 ControlProgram: []byte{0x51},
105 state.CoinbaseReward{
107 ControlProgram: []byte{0x52},
113 desc: "wrong coinbase transaction with block is empty",
115 rewards: []state.CoinbaseReward{},
116 err: ErrWrongCoinbaseTransaction,
119 desc: "wrong coinbase transaction with dismatch number of outputs",
121 types.NewTx(types.TxData{
122 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
123 Outputs: []*types.TxOutput{
124 types.NewIntraChainOutput(*consensus.BTMAssetID, 0, []byte{0x51}),
125 types.NewIntraChainOutput(*consensus.BTMAssetID, 20000, []byte{0x51}),
126 types.NewIntraChainOutput(*consensus.BTMAssetID, 10000, []byte{0x52}),
130 rewards: []state.CoinbaseReward{
131 state.CoinbaseReward{
133 ControlProgram: []byte{0x51},
136 err: ErrWrongCoinbaseTransaction,
139 desc: "wrong coinbase transaction with dismatch output amount",
141 types.NewTx(types.TxData{
142 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
143 Outputs: []*types.TxOutput{
144 types.NewIntraChainOutput(*consensus.BTMAssetID, 0, []byte{0x51}),
145 types.NewIntraChainOutput(*consensus.BTMAssetID, 20000, []byte{0x51}),
146 types.NewIntraChainOutput(*consensus.BTMAssetID, 10000, []byte{0x52}),
150 rewards: []state.CoinbaseReward{
151 state.CoinbaseReward{
153 ControlProgram: []byte{0x51},
155 state.CoinbaseReward{
157 ControlProgram: []byte{0x52},
160 err: ErrWrongCoinbaseTransaction,
164 block := new(types.Block)
165 for i, c := range cases {
166 block.Transactions = c.txs
167 if err := checkCoinbaseTx(types.MapBlock(block), c.rewards); rootErr(err) != c.err {
168 t.Errorf("case %d got error %s, want %T", i, err, c.err)
173 func TestValidateBlockHeader(t *testing.T) {
174 parent := &types.BlockHeader{
177 Timestamp: 1523352600000,
179 parentHash := parent.Hash()
184 parent *types.BlockHeader
188 desc: "dismatch version",
189 block: &bc.Block{BlockHeader: &bc.BlockHeader{
192 parent: &types.BlockHeader{
195 err: errVersionRegression,
198 desc: "misordered block height",
199 block: &bc.Block{BlockHeader: &bc.BlockHeader{
203 parent: &types.BlockHeader{
207 err: errMisorderedBlockHeight,
210 desc: "the prev block hash not equals to the hash of parent",
211 block: &bc.Block{BlockHeader: &bc.BlockHeader{
214 PreviousBlockId: &bc.Hash{V0: 20},
216 parent: &types.BlockHeader{
219 PreviousBlockHash: bc.Hash{V0: 19},
221 err: errMismatchedBlock,
224 desc: "normal block",
227 BlockHeader: &bc.BlockHeader{
230 Timestamp: 1523352601000,
231 PreviousBlockId: &parentHash,
238 desc: "version greater than 1",
241 BlockHeader: &bc.BlockHeader{
245 parent: &types.BlockHeader{
248 err: errVersionRegression,
251 desc: "version equals 0",
254 BlockHeader: &bc.BlockHeader{
258 parent: &types.BlockHeader{
261 err: errVersionRegression,
264 desc: "version equals max uint64",
267 BlockHeader: &bc.BlockHeader{
268 Version: math.MaxUint64,
271 parent: &types.BlockHeader{
274 err: errVersionRegression,
278 for i, c := range cases {
279 if err := ValidateBlockHeader(c.block, c.parent); rootErr(err) != c.err {
280 t.Errorf("case %d (%s) got error %s, want %s", i, c.desc, err, c.err)
285 func TestValidateBlock(t *testing.T) {
286 cp, _ := vmutil.DefaultCoinbaseProgram()
287 parent := &types.BlockHeader{
290 Timestamp: 1523352600000,
291 PreviousBlockHash: bc.Hash{V0: 0},
293 parentHash := parent.Hash()
294 txsRoot := testutil.MustDecodeHash("001e21b9618c503d909c1e0b32bab9ccf80c538b35d49ac7fffcef98eb373b23")
295 txStatusHash := testutil.MustDecodeHash("6978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a")
298 types.MapTx(&types.TxData{
301 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
302 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 0, cp)},
306 for i := 1; i <= 100; i++ {
307 txs = append(txs, types.MapTx(&types.TxData{
309 SerializedSize: 100000,
310 Inputs: []*types.TxInput{types.NewSpendInput([][]byte{}, bc.Hash{V0: uint64(i)}, *consensus.BTMAssetID, 10000000000, 0, cp)},
311 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 9000000000, cp)},
318 parent *types.BlockHeader
319 rewards []state.CoinbaseReward
323 desc: "validate transactions with output amount great than input amount",
326 BlockHeader: &bc.BlockHeader{
329 Timestamp: 1523352601000,
330 PreviousBlockId: &parentHash,
331 TransactionsRoot: &bc.Hash{V0: 16229071813194843118, V1: 7413717724217377663, V2: 10255217553502780716, V3: 17975900656333257644},
332 TransactionStatusHash: &txStatusHash,
334 Transactions: []*bc.Tx{
335 types.MapTx(&types.TxData{
338 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
339 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 0, cp)},
341 types.MapTx(&types.TxData{
344 Inputs: []*types.TxInput{types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp)},
345 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 100000000, cp)},
347 types.MapTx(&types.TxData{
350 Inputs: []*types.TxInput{types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp)},
351 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 200000000, cp)},
356 err: ErrGasCalculate,
359 desc: "validate block with the total transations used gas is over than the limit",
362 BlockHeader: &bc.BlockHeader{
365 Timestamp: 1523352601000,
366 PreviousBlockId: &parentHash,
367 TransactionsRoot: &bc.Hash{V0: 11799591616144015196, V1: 10485585098288308103, V2: 9819002243760462505, V3: 10203115105872271656},
368 TransactionStatusHash: &txStatusHash,
373 err: errOverBlockLimit,
376 desc: "The calculated transaction merkel root hash is not equals to the hash of the block header",
379 BlockHeader: &bc.BlockHeader{
382 Timestamp: 1523352601000,
383 PreviousBlockId: &parentHash,
384 TransactionsRoot: &bc.Hash{V0: 1},
386 Transactions: []*bc.Tx{
387 types.MapTx(&types.TxData{
390 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
391 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 0, cp)},
396 err: errMismatchedMerkleRoot,
399 desc: "The calculated transaction status merkel root hash is not equals to the hash of the block header",
402 BlockHeader: &bc.BlockHeader{
405 Timestamp: 1523352601000,
406 PreviousBlockId: &parentHash,
407 TransactionsRoot: &bc.Hash{V0: 6294987741126419124, V1: 12520373106916389157, V2: 5040806596198303681, V3: 1151748423853876189},
408 TransactionStatusHash: &bc.Hash{V0: 1},
410 Transactions: []*bc.Tx{
411 types.MapTx(&types.TxData{
414 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
415 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 0, cp)},
420 err: errMismatchedMerkleRoot,
423 desc: "the coinbase amount is not equal to the real coinbase outputs",
426 BlockHeader: &bc.BlockHeader{
429 Timestamp: 1523352601000,
430 PreviousBlockId: &parentHash,
431 TransactionsRoot: &txsRoot,
432 TransactionStatusHash: &txStatusHash,
434 Transactions: []*bc.Tx{
435 types.MapTx(&types.TxData{
438 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
439 Outputs: []*types.TxOutput{
440 types.NewIntraChainOutput(*consensus.BTMAssetID, 0, cp),
441 types.NewIntraChainOutput(*consensus.BTMAssetID, 20000, []byte{0x51}),
447 rewards: []state.CoinbaseReward{
448 state.CoinbaseReward{
450 ControlProgram: []byte{0x51},
452 state.CoinbaseReward{
454 ControlProgram: []byte{0x52},
457 err: ErrWrongCoinbaseTransaction,
460 desc: "the coinbase amount is equal to the real coinbase amount",
463 BlockHeader: &bc.BlockHeader{
466 Timestamp: 1523352601000,
467 PreviousBlockId: &parentHash,
468 TransactionsRoot: &bc.Hash{V0: 16229071813194843118, V1: 7413717724217377663, V2: 10255217553502780716, V3: 17975900656333257644},
469 TransactionStatusHash: &txStatusHash,
471 Transactions: []*bc.Tx{
472 types.MapTx(&types.TxData{
475 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
476 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 0, cp)},
478 types.MapTx(&types.TxData{
481 Inputs: []*types.TxInput{types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp)},
482 Outputs: []*types.TxOutput{
483 types.NewIntraChainOutput(*consensus.BTMAssetID, 0, cp),
484 types.NewIntraChainOutput(*consensus.BTMAssetID, 100000000, cp),
494 for i, c := range cases {
495 err := ValidateBlock(c.block, c.parent, c.rewards)
496 if rootErr(err) != c.err {
497 t.Errorf("case #%d (%s) got error %s, want %s", i, c.desc, err, c.err)
502 // TestSetTransactionStatus verify the transaction status is set correctly (blocktest#1010)
503 func TestSetTransactionStatus(t *testing.T) {
504 cp, _ := vmutil.DefaultCoinbaseProgram()
505 parent := &types.BlockHeader{
508 Timestamp: 1523352600000,
509 PreviousBlockHash: bc.Hash{V0: 0},
511 parentHash := parent.Hash()
515 BlockHeader: &bc.BlockHeader{
518 Timestamp: 1523352601000,
519 PreviousBlockId: &parentHash,
520 TransactionsRoot: &bc.Hash{V0: 8176741810667217458, V1: 14830712230021600370, V2: 8921661778795432162, V3: 3391855546006364086},
521 TransactionStatusHash: &bc.Hash{V0: 8682965660674182538, V1: 8424137560837623409, V2: 6979974817894224946, V3: 4673809519342015041},
523 Transactions: []*bc.Tx{
524 types.MapTx(&types.TxData{
527 Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)},
528 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 0, cp)},
530 types.MapTx(&types.TxData{
533 Inputs: []*types.TxInput{
534 types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
535 types.NewSpendInput([][]byte{}, *newHash(8), bc.AssetID{V0: 1}, 1000, 0, []byte{byte(vm.OP_FALSE)}),
537 Outputs: []*types.TxOutput{
538 types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
539 types.NewIntraChainOutput(bc.AssetID{V0: 1}, 1000, cp),
542 types.MapTx(&types.TxData{
545 Inputs: []*types.TxInput{
546 types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
548 Outputs: []*types.TxOutput{
549 types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
555 if err := ValidateBlock(block, parent, []state.CoinbaseReward{}); err != nil {
559 expectTxStatuses := []bool{false, true, false}
560 txStatuses := block.GetTransactionStatus().VerifyStatus
561 if len(expectTxStatuses) != len(txStatuses) {
562 t.Error("the size of expect tx status is not equals to size of got tx status")
565 for i, status := range txStatuses {
566 if expectTxStatuses[i] != status.StatusFail {
567 t.Errorf("got tx status: %v, expect tx status: %v\n", status.StatusFail, expectTxStatuses[i])