10 "github.com/vapor/account"
11 "github.com/vapor/blockchain/pseudohsm"
12 "github.com/vapor/blockchain/signers"
13 "github.com/vapor/blockchain/txbuilder"
14 "github.com/vapor/consensus"
15 "github.com/vapor/consensus/difficulty"
16 "github.com/vapor/crypto/ed25519/chainkd"
17 "github.com/vapor/database"
18 dbm "github.com/vapor/database/leveldb"
19 "github.com/vapor/database/storage"
20 "github.com/vapor/event"
21 "github.com/vapor/mining"
22 "github.com/vapor/protocol"
23 "github.com/vapor/protocol/bc"
24 "github.com/vapor/protocol/bc/types"
25 "github.com/vapor/protocol/state"
28 func BenchmarkChain_CoinBaseTx_NoAsset(b *testing.B) {
29 benchInsertChain(b, 0, 0, "")
32 func BenchmarkChain_BtmTx_NoAsset_BASE(b *testing.B) {
33 benchInsertChain(b, 1, 0, "")
36 func BenchmarkChain_5000BtmTx_NoAsset_BASE(b *testing.B) {
37 benchInsertChain(b, 5000, 0, "")
40 func BenchmarkChain_5000BtmTx_1Asset_BASE(b *testing.B) {
41 benchInsertChain(b, 5000, 1, "")
44 // standard Transaction
45 func BenchmarkChain_BtmTx_NoAsset_P2PKH(b *testing.B) {
46 benchInsertChain(b, 1000, 0, "P2PKH")
49 func BenchmarkChain_BtmTx_1Asset_P2PKH(b *testing.B) {
50 benchInsertChain(b, 1000, 1, "P2PKH")
53 func BenchmarkChain_BtmTx_NoAsset_P2SH(b *testing.B) {
54 benchInsertChain(b, 100, 0, "P2SH")
57 func BenchmarkChain_BtmTx_1Asset_P2SH(b *testing.B) {
58 benchInsertChain(b, 100, 1, "P2SH")
61 func BenchmarkChain_BtmTx_NoAsset_MultiSign(b *testing.B) {
62 benchInsertChain(b, 100, 0, "MultiSign")
65 func BenchmarkChain_BtmTx_1Asset_MultiSign(b *testing.B) {
66 benchInsertChain(b, 100, 1, "MultiSign")
69 func benchInsertChain(b *testing.B, blockTxNumber int, otherAssetNum int, txType string) {
72 totalTxNumber := testNumber * blockTxNumber
74 dirPath, err := ioutil.TempDir(".", "testDB")
76 b.Fatal("create dirPath err:", err)
78 defer os.RemoveAll(dirPath)
80 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
83 // Generate a chain test data.
84 chain, txs, txPool, err := GenerateChainData(dirPath, testDB, totalTxNumber, otherAssetNum, txType)
86 b.Fatal("GenerateChainData err:", err)
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)
100 func GenerateChainData(dirPath string, testDB dbm.DB, txNumber, otherAssetNum int, txType string) (*protocol.Chain, []*types.Tx, *protocol.TxPool, error) {
103 // generate transactions
107 txs, err = MockTxsP2PKH(dirPath, testDB, txNumber, otherAssetNum)
109 return nil, nil, nil, err
112 txs, err = MockTxsP2SH(dirPath, testDB, txNumber, otherAssetNum)
114 return nil, nil, nil, err
117 txs, err = MockTxsMultiSign(dirPath, testDB, txNumber, otherAssetNum)
119 return nil, nil, nil, err
122 txs, err = CreateTxbyNum(txNumber, otherAssetNum)
124 return nil, nil, nil, err
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
137 if err := SetUtxoView(testDB, utxoView); err != nil {
138 return nil, nil, nil, err
141 store := database.NewStore(testDB)
142 dispatcher := event.NewDispatcher()
143 txPool := protocol.NewTxPool(store, dispatcher)
144 chain, err := protocol.NewChain(store, txPool)
146 return nil, nil, nil, err
149 go processNewTxch(txPool)
151 return chain, txs, txPool, nil
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 {
161 block, err := mining.NewBlockTemplate(chain, txPool, nil)
166 blockSize, err := block.MarshalText()
171 fmt.Println("blocksize:", uint64(len(blockSize)))
172 fmt.Println("block tx count:", uint64(len(block.Transactions)))
173 fmt.Println("coinbase txsize:", uint64(block.Transactions[0].SerializedSize))
174 if len(block.Transactions) > 1 {
175 fmt.Println("txsize:", uint64(block.Transactions[1].SerializedSize))
178 seed, err := chain.CalcNextSeed(&block.PreviousBlockHash)
183 if err := SolveBlock(seed, block); err != nil {
187 if _, err := chain.ProcessBlock(block); err != nil {
194 func processNewTxch(txPool *protocol.TxPool) {
197 func SolveBlock(seed *bc.Hash, block *types.Block) error {
198 maxNonce := ^uint64(0) // 2^64 - 1
199 header := &block.BlockHeader
200 for i := uint64(0); i < maxNonce; i++ {
202 headerHash := header.Hash()
203 if difficulty.CheckProofOfWork(&headerHash, seed, header.Bits) {
210 func MockSimpleUtxo(index uint64, assetID *bc.AssetID, amount uint64, ctrlProg *account.CtrlProgram) *account.UTXO {
212 ctrlProg = &account.CtrlProgram{
216 ControlProgram: []byte{81},
221 utxo := &account.UTXO{
222 OutputID: bc.Hash{V0: 1},
223 SourceID: bc.Hash{V0: 1},
227 ControlProgram: ctrlProg.ControlProgram,
228 ControlProgramIndex: ctrlProg.KeyIndex,
229 AccountID: ctrlProg.AccountID,
230 Address: ctrlProg.Address,
237 func GenerateBaseUtxos(num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
238 utxos := []*account.UTXO{}
239 for i := 0; i < num; i++ {
240 utxo := MockSimpleUtxo(uint64(i), consensus.BTMAssetID, amount, ctrlProg)
241 utxos = append(utxos, utxo)
247 func GenerateOtherUtxos(typeCount, num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
248 utxos := []*account.UTXO{}
250 assetID := &bc.AssetID{
251 V0: uint64(typeCount),
257 for i := 0; i < num; i++ {
258 utxo := MockSimpleUtxo(uint64(typeCount*num+i), assetID, amount, ctrlProg)
259 utxos = append(utxos, utxo)
265 func AddTxInputFromUtxo(utxo *account.UTXO, singer *signers.Signer) (*types.TxInput, *txbuilder.SigningInstruction, error) {
266 txInput, signInst, err := account.UtxoToInputs(singer, utxo)
271 return txInput, signInst, nil
274 func AddTxOutput(assetID bc.AssetID, amount uint64, controlProgram []byte) *types.TxOutput {
275 out := types.NewTxOutput(assetID, amount, controlProgram)
279 func CreateTxBuilder(baseUtxo *account.UTXO, btmServiceFlag bool, signer *signers.Signer) (*txbuilder.TemplateBuilder, error) {
280 tplBuilder := txbuilder.NewBuilder(time.Now())
283 txInput, signInst, err := AddTxInputFromUtxo(baseUtxo, signer)
287 tplBuilder.AddInput(txInput, signInst)
289 // if the btm is the service charge, didn't need to add the output
291 txOutput := AddTxOutput(baseUtxo.AssetID, 100, baseUtxo.ControlProgram)
292 tplBuilder.AddOutput(txOutput)
295 return tplBuilder, nil
298 func AddTxBuilder(tplBuilder *txbuilder.TemplateBuilder, utxo *account.UTXO, signer *signers.Signer) error {
299 txInput, signInst, err := AddTxInputFromUtxo(utxo, signer)
303 tplBuilder.AddInput(txInput, signInst)
305 txOutput := AddTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
306 tplBuilder.AddOutput(txOutput)
311 func BuildTx(baseUtxo *account.UTXO, otherUtxos []*account.UTXO, signer *signers.Signer) (*txbuilder.Template, error) {
312 btmServiceFlag := false
313 if otherUtxos == nil || len(otherUtxos) == 0 {
314 btmServiceFlag = true
317 tplBuilder, err := CreateTxBuilder(baseUtxo, btmServiceFlag, signer)
322 for _, u := range otherUtxos {
323 if err := AddTxBuilder(tplBuilder, u, signer); err != nil {
328 tpl, _, err := tplBuilder.Build()
336 func GenetrateTxbyUtxo(baseUtxo []*account.UTXO, otherUtxo [][]*account.UTXO) ([]*types.Tx, error) {
337 tmpUtxo := []*account.UTXO{}
339 otherUtxoFlag := true
341 if len(otherUtxo) == 0 || len(otherUtxo) != len(baseUtxo) {
342 otherUtxoFlag = false
345 for i := 0; i < len(baseUtxo); i++ {
347 tmpUtxo = otherUtxo[i]
352 tpl, err := BuildTx(baseUtxo[i], tmpUtxo, nil)
357 txs = append(txs, tpl.Transaction)
363 func CreateTxbyNum(txNumber, otherAssetNum int) ([]*types.Tx, error) {
364 baseUtxos := GenerateBaseUtxos(txNumber, 1000000000, nil)
365 otherUtxos := make([][]*account.UTXO, 0, txNumber)
366 if otherAssetNum != 0 {
367 for i := 0; i < txNumber; i++ {
368 utxos := GenerateOtherUtxos(i, otherAssetNum, 6000, nil)
369 otherUtxos = append(otherUtxos, utxos)
373 txs, err := GenetrateTxbyUtxo(baseUtxos, otherUtxos)
381 func SetUtxoView(db dbm.DB, view *state.UtxoViewpoint) error {
382 batch := db.NewBatch()
383 if err := database.SaveUtxoView(batch, view); err != nil {
390 //-------------------------Mock actual transaction----------------------------------
391 func MockTxsP2PKH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
392 accountManager := account.NewManager(testDB, nil)
393 hsm, err := pseudohsm.New(keyDirPath)
398 xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
404 for i := 0; i < txNumber; i++ {
405 testAccountAlias := fmt.Sprintf("testAccount%d", i)
406 testAccount, err := accountManager.Create([]chainkd.XPub{xpub.XPub}, 1, testAccountAlias, signers.BIP0044)
411 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
416 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
417 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
418 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
423 if _, err := MockSign(tpl, hsm, "password"); err != nil {
427 txs = append(txs, tpl.Transaction)
433 func MockTxsP2SH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
434 accountManager := account.NewManager(testDB, nil)
435 hsm, err := pseudohsm.New(keyDirPath)
440 xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
445 xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
451 for i := 0; i < txNumber; i++ {
452 testAccountAlias := fmt.Sprintf("testAccount%d", i)
453 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
458 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
463 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
464 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
465 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
470 if _, err := MockSign(tpl, hsm, "password"); err != nil {
474 txs = append(txs, tpl.Transaction)
480 func MockTxsMultiSign(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
481 accountManager := account.NewManager(testDB, nil)
482 hsm, err := pseudohsm.New(keyDirPath)
487 xpub1, _, err := hsm.XCreate("TestMultilNodeSign1", "password1", "en")
492 xpub2, _, err := hsm.XCreate("TestMultilNodeSign2", "password2", "en")
497 for i := 0; i < txNumber; i++ {
498 testAccountAlias := fmt.Sprintf("testAccount%d", i)
499 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
504 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
509 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
510 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
511 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
516 if _, err := MockSign(tpl, hsm, "password1"); err != nil {
520 if _, err := MockSign(tpl, hsm, "password2"); err != nil {
524 txs = append(txs, tpl.Transaction)