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