Skip to content

Commit

Permalink
Finished implementation of fetchNodeDetails (sync):
Browse files Browse the repository at this point in the history
  • Loading branch information
metallicalfa2 committed Mar 18, 2020
1 parent 3f9d6db commit 0c460b9
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 89 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
ReferencedContainer = "container:">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "fetch_node_detailsTests/testBigIntCapabilities()">
</Test>
<Test
Identifier = "fetch_node_detailsTests/testExample()">
</Test>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,4 @@
uuid = "F11E7A9B-0F4D-4A44-B120-0886E4AB461D"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "3CC16AC8-5A3F-4FA9-98C7-E25D520A3971"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Sources/fetch-node-details/fetchNodeDetails.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "147"
endingLineNumber = "147"
landmarkName = "getNodeDetails()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
File renamed without changes.
13 changes: 13 additions & 0 deletions Sources/fetch-node-details/extensions/String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// String.swift
//
//
// Created by Shubham on 18/3/20.
//
import Foundation

// Used in error thrown from guard let
extension String: LocalizedError {
public var errorDescription: String? { return self }
}

102 changes: 50 additions & 52 deletions Sources/fetch-node-details/fetchNodeDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,74 +16,70 @@ public class FetchNodeDetails {
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


public init(){
self.web3 = Web3.InfuraMainnetWeb3()
self.nodeDetails = NodeDetails()
self.contract = web3.contract(yourContractABI, at: proxyAddress, abiVersion: 2)!
self.nodeDetails = NodeDetails() //Initialize with default values
}

public func getCurrentEpoch() -> Int{

//let abiVersion: Int = 2
let contract = web3.contract(yourContractABI, at: proxyAddress, abiVersion: 2)!
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 = contract.read(
let tx = self.contract.read(
contractMethod,
parameters: parameters,
extraData: extraData,
transactionOptions: options)!

let result : [String:Any] = try! tx.call()
//print("result is", type(of: result.first?.value), result)
let result : [String:Any] = try! tx.call() // Explicit Conversion from Any? -> Any -> String -> Int
let epoch = result.first?.value

// Explicit Conversion from Any? -> Any -> String -> Int
guard let newEpoch = epoch else { return 0}
//print(epoch, newEpoch)
return Int("\(newEpoch)")!
}

public func getEpochInfo(epoch : Int) throws -> EpochInfo{
let contractMethod = "getEpochInfo"
let parameters: [AnyObject] = [18 as AnyObject] // Parameters for contract method
let extraData: Data = Data() // Extra data for contract method
let contract = web3.contract(yourContractABI, at: proxyAddress, abiVersion: 2)!
var options = TransactionOptions.defaultOptions
options.from = walletAddress
options.gasPrice = .automatic
options.gasLimit = .automatic
//print(extraData)

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

let result = try! tx.call()
print(result.keys)
//print(result.keys)
//print(result["prevEpoch"])

var nodeList = result["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)
//print(nodeList)

guard let id = result["id"] else { throw "asdf"}
guard let n = result["n"] else { throw "asdf"}
guard let k = result["k"] else { throw "asdf"}
guard let t = result["t"] else { throw "asdf"}
guard let prevEpoch = result["prevEpoch"] else { throw "asdf"}
guard let nextEpoch = result["nextEpoch"] else { throw "asdf"}
guard let id = result["id"] else { throw "Casting for id from Any? -> Any failed"}
guard let n = result["n"] else { throw "Casting for n from Any? -> Any failed"}
guard let k = result["k"] else { throw "Casting for k from Any? -> Any failed"}
guard let t = result["t"] else { throw "Casting for t from Any? -> Any failed"}
guard let prevEpoch = result["prevEpoch"] else { throw "Casting for prevEpoch from Any? -> Any failed"}
guard let nextEpoch = result["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)")
return object
Expand All @@ -93,47 +89,39 @@ public class FetchNodeDetails {
let contractMethod = "getNodeDetails"
let parameters: [AnyObject] = [nodeEthAddress as AnyObject] // Parameters for contract method
let extraData: Data = Data() // Extra data for contract method
let contract = web3.contract(yourContractABI, at: proxyAddress, abiVersion: 2)!
var options = TransactionOptions.defaultOptions
options.from = walletAddress
options.gasPrice = .automatic
options.gasLimit = .automatic
//print(extraData)

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

let result = try! tx.call()
print(result.keys)
print(result.values)
//print(result.keys)
//print(result.values)

// Unwraping Any? -> Any
guard let declaredIp = result["declaredIp"] else { throw "some error" }
guard let position = result["position"] else { throw "some error" }
guard let pubKx = result["pubKx"] else { throw "some error" }
guard let pubKy = result["pubKy"] else { throw "some error" }
guard let tmP2PListenAddress = result["tmP2PListenAddress"] else { throw "some error" }
guard let p2pListenAddress = result["p2pListenAddress"] else { throw "some error" }
guard let declaredIp = result["declaredIp"] else { throw "Casting for declaredIp from Any? to Any failed" }
guard let position = result["position"] else { throw "Casting for position from Any? to Any failed" }
guard let pubKx = result["pubKx"] else { throw "Casting for pubKx from Any? to Any failed" }
guard let pubKy = result["pubKy"] else { throw "Casting for pubKy from Any? to Any failed" }
guard let tmP2PListenAddress = result["tmP2PListenAddress"] else { throw "Casting for tmP2PListenAddress from Any? to Any failed" }
guard let p2pListenAddress = result["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)")
return object
}

public func getNodeDetails(){
//if(self.nodeDetails.getUpdated()) return this.nodeDetails
let currentEpoch = self.getCurrentEpoch();
print(self.proxyAddress.address)
// let newNodeDetails = NodeDetails()

self.nodeDetails.setNodeListAddress(nodeListAddress: self.proxyAddress.address);
self.nodeDetails.setCurrentEpoch(currentEpoch: String(currentEpoch));
// self.nodeDetails.setTorusNodeIndexes()

public func getNodeDetails() -> NodeDetails{
if(self.nodeDetails.getUpdated()) { return self.nodeDetails }

let currentEpoch = self.getCurrentEpoch();
let epochInfo = try! getEpochInfo(epoch: currentEpoch);
let nodelist = epochInfo.getNodeList();

Expand All @@ -144,23 +132,33 @@ public class FetchNodeDetails {
torusIndexes.append(BigInt(i+1))
nodeEndPoints.append(try! getNodeEndpoint(nodeEthAddress: nodelist[i]))
}
// print(torusIndexes, nodeEndPoints)

var updatedEndpoints: Array<String> = Array()
var updatedNodePub:Array<TorusNodePub> = Array()

for i in 0..<nodeEndPoints.count{
let endPointElement:NodeInfo = nodeEndPoints[i];
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))
//print(hexPubX,hexPubY)
}

// print(updatedNodePub, updatedEndpoints)

}

private func setupWeb3() {
//let contractAddress = try! EthereumAddress(hex: self.proxyAddress, eip55: false)
//self.proxyContract = try! self.web3.eth.Contract(json: Data(contractABIString.utf8), abiKey: nil, address: contractAddress)
//print(self.proxyContract.methods.count)
//self.getCurrentEpoch()
// print(self.proxyContract)
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);
return self.nodeDetails
}

private func getProxyUrl() -> String{
return "https://api.infura.io/v1/jsonrpc/" + self.network.rawValue;
}
}

extension String: LocalizedError {
public var errorDescription: String? { return self }
}
9 changes: 5 additions & 4 deletions Sources/fetch-node-details/nodeDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ public class NodeDetails {
private var nodeListAddress : String?;
private var torusNodeEndpoints : Array<String>?;
private var torusIndexes : Array<BigInt>?;
private var torusNodePub : Array<Array<String>>?;
private var torusNodePub : Array<TorusNodePub>?;
private var updated = false;

init() {

}

init(_currentEpoch : String, _nodeListAddress : String, _torusNodeEndpoints : Array<String>, _torusIndexes : Array<BigInt>, _torusNodePub : Array<Array<String>>, _updated : Bool) {
// Not currently in use
init(_currentEpoch : String, _nodeListAddress : String, _torusNodeEndpoints : Array<String>, _torusIndexes : Array<BigInt>, _torusNodePub : Array<TorusNodePub>, _updated : Bool) {
self.currentEpoch = _currentEpoch;
self.nodeListAddress = _nodeListAddress;
self.torusNodeEndpoints = _torusNodeEndpoints;
Expand Down Expand Up @@ -69,11 +70,11 @@ public class NodeDetails {
self.torusNodeEndpoints = torusNodeEndpoints;
}

public func getTorusNodePub() -> Array<Array<String>> {
public func getTorusNodePub() -> Array<TorusNodePub> {
return torusNodePub!;
}

public func setTorusNodePub(torusNodePub : Array<Array<String>>) {
public func setTorusNodePub(torusNodePub : Array<TorusNodePub>) {
self.torusNodePub = torusNodePub;
}
}
1 change: 0 additions & 1 deletion Sources/fetch-node-details/nodeInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation


public class NodeInfo {
private var declaredIp: String;
private var position: String;
Expand Down
26 changes: 12 additions & 14 deletions Tests/fetch-node-detailsTests/fetch_node_detailsTests.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import XCTest
import BigInt
@testable import fetch_node_details

final class fetch_node_detailsTests: XCTestCase {
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
let fnd = FetchNodeDetails();
//let currentEpoch = fnd.getCurrentEpoch()
//print("currentEpochValue", currentEpoch)
//let epochInfo = fnd.getEpochInfo(epoch: currentEpoch)
//let nodeDetails = fnd.getNodeEndpoint(nodeEthAddress: "0x40e8f0D606281b0a1d9D8Ac9030AaaE9D51229D1")

//let details = fnd.getNodeDetails()
}

func testGetNodeEndpoints(){
let fnd = FetchNodeDetails();
Expand All @@ -24,11 +13,20 @@ final class fetch_node_detailsTests: XCTestCase {
func testGetNodeDetails(){
let fnd = FetchNodeDetails();
let details = fnd.getNodeDetails()
print(details)
}

func testBigIntCapabilities(){
var a = "107082045066154177533035871355946698712954231105108535436156312542445529617311"
var newint = BigInt(a, radix:10)
//print(newint, BigInt(newint!, radix: 16))
print(String(newint!, radix:16, uppercase: true))

}

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

0 comments on commit 0c460b9

Please sign in to comment.