From 4424a85fbde1ba1abd614a22b3e73b87f516302e Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 30 Nov 2014 18:31:44 -0800 Subject: [PATCH] start writing rpc --- .gitignore | 1 + consensus/state.go | 1 + rpc/blocks.go | 14 ++++ rpc/{handler.go => http_handler.go} | 1 + rpc/http_params.go | 115 ++++++++++++++++++++++++++++ rpc/http_server.go | 3 +- 6 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 rpc/blocks.go rename rpc/{handler.go => http_handler.go} (99%) create mode 100644 rpc/http_params.go diff --git a/.gitignore b/.gitignore index 98bb6db8..1c593002 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.swp +*.swo .bak tendermint diff --git a/consensus/state.go b/consensus/state.go index d798487a..1c2ee777 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -817,6 +817,7 @@ func (cs *ConsensusState) AddProposalBlockPart(height uint32, round uint16, part var err error cs.ProposalBlock = ReadBlock(cs.ProposalBlockParts.GetReader(), &n, &err) cs.queueAction(RoundAction{cs.Height, cs.Round, RoundActionTryFinalize}) + // XXX If POL is valid, consider unlocking. return true, err } return true, nil diff --git a/rpc/blocks.go b/rpc/blocks.go new file mode 100644 index 00000000..5359e825 --- /dev/null +++ b/rpc/blocks.go @@ -0,0 +1,14 @@ +// Maybe move this to blocks/handler.go +package rpc + +import ( + "net/http" + //. "github.com/tendermint/tendermint/blocks" +) + +func BlockHandler(w http.ResponseWriter, r *http.Request) { + //height, _ := GetParamUint64Safe(r, "height") + //count, _ := GetParamUint64Safe(r, "count") + + ReturnJSON(API_OK, "hello") +} diff --git a/rpc/handler.go b/rpc/http_handler.go similarity index 99% rename from rpc/handler.go rename to rpc/http_handler.go index 67c2a404..8780e9bf 100644 --- a/rpc/handler.go +++ b/rpc/http_handler.go @@ -1,3 +1,4 @@ +// Commons for HTTP handling package rpc import ( diff --git a/rpc/http_params.go b/rpc/http_params.go new file mode 100644 index 00000000..1ad23f2a --- /dev/null +++ b/rpc/http_params.go @@ -0,0 +1,115 @@ +package rpc + +import ( + "fmt" + "net/http" + "regexp" + "strconv" + + . "github.com/tendermint/tendermint/common" +) + +var ( + // Parts of regular expressions + atom = "[A-Z0-9!#$%&'*+\\-/=?^_`{|}~]+" + dotAtom = atom + `(?:\.` + atom + `)*` + domain = `[A-Z0-9.-]+\.[A-Z]{2,4}` + + RE_HEX = regexp.MustCompile(`^(?i)[a-f0-9]+$`) + RE_EMAIL = regexp.MustCompile(`^(?i)(` + dotAtom + `)@(` + dotAtom + `)$`) + RE_ADDRESS = regexp.MustCompile(`^(?i)[a-z0-9]{25,34}$`) + RE_HOST = regexp.MustCompile(`^(?i)(` + domain + `)$`) + + //RE_ID12 = regexp.MustCompile(`^[a-zA-Z0-9]{12}$`) +) + +func panicAPI(err error) { + panic(APIResponse{API_INVALID_PARAM, err.Error()}) +} + +func GetParam(r *http.Request, param string) string { + s := r.URL.Query().Get(param) + if s == "" { + s = r.FormValue(param) + } + return s +} + +func GetParamInt64Safe(r *http.Request, param string) (int64, error) { + s := GetParam(r, param) + i, err := strconv.ParseInt(s, 10, 64) + if err != nil { + return 0, Errorf(param, err.Error()) + } + 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) { + s := GetParam(r, param) + i, err := strconv.ParseInt(s, 10, 32) + if err != nil { + return 0, Errorf(param, err.Error()) + } + 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) { + s := GetParam(r, param) + i, err := strconv.ParseUint(s, 10, 64) + if err != nil { + return 0, Errorf(param, err.Error()) + } + 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) { + s := GetParam(r, param) + if !re.MatchString(s) { + return "", Errorf(param, "Did not match regular expression %v", re.String()) + } + 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) { + s := GetParam(r, param) + f, err := strconv.ParseFloat(s, 64) + if err != nil { + return 0, Errorf(param, err.Error()) + } + return f, nil +} +func GetParamFloat64(r *http.Request, param string) float64 { + f, err := GetParamFloat64Safe(r, param) + if err != nil { + panicAPI(err) + } + return f +} diff --git a/rpc/http_server.go b/rpc/http_server.go index 4c3d6a92..12709130 100644 --- a/rpc/http_server.go +++ b/rpc/http_server.go @@ -9,8 +9,7 @@ import ( func StartHTTPServer() { - //http.HandleFunc("/path", handler) - //http.HandleFunc("/path", handler) + http.HandleFunc("/block", BlockHandler) // Serve HTTP on localhost only. // Let something like Nginx handle HTTPS connections.