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"
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)
81 // defer testDB.Close()
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
104 // txs := []*types.Tx{}
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 {
160 // // imitate block broadcast deplay
161 // time.Sleep(time.Second)
162 // block, err := mining.NewBlockTemplate(chain, txPool, nil)
166 // 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))
176 // if _, err := chain.ProcessBlock(block); err != nil {
183 //func processNewTxch(txPool *protocol.TxPool) {
186 //func MockSimpleUtxo(index uint64, assetID *bc.AssetID, amount uint64, ctrlProg *account.CtrlProgram) *account.UTXO {
187 // if ctrlProg == nil {
188 // ctrlProg = &account.CtrlProgram{
191 // KeyIndex: uint64(0),
192 // ControlProgram: []byte{81},
197 // utxo := &account.UTXO{
198 // OutputID: bc.Hash{V0: 1},
199 // SourceID: bc.Hash{V0: 1},
200 // AssetID: *assetID,
203 // ControlProgram: ctrlProg.ControlProgram,
204 // ControlProgramIndex: ctrlProg.KeyIndex,
205 // AccountID: ctrlProg.AccountID,
206 // Address: ctrlProg.Address,
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)
223 //func GenerateOtherUtxos(typeCount, num int, amount uint64, ctrlProg *account.CtrlProgram) []*account.UTXO {
224 // utxos := []*account.UTXO{}
226 // assetID := &bc.AssetID{
227 // V0: uint64(typeCount),
233 // for i := 0; i < num; i++ {
234 // utxo := MockSimpleUtxo(uint64(typeCount*num+i), assetID, amount, ctrlProg)
235 // utxos = append(utxos, utxo)
241 //func AddTxInputFromUtxo(utxo *account.UTXO, singer *signers.Signer) (*types.TxInput, *txbuilder.SigningInstruction, error) {
242 // txInput, signInst, err := account.UtxoToInputs(singer, utxo)
244 // return nil, nil, err
247 // return txInput, signInst, nil
250 //func AddTxOutput(assetID bc.AssetID, amount uint64, controlProgram []byte) *types.TxOutput {
251 // out := types.NewOriginalTxOutput(assetID, amount, controlProgram)
255 //func CreateTxBuilder(baseUtxo *account.UTXO, btmServiceFlag bool, signer *signers.Signer) (*txbuilder.TemplateBuilder, error) {
256 // tplBuilder := txbuilder.NewBuilder(time.Now())
259 // txInput, signInst, err := AddTxInputFromUtxo(baseUtxo, signer)
263 // tplBuilder.AddInput(txInput, signInst)
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)
271 // return tplBuilder, nil
274 //func AddTxBuilder(tplBuilder *txbuilder.TemplateBuilder, utxo *account.UTXO, signer *signers.Signer) error {
275 // txInput, signInst, err := AddTxInputFromUtxo(utxo, signer)
279 // tplBuilder.AddInput(txInput, signInst)
281 // txOutput := AddTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
282 // tplBuilder.AddOutput(txOutput)
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
293 // tplBuilder, err := CreateTxBuilder(baseUtxo, btmServiceFlag, signer)
298 // for _, u := range otherUtxos {
299 // if err := AddTxBuilder(tplBuilder, u, signer); err != nil {
304 // tpl, _, err := tplBuilder.Build()
312 //func GenetrateTxbyUtxo(baseUtxo []*account.UTXO, otherUtxo [][]*account.UTXO) ([]*types.Tx, error) {
313 // tmpUtxo := []*account.UTXO{}
314 // txs := []*types.Tx{}
315 // otherUtxoFlag := true
317 // if len(otherUtxo) == 0 || len(otherUtxo) != len(baseUtxo) {
318 // otherUtxoFlag = false
321 // for i := 0; i < len(baseUtxo); i++ {
322 // if otherUtxoFlag {
323 // tmpUtxo = otherUtxo[i]
328 // tpl, err := BuildTx(baseUtxo[i], tmpUtxo, nil)
333 // txs = append(txs, tpl.Transaction)
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)
349 // txs, err := GenetrateTxbyUtxo(baseUtxos, otherUtxos)
357 //func SetUtxoView(db dbm.DB, view *state.UtxoViewpoint) error {
358 // batch := db.NewBatch()
359 // if err := database.SaveUtxoView(batch, view); err != nil {
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)
374 // xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
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)
387 // controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
392 // utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
393 // otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
394 // tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
399 // if _, err := MockSign(tpl, hsm, "password"); err != nil {
403 // txs = append(txs, tpl.Transaction)
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)
416 // xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
421 // xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
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)
434 // controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
439 // utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
440 // otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
441 // tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
446 // if _, err := MockSign(tpl, hsm, "password"); err != nil {
450 // txs = append(txs, tpl.Transaction)
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)
463 // xpub1, _, err := hsm.XCreate("TestMultilNodeSign1", "password1", "en")
468 // xpub2, _, err := hsm.XCreate("TestMultilNodeSign2", "password2", "en")
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)
480 // controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
485 // utxo := MockSimpleUtxo(0, consensus.BTMAssetID, 1000000000, controlProg)
486 // otherUtxos := GenerateOtherUtxos(i, otherAssetNum, 6000, controlProg)
487 // tpl, err := BuildTx(utxo, otherUtxos, testAccount.Signer)
492 // if _, err := MockSign(tpl, hsm, "password1"); err != nil {
496 // if _, err := MockSign(tpl, hsm, "password2"); err != nil {
500 // txs = append(txs, tpl.Transaction)