/// <summary> /// Fetch the the NetworkSettings, and set the NetworkSettings variable, /// which is used for every subsequent request after the initial ones. /// </summary> /// <param name="initialPeer">Initial Peer <inheritdoc cref="PeerApi"/> from which the settings are fetched.</param> /// <returns>Instiate a <inheritdoc cref="PeerApi"/> based on the initial peer provided.</returns> private async Task SetNetworkSettings(PeerApi initialPeer) { // Request the NetworkSettings, Fees, and more peer address from the peers it connects to. var responseAutoConfigure = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, ArkStaticStrings.ArkApiPaths.Loader.GET_AUTO_CONFIGURE).ConfigureAwait(false); var responseFees = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, ArkStaticStrings.ArkApiPaths.Block.GET_FEES).ConfigureAwait(false); var responsePeer = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, string.Format(ArkStaticStrings.ArkApiPaths.Peer.GET, initialPeer.Ip, initialPeer.Port)).ConfigureAwait(false); // Auto-configures what has been fetched previously var autoConfig = JsonConvert.DeserializeObject <ArkLoaderNetworkResponse>(responseAutoConfigure); var fees = JsonConvert.DeserializeObject <Fees>(JObject.Parse(responseFees)["fees"].ToString()); var peer = JsonConvert.DeserializeObject <ArkPeerResponse>(responsePeer); // Fill the NetworkSettings with what has been fetched / auto-configured previously. NetworkApi.NetworkSettings = new ArkNetworkSettings() { Port = initialPeer.Port, BytePrefix = (byte)autoConfig.Network.Version, Version = peer.Peer.Version, NetHash = autoConfig.Network.NetHash, Fee = fees }; await NetworkApi.WarmUp(new PeerApi(NetworkApi, initialPeer.Ip, initialPeer.Port)).ConfigureAwait(false); }
private async Task SetNetworkSettings(PeerApi initialPeer) { var responseAutoConfigure = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, ArkStaticStrings.ArkApiPaths.Loader.GET_AUTO_CONFIGURE); var responseFees = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, ArkStaticStrings.ArkApiPaths.Block.GET_FEES); var responsePeer = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, string.Format(ArkStaticStrings.ArkApiPaths.Peer.GET, initialPeer.ip, initialPeer.port)); var autoConfig = JsonConvert.DeserializeObject <ArkLoaderNetworkResponse>(responseAutoConfigure); var fees = JsonConvert.DeserializeObject <Fees>(JObject.Parse(responseFees)["fees"].ToString()); var peer = JsonConvert.DeserializeObject <ArkPeerResponse>(responsePeer); NetworkSettings = new ArkNetworkSettings() { Port = initialPeer.port, BytePrefix = (byte)autoConfig.Network.Version, Version = peer.Peer.Version, NetHash = autoConfig.Network.NetHash, MaxNumOfBroadcasts = 5, Fee = fees, PeerSeedList = new List <string> { string.Format("{0}:{1}", initialPeer.ip, initialPeer.port) } }; }
/// <summary> /// Is needed to fetch the peer. /// </summary> /// <param name="peers">List of peers to try</param> /// <param name="retryCount">Number of retry before a timeout</param> /// <returns></returns> private async Task <PeerApi> GetInitialPeer(List <ArkPeerAddress> peers, int retryCount = 0) { try { //Picks a peer randomly from the list var peerUrl = peers[new Random().Next(peers.Count)]; LoggingApi.Info(string.Format("Getting initial peer. ip: <<{0}>>, port: <<{1}>>, retrycount: <<{2}>>", peerUrl.Ip, peerUrl.Port, retryCount)); // create a peer out of peerurl, and returns if the peer is online. // var peer = new PeerApi(this, peerUrl.Ip, peerUrl.Port); if (await peer.IsOnline().ConfigureAwait(false)) { return(peer); } LoggingApi.Warn(string.Format("Peer is not online. ip: <<{0}>>, port: <<{1}>>", peerUrl.Ip, peerUrl.Port)); // Throw an exception if all of the initial peers have been tried. // if (retryCount == peers.Count) { LoggingApi.Error(string.Format("Unable to connect to a seed peer. Retried <<{0}>> times", retryCount)); throw new Exception("Unable to connect to a seed peer"); } // redo the check and increment the retry count // return(await GetInitialPeer(peers, retryCount + 1).ConfigureAwait(false)); } catch (Exception e) { LoggingApi.Error(e.ToString()); throw e; } }
public bool Start() { if (mainLoop != null) { throw new Exception("Service is already running!"); } golemApi = new PeerApi(Options.GolemHubUrl); ServerPort = Options.GolemServerPort; taskQueue = new ConcurrentQueue <CompilationTask>(); cancellationSource = new System.Threading.CancellationTokenSource(); //run the hub info loop (peer discovery) hubInfoLoop = GolemHubQueryTask(cancellationSource.Token); //hubInfoLoop.Start(); //run main task loop mainLoop = TaskDispatcher(cancellationSource.Token); //run the http server httpService = new GolemHttpService(); httpService.Start(); return(mainLoop != null); }
/// <summary> /// Is needed to fetch the peer. /// </summary> /// <param name="type"> /// <inheritdoc cref="NetworkType"/>Can be : /// -- DevNet (test), ask Dark (testnet coins) on the slack. /// -- MainNet (live, beware real money, financial loss possible there). /// </param> /// <param name="retryCount">Number of retry before a timeout</param> /// <returns>Returns the first <inheritdoc cref="PeerApi"/> that is online</returns> private async Task <PeerApi> GetInitialPeer(NetworkType type, int retryCount = 0) { // Pick a peer randomly in _peerSeedListMainNet // var peerUrl = _peerSeedListMainNet[new Random().Next(_peerSeedListMainNet.Count)]; // If the Network is set to DevNet, change the peer picked above by a peer from _peerSeedListDevNet // if (type == NetworkType.DevNet) { peerUrl = _peerSeedListDevNet[new Random().Next(_peerSeedListDevNet.Count)]; } // create a peer out of peerurl, and returns if the peer is online. // var peer = new PeerApi(NetworkApi, peerUrl.Item1, peerUrl.Item2); if (await peer.IsOnline().ConfigureAwait(false)) { return(peer); } // Throw an exception if all of the initial peers have been tried. // if ((type == NetworkType.DevNet && retryCount == _peerSeedListDevNet.Count) || (type == NetworkType.MainNet && retryCount == _peerSeedListMainNet.Count)) { throw new Exception("Unable to connect to a seed peer"); } // redo the check and increment the retry count // return(await GetInitialPeer(type, retryCount + 1).ConfigureAwait(false)); }
public async Task Start(NetworkType type) { var initialPeer = new PeerApi("5.39.9.240:4001"); if (type == NetworkType.DevNet) { initialPeer = new PeerApi("167.114.29.55:4002"); } await SetNetworkSettings(initialPeer); await NetworkApi.Instance.WarmUp(); }
private async Task TaskProc(PeerApi golemApi, DeploymentSpec spec) { try { if (deployment == null) { deployment = spec; deploymentID = await golemApi.CreateDeploymentAsync(Peer.NodeId, deployment); } //1. Take all input files and includes and package them into one TAR package + notify HttpServer about that file string packedFileName = PackFilesPreProcessed(taskList); //2. Create command to compile those source files -> cl.exe .... ExecCommand compileCmd = GenerateCompileCommand(packedFileName, taskList); var results = await golemApi.UpdateDeploymentAsync(Peer.NodeId, deploymentID, new List <Command>() { new DownloadFileCommand(Service.GetHttpDownloadUri(packedFileName), packedFileName + ".tar", FileFormat.Tar), compileCmd }); bool error = false; string[] lines = results[0].Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); foreach (string line in lines) { if (line.Contains(" error") || line.Contains("fatal error")) { Logger.LogError("[ERROR] " + packedFileName + ": " + line); error = true; } else if (line.Contains("warning")) { Logger.LogMessage("[WARNING] " + packedFileName + ": " + line); } } if (!error) { Logger.LogMessage("[SUCCESS] " + packedFileName); // Upload output.zip results = await golemApi.UpdateDeploymentAsync(Peer.NodeId, deploymentID, new List <Command>() { new UploadFileCommand(Service.GetHttpUploadUri(packedFileName), packedFileName + ".tar/output.zip") }); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Fetch the the NetworkSettings, and set the NetworkSettings variable, /// which is used for every subsequent request after the initial ones. /// </summary> /// <param name="initialPeer">Initial Peer <inheritdoc cref="PeerApi"/> from which the settings are fetched.</param> /// <returns>Instiate a <inheritdoc cref="PeerApi"/> based on the initial peer provided.</returns> private async Task SetNetworkSettings(PeerApi initialPeer) { try { // Request the NetworkSettings, Fees, and more peer address from the peers it connects to. var responseAutoConfigure = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, ArkStaticStrings.ArkApiPaths.Loader.GET_AUTO_CONFIGURE).ConfigureAwait(false); var responseFees = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, ArkStaticStrings.ArkApiPaths.Block.GET_FEES).ConfigureAwait(false); var responsePeer = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, string.Format(ArkStaticStrings.ArkApiPaths.Peer.GET, initialPeer.Ip, initialPeer.Port)).ConfigureAwait(false); // Auto-configures what has been fetched previously var autoConfig = JsonConvert.DeserializeObject <ArkLoaderNetworkResponse>(responseAutoConfigure); var fees = JsonConvert.DeserializeObject <Fees>(JObject.Parse(responseFees)["fees"].ToString()); var peer = JsonConvert.DeserializeObject <ArkPeerResponse>(responsePeer); // Fill the NetworkSettings with what has been fetched / auto-configured previously. NetworkApi.NetworkSettings = new ArkNetworkSettings() { Port = initialPeer.Port, BytePrefix = (byte)autoConfig.Network.Version, Version = peer.Peer.Version, NetHash = autoConfig.Network.NetHash, Fee = fees, Token = autoConfig.Network.Token, Symbol = autoConfig.Network.Symbol, Explorer = autoConfig.Network.Explorer }; LoggingApi.Info(string.Format("Set network settings to <<{0}>>", JsonConvert.SerializeObject(NetworkApi.NetworkSettings))); await NetworkApi.WarmUp(new PeerApi(this, initialPeer.Ip, initialPeer.Port)).ConfigureAwait(false); } catch (Exception e) { LoggingApi.Error(e.ToString()); throw e; } }
/// <summary> /// Is needed to fetch the peer. /// </summary> /// <param name="peers">List of peers to try</param> /// <param name="retryCount">Number of retry before a timeout</param> /// <returns></returns> private async Task <PeerApi> GetInitialPeer(List <ArkPeerAddress> peers, int retryCount = 0) { //Picks a peer randomly from the list var peerUrl = peers[new Random().Next(peers.Count)]; // create a peer out of peerurl, and returns if the peer is online. // var peer = new PeerApi(NetworkApi, peerUrl.Ip, peerUrl.Port); if (await peer.IsOnline().ConfigureAwait(false)) { return(peer); } // Throw an exception if all of the initial peers have been tried. // if (retryCount == peers.Count) { throw new Exception("Unable to connect to a seed peer"); } // redo the check and increment the retry count // return(await GetInitialPeer(peers, retryCount + 1).ConfigureAwait(false)); }
private async Task SetNetworkSettings(PeerApi initialPeer) { var responseAutoConfigure = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, ArkStaticStrings.ArkApiPaths.Loader.GET_AUTO_CONFIGURE); var responseFees = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, ArkStaticStrings.ArkApiPaths.Block.GET_FEES); var responsePeer = await initialPeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.GET, string.Format(ArkStaticStrings.ArkApiPaths.Peer.GET, initialPeer.Ip, initialPeer.Port)); var autoConfig = JsonConvert.DeserializeObject <ArkLoaderNetworkResponse>(responseAutoConfigure); var fees = JsonConvert.DeserializeObject <Fees>(JObject.Parse(responseFees)["fees"].ToString()); var peer = JsonConvert.DeserializeObject <ArkPeerResponse>(responsePeer); NetworkSettings = new ArkNetworkSettings() { Port = initialPeer.Port, BytePrefix = (byte)autoConfig.Network.Version, Version = peer.Peer.Version, NetHash = autoConfig.Network.NetHash, Fee = fees }; await NetworkApi.Instance.WarmUp(new PeerApi(initialPeer.Ip, initialPeer.Port)); }
private async Task <PeerApi> GetInitialPeer(NetworkType type, int retryCount = 0) { var peerUrl = _peerSeedListMainNet[new Random().Next(_peerSeedListMainNet.Count)]; if (type == NetworkType.DevNet) { peerUrl = _peerSeedListDevNet[new Random().Next(_peerSeedListDevNet.Count)]; } var peer = new PeerApi(peerUrl.Item1, peerUrl.Item2); if (await peer.IsOnline()) { return(peer); } if ((type == NetworkType.DevNet && retryCount == _peerSeedListDevNet.Count) || (type == NetworkType.MainNet && retryCount == _peerSeedListMainNet.Count)) { throw new Exception("Unable to connect to a seed peer"); } return(await GetInitialPeer(type, retryCount + 1)); }
public async static Task <ArkTransactionPostResponse> PostTransactionAsync(TransactionApi transaction, PeerApi peer = null) { string body = "{transactions: [" + transaction.ToObject(true) + "]} "; var response = string.Empty; if (peer == null) { response = await NetworkApi.Instance.ActivePeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.POST, ArkStaticStrings.ArkApiPaths.Transaction.POST, body); } else { response = await peer.MakeRequest(ArkStaticStrings.ArkHttpMethods.POST, ArkStaticStrings.ArkApiPaths.Transaction.POST, body); } return(JsonConvert.DeserializeObject <ArkTransactionPostResponse>(response)); }
public static ArkTransactionPostResponse PostTransaction(TransactionApi transaction, PeerApi peer = null) { return(PostTransactionAsync(transaction, peer).Result); }
public void Dispatch(PeerApi golemApi, DeploymentSpec spec, Action onSuccess) { TaskProc(golemApi, spec).ContinueWith((task) => { onSuccess(); }); }
public void Init() { instance = new PeerApi(); }
/// <summary> /// Asynchronously creates a request to the peer to create a transaction. /// </summary> /// /// <param name="transaction">A reference to a <see cref="TransactionApi"/> type.</param> /// /// <param name="peer">The peer to create the request to.</param> /// /// <returns>Returns an <see cref="Task{ArkTransactionPostResponse}"/> type.</returns> /// public async Task <ArkTransactionPostResponse> PostTransactionAsync(TransactionApi transaction, PeerApi peer = null) { try { string body = "{transactions: [" + transaction.ToObject(true) + "]} "; _logger.Info(string.Format("Send transaction <<{0}>>", body)); var response = string.Empty; if (peer == null) { response = await _networkApi.ActivePeer.MakeRequest(ArkStaticStrings.ArkHttpMethods.POST, ArkStaticStrings.ArkApiPaths.Transaction.POST, body).ConfigureAwait(false); } else { response = await peer.MakeRequest(ArkStaticStrings.ArkHttpMethods.POST, ArkStaticStrings.ArkApiPaths.Transaction.POST, body).ConfigureAwait(false); } return(JsonConvert.DeserializeObject <ArkTransactionPostResponse>(response)); } catch (Exception e) { _logger.Error(e.ToString()); throw e; } }
static void Main(string[] args) { PeerApi peerApi = new PeerApi("http://10.30.10.121:6162"); var info = peerApi.GetHubInfo(); System.Console.WriteLine(info.ToString()); string output = ""; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet && item.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { output = ip.Address.ToString(); } } } } var myIP = Dns.GetHostAddresses(Dns.GetHostName()); runServer(); var peers = peerApi.ListPeers(); int myPeer = -1; for (int i = 0; i < peers.Count; i++) { if (peers[i].PeerAddr.Contains("10.30.8.5")) { myPeer = i; break; } } if (myPeer == -1) { return; } { string hash = "SHA1:213fad4e430ded42e6a949f61cf560ac96ec9878"; DeploymentSpecImage specImg = new DeploymentSpecImage(hash, "http://10.30.8.5:6000/generatedID/test1.hdi"); DeploymentSpec spec = new DeploymentSpec(EnvType.Hd, specImg, "compiler", new List <string>() { "dupa" }); var peer = peers[myPeer]; string depId = peerApi.CreateDeployment(peer.NodeId, spec); depId = depId.Replace("\"", ""); // Run batch file var results = peerApi.UpdateDeployment(peer.NodeId, depId, new List <Command>() { new ExecCommand("Debug/golemtest.bat", new List <string>()) }); // Upload output.zip results = peerApi.UpdateDeployment(peer.NodeId, depId, new List <Command>() { new UploadFileCommand("http://10.30.8.5:6000/generatedID/", "output.zip") }); peerApi.DropDeployment(peer.NodeId, depId); System.Console.WriteLine(depId); stopServer(); return; } //session { SessionApi sessionApi = new SessionApi("http://10.30.10.121:6162"); //create session var body = new HubSession(); long?sessionId = sessionApi.CreateSession(body); body = sessionApi.GetSession(sessionId); //add peer var res1 = sessionApi.AddSessionPeers(sessionId, new List <string>() { peers[0].NodeId }); var sPeers = sessionApi.ListSessionPeers(sessionId); body = sessionApi.GetSession(sessionId); var deploymentSpec = new DeploymentInfo(); deploymentSpec.Name = "dupa"; string result = sessionApi.CreateDeployment(sessionId, peers[0].NodeId, deploymentSpec); System.Console.WriteLine(result); } }