get_account and sequence updating

This commit is contained in:
Jae Kwon
2015-01-11 23:12:33 -08:00
parent 6cd858add3
commit 9e0319ef76
5 changed files with 64 additions and 48 deletions
+4 -2
View File
@@ -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,
+43 -16
View File
@@ -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})
}
+10 -24
View File
@@ -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})
}
+3 -2
View File
@@ -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)
}
+4 -4
View File
@@ -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
}