Skip to content

Commit

Permalink
Merge pull request #11 from golemfactory/golem-config-prices
Browse files Browse the repository at this point in the history
Golem config prices
  • Loading branch information
staszek-krotki authored Nov 16, 2023
2 parents eea6ddb + ae03b2b commit 4b3a2c8
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 209 deletions.
42 changes: 40 additions & 2 deletions Golem.Tests/GolemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task StartStop_VerifyStatusAsync()
status = v;
};

golem.PropertyChanged += new PropertyChangedHandler<GolemStatus>(nameof(IGolem.Status), updateStatus).Subscribe();
golem.PropertyChanged += new PropertyChangedHandler<Golem, GolemStatus>(nameof(IGolem.Status), updateStatus).Subscribe();

await golem.Start();

Expand Down Expand Up @@ -62,7 +62,7 @@ public async Task Start_ChangeWallet_VerifyStatusAsync()
status = v;
};

golem.PropertyChanged += new PropertyChangedHandler<GolemStatus>(nameof(IGolem.Status), updateStatus).Subscribe();
golem.PropertyChanged += new PropertyChangedHandler<Golem, GolemStatus>(nameof(IGolem.Status), updateStatus).Subscribe();

await golem.Start();

Expand All @@ -72,5 +72,43 @@ public async Task Start_ChangeWallet_VerifyStatusAsync()

Assert.Equal(GolemStatus.Off, status);
}

[Fact]
public async Task Start_ChangePrices_VerifyPriceAsync()
{
string golemPath = await PackageBuilder.BuildTestDirectory("Start_ChangePrices_VerifyPriceAsync");
Console.WriteLine("Path: " + golemPath);

var golem = new Golem(PackageBuilder.BinariesDir(golemPath), PackageBuilder.DataDir(golemPath), loggerFactory);

decimal price = 0;

Action<decimal> updatePrice = (v) => price = v;

golem.Price.PropertyChanged += new PropertyChangedHandler<GolemPrice, decimal>(nameof(GolemPrice.StartPrice), updatePrice).Subscribe();
golem.Price.PropertyChanged += new PropertyChangedHandler<GolemPrice, decimal>(nameof(GolemPrice.GpuPerHour), updatePrice).Subscribe();
golem.Price.PropertyChanged += new PropertyChangedHandler<GolemPrice, decimal>(nameof(GolemPrice.EnvPerHour), updatePrice).Subscribe();
golem.Price.PropertyChanged += new PropertyChangedHandler<GolemPrice, decimal>(nameof(GolemPrice.NumRequests), updatePrice).Subscribe();


//Assert property changes
golem.Price.StartPrice = 0.005m;
Assert.Equal(0.005m, price);

golem.Price.GpuPerHour = 0.006m;
Assert.Equal(0.006m, price);

golem.Price.EnvPerHour = 0.007m;
Assert.Equal(0.007m, price);

golem.Price.NumRequests = 0.008m;
Assert.Equal(0.008m, price);

//Assert property returns correct value
Assert.Equal(0.005m, golem.Price.StartPrice);
Assert.Equal(0.006m, golem.Price.GpuPerHour);
Assert.Equal(0.007m, golem.Price.EnvPerHour);
Assert.Equal(0.008m, golem.Price.NumRequests);
}
}
}
14 changes: 7 additions & 7 deletions Golem.Tests/Tools/PropertyChangedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Golem.IntegrationTests.Tools
{

public class PropertyChangedHandler<T>
public class PropertyChangedHandler<T, V>
{
private Action<T> Handler { get; set; }
private Action<V> Handler { get; set; }
private string PropertyName { get; set; }

public PropertyChangedHandler(string propertyName, Action<T> handler)
public PropertyChangedHandler(string propertyName, Action<V> handler)
{
Handler = handler;
PropertyName = propertyName;
Expand All @@ -33,12 +33,12 @@ private void HandlerImpl(object? sender, PropertyChangedEventArgs e)
if (value is null)
return;

Handler((T)value);

if (sender is not Golem golem || e.PropertyName != PropertyName)
if (sender is not T || e.PropertyName != PropertyName)
return;

Console.WriteLine($"Property has changed: {e.PropertyName} to {golem.Status}");
Handler((V)value);

Console.WriteLine($"Property has changed: {e.PropertyName} to {value}");
}
}
}
46 changes: 41 additions & 5 deletions Golem/Golem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace Golem
{


public class Golem : IGolem, IAsyncDisposable
{
private YagnaService Yagna { get; set; }
Expand All @@ -32,21 +34,41 @@ public class Golem : IGolem, IAsyncDisposable

private readonly HttpClient _httpClient;

private GolemPrice price;
private readonly GolemPrice _golemPrice;

public GolemPrice Price
{
get
{
return price;
// var price = ProviderConfig.GolemPrice;
// _golemPrice.StartPrice = price.StartPrice;
// _golemPrice.GpuPerHour = price.GpuPerHour;
// _golemPrice.EnvPerHour = price.EnvPerHour;
// _golemPrice.NumRequests = price.NumRequests;
return _golemPrice;
}
set
{
price = value;
_golemPrice.StartPrice = value.StartPrice;
_golemPrice.GpuPerHour = value.GpuPerHour;
_golemPrice.EnvPerHour = value.EnvPerHour;
_golemPrice.NumRequests = value.NumRequests;

OnPropertyChanged();
}
}

public uint NetworkSpeed { get; set; }
private uint _networkSpeed;

public uint NetworkSpeed
{
get =>_networkSpeed;
set
{
_networkSpeed = value;
OnPropertyChanged();
}
}

private GolemStatus status;

Expand Down Expand Up @@ -77,7 +99,10 @@ public string WalletAddress
return walletAddress ?? "";
}

set => ProviderConfig.WalletAddress = value;
set
{
ProviderConfig.WalletAddress = value;
}
}

public event PropertyChangedEventHandler? PropertyChanged;
Expand Down Expand Up @@ -163,11 +188,22 @@ public Golem(string golemPath, string? dataDir, ILoggerFactory? loggerFactory =
Yagna = new YagnaService(golemPath, yagna_datadir, loggerFactory);
Provider = new Provider(golemPath, prov_datadir, loggerFactory);
ProviderConfig = new ProviderConfigService(Provider, YagnaOptionsFactory.DefaultNetwork);
_golemPrice = ProviderConfig.GolemPrice;

_httpClient = new HttpClient
{
BaseAddress = new Uri(YagnaOptionsFactory.DefaultYagnaApiUrl)
};

this.Price.PropertyChanged += GolemPrice_PropertyChangedHandler;
}

private void GolemPrice_PropertyChangedHandler(object? sender, PropertyChangedEventArgs e)
{
if (sender is GolemPrice price)
{
ProviderConfig.GolemPrice = price;
}
}

private async Task<bool> StartupYagnaAsync(YagnaStartupOptions yagnaOptions)
Expand Down
172 changes: 0 additions & 172 deletions Golem/PaymentService.cs

This file was deleted.

Loading

0 comments on commit 4b3a2c8

Please sign in to comment.