update btctxgen so that it will iterate tx on by on.

pull/53/head
Richard Liu 6 years ago
parent 47501a2df2
commit 07fbeb6c8b
  1. 53
      client/btctxgen/main.go
  2. 61
      client/btctxiter/btctxiter.go

@ -68,37 +68,34 @@ func generateSimulatedTransactions(shardID int, dataNodes []*node.Node) ([]*bloc
LOOP:
for true {
blk := btcTXIter.IterateBTCTX()
blk.BuildTxList()
for _, btcTx := range blk.Txs {
tx := blockchain.Transaction{}
// tx.ID = tx.Hash.String()
if btcTx.IsCoinBase() {
// TxIn coinbase, newly generated coins
prevTxID := [32]byte{}
// TODO: merge txID with txIndex
tx.TxInput = []blockchain.TXInput{blockchain.TXInput{prevTxID, -1, "", nodeShardID}}
} else {
for _, txi := range btcTx.TxIn {
tx.TxInput = append(tx.TxInput, blockchain.TXInput{txi.Input.Hash, int(txi.Input.Vout), "", nodeShardID})
}
btcTx := btcTXIter.NextTx()
tx := blockchain.Transaction{}
// tx.ID = tx.Hash.String()
if btcTx.IsCoinBase() {
// TxIn coinbase, newly generated coins
prevTxID := [32]byte{}
// TODO: merge txID with txIndex in TxInput
tx.TxInput = []blockchain.TXInput{blockchain.TXInput{prevTxID, -1, "", nodeShardID}}
} else {
for _, txi := range btcTx.TxIn {
tx.TxInput = append(tx.TxInput, blockchain.TXInput{txi.Input.Hash, int(txi.Input.Vout), "", nodeShardID})
}
}
for _, txo := range btcTx.TxOut {
txoAddr := btc.NewAddrFromPkScript(txo.Pk_script, false)
if txoAddr == nil {
log.Warn("TxOut: can't decode address")
}
txout := blockchain.TXOutput{int(txo.Value), txoAddr.String(), nodeShardID}
tx.TxOutput = append(tx.TxOutput, txout)
}
tx.SetID()
txs = append(txs, &tx)
log.Debug("[Generator] transformed btc tx", "block height", btcTXIter.GetIndex(), "txi", len(tx.TxInput), "txo", len(tx.TxOutput), "txCount", cnt)
cnt++
if cnt >= setting.maxNumTxsPerBatch {
break LOOP
for _, txo := range btcTx.TxOut {
txoAddr := btc.NewAddrFromPkScript(txo.Pk_script, false)
if txoAddr == nil {
log.Warn("TxOut: can't decode address")
}
txout := blockchain.TXOutput{int(txo.Value), txoAddr.String(), nodeShardID}
tx.TxOutput = append(tx.TxOutput, txout)
}
tx.SetID()
txs = append(txs, &tx)
// log.Debug("[Generator] transformed btc tx", "block height", btcTXIter.GetBlockIndex(), "block tx count", btcTXIter.GetBlock().TxCount, "block tx cnt", len(btcTXIter.GetBlock().Txs), "txi", len(tx.TxInput), "txo", len(tx.TxOutput), "txCount", cnt)
cnt++
if cnt >= setting.maxNumTxsPerBatch {
break LOOP
}
}

@ -8,7 +8,10 @@ import (
)
type BTCTXIterator struct {
index int
blockIndex int
block *btc.Block
txIndex int
tx *btc.Tx
blockDatabase *blockdb.BlockDB
}
@ -18,25 +21,63 @@ func (iter *BTCTXIterator) Init() {
// Specify blocks directory
iter.blockDatabase = blockdb.NewBlockDB("/Users/ricl/Library/Application Support/Bitcoin/blocks", Magic)
iter.index = -1
iter.blockIndex = -1
iter.block = nil
iter.nextBlock()
}
func (iter *BTCTXIterator) IterateBTCTX() *btc.Block {
iter.index++
// Move to the next transaction
func (iter *BTCTXIterator) NextTx() *btc.Tx {
iter.txIndex++
if iter.txIndex >= iter.block.TxCount {
iter.nextBlock()
iter.txIndex++
}
iter.tx = iter.block.Txs[iter.txIndex]
return iter.tx
}
// Gets the index/height of the current block
func (iter *BTCTXIterator) GetBlockIndex() int {
return iter.blockIndex
}
// Gets the current block
func (iter *BTCTXIterator) GetBlock() *btc.Block {
return iter.block
}
// Gets the index of the current transaction
func (iter *BTCTXIterator) GetTxIndex() int {
return iter.txIndex
}
// Gets the current transaction
func (iter *BTCTXIterator) GetTx() *btc.Tx {
return iter.tx
}
func (iter *BTCTXIterator) resetTx() {
iter.txIndex = -1
iter.tx = nil
}
// Move to the next block
func (iter *BTCTXIterator) nextBlock() *btc.Block {
iter.blockIndex++
dat, err := iter.blockDatabase.FetchNextBlock()
if dat == nil || err != nil {
log.Println("END of DB file")
}
blk, err := btc.NewBlock(dat[:])
iter.block, err = btc.NewBlock(dat[:])
if err != nil {
println("Block inconsistent:", err.Error())
}
blk.BuildTxList()
return blk
}
iter.block.BuildTxList()
iter.resetTx()
func (iter *BTCTXIterator) GetIndex() int {
return iter.index
return iter.block
}

Loading…
Cancel
Save