Skip to content

Commit fa106f5

Browse files
authored
[Peristence] Fix TxIndexer to index sender and recipient transactions with height and index suffixes (#677)
## Description This PR fixes the `TxIndexer` so that it can index multiple transactions by sender and recipient instead of simply overwriting the previous transaction. By adding the height and index of the index as a suffix to the key when storing the transaction bytes in the TxIndexer KVStore, we can now query all transactions sent or received by a specific address, through the TxIndexer. ## Issue Fixes N/A ## Type of change Please mark the relevant option(s): - [x] New feature, functionality or library - [x] Bug fix - [ ] Code health or cleanup - [ ] Major breaking change - [ ] Documentation - [ ] Other <!-- add details here if it a different type of change --> ## List of changes - Index by Sender and Recipient with height and index suffixes ## Testing - [x] `make develop_test`; if any code changes were made - [x] [Docker Compose LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md); if any major functionality was changed or introduced - [x] [k8s LocalNet](https://github.com/pokt-network/pocket/blob/main/build/localnet/README.md); if any infrastructure or configuration changes were made ## Required Checklist - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have added, or updated, [`godoc` format comments](https://go.dev/blog/godoc) on touched members (see: [tip.golang.org/doc/comment](https://tip.golang.org/doc/comment)) - [x] I have tested my changes using the available tooling - [x] I have updated the corresponding CHANGELOG ### If Applicable Checklist - [ ] I have updated the corresponding README(s); local and/or global - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have added, or updated, [mermaid.js](https://mermaid-js.github.io) diagrams in the corresponding README(s) - [ ] I have added, or updated, documentation and [mermaid.js](https://mermaid-js.github.io) diagrams in `shared/docs/*` if I updated `shared/*`README(s)
1 parent 5dd7b7a commit fa106f5

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

persistence/docs/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.49] - 2023-04-14
11+
12+
- Index transactions in the `TxIndexer` by sender and recipient using the height and block index of the transactions
13+
1014
## [0.0.0.48] - 2023-04-13
1115

1216
- Remove persistent specific `TxResult` protobuf and interface

persistence/indexer/indexer.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ func (indexer *txIndexer) Index(result *coreTypes.TxResult) error {
8383
if err := indexer.indexByHeightAndIndex(result.GetHeight(), result.GetIndex(), hashKey); err != nil {
8484
return err
8585
}
86-
if err := indexer.indexBySender(result.GetSignerAddr(), hashKey); err != nil {
86+
if err := indexer.indexBySenderHeightAndBlockIndex(result.GetSignerAddr(), result.GetHeight(), result.GetIndex(), hashKey); err != nil {
8787
return err
8888
}
89-
if err := indexer.indexByRecipient(result.GetRecipientAddr(), hashKey); err != nil {
89+
if err := indexer.indexByRecipientHeightAndBlockIndex(result.GetRecipientAddr(), result.GetHeight(), result.GetIndex(), hashKey); err != nil {
9090
return err
9191
}
9292
return nil
@@ -148,15 +148,18 @@ func (indexer *txIndexer) indexByHeightAndIndex(height int64, index int32, bz []
148148
return indexer.db.Set(indexer.heightAndIndexKey(height, index), bz)
149149
}
150150

151-
func (indexer *txIndexer) indexBySender(sender string, bz []byte) error {
152-
return indexer.db.Set(indexer.senderKey(sender), bz)
151+
func (indexer *txIndexer) indexBySenderHeightAndBlockIndex(sender string, height int64, blockIndex int32, bz []byte) error {
152+
if sender == "" {
153+
return nil
154+
}
155+
return indexer.db.Set(indexer.senderHeightAndBlockIndexKey(sender, height, blockIndex), bz)
153156
}
154157

155-
func (indexer *txIndexer) indexByRecipient(recipient string, bz []byte) error {
158+
func (indexer *txIndexer) indexByRecipientHeightAndBlockIndex(recipient string, height int64, blockIndex int32, bz []byte) error {
156159
if recipient == "" {
157160
return nil
158161
}
159-
return indexer.db.Set(indexer.recipientKey(recipient), bz)
162+
return indexer.db.Set(indexer.recipientHeightAndBlockIndexKey(recipient, height, blockIndex), bz)
160163
}
161164

162165
// key helper functions
@@ -174,11 +177,25 @@ func (indexer *txIndexer) heightKey(height int64) []byte {
174177
}
175178

176179
func (indexer *txIndexer) senderKey(address string) []byte {
177-
return indexer.key(senderPrefix, address)
180+
return indexer.key(senderPrefix, address+"/")
181+
}
182+
183+
func (indexer *txIndexer) senderHeightAndBlockIndexKey(address string, height int64, blockIndex int32) []byte {
184+
key := indexer.senderKey(address)
185+
key = append(key, []byte(elenEncoder.EncodeInt(int(height))+"/")...)
186+
key = append(key, []byte(elenEncoder.EncodeInt(int(blockIndex)))...)
187+
return key
178188
}
179189

180190
func (indexer *txIndexer) recipientKey(address string) []byte {
181-
return indexer.key(recipientPrefix, address)
191+
return indexer.key(recipientPrefix, address+"/")
192+
}
193+
194+
func (indexer *txIndexer) recipientHeightAndBlockIndexKey(address string, height int64, blockIndex int32) []byte {
195+
key := indexer.recipientKey(address)
196+
key = append(key, []byte(elenEncoder.EncodeInt(int(height))+"/")...)
197+
key = append(key, []byte(elenEncoder.EncodeInt(int(blockIndex)))...)
198+
return key
182199
}
183200

184201
func (indexer *txIndexer) key(prefix rune, postfix string) []byte {

0 commit comments

Comments
 (0)