cleanup code

pull/200/head
Minh Doan 6 years ago committed by Minh Doan
parent 5863353490
commit 054e27f430
  1. 39
      api/services/explorer/service.go
  2. 17
      api/services/explorer/storage.go
  3. 9
      api/services/explorer/structs.go
  4. 12
      api/services/explorer/test2/main.go

@ -27,7 +27,7 @@ type Service struct {
storage *Storage
}
// GetExplorerPort ...
// GetExplorerPort returns the port serving explorer dashboard. This port is explorerPortDifference less than the node port.
func GetExplorerPort(nodePort string) string {
if port, err := strconv.Atoi(nodePort); err == nil {
return fmt.Sprintf("%d", port-explorerPortDifference)
@ -36,7 +36,7 @@ func GetExplorerPort(nodePort string) string {
return ""
}
// Init is to do init for ExplorerService.
// Init is to initialize for ExplorerService.
func (s *Service) Init(remove bool) {
s.storage = GetStorageInstance(s.IP, s.Port, remove)
}
@ -46,14 +46,16 @@ func (s *Service) Run() {
// Init address.
addr := net.JoinHostPort("", GetExplorerPort(s.Port))
// Set up router
s.router = mux.NewRouter()
// Set up router for blocks.
s.router.Path("/blocks").Queries("from", "{[0-9]*?}", "to", "{[0-9]*?}").HandlerFunc(s.GetExplorerBlocks).Methods("GET")
s.router.Path("/blocks").HandlerFunc(s.GetExplorerBlocks)
// Set up router for tx.
s.router.Path("/tx").Queries("id", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.GetExplorerTransaction).Methods("GET")
s.router.Path("/tx").HandlerFunc(s.GetExplorerTransaction)
// Set up router for address.
s.router.Path("/address").Queries("id", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.GetExplorerAddress).Methods("GET")
s.router.Path("/address").HandlerFunc(s.GetExplorerAddress)
@ -62,28 +64,7 @@ func (s *Service) Run() {
log.Fatal(http.ListenAndServe(addr, s.router))
}
// PopulateBlockInfo ...
func (s *Service) PopulateBlockInfo(from, to int) []*BlockInfo {
blocks := []*BlockInfo{}
for i := from; i <= to; i++ {
key := GetBlockInfoKey(i)
fmt.Println("getting blockinfo with key ", key)
data, err := storage.db.Get([]byte(key))
if err != nil {
Log.Error("Error on getting from db")
os.Exit(1)
}
block := new(BlockInfo)
if rlp.DecodeBytes(data, block) != nil {
Log.Error("Error on getting from db")
os.Exit(1)
}
blocks = append(blocks, block)
}
return blocks
}
// GetAccountBlocks ...
// GetAccountBlocks returns a list of types.Block to server blocks end-point.
func (s *Service) GetAccountBlocks(from, to int) []*types.Block {
blocks := []*types.Block{}
for i := from - 1; i <= to+1; i++ {
@ -107,7 +88,7 @@ func (s *Service) GetAccountBlocks(from, to int) []*types.Block {
return blocks
}
// GetExplorerBlocks ...
// GetExplorerBlocks serves end-point /blocks
func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
from := r.FormValue("from")
@ -183,7 +164,7 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(data.Blocks)
}
// GetExplorerTransaction ...
// GetExplorerTransaction servers /tx end-point.
func (s *Service) GetExplorerTransaction(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
id := r.FormValue("id")
@ -208,14 +189,12 @@ func (s *Service) GetExplorerTransaction(w http.ResponseWriter, r *http.Request)
json.NewEncoder(w).Encode(data.TX)
}
// GetExplorerAddress ...
// GetExplorerAddress serves /address end-point.
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(nil)

@ -98,22 +98,6 @@ func (storage *Storage) Dump(block *types.Block, height uint32) {
Log.Debug("Failed to serialize block ", "error", err)
}
// Store block info.
blockInfo := BlockInfo{
ID: block.Hash().Hex(),
Height: string(height),
Timestamp: strconv.Itoa(int(block.Time().Int64() * 1000)),
TXCount: string(block.Transactions().Len()),
Size: block.Size().String(),
}
if data, err := rlp.EncodeToBytes(blockInfo); err == nil {
key := GetBlockInfoKey(int(height))
storage.db.Put([]byte(key), data)
} else {
Log.Error("EncodeRLP blockInfo error")
}
// Store txs
for _, tx := range block.Transactions() {
if tx.To() == nil {
@ -121,7 +105,6 @@ func (storage *Storage) Dump(block *types.Block, height uint32) {
}
explorerTransaction := GetTransaction(tx, block)
storage.UpdateTXStorage(explorerTransaction, tx)
storage.UpdateAddress(explorerTransaction, tx)
}

@ -36,15 +36,6 @@ type Transaction struct {
Bytes string `json:"bytes"`
}
// BlockInfo ...
type BlockInfo struct {
ID string `json:"id"`
Height string `json:"height"`
Timestamp string `json:"timestamp"`
TXCount string `json:"txCount"`
Size string `json:"size"`
}
// Block ...
type Block struct {
Height string `json:"height"`

@ -0,0 +1,12 @@
package main
import "github.com/harmony-one/harmony/api/services/explorer"
func main() {
service := &explorer.Service{
IP: "127.0.0.1",
Port: "9000",
}
service.Init(false)
service.Run()
}
Loading…
Cancel
Save