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
af016950
Commit
af016950
authored
Jun 15, 2017
by
Dan Gershony
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reorg on the light wallet
parent
398ec79f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
7 deletions
+39
-7
LightWalletSyncManager.cs
Breeze/src/Breeze.Wallet/LightWalletSyncManager.cs
+39
-7
No files found.
Breeze/src/Breeze.Wallet/LightWalletSyncManager.cs
View file @
af016950
...
...
@@ -9,6 +9,7 @@ using Stratis.Bitcoin.Notifications;
using
Stratis.Bitcoin.Utilities
;
using
Stratis.Bitcoin.Wallet
;
using
Stratis.Bitcoin.Wallet.Notifications
;
using
System.Collections.Generic
;
namespace
Breeze.Wallet
{
...
...
@@ -21,6 +22,8 @@ namespace Breeze.Wallet
private
readonly
ILogger
logger
;
private
readonly
Signals
signals
;
private
ChainedBlock
lastReceivedBlock
;
public
LightWalletSyncManager
(
ILoggerFactory
loggerFactory
,
IWalletManager
walletManager
,
ConcurrentChain
chain
,
Network
network
,
BlockNotification
blockNotification
,
Signals
signals
)
// :base(loggerFactory, walletManager, chain, network)
{
...
...
@@ -35,6 +38,10 @@ namespace Breeze.Wallet
/// <inheritdoc />
public
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
await
this
.
WaitForChainDownloadAsync
();
...
...
@@ -106,16 +113,41 @@ namespace Breeze.Wallet
/// <inheritdoc />
public
void
ProcessBlock
(
Block
block
)
{
var
chainedBlock
=
this
.
chain
.
GetBlock
(
block
.
GetHash
());
// if the new block previous hash is the same as the
// wallet hash then just pass the block to the manager
if
(
block
.
Header
.
HashPrevBlock
!=
this
.
lastReceivedBlock
.
HashBlock
)
{
// 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
var
blockstoremove
=
new
List
<
uint256
>();
var
fork
=
this
.
lastReceivedBlock
;
// if the newly received block is too far forward, ignore it (for example when we start receiving blocks before the wallets start their syncing)
if
(
chainedBlock
.
Height
>
this
.
walletManager
.
LastBlockHeight
()
+
1
)
// we walk back the chained block object to find the fork
while
(
this
.
chain
.
GetBlock
(
fork
.
HashBlock
)
==
null
)
{
this
.
logger
.
LogDebug
(
$"block received with height:
{
chainedBlock
.
Height
}
and hash:
{
block
.
Header
.
GetHash
()}
is too far in advance. Ignoring."
);
blockstoremove
.
Add
(
fork
.
HashBlock
);
fork
=
fork
.
Previous
;
}
this
.
walletManager
.
RemoveBlocks
(
fork
);
}
else
if
(
current
.
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. Ignoring."
);
this
.
SyncFrom
(
this
.
lastReceivedBlock
.
Height
);
return
;
}
}
this
.
walletManager
.
ProcessBlock
(
block
);
var
chainedBlock
=
this
.
chain
.
GetBlock
(
block
.
GetHash
());
this
.
walletManager
.
ProcessBlock
(
block
,
chainedBlock
);
}
/// <inheritdoc />
...
...
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