OSDN Git Service

Bump golang.org/x/text from 0.3.6 to 0.3.8 in /lib/golang.org/x/net
[bytom/bytom.git] / test / bench_blockchain_test.go
1 package test
2
3 //
4 //import (
5 //      "fmt"
6 //      "io/ioutil"
7 //      "os"
8 //      "testing"
9 //      "time"
10 //
11 //      "github.com/bytom/bytom/account"
12 //      "github.com/bytom/bytom/blockchain/pseudohsm"
13 //      "github.com/bytom/bytom/blockchain/signers"
14 //      "github.com/bytom/bytom/blockchain/txbuilder"
15 //      "github.com/bytom/bytom/consensus"
16 //      "github.com/bytom/bytom/crypto/ed25519/chainkd"
17 //      "github.com/bytom/bytom/database"
18 //      dbm "github.com/bytom/bytom/database/leveldb"
19 //      "github.com/bytom/bytom/database/storage"
20 //      "github.com/bytom/bytom/event"
21 //      "github.com/bytom/bytom/mining"
22 //      "github.com/bytom/bytom/protocol"
23 //      "github.com/bytom/bytom/protocol/bc"
24 //      "github.com/bytom/bytom/protocol/bc/types"
25 //      "github.com/bytom/bytom/protocol/state"
26 //)
27 //
28 //func BenchmarkChain_CoinBaseTx_NoAsset(b *testing.B) {
29 //      benchInsertChain(b, 0, 0, "")
30 //}
31 //
32 //func BenchmarkChain_BtmTx_NoAsset_BASE(b *testing.B) {
33 //      benchInsertChain(b, 1, 0, "")
34 //}
35 //
36 //func BenchmarkChain_5000BtmTx_NoAsset_BASE(b *testing.B) {
37 //      benchInsertChain(b, 5000, 0, "")
38 //}
39 //
40 //func BenchmarkChain_5000BtmTx_1Asset_BASE(b *testing.B) {
41 //      benchInsertChain(b, 5000, 1, "")
42 //}
43 //
44 //// standard Transaction
45 //func BenchmarkChain_BtmTx_NoAsset_P2PKH(b *testing.B) {
46 //      benchInsertChain(b, 1000, 0, "P2PKH")
47 //}
48 //
49 //func BenchmarkChain_BtmTx_1Asset_P2PKH(b *testing.B) {
50 //      benchInsertChain(b, 1000, 1, "P2PKH")
51 //}
52 //
53 //func BenchmarkChain_BtmTx_NoAsset_P2SH(b *testing.B) {
54 //      benchInsertChain(b, 100, 0, "P2SH")
55 //}
56 //
57 //func BenchmarkChain_BtmTx_1Asset_P2SH(b *testing.B) {
58 //      benchInsertChain(b, 100, 1, "P2SH")
59 //}
60 //
61 //func BenchmarkChain_BtmTx_NoAsset_MultiSign(b *testing.B) {
62 //      benchInsertChain(b, 100, 0, "MultiSign")
63 //}
64 //
65 //func BenchmarkChain_BtmTx_1Asset_MultiSign(b *testing.B) {
66 //      benchInsertChain(b, 100, 1, "MultiSign")
67 //}
68 //
69 //func benchInsertChain(b *testing.B, blockTxNumber int, otherAssetNum int, txType string) {
70 //      b.StopTimer()
71 //      testNumber := b.N
72 //      totalTxNumber := testNumber * blockTxNumber
73 //
74 //      dirPath, err := ioutil.TempDir(".", "testDB")
75 //      if err != nil {
76 //              b.Fatal("create dirPath err:", err)
77 //      }
78 //      defer os.RemoveAll(dirPath)
79 //
80 //      testDB := dbm.NewDB("testdb", "leveldb", dirPath)
81 //      defer testDB.Close()
82 //
83 //      // Generate a chain test data.
84 //      chain, txs, txPool, err := GenerateChainData(dirPath, testDB, totalTxNumber, otherAssetNum, txType)
85 //      if err != nil {
86 //              b.Fatal("GenerateChainData err:", err)
87 //      }
88 //
89 //      b.ReportAllocs()
90 //      b.StartTimer()
91 //
92 //      for i := 0; i < b.N; i++ {
93 //              testTxs := txs[blockTxNumber*i : blockTxNumber*(i+1)]
94 //              if err := InsertChain(chain, txPool, testTxs); err != nil {
95 //                      b.Fatal("Failed to insert block into chain:", err)
96 //              }
97 //      }
98 //}
99 //
100 //func GenerateChainData(dirPath string, testDB dbm.DB, txNumber, otherAssetNum int, txType string) (*protocol.Chain, []*types.Tx, *protocol.TxPool, error) {
101 //      var err error
102 //
103 //      // generate transactions
104 //      txs := []*types.Tx{}
105 //      switch txType {
106 //      case "P2PKH":
107 //              txs, err = MockTxsP2PKH(dirPath, testDB, txNumber, otherAssetNum)
108 //              if err != nil {
109 //                      return nil, nil, nil, err
110 //              }
111 //      case "P2SH":
112 //              txs, err = MockTxsP2SH(dirPath, testDB, txNumber, otherAssetNum)
113 //              if err != nil {
114 //                      return nil, nil, nil, err
115 //              }
116 //      case "MultiSign":
117 //              txs, err = MockTxsMultiSign(dirPath, testDB, txNumber, otherAssetNum)
118 //              if err != nil {
119 //                      return nil, nil, nil, err
120 //              }
121 //      default:
122 //              txs, err = CreateTxbyNum(txNumber, otherAssetNum)
123 //              if err != nil {
124 //                      return nil, nil, nil, err
125 //              }
126 //      }
127 //
128 //      // init UtxoViewpoint
129 //      utxoView := state.NewUtxoViewpoint()
130 //      utxoEntry := storage.NewUtxoEntry(false, 1, false)
131 //      for _, tx := range txs {
132 //              for _, id := range tx.SpentOutputIDs {
133 //                      utxoView.Entries[id] = utxoEntry
134 //              }
135 //      }
136 //
137 //      if err := SetUtxoView(testDB, utxoView); err != nil {
138 //              return nil, nil, nil, err
139 //      }
140 //
141 //      store := database.NewStore(testDB)
142 //      dispatcher := event.NewDispatcher()
143 //      txPool := protocol.NewTxPool(store, dispatcher)
144 //      chain, err := protocol.NewChain(store, txPool)
145 //      if err != nil {
146 //              return nil, nil, nil, err
147 //      }
148 //
149 //      go processNewTxch(txPool)
150 //
151 //      return chain, txs, txPool, nil
152 //}
153 //
154 //func InsertChain(chain *protocol.Chain, txPool *protocol.TxPool, txs []*types.Tx) error {
155 //      for _, tx := range txs {
156 //              if err := txbuilder.FinalizeTx(nil, chain, tx); err != nil {
157 //                      return err
158 //              }
159 //      }
160 //      // imitate block broadcast deplay
161 //      time.Sleep(time.Second)
162 //      block, err := mining.NewBlockTemplate(chain, txPool, nil)
163 //      if err != nil {
164 //              return err
165 //      }
166 //      blockSize, err := block.MarshalText()
167 //      if err != nil {
168 //              return err
169 //      }
170 //      fmt.Println("blocksize:", uint64(len(blockSize)))
171 //      fmt.Println("block tx count:", uint64(len(block.Transactions)))
172 //      fmt.Println("coinbase txsize:", uint64(block.Transactions[0].SerializedSize))
173 //      if len(block.Transactions) > 1 {
174 //              fmt.Println("txsize:", uint64(block.Transactions[1].SerializedSize))
175 //      }
176 //      if _, err := chain.ProcessBlock(block); err != nil {
177 //              return err
178 //      }
179 //
180 //      return nil
181 //}
182 //
183 //func processNewTxch(txPool *protocol.TxPool) {
184 //}
185 //
186 //func MockSimpleUtxo(index uint64, assetID *bc.AssetID, amount uint64, ctrlProg *account.CtrlProgram) *account.UTXO {
187 //      if ctrlProg == nil {
188 //              ctrlProg = &account.CtrlProgram{
189 //                      AccountID:      "",
190 //                      Address:        "",
191 //                      KeyIndex:       uint64(0),
192 //                      ControlProgram: []byte{81},
193 //                      Change:         false,
194 //              }
195 //      }
196 //
197 //      utxo := &account.UTXO{
198 //              OutputID:            bc.Hash{V0: 1},
199 //              SourceID:            bc.Hash{V0: 1},
200 //              AssetID:             *assetID,
201 //              Amount:              amount,
202 //              SourcePos:           index,
203 //              ControlProgram:      ctrlProg.ControlProgram,
204 //              ControlProgramIndex: ctrlProg.KeyIndex,
205 //              AccountID:           ctrlProg.AccountID,
206 //              Address:             ctrlProg.Address,
207 //              ValidHeight:         0,
208 //      }
209 //
210 //      return utxo
211 //}
212 //
213 //func GenerateBaseUtxos(num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
214 //      utxos := []*account.UTXO{}
215 //      for i := 0; i < num; i++ {
216 //              utxo := MockSimpleUtxo(uint64(i), consensus.BTMAssetID, amount, ctrlProg)
217 //              utxos = append(utxos, utxo)
218 //      }
219 //
220 //      return utxos
221 //}
222 //
223 //func GenerateOtherUtxos(typeCount, num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
224 //      utxos := []*account.UTXO{}
225 //
226 //      assetID := &bc.AssetID{
227 //              V0: uint64(typeCount),
228 //              V1: uint64(1),
229 //              V2: uint64(0),
230 //              V3: uint64(1),
231 //      }
232 //
233 //      for i := 0; i < num; i++ {
234 //              utxo := MockSimpleUtxo(uint64(typeCount*num+i), assetID, amount, ctrlProg)
235 //              utxos = append(utxos, utxo)
236 //      }
237 //
238 //      return utxos
239 //}
240 //
241 //func AddTxInputFromUtxo(utxo *account.UTXO, singer *signers.Signer) (*types.TxInput, *txbuilder.SigningInstruction, error) {
242 //      txInput, signInst, err := account.UtxoToInputs(singer, utxo)
243 //      if err != nil {
244 //              return nil, nil, err
245 //      }
246 //
247 //      return txInput, signInst, nil
248 //}
249 //
250 //func AddTxOutput(assetID bc.AssetID, amount uint64, controlProgram []byte) *types.TxOutput {
251 //      out := types.NewOriginalTxOutput(assetID, amount, controlProgram)
252 //      return out
253 //}
254 //
255 //func CreateTxBuilder(baseUtxo *account.UTXO, btmServiceFlag bool, signer *signers.Signer) (*txbuilder.TemplateBuilder, error) {
256 //      tplBuilder := txbuilder.NewBuilder(time.Now())
257 //
258 //      // add input
259 //      txInput, signInst, err := AddTxInputFromUtxo(baseUtxo, signer)
260 //      if err != nil {
261 //              return nil, err
262 //      }
263 //      tplBuilder.AddInput(txInput, signInst)
264 //
265 //      // if the btm is the service charge, didn't need to add the output
266 //      if btmServiceFlag {
267 //              txOutput := AddTxOutput(baseUtxo.AssetID, 100, baseUtxo.ControlProgram)
268 //              tplBuilder.AddOutput(txOutput)
269 //      }
270 //
271 //      return tplBuilder, nil
272 //}
273 //
274 //func AddTxBuilder(tplBuilder *txbuilder.TemplateBuilder, utxo *account.UTXO, signer *signers.Signer) error {
275 //      txInput, signInst, err := AddTxInputFromUtxo(utxo, signer)
276 //      if err != nil {
277 //              return err
278 //      }
279 //      tplBuilder.AddInput(txInput, signInst)
280 //
281 //      txOutput := AddTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
282 //      tplBuilder.AddOutput(txOutput)
283 //
284 //      return nil
285 //}
286 //
287 //func BuildTx(baseUtxo *account.UTXO, otherUtxos []*account.UTXO, signer *signers.Signer) (*txbuilder.Template, error) {
288 //      btmServiceFlag := false
289 //      if otherUtxos == nil || len(otherUtxos) == 0 {
290 //              btmServiceFlag = true
291 //      }
292 //
293 //      tplBuilder, err := CreateTxBuilder(baseUtxo, btmServiceFlag, signer)
294 //      if err != nil {
295 //              return nil, err
296 //      }
297 //
298 //      for _, u := range otherUtxos {
299 //              if err := AddTxBuilder(tplBuilder, u, signer); err != nil {
300 //                      return nil, err
301 //              }
302 //      }
303 //
304 //      tpl, _, err := tplBuilder.Build()
305 //      if err != nil {
306 //              return nil, err
307 //      }
308 //
309 //      return tpl, nil
310 //}
311 //
312 //func GenetrateTxbyUtxo(baseUtxo []*account.UTXO, otherUtxo [][]*account.UTXO) ([]*types.Tx, error) {
313 //      tmpUtxo := []*account.UTXO{}
314 //      txs := []*types.Tx{}
315 //      otherUtxoFlag := true
316 //
317 //      if len(otherUtxo) == 0 || len(otherUtxo) != len(baseUtxo) {
318 //              otherUtxoFlag = false
319 //      }
320 //
321 //      for i := 0; i < len(baseUtxo); i++ {
322 //              if otherUtxoFlag {
323 //                      tmpUtxo = otherUtxo[i]
324 //              } else {
325 //                      tmpUtxo = nil
326 //              }
327 //
328 //              tpl, err := BuildTx(baseUtxo[i], tmpUtxo, nil)
329 //              if err != nil {
330 //                      return nil, err
331 //              }
332 //
333 //              txs = append(txs, tpl.Transaction)
334 //      }
335 //
336 //      return txs, nil
337 //}
338 //
339 //func CreateTxbyNum(txNumber, otherAssetNum int) ([]*types.Tx, error) {
340 //      baseUtxos := GenerateBaseUtxos(txNumber, 1000000000, nil)
341 //      otherUtxos := make([][]*account.UTXO, 0, txNumber)
342 //      if otherAssetNum != 0 {
343 //              for i := 0; i < txNumber; i++ {
344 //                      utxos := GenerateOtherUtxos(i, otherAssetNum, 6000, nil)
345 //                      otherUtxos = append(otherUtxos, utxos)
346 //              }
347 //      }
348 //
349 //      txs, err := GenetrateTxbyUtxo(baseUtxos, otherUtxos)
350 //      if err != nil {
351 //              return nil, err
352 //      }
353 //
354 //      return txs, nil
355 //}
356 //
357 //func SetUtxoView(db dbm.DB, view *state.UtxoViewpoint) error {
358 //      batch := db.NewBatch()
359 //      if err := database.SaveUtxoView(batch, view); err != nil {
360 //              return err
361 //      }
362 //      batch.Write()
363 //      return nil
364 //}
365 //
366 ////-------------------------Mock actual transaction----------------------------------
367 //func MockTxsP2PKH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
368 //      accountManager := account.NewManager(testDB, nil)
369 //      hsm, err := pseudohsm.New(keyDirPath)
370 //      if err != nil {
371 //              return nil, err
372 //      }
373 //
374 //      xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
375 //      if err != nil {
376 //              return nil, err
377 //      }
378 //
379 //      txs := []*types.Tx{}
380 //      for i := 0; i < txNumber; i++ {
381 //              testAccountAlias := fmt.Sprintf("testAccount%d", i)
382 //              testAccount, err := accountManager.Create([]chainkd.XPub{xpub.XPub}, 1, testAccountAlias, signers.BIP0044)
383 //              if err != nil {
384 //                      return nil, err
385 //              }
386 //
387 //              controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
388 //              if err != nil {
389 //                      return nil, err
390 //              }
391 //
392 //              utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
393 //              otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
394 //              tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
395 //              if err != nil {
396 //                      return nil, err
397 //              }
398 //
399 //              if _, err := MockSign(tpl, hsm, "password"); err != nil {
400 //                      return nil, err
401 //              }
402 //
403 //              txs = append(txs, tpl.Transaction)
404 //      }
405 //
406 //      return txs, nil
407 //}
408 //
409 //func MockTxsP2SH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
410 //      accountManager := account.NewManager(testDB, nil)
411 //      hsm, err := pseudohsm.New(keyDirPath)
412 //      if err != nil {
413 //              return nil, err
414 //      }
415 //
416 //      xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
417 //      if err != nil {
418 //              return nil, err
419 //      }
420 //
421 //      xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
422 //      if err != nil {
423 //              return nil, err
424 //      }
425 //
426 //      txs := []*types.Tx{}
427 //      for i := 0; i < txNumber; i++ {
428 //              testAccountAlias := fmt.Sprintf("testAccount%d", i)
429 //              testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
430 //              if err != nil {
431 //                      return nil, err
432 //              }
433 //
434 //              controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
435 //              if err != nil {
436 //                      return nil, err
437 //              }
438 //
439 //              utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
440 //              otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
441 //              tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
442 //              if err != nil {
443 //                      return nil, err
444 //              }
445 //
446 //              if _, err := MockSign(tpl, hsm, "password"); err != nil {
447 //                      return nil, err
448 //              }
449 //
450 //              txs = append(txs, tpl.Transaction)
451 //      }
452 //
453 //      return txs, nil
454 //}
455 //
456 //func MockTxsMultiSign(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
457 //      accountManager := account.NewManager(testDB, nil)
458 //      hsm, err := pseudohsm.New(keyDirPath)
459 //      if err != nil {
460 //              return nil, err
461 //      }
462 //
463 //      xpub1, _, err := hsm.XCreate("TestMultilNodeSign1", "password1", "en")
464 //      if err != nil {
465 //              return nil, err
466 //      }
467 //
468 //      xpub2, _, err := hsm.XCreate("TestMultilNodeSign2", "password2", "en")
469 //      if err != nil {
470 //              return nil, err
471 //      }
472 //      txs := []*types.Tx{}
473 //      for i := 0; i < txNumber; i++ {
474 //              testAccountAlias := fmt.Sprintf("testAccount%d", i)
475 //              testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
476 //              if err != nil {
477 //                      return nil, err
478 //              }
479 //
480 //              controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
481 //              if err != nil {
482 //                      return nil, err
483 //              }
484 //
485 //              utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
486 //              otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
487 //              tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
488 //              if err != nil {
489 //                      return nil, err
490 //              }
491 //
492 //              if _, err := MockSign(tpl, hsm, "password1"); err != nil {
493 //                      return nil, err
494 //              }
495 //
496 //              if _, err := MockSign(tpl, hsm, "password2"); err != nil {
497 //                      return nil, err
498 //              }
499 //
500 //              txs = append(txs, tpl.Transaction)
501 //      }
502 //
503 //      return txs, nil
504 //}