store txs for address

pull/169/head
Minh Doan 6 years ago committed by Minh Doan
parent a8b8ba05e3
commit d4e100d470
  1. 37
      services/explorer/storage.go

@ -17,6 +17,7 @@ const (
BlockInfoPrefix = "bi"
BlockPrefix = "b"
TXPrefix = "tx"
AddressPrefix = "ad"
)
// GetBlockInfoKey ...
@ -24,6 +25,11 @@ func GetBlockInfoKey(id int) string {
return fmt.Sprintf("%s_%d", BlockInfoPrefix, id)
}
// GetAddressKey ...
func GetAddressKey(address string) string {
return fmt.Sprintf("%s_%s", AddressPrefix, address)
}
// GetBlockKey ...
func GetBlockKey(id int) string {
return fmt.Sprintf("%s_%d", BlockPrefix, id)
@ -111,6 +117,14 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) {
if tx.To() == nil {
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)),
@ -127,4 +141,27 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) {
os.Exit(1)
}
}
// UpdateAddressStorage ...
func (storage *Storage) UpdateAddressStorage(tx *types.Transaction) {
toAddress := tx.To().Hex()
key := GetAddressKey(toAddress)
txID := tx.Hash().Hex()
if data, err := storage.db.Get([]byte(key)); err == nil {
var txIDs []string
err = rlp.DecodeBytes(data, txIDs)
if err == nil {
txIDs = append(txIDs, txID)
storage.PutArrayOfString(key, txIDs)
}
} else {
storage.PutArrayOfString(key, []string{tx.Hash().Hex()})
}
}
// PutArrayOfString ...
func (storage *Storage) PutArrayOfString(key string, arr []string) {
encoded, _ := rlp.EncodeToBytes(arr)
storage.db.Put([]byte(key), encoded)
}

Loading…
Cancel
Save