Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.

Commit 2112cec

Browse files
LysanderGGixje
authored andcommitted
Migrate command: wallet rebuild (#747)
Signed-off-by: Guillaume George <lysandergc@gmail.com>
1 parent 0ba475b commit 2112cec

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

neo/Implementations/Wallets/peewee/UserWallet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ def Migrate(self):
6363
def DB(self):
6464
return self._db
6565

66-
def Rebuild(self):
66+
def Rebuild(self, start_block=0):
6767
try:
68-
super(UserWallet, self).Rebuild()
68+
super(UserWallet, self).Rebuild(start_block)
6969

7070
logger.debug("wallet rebuild: deleting %s coins and %s transactions" %
7171
(Coin.select().count(), Transaction.select().count()))

neo/Prompt/Commands/Wallet.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(self):
3838
self.register_sub_command(CommandWalletSend())
3939
self.register_sub_command(CommandWalletSendMany())
4040
self.register_sub_command(CommandWalletSign())
41+
self.register_sub_command(CommandWalletRebuild())
4142

4243
def command_desc(self):
4344
return CommandDesc('wallet', 'manage wallets')
@@ -210,6 +211,28 @@ def command_desc(self):
210211
return CommandDesc('create_addr', 'create a new wallet address', params=[p1])
211212

212213

214+
class CommandWalletRebuild(CommandBase):
215+
216+
def __init__(self):
217+
super().__init__()
218+
219+
def execute(self, arguments):
220+
PromptData.Prompt.stop_wallet_loop()
221+
222+
start_block = get_arg(arguments, 0, convert_to_int=True)
223+
if not start_block or start_block < 0:
224+
start_block = 0
225+
print(f"Restarting at block {start_block}")
226+
227+
PromptData.Wallet.Rebuild(start_block)
228+
229+
PromptData.Prompt.start_wallet_loop()
230+
231+
def command_desc(self):
232+
p1 = ParameterDesc('start_block', 'block number to start the resync at', optional=True)
233+
return CommandDesc('rebuild', 'rebuild the wallet index', params=[p1])
234+
235+
213236
#########################################################################
214237
#########################################################################
215238

neo/Prompt/Commands/tests/test_wallet_commands.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,26 @@ def test_wallet_create_address(self):
240240
self.assertEqual(type(res), UserWallet)
241241
self.assertEqual(len(res.Addresses), 9)
242242

243+
def test_wallet_rebuild(self):
244+
with patch('neo.Prompt.PromptData.PromptData.Prompt'):
245+
# test wallet rebuild with no wallet open
246+
args = ['rebuild']
247+
res = CommandWallet().execute(args)
248+
self.assertFalse(res)
249+
250+
self.OpenWallet()
251+
PromptData.Wallet._current_height = 12345
252+
253+
# test wallet rebuild with no argument
254+
args = ['rebuild']
255+
CommandWallet().execute(args)
256+
self.assertEqual(PromptData.Wallet._current_height, 0)
257+
258+
# test wallet rebuild with start block
259+
args = ['rebuild', '42']
260+
CommandWallet().execute(args)
261+
self.assertEqual(PromptData.Wallet._current_height, 42)
262+
243263
##########################################################
244264
##########################################################
245265
def test_1_import_addr(self):

neo/Wallets/Wallet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,13 +749,13 @@ def ProcessNewBlock(self, block):
749749
traceback.print_exc()
750750
logger.error("could not process %s " % e)
751751

752-
def Rebuild(self):
752+
def Rebuild(self, start_block=0):
753753
"""
754754
Sets the current height to 0 and now `ProcessBlocks` will start from
755755
the beginning of the blockchain.
756756
"""
757757
self._coins = {}
758-
self._current_height = 0
758+
self._current_height = start_block
759759

760760
def OnProcessNewBlock(self, block, added, changed, deleted):
761761
# abstract

0 commit comments

Comments
 (0)