From 33aff6b26b54eba52025e2633e0c767d9786f65c Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 11 Jan 2015 18:21:17 -0800 Subject: [PATCH] list_validators API --- rpc/{account.go => accounts.go} | 37 +++++++++++++-------------------- rpc/http_handlers.go | 3 ++- rpc/validators.go | 30 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 23 deletions(-) rename rpc/{account.go => accounts.go} (75%) create mode 100644 rpc/validators.go diff --git a/rpc/account.go b/rpc/accounts.go similarity index 75% rename from rpc/account.go rename to rpc/accounts.go index f05ada22f..d93b64f70 100644 --- a/rpc/account.go +++ b/rpc/accounts.go @@ -9,25 +9,16 @@ import ( . "github.com/tendermint/tendermint/common" ) -type GenPrivAccountResponse struct { - PrivAccount *account.PrivAccount -} - func GenPrivAccountHandler(w http.ResponseWriter, r *http.Request) { privAccount := account.GenPrivAccount() - res := GenPrivAccountResponse{ - PrivAccount: privAccount, - } - WriteAPIResponse(w, API_OK, res) + WriteAPIResponse(w, API_OK, struct { + PrivAccount *account.PrivAccount + }{privAccount}) } //----------------------------------------------------------------------------- -type SignSendTxResponse struct { - SendTx *block.SendTx -} - func SignSendTxHandler(w http.ResponseWriter, r *http.Request) { sendTxStr := GetParam(r, "sendTx") privAccountsStr := GetParam(r, "privAccounts") @@ -49,23 +40,25 @@ func SignSendTxHandler(w http.ResponseWriter, r *http.Request) { input.Signature = privAccounts[i].Sign(sendTx) } - res := SignSendTxResponse{ - SendTx: sendTx, - } - WriteAPIResponse(w, API_OK, res) + WriteAPIResponse(w, API_OK, struct { + SendTx *block.SendTx + }{sendTx}) } //----------------------------------------------------------------------------- -type ListAccountsResponse struct { - Accounts []*account.Account -} - func ListAccountsHandler(w http.ResponseWriter, r *http.Request) { + var blockHeight uint + var accounts []*account.Account state := consensusState.GetState() + blockHeight = state.LastBlockHeight state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool { - log.Warn(">>", "key", key, "value", value) + accounts = append(accounts, value.(*account.Account)) return false }) - WriteAPIResponse(w, API_OK, state) + + WriteAPIResponse(w, API_OK, struct { + BlockHeight uint + Accounts []*account.Account + }{blockHeight, accounts}) } diff --git a/rpc/http_handlers.go b/rpc/http_handlers.go index 459ba0781..64c0731a1 100644 --- a/rpc/http_handlers.go +++ b/rpc/http_handlers.go @@ -10,5 +10,6 @@ func initHandlers() { http.HandleFunc("/broadcast_tx", BroadcastTxHandler) http.HandleFunc("/gen_priv_account", GenPrivAccountHandler) http.HandleFunc("/sign_send_tx", SignSendTxHandler) - http.HandleFunc("/accounts", ListAccountsHandler) + http.HandleFunc("/list_accounts", ListAccountsHandler) + http.HandleFunc("/list_validators", ListValidatorsHandler) } diff --git a/rpc/validators.go b/rpc/validators.go new file mode 100644 index 000000000..6e1ba6013 --- /dev/null +++ b/rpc/validators.go @@ -0,0 +1,30 @@ +package rpc + +import ( + "net/http" + + state_ "github.com/tendermint/tendermint/state" +) + +func ListValidatorsHandler(w http.ResponseWriter, r *http.Request) { + var blockHeight uint + var bondedValidators []*state_.Validator + var unbondingValidators []*state_.Validator + + state := consensusState.GetState() + blockHeight = state.LastBlockHeight + state.BondedValidators.Iterate(func(index uint, val *state_.Validator) bool { + bondedValidators = append(bondedValidators, val) + return false + }) + state.UnbondingValidators.Iterate(func(index uint, val *state_.Validator) bool { + unbondingValidators = append(unbondingValidators, val) + return false + }) + + WriteAPIResponse(w, API_OK, struct { + BlockHeight uint + BondedValidators []*state_.Validator + UnbondingValidators []*state_.Validator + }{blockHeight, bondedValidators, unbondingValidators}) +}