diff --git a/Sources/KukaiCoreSwift/Models/RPC.swift b/Sources/KukaiCoreSwift/Models/RPC.swift index e64c34c7..7a2f1748 100644 --- a/Sources/KukaiCoreSwift/Models/RPC.swift +++ b/Sources/KukaiCoreSwift/Models/RPC.swift @@ -175,13 +175,21 @@ extension RPC where T == [OperationResponse] { extension RPC where T == OperationResponse { - /// Creates an RPC to estimate an operation + /// Creates an RPC to estimate an operation - deprecated public static func runOperation(runOperationPayload: RunOperationPayload) -> RPC? { guard let payloadData = RPC.encodableToData(encodable: runOperationPayload) else { return nil } - //return RPC(endpoint: "chains/main/blocks/head/helpers/scripts/run_operation", payload: payloadData, responseType: OperationResponse.self) + return RPC(endpoint: "chains/main/blocks/head/helpers/scripts/run_operation", payload: payloadData, responseType: OperationResponse.self) + } + + /// Creates an RPC to estimate an operation + public static func simulateOperation(runOperationPayload: RunOperationPayload) -> RPC? { + guard let payloadData = RPC.encodableToData(encodable: runOperationPayload) else { + return nil + } + return RPC(endpoint: "chains/main/blocks/head/helpers/scripts/simulate_operation", payload: payloadData, responseType: OperationResponse.self) } } diff --git a/Sources/KukaiCoreSwift/Services/FeeEstimatorService.swift b/Sources/KukaiCoreSwift/Services/FeeEstimatorService.swift index c1aab166..244d4ec7 100644 --- a/Sources/KukaiCoreSwift/Services/FeeEstimatorService.swift +++ b/Sources/KukaiCoreSwift/Services/FeeEstimatorService.swift @@ -158,7 +158,7 @@ public class FeeEstimatorService { /// Breaking out part of the estimation process to keep code cleaner private func estimate(runOperationPayload: RunOperationPayload, preparedOperationsCopy: [Operation], constants: NetworkConstants, forgedHex: String, originalRemoteOps: [Operation], completion: @escaping ((Result) -> Void)) { - guard let rpc = RPC.runOperation(runOperationPayload: runOperationPayload) else { + guard let rpc = RPC.simulateOperation(runOperationPayload: runOperationPayload) else { Logger.kukaiCoreSwift.error("Unable to create runOperation RPC, cancelling event") completion(Result.failure(KukaiError.internalApplicationError(error: FeeEstimatorServiceError.unableToSetupRunOperation))) return diff --git a/Tests/KukaiCoreSwiftTests/MockConstants.swift b/Tests/KukaiCoreSwiftTests/MockConstants.swift index c0e8882c..1bb0e973 100644 --- a/Tests/KukaiCoreSwiftTests/MockConstants.swift +++ b/Tests/KukaiCoreSwiftTests/MockConstants.swift @@ -139,6 +139,8 @@ public struct MockConstants { baseURL.appendingPathComponent("chains/main/blocks/head~3"): (MockConstants.jsonStub(fromFilename: "head"), MockConstants.http200), baseURL.appendingPathComponent("chains/main/blocks/head/helpers/scripts/run_operation"): (MockConstants.jsonStub(fromFilename: "run_operation"), MockConstants.http200), secondBaseURL.appendingPathComponent("chains/main/blocks/head/helpers/scripts/run_operation"): (MockConstants.jsonStub(fromFilename: "run_operation"), MockConstants.http200), + baseURL.appendingPathComponent("chains/main/blocks/head/helpers/scripts/simulate_operation"): (MockConstants.jsonStub(fromFilename: "run_operation"), MockConstants.http200), + secondBaseURL.appendingPathComponent("chains/main/blocks/head/helpers/scripts/simulate_operation"): (MockConstants.jsonStub(fromFilename: "run_operation"), MockConstants.http200), baseURL.appendingPathComponent("chains/main/blocks/head/helpers/forge/operations"): (MockConstants.jsonStub(fromFilename: "forge"), MockConstants.http200), // Parse is handled inside MockURLProtocol due to its special requirements baseURL.appendingPathComponent("chains/main/blocks/head/helpers/preapply/operations"): (MockConstants.jsonStub(fromFilename: "preapply"), MockConstants.http200), diff --git a/Tests/KukaiCoreSwiftTests/MockURLProtocol.swift b/Tests/KukaiCoreSwiftTests/MockURLProtocol.swift index fbde5483..ba82bbf2 100644 --- a/Tests/KukaiCoreSwiftTests/MockURLProtocol.swift +++ b/Tests/KukaiCoreSwiftTests/MockURLProtocol.swift @@ -151,13 +151,13 @@ class MockURLProtocol: URLProtocol { } - static func triggerGasExhaustedErrorOnRunOperation(nodeUrl: Int = 0) { - let url = MockConstants.shared.config.nodeURLs[nodeUrl].appendingPathComponent("chains/main/blocks/head/helpers/scripts/run_operation") + static func triggerGasExhaustedErrorOnSimulateOperation(nodeUrl: Int = 0) { + let url = MockConstants.shared.config.nodeURLs[nodeUrl].appendingPathComponent("chains/main/blocks/head/helpers/scripts/simulate_operation") MockURLProtocol.errorURLs[url] = (data: MockConstants.jsonStub(fromFilename: "rpc_error_gas"), response: MockConstants.http200) } - static func triggerAssertErrorOnRunOperation(nodeUrl: Int = 0) { - let url = MockConstants.shared.config.nodeURLs[nodeUrl].appendingPathComponent("chains/main/blocks/head/helpers/scripts/run_operation") + static func triggerAssertErrorOnSimulateOperation(nodeUrl: Int = 0) { + let url = MockConstants.shared.config.nodeURLs[nodeUrl].appendingPathComponent("chains/main/blocks/head/helpers/scripts/simulate_operation") MockURLProtocol.errorURLs[url] = (data: MockConstants.jsonStub(fromFilename: "rpc_error_assert"), response: MockConstants.http200) } @@ -166,8 +166,8 @@ class MockURLProtocol: URLProtocol { MockURLProtocol.errorURLs[url] = (data: MockConstants.jsonStub(fromFilename: "rpc_error_counter-in-future"), response: MockConstants.http500) } - static func triggerHttp500ErrorOnRunOperation(nodeUrl: Int = 0) { - let url = MockConstants.shared.config.nodeURLs[nodeUrl].appendingPathComponent("chains/main/blocks/head/helpers/scripts/run_operation") + static func triggerHttp500ErrorOnSimulateOperation(nodeUrl: Int = 0) { + let url = MockConstants.shared.config.nodeURLs[nodeUrl].appendingPathComponent("chains/main/blocks/head/helpers/scripts/simulate_operation") MockURLProtocol.errorURLs[url] = (data: nil, response: MockConstants.http500) } } diff --git a/Tests/KukaiCoreSwiftTests/Services/NetworkServiceTests.swift b/Tests/KukaiCoreSwiftTests/Services/NetworkServiceTests.swift index b6cd7007..99922681 100644 --- a/Tests/KukaiCoreSwiftTests/Services/NetworkServiceTests.swift +++ b/Tests/KukaiCoreSwiftTests/Services/NetworkServiceTests.swift @@ -62,12 +62,12 @@ class NetworkServiceTests: XCTestCase { } func testErrorGas() { - MockURLProtocol.triggerGasExhaustedErrorOnRunOperation(nodeUrl: 0) - MockURLProtocol.triggerGasExhaustedErrorOnRunOperation(nodeUrl: 1) + MockURLProtocol.triggerGasExhaustedErrorOnSimulateOperation(nodeUrl: 0) + MockURLProtocol.triggerGasExhaustedErrorOnSimulateOperation(nodeUrl: 1) let expectation = XCTestExpectation(description: "network service error gas") - if let rpc = RPC.runOperation(runOperationPayload: RunOperationPayload(chainID: MockConstants.blockchainHead.chainID, operation: MockConstants.sendOperationPayload)) { + if let rpc = RPC.simulateOperation(runOperationPayload: RunOperationPayload(chainID: MockConstants.blockchainHead.chainID, operation: MockConstants.sendOperationPayload)) { MockConstants.shared.networkService.send(rpc: rpc, withNodeURLs: MockConstants.shared.config.nodeURLs) { result in switch result { @@ -76,7 +76,7 @@ class NetworkServiceTests: XCTestCase { case .failure(let error): XCTAssert(error.errorType == .rpc, error.description) - XCTAssert(error.requestURL?.absoluteString == MockConstants.shared.config.nodeURLs[1].appendingPathComponent("chains/main/blocks/head/helpers/scripts/run_operation").absoluteString, error.requestURL?.absoluteString ?? "-") + XCTAssert(error.requestURL?.absoluteString == MockConstants.shared.config.nodeURLs[1].appendingPathComponent("chains/main/blocks/head/helpers/scripts/simulate_operation").absoluteString, error.requestURL?.absoluteString ?? "-") XCTAssert(error.requestJSON != nil) XCTAssert(error.responseJSON != nil) } @@ -92,11 +92,11 @@ class NetworkServiceTests: XCTestCase { } func testErrorAssert() { - MockURLProtocol.triggerAssertErrorOnRunOperation() + MockURLProtocol.triggerAssertErrorOnSimulateOperation() let expectation = XCTestExpectation(description: "network service error gas") - if let rpc = RPC.runOperation(runOperationPayload: RunOperationPayload(chainID: MockConstants.blockchainHead.chainID, operation: MockConstants.sendOperationPayload)) { + if let rpc = RPC.simulateOperation(runOperationPayload: RunOperationPayload(chainID: MockConstants.blockchainHead.chainID, operation: MockConstants.sendOperationPayload)) { MockConstants.shared.networkService.send(rpc: rpc, withNodeURLs: MockConstants.shared.config.nodeURLs) { result in switch result { @@ -106,7 +106,7 @@ class NetworkServiceTests: XCTestCase { case .failure(let error): XCTAssert(error.errorType == .unknown) XCTAssert(error.description == "Unknown: Assert_failure src/proto_009_PsFLoren/lib_protocol/operation_repr.ml:203:6", error.description) - XCTAssert(error.requestURL?.absoluteString == MockConstants.shared.config.nodeURLs[0].appendingPathComponent("chains/main/blocks/head/helpers/scripts/run_operation").absoluteString) + XCTAssert(error.requestURL?.absoluteString == MockConstants.shared.config.nodeURLs[0].appendingPathComponent("chains/main/blocks/head/helpers/scripts/simulate_operation").absoluteString) XCTAssert(error.requestJSON != nil) XCTAssert(error.responseJSON != nil) } @@ -183,11 +183,11 @@ class NetworkServiceTests: XCTestCase { } func testRpcRetryLogic() { - MockURLProtocol.triggerHttp500ErrorOnRunOperation() + MockURLProtocol.triggerHttp500ErrorOnSimulateOperation() let expectation = XCTestExpectation(description: "network service retry logic") - if let rpc = RPC.runOperation(runOperationPayload: RunOperationPayload(chainID: MockConstants.blockchainHead.chainID, operation: MockConstants.sendOperationPayload)) { + if let rpc = RPC.simulateOperation(runOperationPayload: RunOperationPayload(chainID: MockConstants.blockchainHead.chainID, operation: MockConstants.sendOperationPayload)) { MockConstants.shared.networkService.send(rpc: rpc, withNodeURLs: MockConstants.shared.config.nodeURLs) { result in switch result {