From 8fac27fabe282c4b235e256b8602ef150cfb8add Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 27 Feb 2024 15:45:05 +0530 Subject: [PATCH] Use multi index for auction id on authorities map --- x/registry/keeper/keeper.go | 8 ++++---- x/registry/keeper/record_keeper.go | 12 ++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/x/registry/keeper/keeper.go b/x/registry/keeper/keeper.go index 11020b18..a42ac64b 100644 --- a/x/registry/keeper/keeper.go +++ b/x/registry/keeper/keeper.go @@ -53,16 +53,16 @@ func newRecordIndexes(sb *collections.SchemaBuilder) RecordsIndexes { // TODO type AuthoritiesIndexes struct { - AuctionId *indexes.Unique[string, string, registrytypes.NameAuthority] + AuctionId *indexes.Multi[string, string, registrytypes.NameAuthority] } -func (b AuthoritiesIndexes) IndexesList() []collections.Index[string, registrytypes.NameAuthority] { - return []collections.Index[string, registrytypes.NameAuthority]{} +func (a AuthoritiesIndexes) IndexesList() []collections.Index[string, registrytypes.NameAuthority] { + return []collections.Index[string, registrytypes.NameAuthority]{a.AuctionId} } func newAuthorityIndexes(sb *collections.SchemaBuilder) AuthoritiesIndexes { return AuthoritiesIndexes{ - AuctionId: indexes.NewUnique( + AuctionId: indexes.NewMulti( sb, registrytypes.AuthoritiesByAuctionIdIndexPrefix, "authorities_by_auction_id", collections.StringKey, collections.StringKey, func(name string, v registrytypes.NameAuthority) (string, error) { diff --git a/x/registry/keeper/record_keeper.go b/x/registry/keeper/record_keeper.go index 758a6cbe..890adf6e 100644 --- a/x/registry/keeper/record_keeper.go +++ b/x/registry/keeper/record_keeper.go @@ -64,16 +64,24 @@ func (rk RecordKeeper) OnAuctionBid(ctx sdk.Context, auctionId string, bidderAdd func (rk RecordKeeper) OnAuctionWinnerSelected(ctx sdk.Context, auctionId string) { // Update authority status based on auction status/winner. - name, err := rk.k.Authorities.Indexes.AuctionId.MatchExact(ctx, auctionId) + iter, err := rk.k.Authorities.Indexes.AuctionId.MatchExact(ctx, auctionId) if err != nil && !errors.Is(err, collections.ErrNotFound) { panic(err) } - if name == "" { + names, err := iter.PrimaryKeys() + if err != nil { + panic(err) + } + + if len(names) == 0 { // We don't know about this auction, ignore. logger(ctx).Info(fmt.Sprintf("Ignoring auction notification, name mapping not found: %s", auctionId)) return } + // Take the first one as an auction (non-empty) will map to only one name + // MultiIndex being used as there can be multiple entries with empty auction id ("") + name := names[0] if has, err := rk.k.HasNameAuthority(ctx, name); !has { if err != nil { panic(err)