10 dbm "github.com/tendermint/tmlibs/db"
12 "github.com/vapor/account"
13 "github.com/vapor/blockchain/pseudohsm"
14 "github.com/vapor/blockchain/signers"
15 "github.com/vapor/blockchain/txbuilder"
16 "github.com/vapor/consensus"
17 "github.com/vapor/crypto/ed25519/chainkd"
18 "github.com/vapor/database/leveldb"
19 "github.com/vapor/database/storage"
20 "github.com/vapor/mining"
21 "github.com/vapor/protocol"
22 "github.com/vapor/protocol/bc"
23 "github.com/vapor/protocol/bc/types"
24 "github.com/vapor/protocol/state"
27 func BenchmarkChain_CoinBaseTx_NoAsset(b *testing.B) {
28 benchInsertChain(b, 0, 0, "")
31 func BenchmarkChain_BtmTx_NoAsset_BASE(b *testing.B) {
32 benchInsertChain(b, 1, 0, "")
35 func BenchmarkChain_5000BtmTx_NoAsset_BASE(b *testing.B) {
36 benchInsertChain(b, 5000, 0, "")
39 func BenchmarkChain_5000BtmTx_1Asset_BASE(b *testing.B) {
40 benchInsertChain(b, 5000, 1, "")
43 // standard Transaction
44 func BenchmarkChain_BtmTx_NoAsset_P2PKH(b *testing.B) {
45 benchInsertChain(b, 1000, 0, "P2PKH")
48 func BenchmarkChain_BtmTx_1Asset_P2PKH(b *testing.B) {
49 benchInsertChain(b, 1000, 1, "P2PKH")
52 func BenchmarkChain_BtmTx_NoAsset_P2SH(b *testing.B) {
53 benchInsertChain(b, 100, 0, "P2SH")
56 func BenchmarkChain_BtmTx_1Asset_P2SH(b *testing.B) {
57 benchInsertChain(b, 100, 1, "P2SH")
60 func BenchmarkChain_BtmTx_NoAsset_MultiSign(b *testing.B) {
61 benchInsertChain(b, 100, 0, "MultiSign")
64 func BenchmarkChain_BtmTx_1Asset_MultiSign(b *testing.B) {
65 benchInsertChain(b, 100, 1, "MultiSign")
68 func benchInsertChain(b *testing.B, blockTxNumber int, otherAssetNum int, txType string) {
71 totalTxNumber := testNumber * blockTxNumber
73 dirPath, err := ioutil.TempDir(".", "testDB")
75 b.Fatal("create dirPath err:", err)
77 defer os.RemoveAll(dirPath)
79 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
82 // Generate a chain test data.
83 chain, txs, txPool, err := GenerateChainData(dirPath, testDB, totalTxNumber, otherAssetNum, txType)
85 b.Fatal("GenerateChainData err:", err)
91 for i := 0; i < b.N; i++ {
92 testTxs := txs[blockTxNumber*i : blockTxNumber*(i+1)]
93 if err := InsertChain(chain, txPool, testTxs); err != nil {
94 b.Fatal("Failed to insert block into chain:", err)
99 func GenerateChainData(dirPath string, testDB dbm.DB, txNumber, otherAssetNum int, txType string) (*protocol.Chain, []*types.Tx, *protocol.TxPool, error) {
102 // generate transactions
106 txs, err = MockTxsP2PKH(dirPath, testDB, txNumber, otherAssetNum)
108 return nil, nil, nil, err
111 txs, err = MockTxsP2SH(dirPath, testDB, txNumber, otherAssetNum)
113 return nil, nil, nil, err
116 txs, err = MockTxsMultiSign(dirPath, testDB, txNumber, otherAssetNum)
118 return nil, nil, nil, err
121 txs, err = CreateTxbyNum(txNumber, otherAssetNum)
123 return nil, nil, nil, err
127 // init UtxoViewpoint
128 utxoView := state.NewUtxoViewpoint()
129 utxoEntry := storage.NewUtxoEntry(false, 1, false)
130 for _, tx := range txs {
131 for _, id := range tx.SpentOutputIDs {
132 utxoView.Entries[id] = utxoEntry
136 if err := SetUtxoView(testDB, utxoView); err != nil {
137 return nil, nil, nil, err
140 store := leveldb.NewStore(testDB)
141 txPool := protocol.NewTxPool(store)
142 chain, err := protocol.NewChain(store, txPool)
144 return nil, nil, nil, err
147 go processNewTxch(txPool)
149 return chain, txs, txPool, nil
152 func InsertChain(chain *protocol.Chain, txPool *protocol.TxPool, txs []*types.Tx) error {
153 for _, tx := range txs {
154 if err := txbuilder.FinalizeTx(nil, chain, tx); err != nil {
159 block, err := mining.NewBlockTemplate(chain, txPool, nil, nil, nil, uint64(time.Now().Unix()))
164 blockSize, err := block.MarshalText()
169 fmt.Println("blocksize:", uint64(len(blockSize)))
170 fmt.Println("block tx count:", uint64(len(block.Transactions)))
171 fmt.Println("coinbase txsize:", uint64(block.Transactions[0].SerializedSize))
172 if len(block.Transactions) > 1 {
173 fmt.Println("txsize:", uint64(block.Transactions[1].SerializedSize))
176 if _, err := chain.ProcessBlock(block); err != nil {
183 func processNewTxch(txPool *protocol.TxPool) {
184 newTxCh := txPool.GetMsgCh()
185 for tx := range newTxCh {
191 func MockSimpleUtxo(index uint64, assetID *bc.AssetID, amount uint64, ctrlProg *account.CtrlProgram) *account.UTXO {
193 ctrlProg = &account.CtrlProgram{
197 ControlProgram: []byte{81},
202 utxo := &account.UTXO{
203 OutputID: bc.Hash{V0: 1},
204 SourceID: bc.Hash{V0: 1},
208 ControlProgram: ctrlProg.ControlProgram,
209 ControlProgramIndex: ctrlProg.KeyIndex,
210 AccountID: ctrlProg.AccountID,
211 Address: ctrlProg.Address,
218 func GenerateBaseUtxos(num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
219 utxos := []*account.UTXO{}
220 for i := 0; i < num; i++ {
221 utxo := MockSimpleUtxo(uint64(i), consensus.BTMAssetID, amount, ctrlProg)
222 utxos = append(utxos, utxo)
228 func GenerateOtherUtxos(typeCount, num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
229 utxos := []*account.UTXO{}
231 assetID := &bc.AssetID{
232 V0: uint64(typeCount),
238 for i := 0; i < num; i++ {
239 utxo := MockSimpleUtxo(uint64(typeCount*num+i), assetID, amount, ctrlProg)
240 utxos = append(utxos, utxo)
246 func AddTxInputFromUtxo(utxo *account.UTXO, singer *signers.Signer) (*types.TxInput, *txbuilder.SigningInstruction, error) {
247 txInput, signInst, err := account.UtxoToInputs(singer, utxo)
252 return txInput, signInst, nil
255 func AddTxOutput(assetID bc.AssetID, amount uint64, controlProgram []byte) *types.TxOutput {
256 out := types.NewTxOutput(assetID, amount, controlProgram)
260 func CreateTxBuilder(baseUtxo *account.UTXO, btmServiceFlag bool, signer *signers.Signer) (*txbuilder.TemplateBuilder, error) {
261 tplBuilder := txbuilder.NewBuilder(time.Now())
264 txInput, signInst, err := AddTxInputFromUtxo(baseUtxo, signer)
268 tplBuilder.AddInput(txInput, signInst)
270 // if the btm is the service charge, didn't need to add the output
272 txOutput := AddTxOutput(baseUtxo.AssetID, 100, baseUtxo.ControlProgram)
273 tplBuilder.AddOutput(txOutput)
276 return tplBuilder, nil
279 func AddTxBuilder(tplBuilder *txbuilder.TemplateBuilder, utxo *account.UTXO, signer *signers.Signer) error {
280 txInput, signInst, err := AddTxInputFromUtxo(utxo, signer)
284 tplBuilder.AddInput(txInput, signInst)
286 txOutput := AddTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
287 tplBuilder.AddOutput(txOutput)
292 func BuildTx(baseUtxo *account.UTXO, otherUtxos []*account.UTXO, signer *signers.Signer) (*txbuilder.Template, error) {
293 btmServiceFlag := false
294 if otherUtxos == nil || len(otherUtxos) == 0 {
295 btmServiceFlag = true
298 tplBuilder, err := CreateTxBuilder(baseUtxo, btmServiceFlag, signer)
303 for _, u := range otherUtxos {
304 if err := AddTxBuilder(tplBuilder, u, signer); err != nil {
309 tpl, _, err := tplBuilder.Build()
317 func GenetrateTxbyUtxo(baseUtxo []*account.UTXO, otherUtxo [][]*account.UTXO) ([]*types.Tx, error) {
318 tmpUtxo := []*account.UTXO{}
320 otherUtxoFlag := true
322 if len(otherUtxo) == 0 || len(otherUtxo) != len(baseUtxo) {
323 otherUtxoFlag = false
326 for i := 0; i < len(baseUtxo); i++ {
328 tmpUtxo = otherUtxo[i]
333 tpl, err := BuildTx(baseUtxo[i], tmpUtxo, nil)
338 txs = append(txs, tpl.Transaction)
344 func CreateTxbyNum(txNumber, otherAssetNum int) ([]*types.Tx, error) {
345 baseUtxos := GenerateBaseUtxos(txNumber, 1000000000, nil)
346 otherUtxos := make([][]*account.UTXO, 0, txNumber)
347 if otherAssetNum != 0 {
348 for i := 0; i < txNumber; i++ {
349 utxos := GenerateOtherUtxos(i, otherAssetNum, 6000, nil)
350 otherUtxos = append(otherUtxos, utxos)
354 txs, err := GenetrateTxbyUtxo(baseUtxos, otherUtxos)
362 func SetUtxoView(db dbm.DB, view *state.UtxoViewpoint) error {
363 batch := db.NewBatch()
364 if err := leveldb.SaveUtxoView(batch, view); err != nil {
371 //-------------------------Mock actual transaction----------------------------------
372 func MockTxsP2PKH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
373 accountManager := account.NewManager(testDB, nil)
374 hsm, err := pseudohsm.New(keyDirPath)
379 xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
385 for i := 0; i < txNumber; i++ {
386 testAccountAlias := fmt.Sprintf("testAccount%d", i)
387 testAccount, err := accountManager.Create([]chainkd.XPub{xpub.XPub}, 1, testAccountAlias, signers.BIP0044)
392 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
397 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
398 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
399 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
404 if _, err := MockSign(tpl, hsm, "password"); err != nil {
408 txs = append(txs, tpl.Transaction)
414 func MockTxsP2SH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
415 accountManager := account.NewManager(testDB, nil)
416 hsm, err := pseudohsm.New(keyDirPath)
421 xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
426 xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
432 for i := 0; i < txNumber; i++ {
433 testAccountAlias := fmt.Sprintf("testAccount%d", i)
434 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
439 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
444 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
445 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
446 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
451 if _, err := MockSign(tpl, hsm, "password"); err != nil {
455 txs = append(txs, tpl.Transaction)
461 func MockTxsMultiSign(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
462 accountManager := account.NewManager(testDB, nil)
463 hsm, err := pseudohsm.New(keyDirPath)
468 xpub1, _, err := hsm.XCreate("TestMultilNodeSign1", "password1", "en")
473 xpub2, _, err := hsm.XCreate("TestMultilNodeSign2", "password2", "en")
478 for i := 0; i < txNumber; i++ {
479 testAccountAlias := fmt.Sprintf("testAccount%d", i)
480 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
485 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
490 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
491 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
492 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
497 if _, err := MockSign(tpl, hsm, "password1"); err != nil {
501 if _, err := MockSign(tpl, hsm, "password2"); err != nil {
505 txs = append(txs, tpl.Transaction)