Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index NFTs created by a user separately from posts #195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions lib/block_view_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,9 @@ func (bav *UtxoView) GetAllPosts() (_corePosts []*PostEntry, _commentsByPostHash
return allCorePosts, commentsByPostHash, nil
}

func (bav *UtxoView) GetPostsPaginatedForPublicKeyOrderedByTimestamp(publicKey []byte, startPostHash *BlockHash, limit uint64, mediaRequired bool, nftRequired bool) (_posts []*PostEntry, _err error) {
func (bav *UtxoView) GetPostsPaginatedForPublicKeyOrderedByTimestamp(
publicKey []byte, startPostHash *BlockHash, limit uint64, mediaRequired bool, nftRequired bool,
) (_posts []*PostEntry, _err error) {
if bav.Postgres != nil {
var startTime uint64 = math.MaxUint64
if startPostHash != nil {
Expand All @@ -469,7 +471,12 @@ func (bav *UtxoView) GetPostsPaginatedForPublicKeyOrderedByTimestamp(publicKey [
}
} else {
handle := bav.Handle
dbPrefix := append([]byte{}, _PrefixPosterPublicKeyTimestampPostHash...)
var dbPrefix []byte
if !nftRequired {
dbPrefix = append([]byte{}, _PrefixPosterPublicKeyTimestampPostHash...)
} else {
dbPrefix = append([]byte{}, _PrefixPosterPublicKeyTimestampNFTPostHash...)
}
dbPrefix = append(dbPrefix, publicKey...)
var prefix []byte
if startPostHash != nil {
Expand Down Expand Up @@ -523,11 +530,6 @@ func (bav *UtxoView) GetPostsPaginatedForPublicKeyOrderedByTimestamp(publicKey [
continue
}

// nftRequired set to determine if we only want posts that are NFTs
if nftRequired && !postEntry.IsNFT {
continue
}

posts = append(posts, postEntry)
}
return nil
Expand All @@ -550,11 +552,6 @@ func (bav *UtxoView) GetPostsPaginatedForPublicKeyOrderedByTimestamp(publicKey [
continue
}

// nftRequired set to determine if we only want posts that are NFTs
if nftRequired && !postEntry.IsNFT {
continue
}

if reflect.DeepEqual(postEntry.PosterPublicKey, publicKey) {
postEntries = append(postEntries, postEntry)
}
Expand Down
29 changes: 28 additions & 1 deletion lib/db_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ var (
_PrefixCreatorPKIDHODLerPKIDToBalanceEntry = []byte{34}

_PrefixPosterPublicKeyTimestampPostHash = []byte{35}
// Only NFT posts are stored here in this index
_PrefixPosterPublicKeyTimestampNFTPostHash = []byte{59}

// If no mapping exists for a particular public key, then the PKID is simply
// the public key itself.
Expand Down Expand Up @@ -287,7 +289,7 @@ var (
// TODO: This process is a bit error-prone. We should come up with a test or
// something to at least catch cases where people have two prefixes with the
// same ID.
// NEXT_TAG: 59
// NEXT_TAG: 60
)

func DBGetPKIDEntryForPublicKeyWithTxn(txn *badger.Txn, publicKey []byte) *PKIDEntry {
Expand Down Expand Up @@ -3935,6 +3937,14 @@ func _dbKeyForPosterPublicKeyTimestampPostHash(publicKey []byte, timestampNanos
key = append(key, postHash[:]...)
return key
}
func _dbKeyForPosterPublicKeyTimestampNFTPostHash(publicKey []byte, timestampNanos uint64, nftPostHash *BlockHash) []byte {
// Make a copy to avoid multiple calls to this function re-using the same slice.
key := append([]byte{}, _PrefixPosterPublicKeyTimestampNFTPostHash...)
key = append(key, publicKey...)
key = append(key, EncodeUint64(timestampNanos)...)
key = append(key, nftPostHash[:]...)
return key
}
func _dbKeyForTstampPostHash(tstampNanos uint64, postHash *BlockHash) []byte {
// Make a copy to avoid multiple calls to this function re-using the same slice.
key := append([]byte{}, _PrefixTstampNanosPostHash...)
Expand Down Expand Up @@ -4053,6 +4063,14 @@ func DBDeletePostEntryMappingsWithTxn(
}
}

if postEntry.IsNFT {
if err := txn.Delete(_dbKeyForPosterPublicKeyTimestampNFTPostHash(
postEntry.PosterPublicKey, postEntry.TimestampNanos, postEntry.PostHash)); err != nil {
return errors.Wrapf(err, "DbDeletePostEntryMappingsWithTxn: Deleting "+
"tstamp NFT mapping for post hash %v", postHash)
}
}

// Delete the repost entries for the post.
if IsVanillaRepost(postEntry) {
if err := txn.Delete(
Expand Down Expand Up @@ -4151,6 +4169,15 @@ func DBPutPostEntryMappingsWithTxn(
"adding mapping for stakeMultipleBps: %v", postEntry)
}
}

if postEntry.IsNFT {
if err := txn.Set(_dbKeyForPosterPublicKeyTimestampNFTPostHash(
postEntry.PosterPublicKey, postEntry.TimestampNanos, postEntry.PostHash), []byte{}); err != nil {
return errors.Wrapf(err,"DbPutPostEntryMappingsWithTxn: Problem "+
"adding mapping for public key + nft post %v", postEntry)
}
}

// We treat reposting the same for both comments and posts.
// We only store repost entry mappings for vanilla reposts
if IsVanillaRepost(postEntry) {
Expand Down