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

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

@ -68,15 +68,13 @@ func generateSimulatedTransactions(shardID int, dataNodes []*node.Node) ([]*bloc
LOOP: LOOP:
for true { for true {
blk := btcTXIter.IterateBTCTX() btcTx := btcTXIter.NextTx()
blk.BuildTxList()
for _, btcTx := range blk.Txs {
tx := blockchain.Transaction{} tx := blockchain.Transaction{}
// tx.ID = tx.Hash.String() // tx.ID = tx.Hash.String()
if btcTx.IsCoinBase() { if btcTx.IsCoinBase() {
// TxIn coinbase, newly generated coins // TxIn coinbase, newly generated coins
prevTxID := [32]byte{} prevTxID := [32]byte{}
// TODO: merge txID with txIndex // TODO: merge txID with txIndex in TxInput
tx.TxInput = []blockchain.TXInput{blockchain.TXInput{prevTxID, -1, "", nodeShardID}} tx.TxInput = []blockchain.TXInput{blockchain.TXInput{prevTxID, -1, "", nodeShardID}}
} else { } else {
for _, txi := range btcTx.TxIn { for _, txi := range btcTx.TxIn {
@ -94,13 +92,12 @@ LOOP:
} }
tx.SetID() tx.SetID()
txs = append(txs, &tx) txs = append(txs, &tx)
log.Debug("[Generator] transformed btc tx", "block height", btcTXIter.GetIndex(), "txi", len(tx.TxInput), "txo", len(tx.TxOutput), "txCount", cnt) // 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++ cnt++
if cnt >= setting.maxNumTxsPerBatch { if cnt >= setting.maxNumTxsPerBatch {
break LOOP break LOOP
} }
} }
}
utxoPoolMutex.Unlock() utxoPoolMutex.Unlock()

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

Loading…
Cancel
Save