Bitcoin

将btcd的addrindex用户实际业务中的通过地址查询交易列

2019-02-24  本文已影响0人  qishuai

先公布下结果,基本不可行,除非你的用户量少的可怜

测试环境:

先上一下跑分结果:
image
测试方法如下:
  1. 在bitcoin core区块链上随机选择2000个地址(存在交易记录);

  2. 在压测中,保证每次请求的地址不一样,这是为了防止leveldb cache的命中

  3. 此次测试返回的交易列表的请求如下:

    • 得到交易的数量列表:{50, 50, 2, 16, 41, 50, 2, 50, 50, 50, 2, 2, 41, 50, 50, 50, 50, 2, 2, 2, 11, 2, 2, 24, 2, 2, 11, 50, 50, 2, 50, 2, 50, 50, 50, 50, 50, 2, 2, 2, 2, 50, 19, 2, 2, 19, 2, 50, 50, 50, 2, 2, 50, 5, 50, 25, 4, 2, 2, 2, 15, 5, 2, 38, 50, 50, 2, 50, 26, 38, 50, 2, 50, 2, 8, 2, 2, 4, 50, 2, 50, 2, 2, 2, 18, 8, 50, 2, 50, 50, 50, 2, 50, 6, 4, 50, 20, 6, 2, 15}

    • 总共得到交易的总数量: 2353

    • 平均每次返回的结果数量: 23

测试使用的代码:
package main
​
import (
 "fmt"
 "github.com/btcsuite/btcd/chaincfg"
 "github.com/btcsuite/btcd/rpcclient"
 "github.com/btcsuite/btcd/txscript"
 "github.com/btcsuite/btcutil"
 "github.com/gin-gonic/gin"
 "log"
 "math/rand"
 "sync/atomic"
 "time"
)
​
var (
 address []btcutil.Address
 offset int64 = -1
 client *rpcclient.Client
 service *rpcclient.Client
 net = &chaincfg.MainNetParams
 total = 2000
)
​
func main() {
 engine := gin.New()
 gin.SetMode(gin.ReleaseMode)
​
 engine.GET("/history/test", GetTxHistory)
 err := engine.Run(":9090")
 if err != nil {
 panic(err)
 }
}
​
func GetTxHistory(c *gin.Context) {
 txes, err := service.SearchRawTransactions(address[int(atomic.AddInt64(&offset, 1))], 0,50,false,nil )
 fmt.Println("get transaction length:", len(txes))
 if err != nil {
 fmt.Println(err)
 }
}
​
func init() {
 connCfg := &rpcclient.ConnConfig{
 Host:         "127.0.0.1:9332",
 User:         "ksixksjw",
 Pass:         "ThhODEyNDQTg0Nj",
 HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
 DisableTLS:   true, // Bitcoin core does not provide TLS by default
 }
 // Notice the notification parameter is nil since notifications are
 // not supported in HTTP POST mode.
 c, err := rpcclient.New(connCfg, nil)
 if err != nil {
 log.Fatal(err)
 }
 client = c
​
 // init address list
 address = make([]btcutil.Address, 0, total)
 source := rand.NewSource(time.Now().Unix())
 r := rand.New(source)
 for {
 if len(address) >= total {
 break
 }
​
 //if len(address)%500 == 0 {
 fmt.Println("elements:", len(address))
 //}
​
 hash, err := client.GetBlockHash(int64(r.Int31n(564451)))
 if err != nil {
 continue
 }
​
 block, err := client.GetBlock(hash)
 if err != nil {
 continue
 }
​
 txs := block.Transactions
 if len(txs) > 5 {
 for i := 0; i < 10; i++ {
 rtx := txs[int(r.Int31n(int32(len(txs))))]
 rout := rtx.TxOut[int(r.Int31n(int32(len(rtx.TxOut))))]
 _, addrs, _, _ := txscript.ExtractPkScriptAddrs(rout.PkScript, net)
 if len(addrs) >= 1 {
 address = append(address, addrs[0])
 }
 }
 }
 }
​
 client.Shutdown()
​
 fmt.Println("Init completed!")
​
 connCfgService := &rpcclient.ConnConfig{
 Host:         "127.0.0.1:8334",
 User:         "RfTA8mXwQKN6a71",
 Pass:         "crkx+AmBt8NCMRZN8",
 HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
 DisableTLS:   true, // Bitcoin core does not provide TLS by default
 }
 // Notice the notification parameter is nil since notifications are
 // not supported in HTTP POST mode.
 cc, err := rpcclient.New(connCfgService, nil)
 if err != nil {
 log.Fatal(err)
 }
 service = cc
}
上一篇下一篇

猜你喜欢

热点阅读