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 ...@@ -63,7 +63,7 @@ namespace Breeze.TumbleBit.Controllers
try try
{ {
await this.tumbleBitManager.TumbleAsync(request.DestinationWalletName); await this.tumbleBitManager.TumbleAsync(request.OriginWalletName, request.DestinationWalletName);
return this.Ok(); return this.Ok();
} }
catch (Exception e) catch (Exception e)
......
...@@ -17,7 +17,7 @@ namespace Breeze.TumbleBit.Client ...@@ -17,7 +17,7 @@ namespace Breeze.TumbleBit.Client
/// <returns></returns> /// <returns></returns>
Task<ClassicTumblerParameters> ConnectToTumblerAsync(Uri serverAddress); Task<ClassicTumblerParameters> ConnectToTumblerAsync(Uri serverAddress);
Task TumbleAsync(string destinationWalletName); Task TumbleAsync(string originWalletName, string destinationWalletName);
/// <summary> /// <summary>
/// Processes a block received from the network. /// Processes a block received from the network.
......
...@@ -28,7 +28,10 @@ namespace Breeze.TumbleBit.Models ...@@ -28,7 +28,10 @@ namespace Breeze.TumbleBit.Models
public class TumbleRequest 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; } public string DestinationWalletName { get; set; }
} }
} }
...@@ -23,13 +23,11 @@ namespace Breeze.TumbleBit.Client ...@@ -23,13 +23,11 @@ namespace Breeze.TumbleBit.Client
private readonly Network network; private readonly Network network;
private TumblingState tumblingState; private TumblingState tumblingState;
private IDisposable blockReceiver; private IDisposable blockReceiver;
int lastCycleStarted;
private ClassicTumblerParameters TumblerParameters { get; set; } private ClassicTumblerParameters TumblerParameters { get; set; }
public TumbleBitManager(ILoggerFactory loggerFactory, IWalletManager walletManager, ConcurrentChain chain, Network network, Signals signals) public TumbleBitManager(ILoggerFactory loggerFactory, IWalletManager walletManager, ConcurrentChain chain, Network network, Signals signals)
{ {
this.lastCycleStarted = 0;
this.walletManager = walletManager; this.walletManager = walletManager;
this.chain = chain; this.chain = chain;
this.signals = signals; this.signals = signals;
...@@ -64,7 +62,7 @@ namespace Breeze.TumbleBit.Client ...@@ -64,7 +62,7 @@ namespace Breeze.TumbleBit.Client
} }
/// <inheritdoc /> /// <inheritdoc />
public Task TumbleAsync(string destinationWalletName) public Task TumbleAsync(string originWalletName, string destinationWalletName)
{ {
// make sure the tumbler service is initialized // make sure the tumbler service is initialized
if (this.TumblerParameters == null || this.tumblerService == null) if (this.TumblerParameters == null || this.tumblerService == null)
...@@ -81,11 +79,19 @@ namespace Breeze.TumbleBit.Client ...@@ -81,11 +79,19 @@ namespace Breeze.TumbleBit.Client
Wallet destinationWallet = this.walletManager.GetWallet(destinationWalletName); Wallet destinationWallet = this.walletManager.GetWallet(destinationWalletName);
if (destinationWallet == null) 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 // update the state and save
this.tumblingState.DestinationWalletName = destinationWalletName; this.tumblingState.DestinationWalletName = destinationWalletName;
this.tumblingState.OriginWalletName = originWalletName;
this.tumblingState.Save(); this.tumblingState.Save();
// subscribe to receiving blocks // subscribe to receiving blocks
...@@ -113,30 +119,13 @@ namespace Breeze.TumbleBit.Client ...@@ -113,30 +119,13 @@ namespace Breeze.TumbleBit.Client
/// <inheritdoc /> /// <inheritdoc />
public void ProcessBlock(int height, Block block) public void ProcessBlock(int height, Block block)
{ {
// TODO start the state machine this.logger.LogDebug($"Received block with height {height} during tumbling session.");
this.logger.LogDebug($"Receive block with height {height}");
// update the block height in the tumbling state // update the block height in the tumbling state
this.tumblingState.LastBlockReceivedHeight = height; this.tumblingState.LastBlockReceivedHeight = height;
this.tumblingState.Save(); 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 // update the state of the tumbling session in this new block
this.tumblingState.Update(); this.tumblingState.Update();
} }
......
...@@ -4,10 +4,12 @@ using System.IO; ...@@ -4,10 +4,12 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NBitcoin;
using Newtonsoft.Json; using Newtonsoft.Json;
using NTumbleBit.ClassicTumbler; using NTumbleBit.ClassicTumbler;
using NTumbleBit.PuzzlePromise; using NTumbleBit.PuzzlePromise;
using NTumbleBit.PuzzleSolver; using NTumbleBit.PuzzleSolver;
using Stratis.Bitcoin.Wallet;
namespace Breeze.TumbleBit.Client namespace Breeze.TumbleBit.Client
{ {
...@@ -24,16 +26,20 @@ namespace Breeze.TumbleBit.Client ...@@ -24,16 +26,20 @@ namespace Breeze.TumbleBit.Client
[JsonProperty("lastBlockReceivedHeight")] [JsonProperty("lastBlockReceivedHeight")]
public int LastBlockReceivedHeight { get; set; } public int LastBlockReceivedHeight { get; set; }
[JsonProperty("originWalletName")]
public string OriginWalletName { get; set; }
[JsonProperty("destinationWalletName")] [JsonProperty("destinationWalletName")]
public string DestinationWalletName { get; set; } public string DestinationWalletName { get; set; }
[JsonProperty("sessions")]
public IList<Session> Sessions { get; set; } public IList<Session> Sessions { get; set; }
public TumblingState() public TumblingState()
{ {
this.Sessions = new List<Session>(); this.Sessions = new List<Session>();
} }
/// <inheritdoc /> /// <inheritdoc />
public void Save() public void Save()
{ {
...@@ -56,9 +62,22 @@ namespace Breeze.TumbleBit.Client ...@@ -56,9 +62,22 @@ namespace Breeze.TumbleBit.Client
/// <inheritdoc /> /// <inheritdoc />
public void Update() 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 // get a list of cycles we expect to have at this height
var cycles = this.TumblerParameters.CycleGenerator.GetCycles(this.LastBlockReceivedHeight); var cycles = this.TumblerParameters.CycleGenerator.GetCycles(this.LastBlockReceivedHeight);
var existingSessions = cycles.SelectMany(c => this.Sessions.Where(s => s.StartCycle == c.Start)).ToList(); var existingSessions = cycles.SelectMany(c => this.Sessions.Where(s => s.StartCycle == c.Start)).ToList();
foreach (var existingSession in existingSessions) foreach (var existingSession in existingSessions)
{ {
...@@ -75,7 +94,7 @@ namespace Breeze.TumbleBit.Client ...@@ -75,7 +94,7 @@ namespace Breeze.TumbleBit.Client
session.PromiseClientSession = new PromiseClientSession(this.TumblerParameters.CreatePromiseParamaters(), existingSession.PromiseClientState); session.PromiseClientSession = new PromiseClientSession(this.TumblerParameters.CreatePromiseParamaters(), existingSession.PromiseClientState);
if (existingSession.SolverClientState != null) if (existingSession.SolverClientState != null)
session.SolverClientSession = new SolverClientSession(this.TumblerParameters.CreateSolverParamaters(), existingSession.SolverClientState); session.SolverClientSession = new SolverClientSession(this.TumblerParameters.CreateSolverParamaters(), existingSession.SolverClientState);
// update the session // update the session
session.Update(); session.Update();
...@@ -152,10 +171,13 @@ namespace Breeze.TumbleBit.Client ...@@ -152,10 +171,13 @@ namespace Breeze.TumbleBit.Client
public SolverClientSession.State SolverClientState { get; set; } public SolverClientSession.State SolverClientState { get; set; }
[JsonIgnore]
public ClientChannelNegotiation ClientChannelNegotiation { get; set; } public ClientChannelNegotiation ClientChannelNegotiation { get; set; }
[JsonIgnore]
public SolverClientSession SolverClientSession { get; set; } public SolverClientSession SolverClientSession { get; set; }
[JsonIgnore]
public PromiseClientSession PromiseClientSession { get; set; } public PromiseClientSession PromiseClientSession { get; set; }
public void Update() 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