Use multi index for auction id on authorities map

This commit is contained in:
Prathamesh Musale 2024-02-27 15:45:05 +05:30
parent fe40617d9e
commit 8fac27fabe
2 changed files with 14 additions and 6 deletions

View File

@ -53,16 +53,16 @@ func newRecordIndexes(sb *collections.SchemaBuilder) RecordsIndexes {
// TODO // TODO
type AuthoritiesIndexes struct { 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] { func (a AuthoritiesIndexes) IndexesList() []collections.Index[string, registrytypes.NameAuthority] {
return []collections.Index[string, registrytypes.NameAuthority]{} return []collections.Index[string, registrytypes.NameAuthority]{a.AuctionId}
} }
func newAuthorityIndexes(sb *collections.SchemaBuilder) AuthoritiesIndexes { func newAuthorityIndexes(sb *collections.SchemaBuilder) AuthoritiesIndexes {
return AuthoritiesIndexes{ return AuthoritiesIndexes{
AuctionId: indexes.NewUnique( AuctionId: indexes.NewMulti(
sb, registrytypes.AuthoritiesByAuctionIdIndexPrefix, "authorities_by_auction_id", sb, registrytypes.AuthoritiesByAuctionIdIndexPrefix, "authorities_by_auction_id",
collections.StringKey, collections.StringKey, collections.StringKey, collections.StringKey,
func(name string, v registrytypes.NameAuthority) (string, error) { func(name string, v registrytypes.NameAuthority) (string, error) {

View File

@ -64,16 +64,24 @@ func (rk RecordKeeper) OnAuctionBid(ctx sdk.Context, auctionId string, bidderAdd
func (rk RecordKeeper) OnAuctionWinnerSelected(ctx sdk.Context, auctionId string) { func (rk RecordKeeper) OnAuctionWinnerSelected(ctx sdk.Context, auctionId string) {
// Update authority status based on auction status/winner. // 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) { if err != nil && !errors.Is(err, collections.ErrNotFound) {
panic(err) 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. // We don't know about this auction, ignore.
logger(ctx).Info(fmt.Sprintf("Ignoring auction notification, name mapping not found: %s", auctionId)) logger(ctx).Info(fmt.Sprintf("Ignoring auction notification, name mapping not found: %s", auctionId))
return 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 has, err := rk.k.HasNameAuthority(ctx, name); !has {
if err != nil { if err != nil {
panic(err) panic(err)