OSDN Git Service

tmp save for branch merge
[bytom/bytom.git] / protocol / validation / block_test.go
1 package validation
2
3 import (
4         "testing"
5         "time"
6
7         "github.com/bytom/protocol/bc"
8         "github.com/bytom/protocol/bc/legacy"
9         "github.com/bytom/protocol/vm"
10         "github.com/bytom/protocol/vm/vmutil"
11 )
12
13 func TestValidateBlock1(t *testing.T) {
14         b1 := newInitialBlock(t)
15         err := ValidateBlock(b1, nil, b1.ID, dummyValidateTx)
16         if err != nil {
17                 t.Errorf("ValidateBlock(%v, nil) = %v, want nil", b1, err)
18         }
19 }
20
21 func TestValidateBlock1Err(t *testing.T) {
22         b1 := newInitialBlock(t)
23         transactionsRoot := bc.NewHash([32]byte{1})
24         b1.TransactionsRoot = &transactionsRoot // make b1 be invalid
25         err := ValidateBlock(b1, nil, b1.ID, dummyValidateTx)
26         if err == nil {
27                 t.Errorf("ValidateBlock(%v, nil) = nil, want error", b1)
28         }
29 }
30
31 func TestValidateBlock2(t *testing.T) {
32         b1 := newInitialBlock(t)
33         b2 := generate(t, b1)
34         err := ValidateBlock(b2, b1, b2.ID, dummyValidateTx)
35         if err != nil {
36                 t.Errorf("ValidateBlock(%v, %v) = %v, want nil", b2, b1, err)
37         }
38 }
39
40 func TestValidateBlock2Err(t *testing.T) {
41         b1 := newInitialBlock(t)
42         b2 := generate(t, b1)
43         transactionsRoot := bc.NewHash([32]byte{1})
44         b2.TransactionsRoot = &transactionsRoot // make b2 be invalid
45         err := ValidateBlock(b2, b1, b2.ID, dummyValidateTx)
46         if err == nil {
47                 t.Errorf("ValidateBlock(%v, %v) = nil, want error", b2, b1)
48         }
49 }
50
51 func TestValidateBlockSig2(t *testing.T) {
52         b1 := newInitialBlock(t)
53         b2 := generate(t, b1)
54         err := ValidateBlockSig(b2, b1.NextConsensusProgram)
55         if err != nil {
56                 t.Errorf("ValidateBlockSig(%v, %v) = %v, want nil", b2, b1, err)
57         }
58 }
59
60 func TestValidateBlockSig2Err(t *testing.T) {
61         b1 := newInitialBlock(t)
62         b2 := generate(t, b1)
63         prog := []byte{byte(vm.OP_FALSE)} // make b2 be invalid
64         err := ValidateBlockSig(b2, prog)
65         if err == nil {
66                 t.Errorf("ValidateBlockSig(%v, %v) = nil, want error", b2, b1)
67         }
68 }
69
70 func dummyValidateTx(*bc.Tx) error {
71         return nil
72 }
73
74 func newInitialBlock(tb testing.TB) *bc.Block {
75         script, err := vmutil.BlockMultiSigProgram(nil, 0)
76         if err != nil {
77                 tb.Fatal(err)
78         }
79
80         root, err := bc.MerkleRoot(nil) // calculate the zero value of the tx merkle root
81         if err != nil {
82                 tb.Fatal(err)
83         }
84
85         b := &legacy.Block{
86                 BlockHeader: legacy.BlockHeader{
87                         Version:     1,
88                         Height:      1,
89                         TimestampMS: bc.Millis(time.Now()),
90                         BlockCommitment: legacy.BlockCommitment{
91                                 TransactionsMerkleRoot: root,
92                                 ConsensusProgram:       script,
93                         },
94                 },
95         }
96         return legacy.MapBlock(b)
97 }
98
99 func generate(tb testing.TB, prev *bc.Block) *bc.Block {
100         b := &legacy.Block{
101                 BlockHeader: legacy.BlockHeader{
102                         Version:           1,
103                         Height:            prev.Height + 1,
104                         PreviousBlockHash: prev.ID,
105                         TimestampMS:       prev.TimestampMs + 1,
106                         BlockCommitment: legacy.BlockCommitment{
107                                 ConsensusProgram: prev.NextConsensusProgram,
108                         },
109                 },
110         }
111
112         var err error
113         b.TransactionsMerkleRoot, err = bc.MerkleRoot(nil)
114         if err != nil {
115                 tb.Fatal(err)
116         }
117
118         return legacy.MapBlock(b)
119 }