From 8865a0e4b0a592fd2607bd301fef1d2282158c65 Mon Sep 17 00:00:00 2001 From: frank Date: Fri, 2 Dec 2022 18:21:47 +0800 Subject: [PATCH] fix map iteration order (#2991) --- contracts/ethscan/address.go | 6 +++--- protocol/message_persistence.go | 22 ++++++---------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/contracts/ethscan/address.go b/contracts/ethscan/address.go index 95e7cb4c7..69dd5c9f9 100644 --- a/contracts/ethscan/address.go +++ b/contracts/ethscan/address.go @@ -9,9 +9,9 @@ import ( var errorNotAvailableOnChainID = errors.New("not available for chainID") var contractAddressByChainID = map[uint64]common.Address{ - 1: common.HexToAddress("0x08A8fDBddc160A7d5b957256b903dCAb1aE512C5"), // mainnet - 5: common.HexToAddress("0x08A8fDBddc160A7d5b957256b903dCAb1aE512C5"), // goerli - 420: common.HexToAddress("0xf532c75239fa61b66d31e73f44300c46da41aadd"), // goerli optimism + 1: common.HexToAddress("0x08A8fDBddc160A7d5b957256b903dCAb1aE512C5"), // mainnet + 5: common.HexToAddress("0x08A8fDBddc160A7d5b957256b903dCAb1aE512C5"), // goerli + 420: common.HexToAddress("0xf532c75239fa61b66d31e73f44300c46da41aadd"), // goerli optimism 421613: common.HexToAddress("0xec21ebe1918e8975fc0cd0c7747d318c00c0acd5"), // goerli arbitrum } diff --git a/protocol/message_persistence.go b/protocol/message_persistence.go index 0c025ea80..0e2441c03 100644 --- a/protocol/message_persistence.go +++ b/protocol/message_persistence.go @@ -2360,6 +2360,7 @@ func SortByClock(msgs HasClocks) { func getMessagesFromScanRows(db sqlitePersistence, rows *sql.Rows, withCursor bool) ([]*common.Message, error) { messageIdx := make(map[string]*common.Message, 0) + var messages common.Messages for rows.Next() { // There's a possibility of multiple rows per message if the // message has a discordMessage and the discordMessage has multiple @@ -2382,16 +2383,12 @@ func getMessagesFromScanRows(db sqlitePersistence, rows *sql.Rows, withCursor bo if msg, ok := messageIdx[message.ID]; !ok { messageIdx[message.ID] = &message + messages = append(messages, &message) } else if discordMessage := msg.GetDiscordMessage(); discordMessage != nil { msg.Payload = getUpdatedChatMessagePayload(discordMessage, message.GetDiscordMessage()) - messageIdx[message.ID] = msg } } - var messages common.Messages - for _, message := range messageIdx { - messages = append(messages, message) - } SortByClock(messages) return messages, nil @@ -2400,8 +2397,8 @@ func getMessagesFromScanRows(db sqlitePersistence, rows *sql.Rows, withCursor bo func getMessagesAndCursorsFromScanRows(db sqlitePersistence, rows *sql.Rows) ([]*common.Message, []string, error) { var cursors []string + var messages common.Messages messageIdx := make(map[string]*common.Message, 0) - for rows.Next() { // There's a possibility of multiple rows per message if the // message has a discordMessage and the discordMessage has multiple @@ -2420,16 +2417,12 @@ func getMessagesAndCursorsFromScanRows(db sqlitePersistence, rows *sql.Rows) ([] if msg, ok := messageIdx[message.ID]; !ok { messageIdx[message.ID] = &message cursors = append(cursors, cursor) + messages = append(messages, &message) } else if discordMessage := msg.GetDiscordMessage(); discordMessage != nil { msg.Payload = getUpdatedChatMessagePayload(discordMessage, message.GetDiscordMessage()) - messageIdx[message.ID] = msg } } - var messages common.Messages - for _, message := range messageIdx { - messages = append(messages, message) - } SortByClock(messages) return messages, cursors, nil @@ -2438,6 +2431,7 @@ func getMessagesAndCursorsFromScanRows(db sqlitePersistence, rows *sql.Rows) ([] func getPinnedMessagesAndCursorsFromScanRows(db sqlitePersistence, rows *sql.Rows) ([]*common.PinnedMessage, []string, error) { var cursors []string + var messages common.PinnedMessages messageIdx := make(map[string]*common.PinnedMessage, 0) for rows.Next() { @@ -2457,17 +2451,13 @@ func getPinnedMessagesAndCursorsFromScanRows(db sqlitePersistence, rows *sql.Row PinnedBy: pinnedBy, } messageIdx[message.ID] = pinnedMessage + messages = append(messages, pinnedMessage) cursors = append(cursors, cursor) } else if discordMessage := msg.Message.GetDiscordMessage(); discordMessage != nil { msg.Message.Payload = getUpdatedChatMessagePayload(discordMessage, message.GetDiscordMessage()) - messageIdx[message.ID] = msg } } - var messages common.PinnedMessages - for _, message := range messageIdx { - messages = append(messages, message) - } SortByClock(messages) return messages, cursors, nil