Commit 632d01da authored by Jeremy Bokobza's avatar Jeremy Bokobza

added the code to create and update tumblebit sessions

parent ad0d8e76
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\..\StratisBitcoinFullNode\Stratis.Bitcoin.Common\Stratis.Bitcoin.Common.csproj" /> <ProjectReference Include="..\..\..\..\StratisBitcoinFullNode\Stratis.Bitcoin.Common\Stratis.Bitcoin.Common.csproj" />
<ProjectReference Include="..\..\..\..\StratisBitcoinFullNode\Stratis.Bitcoin\Stratis.Bitcoin.csproj" /> <ProjectReference Include="..\..\..\..\StratisBitcoinFullNode\Stratis.Bitcoin\Stratis.Bitcoin.csproj" />
<ProjectReference Include="..\NTumbleBit.Common\NTumbleBit.Common.csproj" />
<ProjectReference Include="..\NTumbleBit\NTumbleBit.csproj" /> <ProjectReference Include="..\NTumbleBit\NTumbleBit.csproj" />
</ItemGroup> </ItemGroup>
......
...@@ -17,5 +17,10 @@ ...@@ -17,5 +17,10 @@
/// Deletes the state of the current tumbling session.. /// Deletes the state of the current tumbling session..
/// </summary> /// </summary>
void Delete(); void Delete();
/// <summary>
/// Updates this the state of the current tumbling session.
/// </summary>
void Update();
} }
} }
using System; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NBitcoin; using NBitcoin;
...@@ -22,11 +23,13 @@ namespace Breeze.TumbleBit.Client ...@@ -22,11 +23,13 @@ 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;
...@@ -115,12 +118,27 @@ namespace Breeze.TumbleBit.Client ...@@ -115,12 +118,27 @@ namespace Breeze.TumbleBit.Client
this.logger.LogDebug($"Receive block with height {height}"); this.logger.LogDebug($"Receive block with height {height}");
// update the block height in the tumbling state // update the block height in the tumbling state
if (this.tumblingState.LastBlockReceivedHeight == 0)
{
this.tumblingState.StartHeight = height;
}
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
this.tumblingState.Update();
} }
} }
} }
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using NTumbleBit.ClassicTumbler; using NTumbleBit.ClassicTumbler;
using NTumbleBit.PuzzlePromise;
using NTumbleBit.PuzzleSolver;
namespace Breeze.TumbleBit.Client namespace Breeze.TumbleBit.Client
{ {
...@@ -16,15 +21,19 @@ namespace Breeze.TumbleBit.Client ...@@ -16,15 +21,19 @@ namespace Breeze.TumbleBit.Client
[JsonProperty("tumblerUri")] [JsonProperty("tumblerUri")]
public Uri TumblerUri { get; set; } public Uri TumblerUri { get; set; }
[JsonProperty("startHeight")]
public int StartHeight { get; set; }
[JsonProperty("lastBlockReceivedHeight")] [JsonProperty("lastBlockReceivedHeight")]
public int LastBlockReceivedHeight { get; set; } public int LastBlockReceivedHeight { get; set; }
[JsonProperty("destinationWalletName")] [JsonProperty("destinationWalletName")]
public string DestinationWalletName { get; set; } public string DestinationWalletName { get; set; }
public IList<Session> Sessions { get; set; }
public TumblingState()
{
this.Sessions = new List<Session>();
}
/// <inheritdoc /> /// <inheritdoc />
public void Save() public void Save()
{ {
...@@ -44,6 +53,55 @@ namespace Breeze.TumbleBit.Client ...@@ -44,6 +53,55 @@ namespace Breeze.TumbleBit.Client
File.Delete(stateFilePath); File.Delete(stateFilePath);
} }
/// <inheritdoc />
public void Update()
{
// get a list of cycles we expect to have at this height
var cycles = this.TumblerParameters.CycleGenerator.GetCycles(this.LastBlockReceivedHeight);
var states = cycles.SelectMany(c => this.Sessions.Where(s => s.StartCycle == c.Start)).ToList();
foreach (var state in states)
{
try
{
// create a new session to be updated
var session = new Session();
if (state.NegotiationClientState != null)
{
session.StartCycle = state.NegotiationClientState.CycleStart;
session.ClientChannelNegotiation = new ClientChannelNegotiation(this.TumblerParameters, state.NegotiationClientState);
}
if (state.PromiseClientState != null)
session.PromiseClientSession = new PromiseClientSession(this.TumblerParameters.CreatePromiseParamaters(), state.PromiseClientState);
if (state.SolverClientState != null)
session.SolverClientSession = new SolverClientSession(this.TumblerParameters.CreateSolverParamaters(), state.SolverClientState);
// update the session
session.Update();
// replace the updated session in the list of existing sessions
int index = this.Sessions.IndexOf(state);
if (index != -1)
{
this.Sessions[index] = session;
}
this.Save();
}
catch (Exception)
{
throw;
}
}
}
public void CreateNewSession(int start)
{
this.Sessions.Add(new Session { StartCycle = start });
this.Save();
}
/// <summary> /// <summary>
/// Loads the saved state of the tumbling execution to the file system. /// Loads the saved state of the tumbling execution to the file system.
/// </summary> /// </summary>
...@@ -81,4 +139,28 @@ namespace Breeze.TumbleBit.Client ...@@ -81,4 +139,28 @@ namespace Breeze.TumbleBit.Client
return Path.Combine(defaultFolderPath, StateFileName); return Path.Combine(defaultFolderPath, StateFileName);
} }
} }
public class Session
{
public int StartCycle { get; set; }
public ClientChannelNegotiation.State NegotiationClientState { get; set; }
public PromiseClientSession.State PromiseClientState { get; set; }
public SolverClientSession.State SolverClientState { get; set; }
public ClientChannelNegotiation ClientChannelNegotiation { get; set; }
public SolverClientSession SolverClientSession { get; set; }
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