OSDN Git Service

Merge branch 'master' into key_alias
[bytom/vapor.git] / test / subprotocol_test.go
1 package test
2
3 import (
4         "os"
5         "testing"
6
7         "github.com/bytom/vapor/application/mov"
8         movDatabase "github.com/bytom/vapor/application/mov/database"
9         "github.com/bytom/vapor/database"
10         dbm "github.com/bytom/vapor/database/leveldb"
11         "github.com/bytom/vapor/protocol"
12         "github.com/bytom/vapor/protocol/bc"
13         "github.com/bytom/vapor/protocol/bc/types"
14         "github.com/bytom/vapor/protocol/state"
15         "github.com/bytom/vapor/testutil"
16 )
17
18 type chainStatus struct {
19         blockHeight uint64
20         blockHash   bc.Hash
21 }
22
23 type chainBlock struct {
24         block       *types.Block
25         inMainChain bool
26 }
27
28 var blocks = map[uint64][]*types.Block{
29         0: {
30                 {
31                         BlockHeader: types.BlockHeader{
32                                 Height:            0,
33                                 Timestamp:         1585814309,
34                                 PreviousBlockHash: bc.Hash{},
35                         },
36                 },
37         },
38         1: {
39                 // prev block is [0][0]
40                 {
41                         BlockHeader: types.BlockHeader{
42                                 Height:            1,
43                                 Timestamp:         1585814310,
44                                 PreviousBlockHash: testutil.MustDecodeHash("2e5406c82fe34f6ee44fe694b05ffc8fb5918a026415b086df03fb03760b42a9"),
45                         },
46                 },
47                 // prev block is [0][0]
48                 {
49                         BlockHeader: types.BlockHeader{
50                                 Height:            1,
51                                 Timestamp:         1585814311,
52                                 PreviousBlockHash: testutil.MustDecodeHash("2e5406c82fe34f6ee44fe694b05ffc8fb5918a026415b086df03fb03760b42a9"),
53                         },
54                 },
55         },
56         2: {
57                 // prev block is [1][0]
58                 {
59                         BlockHeader: types.BlockHeader{
60                                 Height:            2,
61                                 Timestamp:         1585814320,
62                                 PreviousBlockHash: testutil.MustDecodeHash("5bc198f4c0198e7e8b52173a82836cfd3f124d88bf052f53390948d845bf6fe0"),
63                         },
64                 },
65         },
66 }
67
68 func TestSyncProtocolStatus(t *testing.T) {
69         cases := []struct {
70                 desc            string
71                 savedBlocks     []*chainBlock
72                 startHeight     uint64
73                 startHash       *bc.Hash
74                 wantChainStatus *chainStatus
75         }{
76                 {
77                         desc: "start height from 0, mov is not init",
78                         savedBlocks: []*chainBlock{
79                                 {
80                                         block:       blocks[0][0],
81                                         inMainChain: true,
82                                 },
83                                 {
84                                         block:       blocks[1][0],
85                                         inMainChain: true,
86                                 },
87                                 {
88                                         block:       blocks[2][0],
89                                         inMainChain: true,
90                                 },
91                         },
92                         startHeight: 0,
93                         wantChainStatus: &chainStatus{
94                                 blockHeight: 2,
95                                 blockHash:   blocks[2][0].Hash(),
96                         },
97                 },
98                 {
99                         desc: "start height from 1, mov is not init",
100                         savedBlocks: []*chainBlock{
101                                 {
102                                         block:       blocks[0][0],
103                                         inMainChain: true,
104                                 },
105                                 {
106                                         block:       blocks[1][0],
107                                         inMainChain: true,
108                                 },
109                                 {
110                                         block:       blocks[2][0],
111                                         inMainChain: true,
112                                 },
113                         },
114                         startHeight: 1,
115                         wantChainStatus: &chainStatus{
116                                 blockHeight: 2,
117                                 blockHash:   blocks[2][0].Hash(),
118                         },
119                 },
120                 {
121                         desc: "start height from 1, state of mov is not sync completed",
122                         savedBlocks: []*chainBlock{
123                                 {
124                                         block:       blocks[0][0],
125                                         inMainChain: true,
126                                 },
127                                 {
128                                         block:       blocks[1][0],
129                                         inMainChain: true,
130                                 },
131                                 {
132                                         block:       blocks[2][0],
133                                         inMainChain: true,
134                                 },
135                         },
136                         startHeight: 1,
137                         startHash:   hashPtr(blocks[1][0].Hash()),
138                         wantChainStatus: &chainStatus{
139                                 blockHeight: 2,
140                                 blockHash:   blocks[2][0].Hash(),
141                         },
142                 },
143                 {
144                         desc: "chain status of mov is forked",
145                         savedBlocks: []*chainBlock{
146                                 {
147                                         block:       blocks[0][0],
148                                         inMainChain: true,
149                                 },
150                                 {
151                                         block:       blocks[1][0],
152                                         inMainChain: true,
153                                 },
154                                 {
155                                         block:       blocks[1][1],
156                                         inMainChain: false,
157                                 },
158                                 {
159                                         block:       blocks[2][0],
160                                         inMainChain: true,
161                                 },
162                         },
163                         startHeight: 1,
164                         startHash:   hashPtr(blocks[1][1].Hash()),
165                         wantChainStatus: &chainStatus{
166                                 blockHeight: 2,
167                                 blockHash:   blocks[2][0].Hash(),
168                         },
169                 },
170         }
171
172         defer os.RemoveAll("temp")
173
174         for i, c := range cases {
175                 chainDB := dbm.NewDB("core", "leveldb", "temp")
176                 store := database.NewStore(chainDB)
177                 if err := initStore(store, c.savedBlocks); err != nil {
178                         t.Fatal(err)
179                 }
180
181                 movDB := dbm.NewDB("mov", "leveldb", "temp")
182                 movCore := mov.NewCoreWithDB(movDatabase.NewLevelDBMovStore(movDB), c.startHeight)
183                 if c.startHash != nil {
184                         if err := movCore.InitChainStatus(c.startHash); err != nil {
185                                 t.Fatal(err)
186                         }
187                 }
188
189                 _, err := protocol.NewChain(store, nil, []protocol.SubProtocol{movCore}, nil)
190                 if err != nil {
191                         t.Fatal(err)
192                 }
193
194                 gotHeight, gotHash, err := movCore.ChainStatus()
195                 if err != nil {
196                         t.Fatal(err)
197                 }
198
199                 if gotHeight != c.wantChainStatus.blockHeight || *gotHash != c.wantChainStatus.blockHash {
200                         t.Logf("#%d(%s): got chain status of sub protocol is not equals want chain status", i, c.desc)
201                 }
202
203                 movDB.Close()
204                 chainDB.Close()
205                 os.RemoveAll("temp")
206         }
207 }
208
209 func initStore(store *database.Store, savedBlocks []*chainBlock) error {
210         var mainBlockHeaders []*types.BlockHeader
211         for _, block := range savedBlocks {
212                 if err := store.SaveBlock(block.block, bc.NewTransactionStatus()); err != nil {
213                         return err
214                 }
215
216                 if block.inMainChain {
217                         mainBlockHeaders = append(mainBlockHeaders, &block.block.BlockHeader)
218                 }
219
220                 last := len(mainBlockHeaders) - 1
221                 if err := store.SaveChainStatus(mainBlockHeaders[last], mainBlockHeaders[last], mainBlockHeaders, state.NewUtxoViewpoint(), nil); err != nil {
222                         return err
223                 }
224         }
225         return nil
226 }
227
228 func hashPtr(hash bc.Hash) *bc.Hash {
229         return &hash
230 }
231
232 func TestBlockHash(t *testing.T) {
233         blockHash := blocks[1][0].Hash()
234         t.Log(blockHash.String())
235 }