update address storage

pull/169/head
Minh Doan 6 years ago committed by Minh Doan
parent d4e100d470
commit 45c74f8d16
  1. 25
      services/explorer/service.go
  2. 37
      services/explorer/storage.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)
}

@ -118,13 +118,6 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) {
continue
}
storage.UpdateTxStorage(block, tx)
storage.UpdateAddressStorage(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)),
@ -133,6 +126,14 @@ func (storage *Storage) UpdateTxStorage(block *types.Block, tx *types.Transactio
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(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)
}

Loading…
Cancel
Save