-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1353 from WalletConnect/remove-stale-requests
Add invalid requests sanitiser
- Loading branch information
Showing
7 changed files
with
149 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Sources/WalletConnectSign/Services/InvalidRequestsSanitiser.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
import Foundation | ||
|
||
final class InvalidRequestsSanitiser { | ||
private let historyService: HistoryServiceProtocol | ||
private let history: RPCHistoryProtocol | ||
|
||
init(historyService: HistoryServiceProtocol, history: RPCHistoryProtocol) { | ||
self.historyService = historyService | ||
self.history = history | ||
} | ||
|
||
func removeInvalidSessionRequests(validSessionTopics: Set<String>) { | ||
let pendingRequests = historyService.getPendingRequests() | ||
let invalidTopics = Set(pendingRequests.map { $0.request.topic }).subtracting(validSessionTopics) | ||
if !invalidTopics.isEmpty { | ||
history.deleteAll(forTopics: Array(invalidTopics)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Tests/WalletConnectSignTests/InvalidRequestsSanitiserTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import XCTest | ||
@testable import WalletConnectSign | ||
@testable import WalletConnectUtils | ||
|
||
class InvalidRequestsSanitiserTests: XCTestCase { | ||
var sanitiser: InvalidRequestsSanitiser! | ||
var mockHistoryService: MockHistoryService! | ||
var mockRPCHistory: MockRPCHistory! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
mockHistoryService = MockHistoryService() | ||
mockRPCHistory = MockRPCHistory() | ||
sanitiser = InvalidRequestsSanitiser(historyService: mockHistoryService, history: mockRPCHistory) | ||
} | ||
|
||
override func tearDown() { | ||
sanitiser = nil | ||
mockHistoryService = nil | ||
mockRPCHistory = nil | ||
super.tearDown() | ||
} | ||
|
||
func testRemoveInvalidSessionRequests_noPendingRequests() { | ||
let validSessionTopics: Set<String> = ["validTopic1", "validTopic2"] | ||
|
||
sanitiser.removeInvalidSessionRequests(validSessionTopics: validSessionTopics) | ||
|
||
XCTAssertTrue(mockRPCHistory.deletedTopics.isEmpty) | ||
} | ||
|
||
func testRemoveInvalidSessionRequests_allRequestsValid() { | ||
let validSessionTopics: Set<String> = ["validTopic1", "validTopic2"] | ||
mockHistoryService.pendingRequests = [ | ||
(request: try! Request(topic: "validTopic1", method: "method1", params: AnyCodable("params1"), chainId: Blockchain("eip155:1")!), context: nil), | ||
(request: try! Request(topic: "validTopic2", method: "method2", params: AnyCodable("params2"), chainId: Blockchain("eip155:1")!), context: nil) | ||
] | ||
|
||
sanitiser.removeInvalidSessionRequests(validSessionTopics: validSessionTopics) | ||
|
||
XCTAssertTrue(mockRPCHistory.deletedTopics.isEmpty) | ||
} | ||
|
||
func testRemoveInvalidSessionRequests_someRequestsInvalid() { | ||
let validSessionTopics: Set<String> = ["validTopic1", "validTopic2"] | ||
mockHistoryService.pendingRequests = [ | ||
(request: try! Request(topic: "validTopic1", method: "method1", params: AnyCodable("params1"), chainId: Blockchain("eip155:1")!), context: nil), | ||
(request: try! Request(topic: "invalidTopic1", method: "method2", params: AnyCodable("params2"), chainId: Blockchain("eip155:1")!), context: nil), | ||
(request: try! Request(topic: "invalidTopic2", method: "method3", params: AnyCodable("params3"), chainId: Blockchain("eip155:1")!), context: nil) | ||
] | ||
|
||
sanitiser.removeInvalidSessionRequests(validSessionTopics: validSessionTopics) | ||
|
||
XCTAssertEqual(mockRPCHistory.deletedTopics.sorted(), ["invalidTopic1", "invalidTopic2"]) | ||
} | ||
|
||
func testRemoveInvalidSessionRequests_withEmptyValidSessionTopics() { | ||
let validSessionTopics: Set<String> = [] | ||
|
||
mockHistoryService.pendingRequests = [ | ||
(request: try! Request(topic: "invalidTopic1", method: "method1", params: AnyCodable("params1"), chainId: Blockchain("eip155:1")!), context: nil), | ||
(request: try! Request(topic: "invalidTopic2", method: "method2", params: AnyCodable("params2"), chainId: Blockchain("eip155:1")!), context: nil) | ||
] | ||
|
||
sanitiser.removeInvalidSessionRequests(validSessionTopics: validSessionTopics) | ||
|
||
XCTAssertEqual(mockRPCHistory.deletedTopics.sorted(), ["invalidTopic1", "invalidTopic2"]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters