From 9e0319ef769ea3745048b71686c0d92e5c3a1531 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 11 Jan 2015 23:12:33 -0800 Subject: [PATCH] get_account and sequence updating --- account/priv_account.go | 6 +++-- rpc/accounts.go | 59 ++++++++++++++++++++++++++++++----------- rpc/blocks.go | 34 +++++++----------------- rpc/http_handlers.go | 5 ++-- state/state.go | 8 +++--- 5 files changed, 64 insertions(+), 48 deletions(-) diff --git a/account/priv_account.go b/account/priv_account.go index a8c8532b2..4b4107d8e 100644 --- a/account/priv_account.go +++ b/account/priv_account.go @@ -7,6 +7,7 @@ import ( ) type PrivAccount struct { + Address []byte PubKey PubKey PrivKey PrivKey } @@ -14,9 +15,10 @@ type PrivAccount struct { // Generates a new account with private key. func GenPrivAccount() *PrivAccount { privKey := CRandBytes(32) - pubKey := ed25519.MakePubKey(privKey) + pubKey := PubKeyEd25519(ed25519.MakePubKey(privKey)) return &PrivAccount{ - PubKeyEd25519(pubKey), + pubKey.Address(), + pubKey, PrivKeyEd25519{ PubKey: pubKey, PrivKey: privKey, diff --git a/rpc/accounts.go b/rpc/accounts.go index d93b64f70..2f0e29799 100644 --- a/rpc/accounts.go +++ b/rpc/accounts.go @@ -19,30 +19,23 @@ func GenPrivAccountHandler(w http.ResponseWriter, r *http.Request) { //----------------------------------------------------------------------------- -func SignSendTxHandler(w http.ResponseWriter, r *http.Request) { - sendTxStr := GetParam(r, "sendTx") - privAccountsStr := GetParam(r, "privAccounts") +func GetAccountHandler(w http.ResponseWriter, r *http.Request) { + addressStr := GetParam(r, "address") + var address []byte var err error - sendTx := binary.ReadJSON(&block.SendTx{}, []byte(sendTxStr), &err).(*block.SendTx) + binary.ReadJSON(&address, []byte(addressStr), &err) if err != nil { - WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid sendTx: %v", err)) - return - } - privAccounts := binary.ReadJSON([]*account.PrivAccount{}, []byte(privAccountsStr), &err).([]*account.PrivAccount) - if err != nil { - WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid privAccounts: %v", err)) + WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid address: %v", err)) return } - for i, input := range sendTx.Inputs { - input.PubKey = privAccounts[i].PubKey - input.Signature = privAccounts[i].Sign(sendTx) - } + state := consensusState.GetState() + account_ := state.GetAccount(address) WriteAPIResponse(w, API_OK, struct { - SendTx *block.SendTx - }{sendTx}) + Account *account.Account + }{account_}) } //----------------------------------------------------------------------------- @@ -62,3 +55,37 @@ func ListAccountsHandler(w http.ResponseWriter, r *http.Request) { Accounts []*account.Account }{blockHeight, accounts}) } + +//----------------------------------------------------------------------------- + +func SignSendTxHandler(w http.ResponseWriter, r *http.Request) { + sendTxStr := GetParam(r, "sendTx") + privAccountsStr := GetParam(r, "privAccounts") + + var err error + sendTx := binary.ReadJSON(&block.SendTx{}, []byte(sendTxStr), &err).(*block.SendTx) + if err != nil { + WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid sendTx: %v", err)) + return + } + privAccounts := binary.ReadJSON([]*account.PrivAccount{}, []byte(privAccountsStr), &err).([]*account.PrivAccount) + if err != nil { + WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid privAccounts: %v", err)) + return + } + for i, privAccount := range privAccounts { + if privAccount == nil || privAccount.PrivKey == nil { + WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid (empty) privAccount @%v", i)) + return + } + } + + for i, input := range sendTx.Inputs { + input.PubKey = privAccounts[i].PubKey + input.Signature = privAccounts[i].Sign(sendTx) + } + + WriteAPIResponse(w, API_OK, struct { + SendTx *block.SendTx + }{sendTx}) +} diff --git a/rpc/blocks.go b/rpc/blocks.go index 57be068ea..6427b4e28 100644 --- a/rpc/blocks.go +++ b/rpc/blocks.go @@ -7,11 +7,6 @@ import ( . "github.com/tendermint/tendermint/common" ) -type BlockchainInfoResponse struct { - LastHeight uint - BlockMetas []*BlockMeta -} - func BlockchainInfoHandler(w http.ResponseWriter, r *http.Request) { minHeight, _ := GetParamUint(r, "min_height") maxHeight, _ := GetParamUint(r, "max_height") @@ -31,23 +26,15 @@ func BlockchainInfoHandler(w http.ResponseWriter, r *http.Request) { blockMetas = append(blockMetas, blockMeta) } - res := BlockchainInfoResponse{ - LastHeight: blockStore.Height(), - BlockMetas: blockMetas, - } - - WriteAPIResponse(w, API_OK, res) - return + WriteAPIResponse(w, API_OK, struct { + LastHeight uint + BlockMetas []*BlockMeta + }{blockStore.Height(), blockMetas}) } //----------------------------------------------------------------------------- -type BlockResponse struct { - BlockMeta *BlockMeta - Block *Block -} - -func BlockHandler(w http.ResponseWriter, r *http.Request) { +func GetBlockHandler(w http.ResponseWriter, r *http.Request) { height, _ := GetParamUint(r, "height") if height == 0 { WriteAPIResponse(w, API_INVALID_PARAM, "height must be greater than 1") @@ -60,10 +47,9 @@ func BlockHandler(w http.ResponseWriter, r *http.Request) { blockMeta := blockStore.LoadBlockMeta(height) block := blockStore.LoadBlock(height) - res := BlockResponse{ - BlockMeta: blockMeta, - Block: block, - } - WriteAPIResponse(w, API_OK, res) - return + + WriteAPIResponse(w, API_OK, struct { + BlockMeta *BlockMeta + Block *Block + }{blockMeta, block}) } diff --git a/rpc/http_handlers.go b/rpc/http_handlers.go index 64c0731a1..fa4caab26 100644 --- a/rpc/http_handlers.go +++ b/rpc/http_handlers.go @@ -6,10 +6,11 @@ import ( func initHandlers() { http.HandleFunc("/blockchain", BlockchainInfoHandler) - http.HandleFunc("/block", BlockHandler) + http.HandleFunc("/get_block", GetBlockHandler) http.HandleFunc("/broadcast_tx", BroadcastTxHandler) http.HandleFunc("/gen_priv_account", GenPrivAccountHandler) - http.HandleFunc("/sign_send_tx", SignSendTxHandler) + http.HandleFunc("/get_account", GetAccountHandler) http.HandleFunc("/list_accounts", ListAccountsHandler) + http.HandleFunc("/sign_send_tx", SignSendTxHandler) http.HandleFunc("/list_validators", ListValidatorsHandler) } diff --git a/state/state.go b/state/state.go index 5ba6aacc5..0477568f1 100644 --- a/state/state.go +++ b/state/state.go @@ -167,10 +167,6 @@ func (s *State) ValidateInputs(accounts map[string]*Account, signBytes []byte, i if err := in.ValidateBasic(); err != nil { return 0, err } - // Check amount - if account.Balance < in.Amount { - return 0, ErrTxInsufficientFunds - } // Check signatures if !account.PubKey.VerifyBytes(signBytes, in.Signature) { return 0, ErrTxInvalidSignature @@ -179,6 +175,10 @@ func (s *State) ValidateInputs(accounts map[string]*Account, signBytes []byte, i if account.Sequence+1 != in.Sequence { return 0, ErrTxInvalidSequence } + // Check amount + if account.Balance < in.Amount { + return 0, ErrTxInsufficientFunds + } // Good. Add amount to total total += in.Amount }