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/config"
15 "github.com/vapor/consensus"
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/proposal"
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(storage.NormalUTXOType, 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 config.CommonConfig = config.DefaultConfig()
142 store := database.NewStore(testDB)
143 dispatcher := event.NewDispatcher()
144 txPool := protocol.NewTxPool(store, dispatcher)
145 chain, err := protocol.NewChain(store, txPool, dispatcher)
147 return nil, nil, nil, err
150 go processNewTxch(txPool)
152 return chain, txs, txPool, nil
155 func InsertChain(chain *protocol.Chain, txPool *protocol.TxPool, txs []*types.Tx) error {
156 for _, tx := range txs {
157 if err := txbuilder.FinalizeTx(nil, chain, tx); err != nil {
162 block, err := proposal.NewBlockTemplate(chain, txPool, nil, uint64(time.Now().UnixNano() / 1e6))
167 blockSize, err := block.MarshalText()
172 fmt.Println("blocksize:", uint64(len(blockSize)))
173 fmt.Println("block tx count:", uint64(len(block.Transactions)))
174 fmt.Println("coinbase txsize:", uint64(block.Transactions[0].SerializedSize))
175 if len(block.Transactions) > 1 {
176 fmt.Println("txsize:", uint64(block.Transactions[1].SerializedSize))
179 if _, err := chain.ProcessBlock(block); err != nil {
186 func processNewTxch(txPool *protocol.TxPool) {
189 func MockSimpleUtxo(index uint64, assetID *bc.AssetID, amount uint64, ctrlProg *account.CtrlProgram) *account.UTXO {
191 ctrlProg = &account.CtrlProgram{
195 ControlProgram: []byte{81},
200 utxo := &account.UTXO{
201 OutputID: bc.Hash{V0: 1},
202 SourceID: bc.Hash{V0: 1},
206 ControlProgram: ctrlProg.ControlProgram,
207 ControlProgramIndex: ctrlProg.KeyIndex,
208 AccountID: ctrlProg.AccountID,
209 Address: ctrlProg.Address,
216 func GenerateBaseUtxos(num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
217 utxos := []*account.UTXO{}
218 for i := 0; i < num; i++ {
219 utxo := MockSimpleUtxo(uint64(i), consensus.BTMAssetID, amount, ctrlProg)
220 utxos = append(utxos, utxo)
226 func GenerateOtherUtxos(typeCount, num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
227 utxos := []*account.UTXO{}
229 assetID := &bc.AssetID{
230 V0: uint64(typeCount),
236 for i := 0; i < num; i++ {
237 utxo := MockSimpleUtxo(uint64(typeCount*num+i), assetID, amount, ctrlProg)
238 utxos = append(utxos, utxo)
244 func AddTxInputFromUtxo(utxo *account.UTXO, singer *signers.Signer) (*types.TxInput, *txbuilder.SigningInstruction, error) {
245 txInput, signInst, err := account.UtxoToInputs(singer, utxo)
250 return txInput, signInst, nil
253 func AddTxOutput(assetID bc.AssetID, amount uint64, controlProgram []byte) *types.TxOutput {
254 out := types.NewIntraChainOutput(assetID, amount, controlProgram)
258 func CreateTxBuilder(baseUtxo *account.UTXO, btmServiceFlag bool, signer *signers.Signer) (*txbuilder.TemplateBuilder, error) {
259 tplBuilder := txbuilder.NewBuilder(time.Now())
262 txInput, signInst, err := AddTxInputFromUtxo(baseUtxo, signer)
266 tplBuilder.AddInput(txInput, signInst)
268 // if the btm is the service charge, didn't need to add the output
270 txOutput := AddTxOutput(baseUtxo.AssetID, 100, baseUtxo.ControlProgram)
271 tplBuilder.AddOutput(txOutput)
274 return tplBuilder, nil
277 func AddTxBuilder(tplBuilder *txbuilder.TemplateBuilder, utxo *account.UTXO, signer *signers.Signer) error {
278 txInput, signInst, err := AddTxInputFromUtxo(utxo, signer)
282 tplBuilder.AddInput(txInput, signInst)
284 txOutput := AddTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
285 tplBuilder.AddOutput(txOutput)
290 func BuildTx(baseUtxo *account.UTXO, otherUtxos []*account.UTXO, signer *signers.Signer) (*txbuilder.Template, error) {
291 btmServiceFlag := false
292 if otherUtxos == nil || len(otherUtxos) == 0 {
293 btmServiceFlag = true
296 tplBuilder, err := CreateTxBuilder(baseUtxo, btmServiceFlag, signer)
301 for _, u := range otherUtxos {
302 if err := AddTxBuilder(tplBuilder, u, signer); err != nil {
307 tpl, _, err := tplBuilder.Build()
315 func GenetrateTxbyUtxo(baseUtxo []*account.UTXO, otherUtxo [][]*account.UTXO) ([]*types.Tx, error) {
316 tmpUtxo := []*account.UTXO{}
318 otherUtxoFlag := true
320 if len(otherUtxo) == 0 || len(otherUtxo) != len(baseUtxo) {
321 otherUtxoFlag = false
324 for i := 0; i < len(baseUtxo); i++ {
326 tmpUtxo = otherUtxo[i]
331 tpl, err := BuildTx(baseUtxo[i], tmpUtxo, nil)
336 txs = append(txs, tpl.Transaction)
342 func CreateTxbyNum(txNumber, otherAssetNum int) ([]*types.Tx, error) {
343 baseUtxos := GenerateBaseUtxos(txNumber, 1000000000, nil)
344 otherUtxos := make([][]*account.UTXO, 0, txNumber)
345 if otherAssetNum != 0 {
346 for i := 0; i < txNumber; i++ {
347 utxos := GenerateOtherUtxos(i, otherAssetNum, 6000, nil)
348 otherUtxos = append(otherUtxos, utxos)
352 txs, err := GenetrateTxbyUtxo(baseUtxos, otherUtxos)
360 func SetUtxoView(db dbm.DB, view *state.UtxoViewpoint) error {
361 batch := db.NewBatch()
362 if err := database.SaveUtxoView(batch, view); err != nil {
369 //-------------------------Mock actual transaction----------------------------------
370 func MockTxsP2PKH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
371 accountManager := account.NewManager(testDB, nil)
372 hsm, err := pseudohsm.New(keyDirPath)
377 xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
383 for i := 0; i < txNumber; i++ {
384 testAccountAlias := fmt.Sprintf("testAccount%d", i)
385 testAccount, err := accountManager.Create([]chainkd.XPub{xpub.XPub}, 1, testAccountAlias, signers.BIP0044)
390 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
395 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
396 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
397 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
402 if _, err := MockSign(tpl, hsm, "password"); err != nil {
406 txs = append(txs, tpl.Transaction)
412 func MockTxsP2SH(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
413 accountManager := account.NewManager(testDB, nil)
414 hsm, err := pseudohsm.New(keyDirPath)
419 xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
424 xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
430 for i := 0; i < txNumber; i++ {
431 testAccountAlias := fmt.Sprintf("testAccount%d", i)
432 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
437 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
442 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
443 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
444 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
449 if _, err := MockSign(tpl, hsm, "password"); err != nil {
453 txs = append(txs, tpl.Transaction)
459 func MockTxsMultiSign(keyDirPath string, testDB dbm.DB, txNumber, otherAssetNum int) ([]*types.Tx, error) {
460 accountManager := account.NewManager(testDB, nil)
461 hsm, err := pseudohsm.New(keyDirPath)
466 xpub1, _, err := hsm.XCreate("TestMultilNodeSign1", "password1", "en")
471 xpub2, _, err := hsm.XCreate("TestMultilNodeSign2", "password2", "en")
476 for i := 0; i < txNumber; i++ {
477 testAccountAlias := fmt.Sprintf("testAccount%d", i)
478 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, testAccountAlias, signers.BIP0044)
483 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
488 utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
489 otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
490 tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
495 if _, err := MockSign(tpl, hsm, "password1"); err != nil {
499 if _, err := MockSign(tpl, hsm, "password2"); err != nil {
503 txs = append(txs, tpl.Transaction)