Commit b200b823 authored by Jeremy Bokobza's avatar Jeremy Bokobza

Brought creation of a session inside the tumbling state class

parent 0f834536
......@@ -63,7 +63,7 @@ namespace Breeze.TumbleBit.Controllers
try
{
await this.tumbleBitManager.TumbleAsync(request.DestinationWalletName);
await this.tumbleBitManager.TumbleAsync(request.OriginWalletName, request.DestinationWalletName);
return this.Ok();
}
catch (Exception e)
......
......@@ -17,7 +17,7 @@ namespace Breeze.TumbleBit.Client
/// <returns></returns>
Task<ClassicTumblerParameters> ConnectToTumblerAsync(Uri serverAddress);
Task TumbleAsync(string destinationWalletName);
Task TumbleAsync(string originWalletName, string destinationWalletName);
/// <summary>
/// Processes a block received from the network.
......
......@@ -28,7 +28,10 @@ namespace Breeze.TumbleBit.Models
public class TumbleRequest
{
[Required(ErrorMessage = "A wallet name is required.")]
[Required(ErrorMessage = "The name of the origin wallet is required.")]
public string OriginWalletName { get; set; }
[Required(ErrorMessage = "The name of the destination wallet is required.")]
public string DestinationWalletName { get; set; }
}
}
......@@ -23,13 +23,11 @@ namespace Breeze.TumbleBit.Client
private readonly Network network;
private TumblingState tumblingState;
private IDisposable blockReceiver;
int lastCycleStarted;
private ClassicTumblerParameters TumblerParameters { get; set; }
public TumbleBitManager(ILoggerFactory loggerFactory, IWalletManager walletManager, ConcurrentChain chain, Network network, Signals signals)
{
this.lastCycleStarted = 0;
this.walletManager = walletManager;
this.chain = chain;
this.signals = signals;
......@@ -64,7 +62,7 @@ namespace Breeze.TumbleBit.Client
}
/// <inheritdoc />
public Task TumbleAsync(string destinationWalletName)
public Task TumbleAsync(string originWalletName, string destinationWalletName)
{
// make sure the tumbler service is initialized
if (this.TumblerParameters == null || this.tumblerService == null)
......@@ -81,11 +79,19 @@ namespace Breeze.TumbleBit.Client
Wallet destinationWallet = this.walletManager.GetWallet(destinationWalletName);
if (destinationWallet == null)
{
throw new Exception($"Destination not found. Have you created a wallet with name {destinationWalletName}?");
throw new Exception($"Destination wallet not found. Have you created a wallet with name {destinationWalletName}?");
}
Wallet originWallet = this.walletManager.GetWallet(originWalletName);
if (originWallet == null)
{
throw new Exception($"Origin wallet not found. Have you created a wallet with name {originWalletName}?");
}
// update the state and save
this.tumblingState.DestinationWalletName = destinationWalletName;
this.tumblingState.OriginWalletName = originWalletName;
this.tumblingState.Save();
// subscribe to receiving blocks
......@@ -114,29 +120,12 @@ namespace Breeze.TumbleBit.Client
/// <inheritdoc />
public void ProcessBlock(int height, Block block)
{
// TODO start the state machine
this.logger.LogDebug($"Receive block with height {height}");
this.logger.LogDebug($"Received block with height {height} during tumbling session.");
// update the block height in the tumbling state
this.tumblingState.LastBlockReceivedHeight = height;
this.tumblingState.Save();
// get the next cycle to be started
var cycle = this.TumblerParameters.CycleGenerator.GetRegistratingCycle(height);
// check if we need to start a new session starting from the registration cycle
if (this.lastCycleStarted != cycle.Start)
{
this.lastCycleStarted = cycle.Start;
this.logger.LogDebug($"new registration cycle at {cycle.Start}");
if (this.tumblingState.Sessions.SingleOrDefault(s => s.StartCycle == cycle.Start) == null)
{
this.tumblingState.CreateNewSession(cycle.Start);
this.logger.LogDebug($"new session created at {cycle.Start}");
}
}
// update the state of the tumbling session in this new block
this.tumblingState.Update();
}
......
......@@ -4,10 +4,12 @@ using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using NBitcoin;
using Newtonsoft.Json;
using NTumbleBit.ClassicTumbler;
using NTumbleBit.PuzzlePromise;
using NTumbleBit.PuzzleSolver;
using Stratis.Bitcoin.Wallet;
namespace Breeze.TumbleBit.Client
{
......@@ -24,9 +26,13 @@ namespace Breeze.TumbleBit.Client
[JsonProperty("lastBlockReceivedHeight")]
public int LastBlockReceivedHeight { get; set; }
[JsonProperty("originWalletName")]
public string OriginWalletName { get; set; }
[JsonProperty("destinationWalletName")]
public string DestinationWalletName { get; set; }
[JsonProperty("sessions")]
public IList<Session> Sessions { get; set; }
public TumblingState()
......@@ -56,9 +62,22 @@ namespace Breeze.TumbleBit.Client
/// <inheritdoc />
public void Update()
{
// get the next cycle to be started
var cycle = this.TumblerParameters.CycleGenerator.GetRegistratingCycle(this.LastBlockReceivedHeight);
var lastCycleStarted = this.Sessions.Max(s => s.StartCycle);
// check if we need to start a new session starting from the registration cycle
// TODO remove the limitation to have only 1 session
if (lastCycleStarted != cycle.Start && this.Sessions.Count == 0)
{
if (this.Sessions.SingleOrDefault(s => s.StartCycle == cycle.Start) == null)
{
this.CreateNewSession(cycle.Start);
}
}
// get a list of cycles we expect to have at this height
var cycles = this.TumblerParameters.CycleGenerator.GetCycles(this.LastBlockReceivedHeight);
var existingSessions = cycles.SelectMany(c => this.Sessions.Where(s => s.StartCycle == c.Start)).ToList();
foreach (var existingSession in existingSessions)
{
......@@ -152,10 +171,13 @@ namespace Breeze.TumbleBit.Client
public SolverClientSession.State SolverClientState { get; set; }
[JsonIgnore]
public ClientChannelNegotiation ClientChannelNegotiation { get; set; }
[JsonIgnore]
public SolverClientSession SolverClientSession { get; set; }
[JsonIgnore]
public PromiseClientSession PromiseClientSession { get; set; }
public void Update()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment