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").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.
fmt.Println("Listening to:", GetExplorerPort(s.Port))
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 ...
func (s *Service) PopulateBlockInfo(from, to int) []*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) {
w.Header().Set("Content-Type", "application/json")
id := r.FormValue("id")
key := GetAddressKey(id)
fmt.Println("Trying to access id=", id, key)
data := &Data{}
if id == "" {
json.NewEncoder(w).Encode(data.TX)
json.NewEncoder(w).Encode(data.Address)
return
}
db := s.storage.GetDB()
bytes, err := db.Get([]byte(GetTXKey(id)))
bytes, err := db.Get([]byte(key))
if err != nil {
json.NewEncoder(w).Encode(data.TX)
json.NewEncoder(w).Encode(data.Address)
return
}
tx := new(Transaction)
if rlp.DecodeBytes(bytes, tx) != nil {
json.NewEncoder(w).Encode(data.TX)
fmt.Println("retreieve from the key", len(bytes))
var addressAccount Address
if err = rlp.DecodeBytes(bytes, &addressAccount); err != nil {
fmt.Println(err)
json.NewEncoder(w).Encode(data.Address)
return
}
data.TX = *tx
json.NewEncoder(w).Encode(data.TX)
fmt.Println("convert address", id)
data.Address = addressAccount
json.NewEncoder(w).Encode(data.Address)
}

@ -148,19 +148,29 @@ func (storage *Storage) UpdateAddressStorage(explorerTransaction Transaction, tx
toAddress := tx.To().Hex()
key := GetAddressKey(toAddress)
fmt.Println("dumping address", toAddress, key)
var addressAccount Address
if data, err := storage.db.Get([]byte(key)); err == nil {
fmt.Println("the key existed")
err = rlp.DecodeBytes(data, addressAccount)
if err == nil {
addressAccount.Balance += float64(tx.Value().Int64())
addressAccount.TXCount++
addressAccount.Balance.Add(addressAccount.Balance, tx.Value())
txCount, _ := strconv.Atoi(addressAccount.TXCount)
addressAccount.TXCount = strconv.Itoa(txCount + 1)
}
} else {
addressAccount.Balance = float64(tx.Value().Int64())
addressAccount.TXCount = 1
fmt.Println("the key not existed")
addressAccount.Balance = tx.Value()
addressAccount.TXCount = "1"
}
addressAccount.ID = toAddress
addressAccount.TXs = append(addressAccount.TXs, explorerTransaction)
encoded, _ := rlp.EncodeToBytes(addressAccount)
storage.db.Put([]byte(key), encoded)
fmt.Println("trying to encode it")
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
import "math/big"
/*
* All the code here is work of progress for the sprint.
*/
@ -15,8 +17,8 @@ type Data struct {
// Address ...
type Address struct {
ID string `json:"id"`
Balance float64 `json:"balance"`
TXCount int `json:"txCount"`
Balance *big.Int `json:"balance"`
TXCount string `json:"txCount"`
TXs []Transaction `json:"txs"`
}

Loading…
Cancel
Save