tendermint/rpc/core/mempool.go

113 lines
3.6 KiB
Go
Raw Normal View History

package core
import (
"fmt"
2016-06-27 17:43:09 -07:00
"time"
"github.com/tendermint/go-events"
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) {
err := mempoolReactor.BroadcastTx(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-02-08 00:48:58 -08:00
err := mempoolReactor.BroadcastTx(tx, func(res *tmsp.Response) {
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
}
2016-06-27 17:43:09 -07:00
// CONTRACT: returns error==nil iff the tx is included in a block.
//
// If CheckTx fails, return with the response from CheckTx AND an error.
// Else, block until the tx is included in a block,
// and return the result of AppendTx (with no error).
// Even if AppendTx fails, so long as the tx is included in a block this function
2016-07-05 11:41:50 -07:00
// will not return an error - it is the caller's responsibility to check res.Code.
2016-06-27 17:43:09 -07:00
// The function times out after five minutes and returns the result of CheckTx and an error.
// TODO: smarter timeout logic or someway to cancel (tx not getting committed is a sign of a larger problem!)
func BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
// subscribe to tx being committed in block
appendTxResCh := make(chan *tmsp.Response, 1)
eventSwitch.AddListenerForEvent("rpc", types.EventStringTx(tx), func(data events.EventData) {
appendTxResCh <- data.(*tmsp.Response)
})
// broadcast the tx and register checktx callback
checkTxResCh := make(chan *tmsp.Response, 1)
err := mempoolReactor.BroadcastTx(tx, func(res *tmsp.Response) {
checkTxResCh <- res
})
if err != nil {
return nil, fmt.Errorf("Error broadcasting transaction: %v", err)
}
checkTxRes := <-checkTxResCh
checkTxR := checkTxRes.GetCheckTx()
if r := checkTxR; r.Code != tmsp.CodeType_OK {
// CheckTx failed!
return &ctypes.ResultBroadcastTx{
Code: r.Code,
Data: r.Data,
Log: r.Log,
}, fmt.Errorf("Check tx failed with non-zero code: %s. Data: %X; Log: %s", r.Code.String(), r.Data, r.Log)
}
// 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.
// NOTE we don't return an error regardless of the AppendTx code;
// clients must check this to see if they need to send a new tx!
r := appendTxRes.GetAppendTx()
return &ctypes.ResultBroadcastTx{
Code: r.Code,
Data: r.Data,
Log: r.Log,
}, nil
case <-timer.C:
r := checkTxR
return &ctypes.ResultBroadcastTx{
Code: r.Code,
Data: r.Data,
Log: r.Log,
}, 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-07-12 11:58:06 -07:00
txs := mempoolReactor.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-03-07 15:38:05 -08:00
return &ctypes.ResultUnconfirmedTxs{N: mempoolReactor.Mempool.Size()}, nil
}