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/crypto/ed25519/chainkd"
16 "github.com/vapor/database"
17 dbm "github.com/vapor/database/leveldb"
18 "github.com/vapor/database/storage"
19 "github.com/vapor/event"
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 := database.NewStore(testDB)
141 dispatcher := event.NewDispatcher()
142 txPool := protocol.NewTxPool(store, dispatcher)
143 chain, err := protocol.NewChain(store, txPool, dispatcher)
145 return nil, nil, nil, err
148 go processNewTxch(txPool)
150 return chain, txs, txPool, nil
153 func InsertChain(chain *protocol.Chain, txPool *protocol.TxPool, txs []*types.Tx) error {
154 for _, tx := range txs {
155 if err := txbuilder.FinalizeTx(nil, chain, tx); err != nil {
160 block, err := mining.NewBlockTemplate(chain, txPool, nil)
165 blockSize, err := block.MarshalText()
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))
177 if _, err := chain.ProcessBlock(block); err != nil {
184 func processNewTxch(txPool *protocol.TxPool) {
187 func MockSimpleUtxo(index uint64, assetID *bc.AssetID, amount uint64, ctrlProg *account.CtrlProgram) *account.UTXO {
189 ctrlProg = &account.CtrlProgram{
193 ControlProgram: []byte{81},
198 utxo := &account.UTXO{
199 OutputID: bc.Hash{V0: 1},
200 SourceID: bc.Hash{V0: 1},
204 ControlProgram: ctrlProg.ControlProgram,
205 ControlProgramIndex: ctrlProg.KeyIndex,
206 AccountID: ctrlProg.AccountID,
207 Address: ctrlProg.Address,
214 func GenerateBaseUtxos(num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
215 utxos := []*account.UTXO{}
216 for i := 0; i < num; i++ {
217 utxo := MockSimpleUtxo(uint64(i), consensus.BTMAssetID, amount, ctrlProg)
218 utxos = append(utxos, utxo)
224 func GenerateOtherUtxos(typeCount, num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
225 utxos := []*account.UTXO{}
227 assetID := &bc.AssetID{
228 V0: uint64(typeCount),
234 for i := 0; i < num; i++ {
235 utxo := MockSimpleUtxo(uint64(typeCount*num+i), assetID, amount, ctrlProg)
236 utxos = append(utxos, utxo)
242 func AddTxInputFromUtxo(utxo *account.UTXO, singer *signers.Signer) (*types.TxInput, *txbuilder.SigningInstruction, error) {
243 txInput, signInst, err := account.UtxoToInputs(singer, utxo)
248 return txInput, signInst, nil
251 func AddTxOutput(assetID bc.AssetID, amount uint64, controlProgram []byte) *types.TxOutput {
252 out := types.NewIntraChainOutput(assetID, amount, controlProgram)
256 func CreateTxBuilder(baseUtxo *account.UTXO, btmServiceFlag bool, signer *signers.Signer) (*txbuilder.TemplateBuilder, error) {
257 tplBuilder := txbuilder.NewBuilder(time.Now())
260 txInput, signInst, err := AddTxInputFromUtxo(baseUtxo, signer)
264 tplBuilder.AddInput(txInput, signInst)
266 // if the btm is the service charge, didn't need to add the output
268 txOutput := AddTxOutput(baseUtxo.AssetID, 100, baseUtxo.ControlProgram)
269 tplBuilder.AddOutput(txOutput)
272 return tplBuilder, nil
275 func AddTxBuilder(tplBuilder *txbuilder.TemplateBuilder, utxo *account.UTXO, signer *signers.Signer) error {
276 txInput, signInst, err := AddTxInputFromUtxo(utxo, signer)
280 tplBuilder.AddInput(txInput, signInst)
282 txOutput := AddTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
283 tplBuilder.AddOutput(txOutput)
288 func BuildTx(baseUtxo *account.UTXO, otherUtxos []*account.UTXO, signer *signers.Signer) (*txbuilder.Template, error) {
289 btmServiceFlag := false
290 if otherUtxos == nil || len(otherUtxos) == 0 {
291 btmServiceFlag = true
294 tplBuilder, err := CreateTxBuilder(baseUtxo, btmServiceFlag, signer)
299 for _, u := range otherUtxos {
300 if err := AddTxBuilder(tplBuilder, u, signer); err != nil {
305 tpl, _, err := tplBuilder.Build()
313 func GenetrateTxbyUtxo(baseUtxo []*account.UTXO, otherUtxo [][]*account.UTXO) ([]*types.Tx, error) {
314 tmpUtxo := []*account.UTXO{}
316 otherUtxoFlag := true
318 if len(otherUtxo) == 0 || len(otherUtxo) != len(baseUtxo) {
319 otherUtxoFlag = false
322 for i := 0; i < len(baseUtxo); i++ {
324 tmpUtxo = otherUtxo[i]
329 tpl, err := BuildTx(baseUtxo[i], tmpUtxo, nil)
334 txs = append(txs, tpl.Transaction)
340 func CreateTxbyNum(txNumber, otherAssetNum int) ([]*types.Tx, error) {
341 baseUtxos := GenerateBaseUtxos(txNumber, 1000000000, nil)
342 otherUtxos := make([][]*account.UTXO, 0, txNumber)
343 if otherAssetNum != 0 {
344 for i := 0; i < txNumber; i++ {
345 utxos := GenerateOtherUtxos(i, otherAssetNum, 6000, nil)
346 otherUtxos = append(otherUtxos, utxos)
350 txs, err := GenetrateTxbyUtxo(baseUtxos, otherUtxos)
358 func SetUtxoView(db dbm.DB, view *state.UtxoViewpoint) error {
359 batch := db.NewBatch()
360 if err := database.SaveUtxoView(batch, view); err != nil {
367 //-------------------------Mock actual transaction----------------------------------
368 func MockTxsP2PKH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
369 accountManager := account.NewManager(testDB, nil)
370 hsm, err := pseudohsm.New(keyDirPath)
375 xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
381 for i := 0; i < txNumber; i++ {
382 testAccountAlias := fmt.Sprintf("testAccount%d", i)
383 testAccount, err := accountManager.Create([]chainkd.XPub{xpub.XPub}, 1, testAccountAlias, signers.BIP0044)
388 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
393 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
394 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
395 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
400 if _, err := MockSign(tpl, hsm, "password"); err != nil {
404 txs = append(txs, tpl.Transaction)
410 func MockTxsP2SH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
411 accountManager := account.NewManager(testDB, nil)
412 hsm, err := pseudohsm.New(keyDirPath)
417 xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
422 xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
428 for i := 0; i < txNumber; i++ {
429 testAccountAlias := fmt.Sprintf("testAccount%d", i)
430 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
435 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
440 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
441 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
442 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
447 if _, err := MockSign(tpl, hsm, "password"); err != nil {
451 txs = append(txs, tpl.Transaction)
457 func MockTxsMultiSign(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
458 accountManager := account.NewManager(testDB, nil)
459 hsm, err := pseudohsm.New(keyDirPath)
464 xpub1, _, err := hsm.XCreate("TestMultilNodeSign1", "password1", "en")
469 xpub2, _, err := hsm.XCreate("TestMultilNodeSign2", "password2", "en")
474 for i := 0; i < txNumber; i++ {
475 testAccountAlias := fmt.Sprintf("testAccount%d", i)
476 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
481 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
486 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
487 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
488 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
493 if _, err := MockSign(tpl, hsm, "password1"); err != nil {
497 if _, err := MockSign(tpl, hsm, "password2"); err != nil {
501 txs = append(txs, tpl.Transaction)