Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
Breeze
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
3
Issues
3
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DeStream-public
Breeze
Commits
a12d5f8d
Commit
a12d5f8d
authored
Jun 09, 2017
by
Jeremy Bokobza
Committed by
GitHub
Jun 09, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #94 from bokobza/master
Creating TumbleBit sessions
parents
be0beb4f
bb1db893
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
119 additions
and
14 deletions
+119
-14
IStateMachine.cs
Breeze/src/Breeze.TumbleBit.Client/IStateMachine.cs
+5
-0
ITumblerService.cs
Breeze/src/Breeze.TumbleBit.Client/ITumblerService.cs
+1
-1
TumbleBitManager.cs
Breeze/src/Breeze.TumbleBit.Client/TumbleBitManager.cs
+24
-6
TumblerService.cs
Breeze/src/Breeze.TumbleBit.Client/TumblerService.cs
+1
-1
TumblingState.cs
Breeze/src/Breeze.TumbleBit.Client/TumblingState.cs
+88
-6
No files found.
Breeze/src/Breeze.TumbleBit.Client/IStateMachine.cs
View file @
a12d5f8d
...
@@ -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
();
}
}
}
}
Breeze/src/Breeze.TumbleBit.Client/ITumblerService.cs
View file @
a12d5f8d
...
@@ -21,7 +21,7 @@ namespace Breeze.TumbleBit.Client
...
@@ -21,7 +21,7 @@ namespace Breeze.TumbleBit.Client
/// <returns></returns>
/// <returns></returns>
Task
<
ClassicTumblerParameters
>
GetClassicTumblerParametersAsync
();
Task
<
ClassicTumblerParameters
>
GetClassicTumblerParametersAsync
();
Task
<
UnsignedVoucherInformation
>
AskUnsignedVoucher
();
Task
<
UnsignedVoucherInformation
>
AskUnsignedVoucher
Async
();
Task
<
PuzzleSolution
>
SignVoucherAsync
(
SignVoucherRequest
signVoucherRequest
);
Task
<
PuzzleSolution
>
SignVoucherAsync
(
SignVoucherRequest
signVoucherRequest
);
...
...
Breeze/src/Breeze.TumbleBit.Client/TumbleBitManager.cs
View file @
a12d5f8d
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
();
}
}
}
}
}
}
Breeze/src/Breeze.TumbleBit.Client/TumblerService.cs
View file @
a12d5f8d
...
@@ -42,7 +42,7 @@ namespace Breeze.TumbleBit.Client
...
@@ -42,7 +42,7 @@ namespace Breeze.TumbleBit.Client
}
}
/// <inheritdoc />
/// <inheritdoc />
public
async
Task
<
UnsignedVoucherInformation
>
AskUnsignedVoucher
()
public
async
Task
<
UnsignedVoucherInformation
>
AskUnsignedVoucher
Async
()
{
{
UnsignedVoucherInformation
result
=
await
this
.
serverAddress
.
AppendPathSegment
(
"api/v1/tumblers/0/vouchers/"
).
GetJsonAsync
<
UnsignedVoucherInformation
>();
UnsignedVoucherInformation
result
=
await
this
.
serverAddress
.
AppendPathSegment
(
"api/v1/tumblers/0/vouchers/"
).
GetJsonAsync
<
UnsignedVoucherInformation
>();
return
result
;
return
result
;
...
...
Breeze/src/Breeze.TumbleBit.Client/TumblingState.cs
View file @
a12d5f8d
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
()
{
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment