func getInfo(client *btcrpcclient.Client) { // getinfo demo info, err := client.GetInfo() if err != nil { logger.Crit(err.Error()) } logger.Info(fmt.Sprintf("Bitcoind Info: %v", spew.Sdump(info))) }
// makeDestinationScriptSource creates a ChangeSource which is used to receive // all correlated previous input value. A non-change address is created by this // function. func makeDestinationScriptSource(rpcClient *btcrpcclient.Client, accountName string) txauthor.ChangeSource { return func() ([]byte, error) { destinationAddress, err := rpcClient.GetNewAddress(accountName) if err != nil { return nil, err } return txscript.PayToAddrScript(destinationAddress) } }
func checkBlock(client *btcrpcclient.Client, blockNum int64) { blockHash, err := client.GetBlockHash(653895) if err != nil { logger.Crit(err.Error()) return } block, err := client.GetBlock(blockHash) if err != nil { logger.Crit(err.Error()) return } txs := block.Transactions() var processedBlock = &message.ProcessedBlock{make([]*message.ProcessedTx, len(txs))} logger.Info("Processing txs...") for txIndex, tx := range txs { vouts := tx.MsgTx().TxOut result := make([]*message.TxResult, len(vouts)) for i, vout := range vouts { addr := NewAddrFromPkScript(vout.PkScript, true) if addr != nil { result[i] = &message.TxResult{ &message.TxResult_Transfer{ &message.ValueTransfer{ addr.String(), int32(vout.Value), }, }, } } else { result[i] = &message.TxResult{ &message.TxResult_Msg{ &message.OpReturnMsg{string(decodePkScript(vout.PkScript))}, }, } } } processedBlock.Txs[txIndex] = &message.ProcessedTx{ tx.Sha().String(), result, } } spew.Dump(processedBlock) data, err := proto.Marshal(processedBlock) if err != nil { logger.Crit(err.Error()) } else { logger.Info("Publish to ZMQ...") spew.Dump(sender) sender.SendBytes(data, 0) } logger.Info("Process done.") }
// LookupAddress returns the minimum balance of the specified address for the last dur amount of time. func LookupAddress(c *btcrpcclient.Client, addr btcutil.Address, dur time.Duration) (float64, error) { more := true offset := 0 for more { more = false txs, err := c.SearchRawTransactionsVerbose(addr, offset, 1000, false, false, nil) if err != nil { return 0, err } if len(txs) == 1000 { more = true offset += 1000 } for _, tx := range txs { _ = tx } } return 0, nil }
// poolUtxos receives a new block notification from the node server // and pools the newly mined utxos to the corresponding actor's a.utxo func (com *Communication) poolUtxos(client *rpc.Client, actors []*Actor) { defer com.wg.Done() // Update utxo pool on each block connected for { select { case b, ok := <-com.blockQueue.dequeue: if !ok { return } block, err := client.GetBlock(b.hash) if err != nil { log.Printf("Cannot get block: %v", err) return } // add new outputs to unspent pool for i, tx := range block.Transactions() { next: for n, vout := range tx.MsgTx().TxOut { if i == 0 { // in case of coinbase tx, add it to coinbase queue // if the chan is full, the first tx would be mature // so add it to the pool select { case com.coinbaseQueue <- tx: break next default: // dequeue the first mature tx mTx := <-com.coinbaseQueue // enqueue the latest tx com.coinbaseQueue <- tx // we'll process the mature tx next // so point tx to mTx tx = mTx // reset vout as per the new tx vout = tx.MsgTx().TxOut[n] } } // fetch actor who owns this output var actor *Actor if len(actors) == 1 { actor = actors[0] } else { actor, err = com.getActor(actors, vout) if err != nil { log.Printf("Cannot get actor: %v", err) continue next } } txout := com.getUtxo(tx, vout, uint32(n)) // to be usable, the utxo amount should be // split-able after deducting the fee if txout.Amount > btcutil.Amount((*maxSplit))*(minFee) { // if it's usable, add utxo to actor's pool select { case actor.utxoQueue.enqueue <- txout: case <-com.exit: } } } } // allow Communicate to sync with the processed block if b.height == int32(*startBlock)-1 { select { case com.blockQueue.processed <- b: case <-com.exit: return } } if b.height >= int32(*startBlock) { var txCount, utxoCount int for _, a := range actors { utxoCount += len(a.utxoQueue.utxos) } txCount = len(block.Transactions()) log.Printf("Block %s (height %d) attached with %d transactions", b.hash, b.height, txCount) log.Printf("%d transaction outputs available to spend", utxoCount) select { case com.blockQueue.processed <- b: case <-com.exit: return } select { case com.blockTxCount <- txCount: case <-com.exit: return } } case <-com.exit: return } } }
func checkBlock(client *btcrpcclient.Client, blockNum int64) { blockHash, err := client.GetBlockHash(blockNum) if err != nil { logger.Crit(err.Error()) return } block, err := client.GetBlock(blockHash) if err != nil { logger.Crit(err.Error()) return } txs := block.Transactions() var processedBlock = &message.ProcessedBlock{ int32(blockNum), make([]*message.ProcessedTx, 0), } logger.Info("Processing txs...") start := time.Now() var wg sync.WaitGroup for txIndex, tx := range txs { wg.Add(1) go func(txIndex int, tx *btcutil.Tx) { defer wg.Done() vouts := tx.MsgTx().TxOut result := make([]*message.TxResult, len(vouts)) hasReturn := false for i, vout := range vouts { btcAddr := addr.NewAddrFromPkScript(vout.PkScript, isTestnet) if btcAddr != nil { result[i] = &message.TxResult{ &message.TxResult_Transfer{ &message.ValueTransfer{ btcAddr.String(), uint64(vout.Value), }, }, } } else { msg := decodePkScript(vout.PkScript) if msg != nil { result[i] = &message.TxResult{ &message.TxResult_Msg{ &message.OpReturnMsg{string(msg)}, }, } hasReturn = true } } } if hasReturn { processedBlock.Txs = append(processedBlock.Txs, &message.ProcessedTx{ tx.Sha().String(), result, }) } }(txIndex, tx) } wg.Wait() spew.Dump(processedBlock) data, err := proto.Marshal(processedBlock) if err != nil { logger.Crit(err.Error()) } else { logger.Info("Publish to ZMQ...") spew.Dump(data) sender.SendBytes(data, 0) } elapsed := time.Since(start) logger.Info(fmt.Sprintf("Process done in %s", elapsed)) logger.Info(fmt.Sprintf("Block %d has %d OP_Return Txs", blockNum, len(processedBlock.Txs))) }