コード例 #1
0
ファイル: main.go プロジェクト: dyzz/gobtclib
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.")
}
コード例 #2
0
ファイル: main.go プロジェクト: libreoscar/btcwatch
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)))
}