diff --git a/services/explorer/service.go b/services/explorer/service.go index 9da64fdf0..5831c97c9 100644 --- a/services/explorer/service.go +++ b/services/explorer/service.go @@ -257,3 +257,28 @@ func (s *Service) GetExplorerTransaction(w http.ResponseWriter, r *http.Request) data.TX = *tx json.NewEncoder(w).Encode(data.TX) } + +// GetExplorerAddress ... +func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + id := r.FormValue("id") + + data := &Data{} + if id == "" { + json.NewEncoder(w).Encode(data.TX) + return + } + db := s.storage.GetDB() + bytes, err := db.Get([]byte(GetTXKey(id))) + if err != nil { + json.NewEncoder(w).Encode(data.TX) + return + } + tx := new(Transaction) + if rlp.DecodeBytes(bytes, tx) != nil { + json.NewEncoder(w).Encode(data.TX) + return + } + data.TX = *tx + json.NewEncoder(w).Encode(data.TX) +} diff --git a/services/explorer/storage.go b/services/explorer/storage.go index 1ecbe7ca5..5cc57b7f0 100644 --- a/services/explorer/storage.go +++ b/services/explorer/storage.go @@ -118,21 +118,22 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) { continue } - storage.UpdateTxStorage(block, tx) - storage.UpdateAddressStorage(tx) + explorerTransaction := Transaction{ + ID: tx.Hash().Hex(), + Timestamp: strconv.Itoa(int(block.Time().Int64() * 1000)), + From: tx.To().Hex(), + To: tx.To().Hex(), + Value: strconv.Itoa(int(tx.Value().Int64())), + Bytes: strconv.Itoa(int(tx.Size())), + } + + storage.UpdateTxStorage(explorerTransaction, tx) + storage.UpdateAddressStorage(explorerTransaction, tx) } } // UpdateTxStorage ... -func (storage *Storage) UpdateTxStorage(block *types.Block, tx *types.Transaction) { - explorerTransaction := Transaction{ - ID: tx.Hash().Hex(), - Timestamp: strconv.Itoa(int(block.Time().Int64() * 1000)), - From: tx.To().Hex(), - To: tx.To().Hex(), - Value: strconv.Itoa(int(tx.Value().Int64())), - Bytes: strconv.Itoa(int(tx.Size())), - } +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) @@ -143,25 +144,23 @@ func (storage *Storage) UpdateTxStorage(block *types.Block, tx *types.Transactio } // UpdateAddressStorage ... -func (storage *Storage) UpdateAddressStorage(tx *types.Transaction) { +func (storage *Storage) UpdateAddressStorage(explorerTransaction Transaction, tx *types.Transaction) { toAddress := tx.To().Hex() key := GetAddressKey(toAddress) - txID := tx.Hash().Hex() + var addressAccount Address if data, err := storage.db.Get([]byte(key)); err == nil { - var txIDs []string - err = rlp.DecodeBytes(data, txIDs) + err = rlp.DecodeBytes(data, addressAccount) if err == nil { - txIDs = append(txIDs, txID) - storage.PutArrayOfString(key, txIDs) + addressAccount.Balance += float64(tx.Value().Int64()) + addressAccount.TXCount++ } } else { - storage.PutArrayOfString(key, []string{tx.Hash().Hex()}) + addressAccount.Balance = float64(tx.Value().Int64()) + addressAccount.TXCount = 1 } -} - -// PutArrayOfString ... -func (storage *Storage) PutArrayOfString(key string, arr []string) { - encoded, _ := rlp.EncodeToBytes(arr) + addressAccount.ID = toAddress + addressAccount.TXs = append(addressAccount.TXs, explorerTransaction) + encoded, _ := rlp.EncodeToBytes(addressAccount) storage.db.Put([]byte(key), encoded) }