Skip to content

Commit

Permalink
migrate fully to simulate operation, leave some old things named as R…
Browse files Browse the repository at this point in the history
…unOperation, may rename in future
  • Loading branch information
simonmcl committed Jan 30, 2024
1 parent 4913964 commit 085f803
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
12 changes: 10 additions & 2 deletions Sources/KukaiCoreSwift/Models/RPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<OperationResponse>? {
guard let payloadData = RPC.encodableToData(encodable: runOperationPayload) else {
return nil
}

//return RPC<OperationResponse>(endpoint: "chains/main/blocks/head/helpers/scripts/run_operation", payload: payloadData, responseType: OperationResponse.self)
return RPC<OperationResponse>(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<OperationResponse>? {
guard let payloadData = RPC.encodableToData(encodable: runOperationPayload) else {
return nil
}

return RPC<OperationResponse>(endpoint: "chains/main/blocks/head/helpers/scripts/simulate_operation", payload: payloadData, responseType: OperationResponse.self)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/KukaiCoreSwift/Services/FeeEstimatorService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<EstimationResult, KukaiError>) -> 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
Expand Down
2 changes: 2 additions & 0 deletions Tests/KukaiCoreSwiftTests/MockConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
12 changes: 6 additions & 6 deletions Tests/KukaiCoreSwiftTests/MockURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
}
18 changes: 9 additions & 9 deletions Tests/KukaiCoreSwiftTests/Services/NetworkServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 085f803

Please sign in to comment.