tendermint/rpc/core/mempool.go

113 lines
3.3 KiB
Go
Raw Normal View History

package core
import (
"fmt"
2016-06-27 17:43:09 -07:00
"time"
abci "github.com/tendermint/abci/types"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/types"
)
//-----------------------------------------------------------------------------
2016-06-27 17:43:09 -07:00
// NOTE: tx should be signed, but this is only checked at the app level (not by Tendermint!)
2016-06-27 17:43:09 -07:00
// Returns right away, with no response
2016-02-08 00:48:58 -08:00
func BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
2016-10-14 18:36:42 -07:00
err := mempool.CheckTx(tx, nil)
if err != nil {
return nil, fmt.Errorf("Error broadcasting transaction: %v", err)
}
return &ctypes.ResultBroadcastTx{TxID: tx.Hash()}, nil
}
2016-06-27 17:43:09 -07:00
// Returns with the response from CheckTx
2016-02-08 00:48:58 -08:00
func BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
2017-01-12 12:53:32 -08:00
resCh := make(chan *abci.Response, 1)
err := mempool.CheckTx(tx, func(res *abci.Response) {
2016-02-08 00:48:58 -08:00
resCh <- res
})
if err != nil {
return nil, fmt.Errorf("Error broadcasting transaction: %v", err)
}
res := <-resCh
2016-05-14 09:33:27 -07:00
r := res.GetCheckTx()
2016-02-08 00:48:58 -08:00
return &ctypes.ResultBroadcastTx{
2016-05-14 09:33:27 -07:00
Code: r.Code,
Data: r.Data,
Log: r.Log,
TxID: tx.Hash(),
2016-02-08 00:48:58 -08:00
}, nil
}
// CONTRACT: only returns error if mempool.BroadcastTx errs (ie. problem with the app)
2016-11-30 14:28:41 -08:00
// or if we timeout waiting for tx to commit.
2017-01-12 12:55:03 -08:00
// If CheckTx or DeliverTx fail, no error will be returned, but the returned result
2017-01-12 12:53:32 -08:00
// will contain a non-OK ABCI code.
func BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
2016-06-27 17:43:09 -07:00
// subscribe to tx being committed in block
2017-01-12 12:55:03 -08:00
deliverTxResCh := make(chan types.EventDataTx, 1)
2016-10-09 23:58:13 -07:00
types.AddListenerForEvent(eventSwitch, "rpc", types.EventStringTx(tx), func(data types.TMEventData) {
2017-01-12 12:55:03 -08:00
deliverTxResCh <- data.(types.EventDataTx)
2016-06-27 17:43:09 -07:00
})
// broadcast the tx and register checktx callback
2017-01-12 12:53:32 -08:00
checkTxResCh := make(chan *abci.Response, 1)
err := mempool.CheckTx(tx, func(res *abci.Response) {
2016-06-27 17:43:09 -07:00
checkTxResCh <- res
})
if err != nil {
2016-11-30 14:28:41 -08:00
log.Error("err", "err", err)
2016-06-27 17:43:09 -07:00
return nil, fmt.Errorf("Error broadcasting transaction: %v", err)
}
checkTxRes := <-checkTxResCh
checkTxR := checkTxRes.GetCheckTx()
2017-01-12 12:53:32 -08:00
if checkTxR.Code != abci.CodeType_OK {
2016-06-27 17:43:09 -07:00
// CheckTx failed!
return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxR,
2017-01-12 12:55:03 -08:00
DeliverTx: nil,
TxID: tx.Hash(),
}, nil
2016-06-27 17:43:09 -07:00
}
// Wait for the tx to be included in a block,
// timeout after something reasonable.
2016-11-30 14:28:41 -08:00
// TODO: configureable?
timer := time.NewTimer(60 * 2 * time.Second)
2016-06-27 17:43:09 -07:00
select {
2017-01-12 12:55:03 -08:00
case deliverTxRes := <-deliverTxResCh:
2016-06-27 17:43:09 -07:00
// The tx was included in a block.
2017-01-12 12:55:03 -08:00
deliverTxR := &abci.ResponseDeliverTx{
Code: deliverTxRes.Code,
Data: deliverTxRes.Data,
Log: deliverTxRes.Log,
2016-11-30 14:28:41 -08:00
}
2017-01-12 12:55:03 -08:00
log.Notice("DeliverTx passed ", "tx", []byte(tx), "response", deliverTxR)
return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxR,
2017-01-12 12:55:03 -08:00
DeliverTx: deliverTxR,
TxID: tx.Hash(),
2016-06-27 17:43:09 -07:00
}, nil
case <-timer.C:
2016-11-30 14:28:41 -08:00
log.Error("failed to include tx")
return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxR,
2017-01-12 12:55:03 -08:00
DeliverTx: nil,
TxID: tx.Hash(),
2016-06-27 17:43:09 -07:00
}, fmt.Errorf("Timed out waiting for transaction to be included in a block")
}
panic("Should never happen!")
}
2016-02-08 00:48:58 -08:00
func UnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) {
2016-10-14 18:36:42 -07:00
txs := mempool.Reap(-1)
2016-02-14 17:00:33 -08:00
return &ctypes.ResultUnconfirmedTxs{len(txs), txs}, nil
2015-04-25 13:26:36 -07:00
}
func NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) {
2016-10-14 18:36:42 -07:00
return &ctypes.ResultUnconfirmedTxs{N: mempool.Size()}, nil
}