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
810bc30f
Commit
810bc30f
authored
Jun 15, 2017
by
Jeremy Bokobza
Committed by
GitHub
Jun 15, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #99 from dangershony/light-wallet-reorg
Reorg on the light wallet
parents
398ec79f
5c364b79
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
32 deletions
+43
-32
LightWalletFeature.cs
Breeze/src/Breeze.Wallet/LightWalletFeature.cs
+1
-1
LightWalletSyncManager.cs
Breeze/src/Breeze.Wallet/LightWalletSyncManager.cs
+42
-31
No files found.
Breeze/src/Breeze.Wallet/LightWalletFeature.cs
View file @
810bc30f
...
@@ -19,8 +19,8 @@ namespace Breeze.Wallet
...
@@ -19,8 +19,8 @@ namespace Breeze.Wallet
public
override
void
Start
()
public
override
void
Start
()
{
{
this
.
walletSyncManager
.
Initialize
();
this
.
walletManager
.
Initialize
();
this
.
walletManager
.
Initialize
();
this
.
walletSyncManager
.
Initialize
();
}
}
public
override
void
Stop
()
public
override
void
Stop
()
...
...
Breeze/src/Breeze.Wallet/LightWalletSyncManager.cs
View file @
810bc30f
...
@@ -9,10 +9,11 @@ using Stratis.Bitcoin.Notifications;
...
@@ -9,10 +9,11 @@ using Stratis.Bitcoin.Notifications;
using
Stratis.Bitcoin.Utilities
;
using
Stratis.Bitcoin.Utilities
;
using
Stratis.Bitcoin.Wallet
;
using
Stratis.Bitcoin.Wallet
;
using
Stratis.Bitcoin.Wallet.Notifications
;
using
Stratis.Bitcoin.Wallet.Notifications
;
using
System.Collections.Generic
;
namespace
Breeze.Wallet
namespace
Breeze.Wallet
{
{
public
class
LightWalletSyncManager
:
I
WalletSyncManager
public
class
LightWalletSyncManager
:
WalletSyncManager
{
{
private
readonly
WalletManager
walletManager
;
private
readonly
WalletManager
walletManager
;
private
readonly
ConcurrentChain
chain
;
private
readonly
ConcurrentChain
chain
;
...
@@ -22,7 +23,7 @@ namespace Breeze.Wallet
...
@@ -22,7 +23,7 @@ namespace Breeze.Wallet
private
readonly
Signals
signals
;
private
readonly
Signals
signals
;
public
LightWalletSyncManager
(
ILoggerFactory
loggerFactory
,
IWalletManager
walletManager
,
ConcurrentChain
chain
,
Network
network
,
public
LightWalletSyncManager
(
ILoggerFactory
loggerFactory
,
IWalletManager
walletManager
,
ConcurrentChain
chain
,
Network
network
,
BlockNotification
blockNotification
,
Signals
signals
)
//
:base(loggerFactory, walletManager, chain, network)
BlockNotification
blockNotification
,
Signals
signals
)
:
base
(
loggerFactory
,
walletManager
,
chain
,
network
)
{
{
this
.
walletManager
=
walletManager
as
WalletManager
;
this
.
walletManager
=
walletManager
as
WalletManager
;
this
.
chain
=
chain
;
this
.
chain
=
chain
;
...
@@ -33,8 +34,12 @@ namespace Breeze.Wallet
...
@@ -33,8 +34,12 @@ namespace Breeze.Wallet
}
}
/// <inheritdoc />
/// <inheritdoc />
public
async
Task
Initialize
()
public
override
async
Task
Initialize
()
{
{
this
.
lastReceivedBlock
=
this
.
chain
.
GetBlock
(
this
.
walletManager
.
LastReceivedBlock
);
if
(
this
.
lastReceivedBlock
==
null
)
throw
new
WalletException
(
"the wallet tip was not found in the main chain"
);
// get the chain headers. This needs to be up-to-date before we really do anything
// get the chain headers. This needs to be up-to-date before we really do anything
await
this
.
WaitForChainDownloadAsync
();
await
this
.
WaitForChainDownloadAsync
();
...
@@ -45,8 +50,9 @@ namespace Breeze.Wallet
...
@@ -45,8 +50,9 @@ namespace Breeze.Wallet
txSub
.
Subscribe
();
txSub
.
Subscribe
();
// start syncing blocks
// start syncing blocks
var
bestHeightForSyncing
=
this
.
FindBestHeightForSyncing
();
var
bestHeightForSyncing
=
this
.
FindBestHeightForSyncing
();
this
.
SyncFrom
(
bestHeightForSyncing
);
this
.
blockNotification
.
SyncFrom
(
this
.
chain
.
GetBlock
(
bestHeightForSyncing
).
HashBlock
);
this
.
logger
.
LogInformation
(
$"Tracker initialized. Syncing from
{
bestHeightForSyncing
}
."
);
this
.
logger
.
LogInformation
(
$"Tracker initialized. Syncing from
{
bestHeightForSyncing
}
."
);
}
}
...
@@ -89,39 +95,44 @@ namespace Breeze.Wallet
...
@@ -89,39 +95,44 @@ namespace Breeze.Wallet
}
}
/// <inheritdoc />
/// <inheritdoc />
public
void
SyncFrom
(
DateTime
date
)
public
override
void
ProcessBlock
(
Block
block
)
{
{
int
blockSyncStart
=
this
.
chain
.
GetHeightAtTime
(
date
);
// if the new block previous hash is the same as the
// wallet hash then just pass the block to the manager
// start syncing blocks
if
(
block
.
Header
.
HashPrevBlock
!=
this
.
lastReceivedBlock
.
HashBlock
)
this
.
SyncFrom
(
blockSyncStart
);
{
}
// if previous block does not match there might have
// been a reorg, check if the wallet is still on the main chain
var
current
=
this
.
chain
.
GetBlock
(
this
.
lastReceivedBlock
.
HashBlock
);
if
(
current
==
null
)
{
// the current wallet hash was not found on the main chain
// a reorg happenend so bring the wallet back top the last known fork
/// <inheritdoc />
var
fork
=
this
.
lastReceivedBlock
;
public
void
SyncFrom
(
int
height
)
{
this
.
blockNotification
.
SyncFrom
(
this
.
chain
.
GetBlock
(
height
).
HashBlock
);
}
/// <inheritdoc />
// we walk back the chained block object to find the fork
public
void
ProcessBlock
(
Block
block
)
while
(
this
.
chain
.
GetBlock
(
fork
.
HashBlock
)
==
null
)
{
fork
=
fork
.
Previous
;
var
chainedBlock
=
this
.
chain
.
GetBlock
(
block
.
GetHash
());
// if the newly received block is too far forward, ignore it (for example when we start receiving blocks before the wallets start their syncing)
Guard
.
Assert
(
fork
.
HashBlock
==
block
.
Header
.
HashPrevBlock
);
if
(
chainedBlock
.
Height
>
this
.
walletManager
.
LastBlockHeight
()
+
1
)
this
.
walletManager
.
RemoveBlocks
(
fork
);
{
}
this
.
logger
.
LogDebug
(
$"block received with height:
{
chainedBlock
.
Height
}
and hash:
{
block
.
Header
.
GetHash
()}
is too far in advance. Ignoring."
);
else
return
;
{
var
chainedBlock
=
this
.
chain
.
GetBlock
(
block
.
GetHash
());
if
(
chainedBlock
.
Height
>
this
.
lastReceivedBlock
.
Height
)
{
// the wallet is falling behind we need to catch up
this
.
logger
.
LogDebug
(
$"block received with height:
{
current
.
Height
}
and hash:
{
block
.
Header
.
GetHash
()}
is too far in advance. put the pull back."
);
this
.
blockNotification
.
SyncFrom
(
this
.
lastReceivedBlock
.
HashBlock
);
return
;
}
}
}
}
this
.
walletManager
.
ProcessBlock
(
block
);
this
.
lastReceivedBlock
=
this
.
chain
.
GetBlock
(
block
.
GetHash
());
}
this
.
walletManager
.
ProcessBlock
(
block
,
this
.
lastReceivedBlock
);
/// <inheritdoc />
public
void
ProcessTransaction
(
Transaction
transaction
)
{
this
.
walletManager
.
ProcessTransaction
(
transaction
);
}
}
}
}
}
}
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