add address service

pull/169/head
Minh Doan 6 years ago committed by Minh Doan
parent 45c74f8d16
commit d6d2f74a1f
  1. 60
      services/explorer/service.go
  2. 22
      services/explorer/storage.go
  3. 6
      services/explorer/structs.go

@ -56,45 +56,15 @@ func (s *Service) Run() {
s.router.Path("/tx").Queries("id", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.GetExplorerTransaction).Methods("GET") s.router.Path("/tx").Queries("id", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.GetExplorerTransaction).Methods("GET")
s.router.Path("/tx").HandlerFunc(s.GetExplorerTransaction) s.router.Path("/tx").HandlerFunc(s.GetExplorerTransaction)
s.router.Path("/address").Queries("id", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.GetExplorerAddress).Methods("GET")
s.router.Path("/address").HandlerFunc(s.GetExplorerAddress)
// Do serving now. // Do serving now.
fmt.Println("Listening to:", GetExplorerPort(s.Port)) fmt.Println("Listening to:", GetExplorerPort(s.Port))
log.Fatal(http.ListenAndServe(addr, s.router)) log.Fatal(http.ListenAndServe(addr, s.router))
} }
// GetExplorerBlockInfo ...
// func (s *Service) GetExplorerBlockInfo(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Content-Type", "application/json")
// from := r.FormValue("from")
// to := r.FormValue("to")
// data := &Data{
// Blocks: []*BlockInfo{},
// }
// if from == "" {
// json.NewEncoder(w).Encode(data.Blocks)
// return
// }
// db := s.storage.GetDB()
// fromInt, err := strconv.Atoi(from)
// var toInt int
// if to == "" {
// bytes, err := db.Get([]byte(BlockHeightKey))
// if err == nil {
// toInt, err = strconv.Atoi(string(bytes))
// }
// } else {
// toInt, err = strconv.Atoi(to)
// }
// fmt.Println("from", fromInt, "to", toInt)
// if err != nil {
// json.NewEncoder(w).Encode(data.Blocks)
// return
// }
// data.Blocks = s.PopulateBlockInfo(fromInt, toInt)
// json.NewEncoder(w).Encode(data.Blocks)
// }
// PopulateBlockInfo ... // PopulateBlockInfo ...
func (s *Service) PopulateBlockInfo(from, to int) []*BlockInfo { func (s *Service) PopulateBlockInfo(from, to int) []*BlockInfo {
blocks := []*BlockInfo{} blocks := []*BlockInfo{}
@ -262,23 +232,29 @@ func (s *Service) GetExplorerTransaction(w http.ResponseWriter, r *http.Request)
func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) { func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
id := r.FormValue("id") id := r.FormValue("id")
key := GetAddressKey(id)
fmt.Println("Trying to access id=", id, key)
data := &Data{} data := &Data{}
if id == "" { if id == "" {
json.NewEncoder(w).Encode(data.TX) json.NewEncoder(w).Encode(data.Address)
return return
} }
db := s.storage.GetDB() db := s.storage.GetDB()
bytes, err := db.Get([]byte(GetTXKey(id))) bytes, err := db.Get([]byte(key))
if err != nil { if err != nil {
json.NewEncoder(w).Encode(data.TX) json.NewEncoder(w).Encode(data.Address)
return return
} }
tx := new(Transaction) fmt.Println("retreieve from the key", len(bytes))
if rlp.DecodeBytes(bytes, tx) != nil { var addressAccount Address
json.NewEncoder(w).Encode(data.TX) if err = rlp.DecodeBytes(bytes, &addressAccount); err != nil {
fmt.Println(err)
json.NewEncoder(w).Encode(data.Address)
return return
} }
data.TX = *tx fmt.Println("convert address", id)
json.NewEncoder(w).Encode(data.TX) data.Address = addressAccount
json.NewEncoder(w).Encode(data.Address)
} }

@ -148,19 +148,29 @@ func (storage *Storage) UpdateAddressStorage(explorerTransaction Transaction, tx
toAddress := tx.To().Hex() toAddress := tx.To().Hex()
key := GetAddressKey(toAddress) key := GetAddressKey(toAddress)
fmt.Println("dumping address", toAddress, key)
var addressAccount Address var addressAccount Address
if data, err := storage.db.Get([]byte(key)); err == nil { if data, err := storage.db.Get([]byte(key)); err == nil {
fmt.Println("the key existed")
err = rlp.DecodeBytes(data, addressAccount) err = rlp.DecodeBytes(data, addressAccount)
if err == nil { if err == nil {
addressAccount.Balance += float64(tx.Value().Int64()) addressAccount.Balance.Add(addressAccount.Balance, tx.Value())
addressAccount.TXCount++ txCount, _ := strconv.Atoi(addressAccount.TXCount)
addressAccount.TXCount = strconv.Itoa(txCount + 1)
} }
} else { } else {
addressAccount.Balance = float64(tx.Value().Int64()) fmt.Println("the key not existed")
addressAccount.TXCount = 1 addressAccount.Balance = tx.Value()
addressAccount.TXCount = "1"
} }
addressAccount.ID = toAddress addressAccount.ID = toAddress
addressAccount.TXs = append(addressAccount.TXs, explorerTransaction) addressAccount.TXs = append(addressAccount.TXs, explorerTransaction)
encoded, _ := rlp.EncodeToBytes(addressAccount) fmt.Println("trying to encode it")
storage.db.Put([]byte(key), encoded) if encoded, err := rlp.EncodeToBytes(addressAccount); err == nil {
fmt.Println("store addressAccount with length ", len(encoded))
storage.db.Put([]byte(key), encoded)
} else {
fmt.Println("err when encoding ", err)
}
} }

@ -1,5 +1,7 @@
package explorer package explorer
import "math/big"
/* /*
* All the code here is work of progress for the sprint. * All the code here is work of progress for the sprint.
*/ */
@ -15,8 +17,8 @@ type Data struct {
// Address ... // Address ...
type Address struct { type Address struct {
ID string `json:"id"` ID string `json:"id"`
Balance float64 `json:"balance"` Balance *big.Int `json:"balance"`
TXCount int `json:"txCount"` TXCount string `json:"txCount"`
TXs []Transaction `json:"txs"` TXs []Transaction `json:"txs"`
} }

Loading…
Cancel
Save