silly tx RPC command
This commit is contained in:
parent
2fd137d9e5
commit
0d075c7592
|
@ -22,6 +22,7 @@ type Node struct {
|
||||||
pexReactor *p2p.PEXReactor
|
pexReactor *p2p.PEXReactor
|
||||||
mempoolReactor *mempool_.MempoolReactor
|
mempoolReactor *mempool_.MempoolReactor
|
||||||
consensusReactor *consensus.ConsensusReactor
|
consensusReactor *consensus.ConsensusReactor
|
||||||
|
state *state_.State
|
||||||
privValidator *state_.PrivValidator
|
privValidator *state_.PrivValidator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +67,7 @@ func NewNode() *Node {
|
||||||
pexReactor: pexReactor,
|
pexReactor: pexReactor,
|
||||||
mempoolReactor: mempoolReactor,
|
mempoolReactor: mempoolReactor,
|
||||||
consensusReactor: consensusReactor,
|
consensusReactor: consensusReactor,
|
||||||
|
state: state,
|
||||||
privValidator: privValidator,
|
privValidator: privValidator,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -136,6 +138,8 @@ func daemon() {
|
||||||
|
|
||||||
// Run the RPC server.
|
// Run the RPC server.
|
||||||
if config.Config.RPC.HTTPPort != 0 {
|
if config.Config.RPC.HTTPPort != 0 {
|
||||||
|
rpc.SetRPCState(n.state)
|
||||||
|
rpc.SetRPCMempoolReactor(n.mempoolReactor)
|
||||||
rpc.StartHTTPServer()
|
rpc.StartHTTPServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
// Maybe move this to blocks/handler.go
|
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -34,7 +35,12 @@ func GetParam(r *http.Request, param string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetParamInt64Safe(r *http.Request, param string) (int64, error) {
|
func GetParamByteSlice(r *http.Request, param string) ([]byte, error) {
|
||||||
|
s := GetParam(r, param)
|
||||||
|
return hex.DecodeString(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetParamInt64(r *http.Request, param string) (int64, error) {
|
||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
i, err := strconv.ParseInt(s, 10, 64)
|
i, err := strconv.ParseInt(s, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -42,15 +48,8 @@ func GetParamInt64Safe(r *http.Request, param string) (int64, error) {
|
||||||
}
|
}
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
func GetParamInt64(r *http.Request, param string) int64 {
|
|
||||||
i, err := GetParamInt64Safe(r, param)
|
|
||||||
if err != nil {
|
|
||||||
panicAPI(err)
|
|
||||||
}
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetParamInt32Safe(r *http.Request, param string) (int32, error) {
|
func GetParamInt32(r *http.Request, param string) (int32, error) {
|
||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
i, err := strconv.ParseInt(s, 10, 32)
|
i, err := strconv.ParseInt(s, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -58,15 +57,8 @@ func GetParamInt32Safe(r *http.Request, param string) (int32, error) {
|
||||||
}
|
}
|
||||||
return int32(i), nil
|
return int32(i), nil
|
||||||
}
|
}
|
||||||
func GetParamInt32(r *http.Request, param string) int32 {
|
|
||||||
i, err := GetParamInt32Safe(r, param)
|
|
||||||
if err != nil {
|
|
||||||
panicAPI(err)
|
|
||||||
}
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetParamUint64Safe(r *http.Request, param string) (uint64, error) {
|
func GetParamUint64(r *http.Request, param string) (uint64, error) {
|
||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
i, err := strconv.ParseUint(s, 10, 64)
|
i, err := strconv.ParseUint(s, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -74,30 +66,16 @@ func GetParamUint64Safe(r *http.Request, param string) (uint64, error) {
|
||||||
}
|
}
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
func GetParamUint64(r *http.Request, param string) uint64 {
|
|
||||||
i, err := GetParamUint64Safe(r, param)
|
|
||||||
if err != nil {
|
|
||||||
panicAPI(err)
|
|
||||||
}
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetParamRegexpSafe(r *http.Request, param string, re *regexp.Regexp) (string, error) {
|
func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp) (string, error) {
|
||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
if !re.MatchString(s) {
|
if !re.MatchString(s) {
|
||||||
return "", Errorf(param, "Did not match regular expression %v", re.String())
|
return "", Errorf(param, "Did not match regular expression %v", re.String())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp, required bool) string {
|
|
||||||
s, err := GetParamRegexpSafe(r, param, re)
|
|
||||||
if (required || s != "") && err != nil {
|
|
||||||
panicAPI(err)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetParamFloat64Safe(r *http.Request, param string) (float64, error) {
|
func GetParamFloat64(r *http.Request, param string) (float64, error) {
|
||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
f, err := strconv.ParseFloat(s, 64)
|
f, err := strconv.ParseFloat(s, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -105,10 +83,3 @@ func GetParamFloat64Safe(r *http.Request, param string) (float64, error) {
|
||||||
}
|
}
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
func GetParamFloat64(r *http.Request, param string) float64 {
|
|
||||||
f, err := GetParamFloat64Safe(r, param)
|
|
||||||
if err != nil {
|
|
||||||
panicAPI(err)
|
|
||||||
}
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
func StartHTTPServer() {
|
func StartHTTPServer() {
|
||||||
|
|
||||||
http.HandleFunc("/block", BlockHandler)
|
http.HandleFunc("/block", BlockHandler)
|
||||||
|
http.HandleFunc("/mempool", MempoolHandler)
|
||||||
|
|
||||||
// Serve HTTP on localhost only.
|
// Serve HTTP on localhost only.
|
||||||
// Let something like Nginx handle HTTPS connections.
|
// Let something like Nginx handle HTTPS connections.
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/tendermint/tendermint/block"
|
||||||
|
. "github.com/tendermint/tendermint/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MempoolHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
//height, _ := GetParamUint64Safe(r, "height")
|
||||||
|
//count, _ := GetParamUint64Safe(r, "count")
|
||||||
|
txBytes, err := GetParamByteSlice(r, "tx_bytes")
|
||||||
|
if err != nil {
|
||||||
|
ReturnJSON(API_INVALID_PARAM, Fmt("Invalid tx_bytes: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, n := bytes.NewReader(txBytes), new(int64)
|
||||||
|
tx := block.TxDecoder(reader, n, &err).(block.Tx)
|
||||||
|
if err != nil {
|
||||||
|
ReturnJSON(API_INVALID_PARAM, Fmt("Invalid tx_bytes: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = mempoolReactor.BroadcastTx(tx)
|
||||||
|
if err != nil {
|
||||||
|
ReturnJSON(API_ERROR, Fmt("Error broadcasting transaction: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnJSON(API_OK, Fmt("Broadcasted tx: %X", tx))
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
curl --data 'tx_bytes=0101146070FF17C39B2B0A64CA2BC431328037FA0F476064000000000000000001407D28F5CEE2065FCB2952CA9B99E9F9855E992B0FA5862442F582F2A84C3B3B31154A86D54DD548AFF080697BDC15AF26E68416AA678EF29449BB8D273B73320502206BD490C212E701A2136EEEA04F06FA4F287EE47E2B7A9B5D62EDD84CD6AD975301146070FF17C39B2B0A64CA2BC431328037FA0F47606400000000000000' -H 'content-type: text/plain;' http://127.0.0.1:8888/mempool
|
||||||
|
tx: 0101146070FF17C39B2B0A64CA2BC431328037FA0F476064000000000000000001407D28F5CEE2065FCB2952CA9B99E9F9855E992B0FA5862442F582F2A84C3B3B31154A86D54DD548AFF080697BDC15AF26E68416AA678EF29449BB8D273B73320502206BD490C212E701A2136EEEA04F06FA4F287EE47E2B7A9B5D62EDD84CD6AD975301146070FF17C39B2B0A64CA2BC431328037FA0F47606400000000000000
|
||||||
|
*/
|
|
@ -0,0 +1,17 @@
|
||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
mempool_ "github.com/tendermint/tendermint/mempool"
|
||||||
|
state_ "github.com/tendermint/tendermint/state"
|
||||||
|
)
|
||||||
|
|
||||||
|
var state *state_.State
|
||||||
|
var mempoolReactor *mempool_.MempoolReactor
|
||||||
|
|
||||||
|
func SetRPCState(state__ *state_.State) {
|
||||||
|
state = state__
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetRPCMempoolReactor(mempoolReactor_ *mempool_.MempoolReactor) {
|
||||||
|
mempoolReactor = mempoolReactor_
|
||||||
|
}
|
Loading…
Reference in New Issue