store txs for address

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

@ -17,6 +17,7 @@ const (
BlockInfoPrefix = "bi" BlockInfoPrefix = "bi"
BlockPrefix = "b" BlockPrefix = "b"
TXPrefix = "tx" TXPrefix = "tx"
AddressPrefix = "ad"
) )
// GetBlockInfoKey ... // GetBlockInfoKey ...
@ -24,6 +25,11 @@ func GetBlockInfoKey(id int) string {
return fmt.Sprintf("%s_%d", BlockInfoPrefix, id) return fmt.Sprintf("%s_%d", BlockInfoPrefix, id)
} }
// GetAddressKey ...
func GetAddressKey(address string) string {
return fmt.Sprintf("%s_%s", AddressPrefix, address)
}
// GetBlockKey ... // GetBlockKey ...
func GetBlockKey(id int) string { func GetBlockKey(id int) string {
return fmt.Sprintf("%s_%d", BlockPrefix, id) return fmt.Sprintf("%s_%d", BlockPrefix, id)
@ -111,20 +117,51 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) {
if tx.To() == nil { if tx.To() == nil {
continue continue
} }
explorerTransaction := Transaction{
ID: tx.Hash().Hex(), storage.UpdateTxStorage(block, tx)
Timestamp: strconv.Itoa(int(block.Time().Int64() * 1000)), storage.UpdateAddressStorage(tx)
From: tx.To().Hex(), }
To: tx.To().Hex(), }
Value: strconv.Itoa(int(tx.Value().Int64())),
Bytes: strconv.Itoa(int(tx.Size())), // UpdateTxStorage ...
} func (storage *Storage) UpdateTxStorage(block *types.Block, tx *types.Transaction) {
if data, err := rlp.EncodeToBytes(explorerTransaction); err == nil { explorerTransaction := Transaction{
key := GetTXKey(tx.Hash().Hex()) ID: tx.Hash().Hex(),
storage.db.Put([]byte(key), data) Timestamp: strconv.Itoa(int(block.Time().Int64() * 1000)),
} else { From: tx.To().Hex(),
fmt.Println("EncodeRLP transaction error") To: tx.To().Hex(),
os.Exit(1) Value: strconv.Itoa(int(tx.Value().Int64())),
Bytes: strconv.Itoa(int(tx.Size())),
}
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)
}
}
// 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