clean up logging

pull/169/head
Minh Doan 6 years ago committed by Minh Doan
parent d6d2f74a1f
commit 2c04f062a2
  1. 21
      services/explorer/service.go
  2. 52
      services/explorer/storage.go

@ -32,7 +32,7 @@ func GetExplorerPort(nodePort string) string {
if port, err := strconv.Atoi(nodePort); err == nil { if port, err := strconv.Atoi(nodePort); err == nil {
return fmt.Sprintf("%d", port-explorerPortDifference) return fmt.Sprintf("%d", port-explorerPortDifference)
} }
fmt.Println("error on parsing.") Log.Error("error on parsing.")
return "" return ""
} }
@ -48,9 +48,6 @@ func (s *Service) Run() {
// Set up router // Set up router
s.router = mux.NewRouter() s.router = mux.NewRouter()
// s.router.Path("/block_info").Queries("from", "{[0-9]*?}", "to", "{[0-9]*?}").HandlerFunc(s.GetExplorerBlockInfo).Methods("GET")
// s.router.Path("/block_info").HandlerFunc(s.GetExplorerBlockInfo)
s.router.Path("/blocks").Queries("from", "{[0-9]*?}", "to", "{[0-9]*?}").HandlerFunc(s.GetExplorerBlocks).Methods("GET") s.router.Path("/blocks").Queries("from", "{[0-9]*?}", "to", "{[0-9]*?}").HandlerFunc(s.GetExplorerBlocks).Methods("GET")
s.router.Path("/blocks").HandlerFunc(s.GetExplorerBlocks) s.router.Path("/blocks").HandlerFunc(s.GetExplorerBlocks)
@ -73,12 +70,12 @@ func (s *Service) PopulateBlockInfo(from, to int) []*BlockInfo {
fmt.Println("getting blockinfo with key ", key) fmt.Println("getting blockinfo with key ", key)
data, err := storage.db.Get([]byte(key)) data, err := storage.db.Get([]byte(key))
if err != nil { if err != nil {
fmt.Println("Error on getting from db") Log.Error("Error on getting from db")
os.Exit(1) os.Exit(1)
} }
block := new(BlockInfo) block := new(BlockInfo)
if rlp.DecodeBytes(data, block) != nil { if rlp.DecodeBytes(data, block) != nil {
fmt.Println("RLP Decoding error") Log.Error("Error on getting from db")
os.Exit(1) os.Exit(1)
} }
blocks = append(blocks, block) blocks = append(blocks, block)
@ -95,7 +92,6 @@ func (s *Service) GetAccountBlocks(from, to int) []*types.Block {
continue continue
} }
key := GetBlockKey(i) key := GetBlockKey(i)
fmt.Println("getting block with key ", key)
data, err := storage.db.Get([]byte(key)) data, err := storage.db.Get([]byte(key))
if err != nil { if err != nil {
blocks = append(blocks, nil) blocks = append(blocks, nil)
@ -103,7 +99,7 @@ func (s *Service) GetAccountBlocks(from, to int) []*types.Block {
} }
block := new(types.Block) block := new(types.Block)
if rlp.DecodeBytes(data, block) != nil { if rlp.DecodeBytes(data, block) != nil {
fmt.Println("RLP Block decoding error") Log.Error("Error on getting from db")
os.Exit(1) os.Exit(1)
} }
blocks = append(blocks, block) blocks = append(blocks, block)
@ -150,7 +146,6 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) {
} else { } else {
toInt, err = strconv.Atoi(to) toInt, err = strconv.Atoi(to)
} }
fmt.Println("from", fromInt, "to", toInt)
if err != nil { if err != nil {
json.NewEncoder(w).Encode(data.Blocks) json.NewEncoder(w).Encode(data.Blocks)
return return
@ -247,14 +242,12 @@ func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(data.Address) json.NewEncoder(w).Encode(data.Address)
return return
} }
fmt.Println("retreieve from the key", len(bytes)) var address Address
var addressAccount Address if err = rlp.DecodeBytes(bytes, &address); err != nil {
if err = rlp.DecodeBytes(bytes, &addressAccount); err != nil {
fmt.Println(err) fmt.Println(err)
json.NewEncoder(w).Encode(data.Address) json.NewEncoder(w).Encode(data.Address)
return return
} }
fmt.Println("convert address", id) data.Address = address
data.Address = addressAccount
json.NewEncoder(w).Encode(data.Address) json.NewEncoder(w).Encode(data.Address)
} }

@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/db" "github.com/harmony-one/harmony/db"
"github.com/harmony-one/harmony/log"
) )
// Constants for storage. // Constants for storage.
@ -20,6 +21,9 @@ const (
AddressPrefix = "ad" AddressPrefix = "ad"
) )
// Log is the temporary log for storage.
var Log = log.New()
// GetBlockInfoKey ... // GetBlockInfoKey ...
func GetBlockInfoKey(id int) string { func GetBlockInfoKey(id int) string {
return fmt.Sprintf("%s_%d", BlockInfoPrefix, id) return fmt.Sprintf("%s_%d", BlockInfoPrefix, id)
@ -64,12 +68,11 @@ func (storage *Storage) Init(ip, port string, remove bool) {
if remove { if remove {
var err = os.RemoveAll(dbFileName) var err = os.RemoveAll(dbFileName)
if err != nil { if err != nil {
fmt.Println(err.Error()) Log.Error(err.Error())
} }
} }
if storage.db, err = db.NewLDBDatabase(dbFileName, 0, 0); err != nil { if storage.db, err = db.NewLDBDatabase(dbFileName, 0, 0); err != nil {
fmt.Println(err.Error()) Log.Error(err.Error())
os.Exit(1)
} }
} }
@ -103,16 +106,12 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) {
if data, err := rlp.EncodeToBytes(blockInfo); err == nil { if data, err := rlp.EncodeToBytes(blockInfo); err == nil {
key := GetBlockInfoKey(int(height)) key := GetBlockInfoKey(int(height))
fmt.Println("store blockinfo with key ", key)
fmt.Println("data to store ", data)
storage.db.Put([]byte(key), data) storage.db.Put([]byte(key), data)
} else { } else {
fmt.Println("EncodeRLP blockInfo error") Log.Error("EncodeRLP blockInfo error")
os.Exit(1)
} }
// Store txs // Store txs
fmt.Println("# of txs ", len(block.Transactions()))
for _, tx := range block.Transactions() { for _, tx := range block.Transactions() {
if tx.To() == nil { if tx.To() == nil {
continue continue
@ -127,19 +126,18 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) {
Bytes: strconv.Itoa(int(tx.Size())), Bytes: strconv.Itoa(int(tx.Size())),
} }
storage.UpdateTxStorage(explorerTransaction, tx) storage.UpdateTXStorage(explorerTransaction, tx)
storage.UpdateAddressStorage(explorerTransaction, tx) storage.UpdateAddressStorage(explorerTransaction, tx)
} }
} }
// UpdateTxStorage ... // UpdateTXStorage ...
func (storage *Storage) UpdateTxStorage(explorerTransaction Transaction, tx *types.Transaction) { func (storage *Storage) UpdateTXStorage(explorerTransaction Transaction, tx *types.Transaction) {
if data, err := rlp.EncodeToBytes(explorerTransaction); err == nil { if data, err := rlp.EncodeToBytes(explorerTransaction); err == nil {
key := GetTXKey(tx.Hash().Hex()) key := GetTXKey(tx.Hash().Hex())
storage.db.Put([]byte(key), data) storage.db.Put([]byte(key), data)
} else { } else {
fmt.Println("EncodeRLP transaction error") Log.Error("EncodeRLP transaction error")
os.Exit(1)
} }
} }
@ -148,29 +146,23 @@ 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 address 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, address)
err = rlp.DecodeBytes(data, addressAccount)
if err == nil { if err == nil {
addressAccount.Balance.Add(addressAccount.Balance, tx.Value()) address.Balance.Add(address.Balance, tx.Value())
txCount, _ := strconv.Atoi(addressAccount.TXCount) txCount, _ := strconv.Atoi(address.TXCount)
addressAccount.TXCount = strconv.Itoa(txCount + 1) address.TXCount = strconv.Itoa(txCount + 1)
} }
} else { } else {
fmt.Println("the key not existed") address.Balance = tx.Value()
addressAccount.Balance = tx.Value() address.TXCount = "1"
addressAccount.TXCount = "1"
} }
addressAccount.ID = toAddress address.ID = toAddress
addressAccount.TXs = append(addressAccount.TXs, explorerTransaction) address.TXs = append(address.TXs, explorerTransaction)
fmt.Println("trying to encode it") if encoded, err := rlp.EncodeToBytes(address); err == nil {
if encoded, err := rlp.EncodeToBytes(addressAccount); err == nil {
fmt.Println("store addressAccount with length ", len(encoded))
storage.db.Put([]byte(key), encoded) storage.db.Put([]byte(key), encoded)
} else { } else {
fmt.Println("err when encoding ", err) Log.Error("Can not encode address account.")
} }
} }

Loading…
Cancel
Save