Skip to content

Commit

Permalink
Merge pull request #1 from torusresearch/promiseImplementation
Browse files Browse the repository at this point in the history
Promise implementation
  • Loading branch information
rathishubham7 authored Mar 24, 2020
2 parents 0c460b9 + f36ce9c commit 9b0eb1a
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 12 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
<Test
Identifier = "fetch_node_detailsTests/testGetNodeEndpoints()">
</Test>
<Test
Identifier = "fetch_node_detailsTestsAsync/test_async_getCurrentEpoch()">
</Test>
<Test
Identifier = "fetch_node_detailsTestsSync">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let package = Package(
.package(url: "https://github.com/attaswift/BigInt.git", from: "5.0.0"),
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.8.4"),
.package(url: "https://github.com/daltoniam/Starscream.git", from: "3.1.0"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.0.0"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.0.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
3 changes: 0 additions & 3 deletions README 2.md

This file was deleted.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# fetch-node-details

A description of this package.

## Installation

### Swift Package Manage

```
.package(url: "https://github.com/torusresearch/fetch-node-details-swift5.git", from: "0.0.1"),
```
187 changes: 187 additions & 0 deletions Sources/fetch-node-details/fetchNodeDetails+promise.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
//
// File.swift
//
//
// Created by Shubham on 13/3/20.
//

import Foundation
import web3swift
import BigInt
import PromiseKit

extension FetchNodeDetails {

public func getCurrentEpochPromise() throws -> Promise<Int>{
let contractMethod = "currentEpoch" // Contract method you want to call
let parameters: [AnyObject] = [] // Parameters for contract method
let extraData: Data = Data() // Extra data for contract method
var options = TransactionOptions.defaultOptions
options.from = walletAddress
options.gasPrice = .automatic
options.gasLimit = .automatic
let tx = self.contract.read(
contractMethod,
parameters: parameters,
extraData: extraData,
transactionOptions: options)!

let txPromise = tx.callPromise()
let rp = Promise<Int>.pending()

txPromise.done{ data in
let epoch = data.first?.value
guard let newEpoch = epoch else { throw "some error"}
print(newEpoch)
rp.resolver.fulfill(Int("\(newEpoch)")!)
}

return rp.promise
}

public func getEpochInfoPromise(epoch : Int) throws -> Promise<EpochInfo>{
let contractMethod = "getEpochInfo"
let parameters: [AnyObject] = [epoch as AnyObject] // Parameters for contract method
let extraData: Data = Data() // Extra data for contract method
var options = TransactionOptions.defaultOptions
options.from = walletAddress
options.gasPrice = .automatic
options.gasLimit = .automatic

let tx = self.contract.read(
contractMethod,
parameters: parameters,
extraData: extraData,
transactionOptions: options)!
//
// let result = tx.callPromise()
// let (promise, resolver) = Promise<EpochInfo>.pending()

let returnPromise = Promise<EpochInfo>{ seal in
let txPromise = tx.callPromise()
txPromise.done{ response in
var nodeList = response["nodeList"] as! Array<Encodable> //Unable to convert to Array<String>
nodeList = nodeList.map{ (el) -> String in
let address = el as! EthereumAddress
return String(describing: address.address)
}
//print(nodeList)

guard let id = response["id"] else { throw "Casting for id from Any? -> Any failed"}
guard let n = response["n"] else { throw "Casting for n from Any? -> Any failed"}
guard let k = response["k"] else { throw "Casting for k from Any? -> Any failed"}
guard let t = response["t"] else { throw "Casting for t from Any? -> Any failed"}
guard let prevEpoch = response["prevEpoch"] else { throw "Casting for prevEpoch from Any? -> Any failed"}
guard let nextEpoch = response["nextEpoch"] else { throw "Casting for nextEpoch from Any? -> Any failed"}

let object = EpochInfo(_id: "\(id)", _n: "\(n)", _k: "\(k)", _t: "\(t)", _nodeList: nodeList as! Array<String>, _prevEpoch: "\(prevEpoch)", _nextEpoch: "\(nextEpoch)")
print()
seal.fulfill(object)

}.catch{error in
seal.reject(error)
}

}
return returnPromise
}

public func getNodeEndpointPromise(nodeEthAddress: String) throws -> Promise<NodeInfo> {
let contractMethod = "getNodeDetails"
let parameters: [AnyObject] = [nodeEthAddress as AnyObject] // Parameters for contract method
let extraData: Data = Data() // Extra data for contract method
var options = TransactionOptions.defaultOptions
options.from = walletAddress
options.gasPrice = .automatic
options.gasLimit = .automatic
//print(extraData)

let tx = self.contract.read(
contractMethod,
parameters: parameters,
extraData: extraData,
transactionOptions: options)!

let returnPromise = Promise<NodeInfo>{ seal in
let txPromise = tx.callPromise()
txPromise.done{ response in
// Unwraping Any? -> Any
guard let declaredIp = response["declaredIp"] else { throw "Casting for declaredIp from Any? to Any failed" }
guard let position = response["position"] else { throw "Casting for position from Any? to Any failed" }
guard let pubKx = response["pubKx"] else { throw "Casting for pubKx from Any? to Any failed" }
guard let pubKy = response["pubKy"] else { throw "Casting for pubKy from Any? to Any failed" }
guard let tmP2PListenAddress = response["tmP2PListenAddress"] else { throw "Casting for tmP2PListenAddress from Any? to Any failed" }
guard let p2pListenAddress = response["p2pListenAddress"] else { throw "Casting for p2pListenAddress from Any? to Any failed" }

let object = NodeInfo(_declaredIp: "\(declaredIp)", _position: "\(position)", _pubKx: "\(pubKx)", _pubKy: "\(pubKy)", _tmP2PListenAddress: "\(tmP2PListenAddress)", _p2pListenAddress: "\(p2pListenAddress)")
seal.fulfill(object)
}.catch{err in seal.reject(err)}
}
return returnPromise
}

public func getNodeDetailsPromise() throws -> Promise<Bool>{
//if(self.nodeDetails.getUpdated()) { return self.nodeDetails }
//let (promise, resolver) = Promise<Int>.pending()
print("ASDF")
var currentEpoch: Int = -1;

let returnPromise = Promise<Bool> { seal in
let currentEpochPromise = try self.getCurrentEpochPromise();
currentEpochPromise.then{ response -> Promise<EpochInfo> in
print("currentEpoch is", response)
currentEpoch = response
return try self.getEpochInfoPromise(epoch: response)
}.then{epochInfo -> Guarantee<[Result<NodeInfo>]> in
let nodelist = epochInfo.getNodeList();
print("nodeList is", nodelist)
var torusIndexes:[BigInt] = Array()
var nodeEndPoints:[NodeInfo] = Array()
var getNodeInfoPromisesArray:[Promise<NodeInfo>] = Array()
for i in 0..<nodelist.count{
torusIndexes.append(BigInt(i+1))
//print(BigInt(i+1))
getNodeInfoPromisesArray.append(try self.getNodeEndpointPromise(nodeEthAddress: nodelist[i]))
}
return when(resolved: getNodeInfoPromisesArray)
}.done{ results in
//print(results)
var updatedEndpoints: Array<String> = Array()
var updatedNodePub:Array<TorusNodePub> = Array()

for result in results{
switch result {
case .fulfilled(let value):
// print(value)
let endPointElement:NodeInfo = value;
let endpoint = "https://" + endPointElement.getDeclaredIp().split(separator: ":")[0] + "/jrpc";
updatedEndpoints.append(endpoint)

let hexPubX = String(BigInt(endPointElement.getPubKx(), radix:10)!, radix:16, uppercase: true)
let hexPubY = String(BigInt(endPointElement.getPubKy(), radix:10)!, radix:16, uppercase: true)
updatedNodePub.append(TorusNodePub(_X: hexPubX, _Y: hexPubY))
default:
seal.reject("error with node info")
}

}
print(updatedNodePub, updatedEndpoints)


self.nodeDetails.setNodeListAddress(nodeListAddress: self.proxyAddress.address);
self.nodeDetails.setCurrentEpoch(currentEpoch: String(currentEpoch));
self.nodeDetails.setTorusNodeEndpoints(torusNodeEndpoints: updatedEndpoints);
self.nodeDetails.setTorusNodePub(torusNodePub: updatedNodePub);
self.nodeDetails.setUpdated(updated: true);

seal.fulfill(true)
}.catch { error in
print(error)
seal.reject("get epoch info failed")
}
}

return returnPromise
}

}
14 changes: 7 additions & 7 deletions Sources/fetch-node-details/fetchNodeDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import BigInt

public class FetchNodeDetails {

private var web3 : web3
private var network : EthereumNetwork = EthereumNetwork.MAINNET;
private var proxyAddress : EthereumAddress = EthereumAddress("0x638646503746d5456209e33a2ff5e3226d698bea")!
private var walletAddress : EthereumAddress = EthereumAddress("0x5F7A02a42bF621da3211aCE9c120a47AA5229fBA")!
private let yourContractABI: String = contractABIString
private var contract : web3.web3contract
private var nodeDetails : NodeDetails
var web3 : web3
var network : EthereumNetwork = EthereumNetwork.MAINNET;
var proxyAddress : EthereumAddress = EthereumAddress("0x638646503746d5456209e33a2ff5e3226d698bea")!
var walletAddress : EthereumAddress = EthereumAddress("0x5F7A02a42bF621da3211aCE9c120a47AA5229fBA")!
let yourContractABI: String = contractABIString
var contract : web3.web3contract
var nodeDetails : NodeDetails


public init(){
Expand Down
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import fetch_node_detailsTests

var tests = [XCTestCaseEntry]()
tests += fetch_node_detailsTests.allTests()
tests +=fetch_node_detailsTestsAsync.allTests()
XCTMain(tests)
1 change: 1 addition & 0 deletions Tests/fetch-node-detailsTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import XCTest
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(fetch_node_detailsTests.allTests),
testCase(fetch_node_detailsTestsAsync.allTests)
]
}
#endif
46 changes: 45 additions & 1 deletion Tests/fetch-node-detailsTests/fetch_node_detailsTests.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import XCTest
import BigInt
import PromiseKit
@testable import fetch_node_details

final class fetch_node_detailsTests: XCTestCase {
final class fetch_node_detailsTestsSync: XCTestCase {

func testgetCurrentEpoch(){
let fnd = FetchNodeDetails();
let test = fnd.getCurrentEpoch();
print(test)
}
func testGetNodeEndpoints(){
let fnd = FetchNodeDetails();
let details = try! fnd.getNodeEndpoint(nodeEthAddress: "0x40e8f0D606281b0a1d9D8Ac9030AaaE9D51229D1")
Expand All @@ -25,8 +31,46 @@ final class fetch_node_detailsTests: XCTestCase {
}

static var allTests = [
("testgetCurrentEpoch", testgetCurrentEpoch),
("testGetNodeEndpoints", testGetNodeEndpoints),
("testGetNodeDetails", testGetNodeDetails),
("testBigIntCapabilities", testBigIntCapabilities)
]
}

final class fetch_node_detailsTestsAsync: XCTestCase{

func test_async_getCurrentEpoch(){
let fnd = FetchNodeDetails();
let test = try! fnd.getCurrentEpochPromise()
print(test)
test.done{ result in
print("result", result)
}
do{
sleep(5)
}
//print(test)
}

func test_getNodeDetailsPromise(){
let fnd = FetchNodeDetails();
let expectation = self.expectation(description: "getting node details")
var results = false;
try! fnd.getNodeDetailsPromise().done{ response in
print("response", response);
results = response
expectation.fulfill()
}.catch{err in print(err)}

waitForExpectations(timeout: 20)
XCTAssertEqual(results, true)

}

static var allTests = [
("test_async_getCurrentEpoch", test_async_getCurrentEpoch),
("test_getNodeDetailsPromise", test_getNodeDetailsPromise)

]
}

0 comments on commit 9b0eb1a

Please sign in to comment.