update btctxgen

pull/75/head
Richard Liu 6 years ago
parent 6b3083a6d2
commit 736f47102b
  1. 35
      client/btctxgen/main.go
  2. 77
      client/btctxiter/btctxiter.go

@ -25,6 +25,9 @@ import (
"sync"
"time"
btcblockchain "github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/simple-rules/harmony-benchmark/blockchain"
"github.com/simple-rules/harmony-benchmark/client"
"github.com/simple-rules/harmony-benchmark/client/btctxiter"
@ -34,8 +37,6 @@ import (
"github.com/simple-rules/harmony-benchmark/node"
"github.com/simple-rules/harmony-benchmark/p2p"
proto_node "github.com/simple-rules/harmony-benchmark/proto/node"
"github.com/piotrnar/gocoin/lib/btc"
)
type txGenSettings struct {
@ -94,31 +95,39 @@ func generateSimulatedTransactions(shardID int, dataNodes []*node.Node) ([]*bloc
LOOP:
for true {
btcTx := btcTXIter.NextTx()
fmt.Println(btcTXIter.GetBlockIndex(), btcTXIter.GetTxIndex(), btcTx)
tx := blockchain.Transaction{}
isCrossShardTx := false
if btcTx.IsCoinBase() {
if btcblockchain.IsCoinBaseTx(btcTx.MsgTx()) {
tx.TxInput = []blockchain.TXInput{*blockchain.NewTXInput(blockchain.NewOutPoint(&blockchain.TxID{}, math.MaxUint32), [20]byte{}, nodeShardID)}
} else {
for _, btcTXI := range btcTx.TxIn {
btcTXIDStr := btc.NewUint256(btcTXI.Input.Hash[:]).String()
for _, btcTXI := range btcTx.MsgTx().TxIn {
btcTXIDStr := btcTXI.PreviousOutPoint.Hash.String()
txRef := utxoMapping[btcTXIDStr]
if txRef.shardID != nodeShardID {
isCrossShardTx = true
}
tx.TxInput = append(tx.TxInput, *blockchain.NewTXInput(blockchain.NewOutPoint(&txRef.txID, btcTXI.Input.Vout), [20]byte{}, txRef.shardID))
tx.TxInput = append(tx.TxInput, *blockchain.NewTXInput(blockchain.NewOutPoint(&txRef.txID, btcTXI.PreviousOutPoint.Index), [20]byte{}, txRef.shardID))
}
}
for _, btcTXO := range btcTx.TxOut {
btcTXOAddr := btc.NewAddrFromPkScript(btcTXO.Pk_script, false)
if btcTXOAddr == nil {
log.Warn("TxOut: can't decode address")
for _, btcTXO := range btcTx.MsgTx().TxOut {
scriptClass, addresses, _, _ := txscript.ExtractPkScriptAddrs(
btcTXO.PkScript, &chaincfg.MainNetParams)
if scriptClass.String() == "pubkey" || scriptClass.String() == "pubkeyhash" {
btcTXOAddr := addresses[0]
if btcTXOAddr == nil {
log.Warn("TxOut: can't decode address")
}
var addr [20]byte
copy(addr[:], btcTXOAddr.ScriptAddress())
txo := blockchain.TXOutput{Amount: int(btcTXO.Value), Address: addr, ShardID: nodeShardID}
tx.TxOutput = append(tx.TxOutput, txo)
}
txo := blockchain.TXOutput{Amount: int(btcTXO.Value), Address: btcTXOAddr.Hash160, ShardID: nodeShardID}
tx.TxOutput = append(tx.TxOutput, txo)
}
tx.SetID()
utxoMapping[btcTx.Hash.String()] = TXRef{tx.ID, nodeShardID}
utxoMapping[btcTx.Hash().String()] = TXRef{tx.ID, nodeShardID}
if isCrossShardTx {
crossTxs = append(crossTxs, &tx)
} else {

@ -3,47 +3,70 @@ package btctxiter
import (
"log"
"github.com/piotrnar/gocoin/lib/btc"
"github.com/piotrnar/gocoin/lib/others/blockdb"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
)
type BTCTXIterator struct {
blockIndex int
block *btc.Block
txIndex int
tx *btc.Tx
blockDatabase *blockdb.BlockDB
blockIndex int64
block *wire.MsgBlock
txIndex int
tx *btcutil.Tx
client *rpcclient.Client
}
func (iter *BTCTXIterator) Init() {
// Set real Bitcoin network
Magic := [4]byte{0xF9, 0xBE, 0xB4, 0xD9}
// Specify blocks directory
iter.blockDatabase = blockdb.NewBlockDB("/Users/ricl/Library/Application Support/Bitcoin/blocks", Magic)
iter.blockIndex = -1
// Connect to local bitcoin core RPC server using HTTP POST mode.
connCfg := &rpcclient.ConnConfig{
Host: "localhost:8332",
User: "ricl",
Pass: "123",
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.
var err error
iter.client, err = rpcclient.New(connCfg, nil)
if err != nil {
log.Fatal(err)
}
iter.blockIndex = 0 // the genesis block cannot retrieved. Skip it intentionally.
iter.block = nil
iter.nextBlock()
// defer iter.client.Shutdown()
}
// Move to the next transaction
func (iter *BTCTXIterator) NextTx() *btc.Tx {
func (iter *BTCTXIterator) NextTx() *btcutil.Tx {
iter.txIndex++
if iter.txIndex >= iter.block.TxCount {
hashes, err := iter.block.TxHashes()
if err != nil {
log.Println("Failed to get tx hashes", iter.blockIndex, iter.txIndex, err)
return nil
}
if iter.txIndex >= len(hashes) {
iter.nextBlock()
iter.txIndex++
}
iter.tx = iter.block.Txs[iter.txIndex]
iter.tx, err = iter.client.GetRawTransaction(&hashes[iter.txIndex])
if err != nil {
log.Println("Failed to get raw tx", iter.blockIndex, iter.txIndex, hashes[iter.txIndex], err)
return nil
}
log.Println("get raw tx", iter.blockIndex, iter.txIndex, hashes[iter.txIndex], iter.tx)
return iter.tx
}
// Gets the index/height of the current block
func (iter *BTCTXIterator) GetBlockIndex() int {
func (iter *BTCTXIterator) GetBlockIndex() int64 {
return iter.blockIndex
}
// Gets the current block
func (iter *BTCTXIterator) GetBlock() *btc.Block {
func (iter *BTCTXIterator) GetBlock() *wire.MsgBlock {
return iter.block
}
@ -53,7 +76,7 @@ func (iter *BTCTXIterator) GetTxIndex() int {
}
// Gets the current transaction
func (iter *BTCTXIterator) GetTx() *btc.Tx {
func (iter *BTCTXIterator) GetTx() *btcutil.Tx {
return iter.tx
}
@ -63,20 +86,16 @@ func (iter *BTCTXIterator) resetTx() {
}
// Move to the next block
func (iter *BTCTXIterator) nextBlock() *btc.Block {
func (iter *BTCTXIterator) nextBlock() *wire.MsgBlock {
iter.blockIndex++
dat, err := iter.blockDatabase.FetchNextBlock()
if dat == nil || err != nil {
log.Println("END of DB file")
hash, err := iter.client.GetBlockHash(iter.blockIndex)
if err != nil {
log.Println("Failed to get block hash at", iter.blockIndex)
}
iter.block, err = btc.NewBlock(dat[:])
iter.block, err = iter.client.GetBlock(hash)
if err != nil {
println("Block inconsistent:", err.Error())
log.Println("Failed to get block", iter.blockIndex, iter.block)
}
iter.block.BuildTxList()
iter.resetTx()
return iter.block

Loading…
Cancel
Save