From 2c04f062a25c188c84ab9d7b76da67f904855c7f Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Mon, 17 Dec 2018 22:45:10 -0800 Subject: [PATCH] clean up logging --- services/explorer/service.go | 21 +++++---------- services/explorer/storage.go | 52 +++++++++++++++--------------------- 2 files changed, 29 insertions(+), 44 deletions(-) diff --git a/services/explorer/service.go b/services/explorer/service.go index f085b643d..51f196c35 100644 --- a/services/explorer/service.go +++ b/services/explorer/service.go @@ -32,7 +32,7 @@ func GetExplorerPort(nodePort string) string { if port, err := strconv.Atoi(nodePort); err == nil { return fmt.Sprintf("%d", port-explorerPortDifference) } - fmt.Println("error on parsing.") + Log.Error("error on parsing.") return "" } @@ -48,9 +48,6 @@ func (s *Service) Run() { // Set up router 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").HandlerFunc(s.GetExplorerBlocks) @@ -73,12 +70,12 @@ func (s *Service) PopulateBlockInfo(from, to int) []*BlockInfo { fmt.Println("getting blockinfo with key ", key) data, err := storage.db.Get([]byte(key)) if err != nil { - fmt.Println("Error on getting from db") + Log.Error("Error on getting from db") os.Exit(1) } block := new(BlockInfo) if rlp.DecodeBytes(data, block) != nil { - fmt.Println("RLP Decoding error") + Log.Error("Error on getting from db") os.Exit(1) } blocks = append(blocks, block) @@ -95,7 +92,6 @@ func (s *Service) GetAccountBlocks(from, to int) []*types.Block { continue } key := GetBlockKey(i) - fmt.Println("getting block with key ", key) data, err := storage.db.Get([]byte(key)) if err != nil { blocks = append(blocks, nil) @@ -103,7 +99,7 @@ func (s *Service) GetAccountBlocks(from, to int) []*types.Block { } block := new(types.Block) if rlp.DecodeBytes(data, block) != nil { - fmt.Println("RLP Block decoding error") + Log.Error("Error on getting from db") os.Exit(1) } blocks = append(blocks, block) @@ -150,7 +146,6 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) { } else { toInt, err = strconv.Atoi(to) } - fmt.Println("from", fromInt, "to", toInt) if err != nil { json.NewEncoder(w).Encode(data.Blocks) return @@ -247,14 +242,12 @@ func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(data.Address) return } - fmt.Println("retreieve from the key", len(bytes)) - var addressAccount Address - if err = rlp.DecodeBytes(bytes, &addressAccount); err != nil { + var address Address + if err = rlp.DecodeBytes(bytes, &address); err != nil { fmt.Println(err) json.NewEncoder(w).Encode(data.Address) return } - fmt.Println("convert address", id) - data.Address = addressAccount + data.Address = address json.NewEncoder(w).Encode(data.Address) } diff --git a/services/explorer/storage.go b/services/explorer/storage.go index 52cc5ec50..6380ea6e7 100644 --- a/services/explorer/storage.go +++ b/services/explorer/storage.go @@ -9,6 +9,7 @@ import ( "github.com/ethereum/go-ethereum/rlp" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/db" + "github.com/harmony-one/harmony/log" ) // Constants for storage. @@ -20,6 +21,9 @@ const ( AddressPrefix = "ad" ) +// Log is the temporary log for storage. +var Log = log.New() + // GetBlockInfoKey ... func GetBlockInfoKey(id int) string { return fmt.Sprintf("%s_%d", BlockInfoPrefix, id) @@ -64,12 +68,11 @@ func (storage *Storage) Init(ip, port string, remove bool) { if remove { var err = os.RemoveAll(dbFileName) if err != nil { - fmt.Println(err.Error()) + Log.Error(err.Error()) } } if storage.db, err = db.NewLDBDatabase(dbFileName, 0, 0); err != nil { - fmt.Println(err.Error()) - os.Exit(1) + Log.Error(err.Error()) } } @@ -103,16 +106,12 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) { if data, err := rlp.EncodeToBytes(blockInfo); err == nil { key := GetBlockInfoKey(int(height)) - fmt.Println("store blockinfo with key ", key) - fmt.Println("data to store ", data) storage.db.Put([]byte(key), data) } else { - fmt.Println("EncodeRLP blockInfo error") - os.Exit(1) + Log.Error("EncodeRLP blockInfo error") } // Store txs - fmt.Println("# of txs ", len(block.Transactions())) for _, tx := range block.Transactions() { if tx.To() == nil { continue @@ -127,19 +126,18 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) { Bytes: strconv.Itoa(int(tx.Size())), } - storage.UpdateTxStorage(explorerTransaction, tx) + storage.UpdateTXStorage(explorerTransaction, tx) storage.UpdateAddressStorage(explorerTransaction, tx) } } -// UpdateTxStorage ... -func (storage *Storage) UpdateTxStorage(explorerTransaction Transaction, tx *types.Transaction) { +// UpdateTXStorage ... +func (storage *Storage) UpdateTXStorage(explorerTransaction Transaction, tx *types.Transaction) { if data, err := rlp.EncodeToBytes(explorerTransaction); err == nil { key := GetTXKey(tx.Hash().Hex()) storage.db.Put([]byte(key), data) } else { - fmt.Println("EncodeRLP transaction error") - os.Exit(1) + Log.Error("EncodeRLP transaction error") } } @@ -148,29 +146,23 @@ func (storage *Storage) UpdateAddressStorage(explorerTransaction Transaction, tx toAddress := tx.To().Hex() key := GetAddressKey(toAddress) - fmt.Println("dumping address", toAddress, key) - - var addressAccount Address + var address Address if data, err := storage.db.Get([]byte(key)); err == nil { - fmt.Println("the key existed") - err = rlp.DecodeBytes(data, addressAccount) + err = rlp.DecodeBytes(data, address) if err == nil { - addressAccount.Balance.Add(addressAccount.Balance, tx.Value()) - txCount, _ := strconv.Atoi(addressAccount.TXCount) - addressAccount.TXCount = strconv.Itoa(txCount + 1) + address.Balance.Add(address.Balance, tx.Value()) + txCount, _ := strconv.Atoi(address.TXCount) + address.TXCount = strconv.Itoa(txCount + 1) } } else { - fmt.Println("the key not existed") - addressAccount.Balance = tx.Value() - addressAccount.TXCount = "1" + address.Balance = tx.Value() + address.TXCount = "1" } - addressAccount.ID = toAddress - addressAccount.TXs = append(addressAccount.TXs, explorerTransaction) - fmt.Println("trying to encode it") - if encoded, err := rlp.EncodeToBytes(addressAccount); err == nil { - fmt.Println("store addressAccount with length ", len(encoded)) + address.ID = toAddress + address.TXs = append(address.TXs, explorerTransaction) + if encoded, err := rlp.EncodeToBytes(address); err == nil { storage.db.Put([]byte(key), encoded) } else { - fmt.Println("err when encoding ", err) + Log.Error("Can not encode address account.") } }