1.代码来自tendermint
2.代码段如下
go 使用定时器从channel获取结果的写法
timer := time.NewTimer(60 * 2 * time.Second)
select {
case deliverTxResMsg := <-deliverTxResCh:
deliverTxRes := deliverTxResMsg.(types.TMEventData).Unwrap().(types.EventDataTx)
// The tx was included in a block.
deliverTxR := deliverTxRes.Result
logger.Info("DeliverTx passed ", "tx", cmn.HexBytes(tx), "response", deliverTxR)
return &ctypes.ResultBroadcastTxCommit{
CheckTx: *checkTxR,
DeliverTx: deliverTxR,
Hash: tx.Hash(),
Height: deliverTxRes.Height,
}, nil
case <-timer.C://超时
logger.Error("failed to include tx")
return &ctypes.ResultBroadcastTxCommit{
CheckTx: *checkTxR,
DeliverTx: abci.ResponseDeliverTx{},
Hash: tx.Hash(),
}, fmt.Errorf("Timed out waiting for transaction to be included in a block")
}
本文介绍了一个使用Go语言的定时器与通道结合的示例代码,该代码展示了如何在一个区块中包含交易并处理超时情况。通过定时器监听交易结果通道,实现了对交易是否被成功包含到区块中的逻辑判断。
906

被折叠的 条评论
为什么被折叠?



