tendermint/rpc/core/mempool.go

99 lines
2.8 KiB
Go
Raw Normal View History

package core
import (
"fmt"
2016-06-27 17:43:09 -07:00
"time"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/types"
2016-02-08 00:48:58 -08:00
tmsp "github.com/tendermint/tmsp/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)
}
2015-11-01 11:34:08 -08:00
return &ctypes.ResultBroadcastTx{}, 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) {
resCh := make(chan *tmsp.Response, 1)
2016-10-14 18:36:42 -07:00
err := mempool.CheckTx(tx, func(res *tmsp.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,
2016-02-08 00:48:58 -08:00
}, nil
}
// CONTRACT: only returns error if mempool.BroadcastTx errs (ie. problem with the app)
// or if we timeout waiting for tx to commit
func BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
2016-06-27 17:43:09 -07:00
// subscribe to tx being committed in block
appendTxResCh := make(chan types.EventDataTx, 1)
2016-10-09 23:58:13 -07:00
types.AddListenerForEvent(eventSwitch, "rpc", types.EventStringTx(tx), func(data types.TMEventData) {
appendTxResCh <- data.(types.EventDataTx)
2016-06-27 17:43:09 -07:00
})
// broadcast the tx and register checktx callback
checkTxResCh := make(chan *tmsp.Response, 1)
2016-10-14 18:36:42 -07:00
err := mempool.CheckTx(tx, func(res *tmsp.Response) {
2016-06-27 17:43:09 -07:00
checkTxResCh <- res
})
if err != nil {
return nil, fmt.Errorf("Error broadcasting transaction: %v", err)
}
checkTxRes := <-checkTxResCh
checkTxR := checkTxRes.GetCheckTx()
if checkTxR.Code != tmsp.CodeType_OK {
2016-06-27 17:43:09 -07:00
// CheckTx failed!
return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxR,
AppendTx: nil,
}, nil
2016-06-27 17:43:09 -07:00
}
// Wait for the tx to be included in a block,
// timeout after something reasonable.
timer := time.NewTimer(60 * 5 * time.Second)
select {
case appendTxRes := <-appendTxResCh:
// The tx was included in a block.
appendTxR := appendTxRes.GetAppendTx()
return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxR,
AppendTx: appendTxR,
2016-06-27 17:43:09 -07:00
}, nil
case <-timer.C:
return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxR,
AppendTx: nil,
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
}