forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBDM.py
421 lines (342 loc) · 14.8 KB
/
BDM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
################################################################################
# #
# Copyright (C) 2011-2014, Armory Technologies, Inc. #
# Distributed under the GNU Affero General Public License (AGPL v3) #
# See LICENSE or http://www.gnu.org/licenses/agpl.html #
# #
################################################################################
import Queue
import os.path
import random
import threading
import traceback
from armoryengine.ArmoryUtils import *
from SDM import SatoshiDaemonManager
from armoryengine.Timer import TimeThisFunction
import CppBlockUtils as Cpp
from armoryengine.BinaryPacker import UINT64
BDM_OFFLINE = 'Offline'
BDM_UNINITIALIZED = 'Uninitialized'
BDM_BLOCKCHAIN_READY = 'BlockChainReady'
BDM_SCANNING = 'Scanning'
FINISH_LOAD_BLOCKCHAIN_ACTION = 'FinishLoadBlockchain'
NEW_ZC_ACTION = 'newZC'
NEW_BLOCK_ACTION = 'newBlock'
REFRESH_ACTION = 'refresh'
STOPPED_ACTION = 'stopped'
WARNING_ACTION = 'warning'
def newTheBDM(isOffline=False):
global TheBDM
if TheBDM:
TheBDM.beginCleanShutdown()
TheBDM = BlockDataManager(isOffline=isOffline)
class PySide_CallBack(Cpp.BDM_CallBack):
def __init__(self, bdm):
Cpp.BDM_CallBack.__init__(self)
self.bdm = bdm
self.bdm.progressComplete=0
self.bdm.secondsRemaining=0
self.bdm.progressPhase=0
self.bdm.progressNumeric=0
def run(self, action, arg, block):
try:
act = ''
arglist = []
# AOTODO replace with constants
if action == Cpp.BDMAction_Ready:
act = FINISH_LOAD_BLOCKCHAIN_ACTION
TheBDM.topBlockHeight = block
TheBDM.setState(BDM_BLOCKCHAIN_READY)
elif action == Cpp.BDMAction_ZC:
act = NEW_ZC_ACTION
castArg = Cpp.BtcUtils_cast_to_LedgerVector(arg)
arglist = castArg
elif action == Cpp.BDMAction_NewBlock:
act = NEW_BLOCK_ACTION
castArg = Cpp.BtcUtils_cast_to_int(arg)
arglist.append(castArg)
TheBDM.topBlockHeight = block
elif action == Cpp.BDMAction_Refresh:
act = REFRESH_ACTION
castArg = Cpp.BtcUtils_cast_to_BinaryDataVector(arg)
arglist = castArg
elif action == Cpp.BDMAction_Exited:
act = STOPPED_ACTION
elif action == Cpp.BDMAction_ErrorMsg:
act = WARNING_ACTION
argstr = Cpp.BtcUtils_cast_to_string(arg)
arglist.append(argstr)
listenerList = TheBDM.getListenerList()
for cppNotificationListener in listenerList:
cppNotificationListener(act, arglist)
except:
LOGEXCEPT('Error in running callback')
print sys.exc_info()
raise
def progress(self, phase, walletId, prog, seconds, progressNumeric):
try:
walletIdString = str(walletId)
if len(walletIdString) == 0:
self.bdm.progressPhase = phase
self.bdm.progressComplete = prog
self.bdm.secondsRemaining = seconds
self.bdm.progressNumeric = progressNumeric
else:
progInfo = [walletIdString, prog]
for cppNotificationListener in TheBDM.getListenerList():
cppNotificationListener('progress', progInfo)
except:
LOGEXCEPT('Error in running progress callback')
print sys.exc_info()
class BDM_Inject(Cpp.BDM_Inject):
def __init__(self):
Cpp.BDM_Inject.__init__(self)
self.command = None
self.response = None
self.hasResponse = False
def run(self):
try:
if self.command:
cmd = self.command
self.command = None
self.response = cmd()
self.hasResponse = True
except:
LOGEXCEPT('Error in running thread callback')
print sys.exc_info()
def runCommand(self, fn):
self.hasResponse = False
self.command = fn
while not self.hasResponse:
self.notify()
self.waitRun();
res = self.response
self.response=None
return res
def getCurrTimeAndBlock():
time0 = long(RightNowUTC())
return (time0, TheBDM.getTopBlockHeight())
# Make TheBDM act like it's a singleton. Always use the global singleton TheBDM
# instance that exists in this module regardless of the instance that passed as self
def ActLikeASingletonBDM(func):
def inner(*args, **kwargs):
if TheBDM and len(args) > 0:
newArgs = (TheBDM,) + args[1:]
return func(*newArgs, **kwargs)
else:
return func(*args, **kwargs)
return inner
################################################################################
class BlockDataManager(object):
#############################################################################
def __init__(self, isOffline=False):
super(BlockDataManager, self).__init__()
#register callbacks
self.callback = PySide_CallBack(self).__disown__()
self.inject = BDM_Inject().__disown__()
self.ldbdir = ""
#dbType
#self.dbType = Cpp.ARMORY_DB_BARE
self.dbType = Cpp.ARMORY_DB_SUPER
self.bdmThread = Cpp.BlockDataManagerThread(self.bdmConfig(forInit=True));
# Flags
self.aboutToRescan = False
self.errorOut = 0
self.currentActivity = 'None'
self.walletsToRegister = []
if isOffline == True: self.bdmState = BDM_OFFLINE
else: self.bdmState = BDM_UNINITIALIZED
self.btcdir = BTC_HOME_DIR
self.ldbdir = LEVELDB_DIR
self.lastPctLoad = 0
self.topBlockHeight = 0
self.cppNotificationListenerList = []
#############################################################################
@ActLikeASingletonBDM
def getListenerList(self):
return self.cppNotificationListenerList
#############################################################################
@ActLikeASingletonBDM
def cleanUpBDMThread(self):
self.bdmThread = None
#############################################################################
@ActLikeASingletonBDM
def BDMshutdownCallback(self, action, args):
if action == 'stopped':
self.cppNotificationListenerList.remove(self.BDMshutdownCallback)
self.cleanUpBDMThread()
#############################################################################
@ActLikeASingletonBDM
def bdv(self):
return self.bdmThread.bdv()
#############################################################################
@ActLikeASingletonBDM
def getTxByHash(self, txHash):
return self.bdv().getTxByHash(txHash)
#############################################################################
@ActLikeASingletonBDM
def getSentValue(self, txIn):
return self.bdv().getSentValue(txIn)
#############################################################################
@ActLikeASingletonBDM
def getTopBlockHeight(self):
return self.topBlockHeight
#############################################################################
@ActLikeASingletonBDM
def getTopBlockDifficulty(self):
return self.bdv().getTopBlockHeader().getDifficulty()
#############################################################################
@ActLikeASingletonBDM
def registerCppNotification(self, cppNotificationListener):
self.cppNotificationListenerList.append(cppNotificationListener)
#############################################################################
@ActLikeASingletonBDM
def unregisterCppNotification(self, cppNotificationListener):
if cppNotificationListener in self.cppNotificationListenerList:
self.cppNotificationListenerList.remove(cppNotificationListener)
#############################################################################
@ActLikeASingletonBDM
def goOnline(self, satoshiDir=None, levelDBDir=None, armoryHomeDir=None):
self.bdmThread.setConfig(self.bdmConfig())
self.bdmState = BDM_SCANNING
self.bdmThread.start(self.bdmMode(), self.callback, self.inject)
#############################################################################
@ActLikeASingletonBDM
def registerWallet(self, prefixedKeys, uniqueIDB58, isNew=False):
#this returns a pointer to the BtcWallet C++ object. This object is
#instantiated at registration and is unique for the BDV object, so we
#should only ever set the cppWallet member here
return self.bdv().registerWallet(prefixedKeys, uniqueIDB58, isNew)
#############################################################################
@ActLikeASingletonBDM
def unregisterWallet(self, uniqueIDB58):
self.bdv().unregisterWallet(uniqueIDB58)
#############################################################################
@ActLikeASingletonBDM
def registerLockbox(self, uniqueIDB58, addressList, isNew=False):
#this returns a pointer to the BtcWallet C++ object. This object is
#instantiated at registration and is unique for the BDV object, so we
#should only ever set the cppWallet member here
return self.bdv().registerLockbox(addressList, uniqueIDB58, isNew)
#############################################################################
@ActLikeASingletonBDM
def setSatoshiDir(self, newBtcDir):
if not os.path.exists(newBtcDir):
LOGERROR('setSatoshiDir: directory does not exist: %s', newBtcDir)
return
self.btcdir = newBtcDir
#############################################################################
@ActLikeASingletonBDM
def setLevelDBDir(self, ldbdir):
if not os.path.exists(ldbdir):
os.makedirs(ldbdir)
self.ldbdir = ldbdir
#############################################################################
@ActLikeASingletonBDM
def bdmMode(self):
if CLI_OPTIONS.rebuild:
mode = 2
elif CLI_OPTIONS.rescan:
mode = 1
else:
mode = 0
if CLI_OPTIONS.clearMempool:
mode += 4
return mode
#############################################################################
@ActLikeASingletonBDM
def bdmConfig(self, forInit=False):
blkdir = ""
if forInit == False:
# Check for the existence of the Bitcoin-Qt directory
if not os.path.exists(self.btcdir):
raise FileExistsError, ('Directory does not exist: %s' % self.btcdir)
blkdir = os.path.join(self.btcdir, 'blocks')
blk1st = os.path.join(blkdir, 'blk00000.dat')
# ... and its blk000X.dat files
if not os.path.exists(blk1st):
LOGERROR('Blockchain data not available: %s', blk1st)
raise FileExistsError, ('Blockchain data not available: %s' % blk1st)
blockdir = blkdir
leveldbdir = self.ldbdir
if OS_WINDOWS:
if isinstance(blkdir, unicode):
blockdir = blkdir.encode('utf8')
if isinstance(self.ldbdir, unicode):
leveldbdir = self.ldbdir.encode('utf8')
bdmConfig = Cpp.BlockDataManagerConfig()
bdmConfig.armoryDbType = self.dbType
bdmConfig.pruneType = Cpp.DB_PRUNE_NONE
bdmConfig.blkFileLocation = blockdir
bdmConfig.levelDBLocation = leveldbdir
bdmConfig.setGenesisBlockHash(GENESIS_BLOCK_HASH)
bdmConfig.setGenesisTxHash(GENESIS_TX_HASH)
bdmConfig.setMagicBytes(MAGIC_BYTES)
return bdmConfig
#############################################################################
@ActLikeASingletonBDM
def predictLoadTime(self):
return (self.progressPhase, self.progressComplete, self.secondsRemaining, self.progressNumeric)
#############################################################################
@TimeThisFunction
@ActLikeASingletonBDM
def createAddressBook(self, cppWlt):
return cppWlt.createAddressBook()
#############################################################################
@ActLikeASingletonBDM
def setState(self, state):
self.bdmState = state
#############################################################################
@ActLikeASingletonBDM
def getState(self):
return self.bdmState
#############################################################################
@ActLikeASingletonBDM
def beginCleanShutdown(self):
if self.bdmThread:
self.bdmState = BDM_UNINITIALIZED
self.registerCppNotification(self.BDMshutdownCallback)
self.bdv().reset()
if self.bdmThread.requestShutdown() == False:
self.cleanUpBDMThread()
#############################################################################
@ActLikeASingletonBDM
def runBDM(self, fn):
return self.inject.runCommand(fn)
################################################################################
# Make TheBDM reference the asyncrhonous BlockDataManager wrapper if we are
# running
TheBDM = None
TheSDM = None
if CLI_OPTIONS.offline:
LOGINFO('Armory loaded in offline-mode. Will not attempt to load ')
LOGINFO('blockchain without explicit command to do so.')
TheBDM = BlockDataManager(isOffline=True)
# Also create the might-be-needed SatoshiDaemonManager
TheSDM = SatoshiDaemonManager()
else:
# NOTE: "TheBDM" is sometimes used in the C++ code to reference the
# singleton BlockDataManager_LevelDB class object. Here,
# "TheBDM" refers to a python BlockDataManagerThead class
# object that wraps the C++ version. It implements some of
# it's own methods, and then passes through anything it
# doesn't recognize to the C++ object.
LOGINFO('Using the asynchronous/multi-threaded BlockDataManager.')
LOGINFO('Blockchain operations will happen in the background. ')
LOGINFO('Devs: check TheBDM.getState() before asking for data.')
LOGINFO('Registering addresses during rescans will queue them for ')
LOGINFO('inclusion after the current scan is completed.')
TheBDM = BlockDataManager(isOffline=False)
cppLogFile = os.path.join(ARMORY_HOME_DIR, 'armorycpplog.txt')
cpplf = cppLogFile
if OS_WINDOWS and isinstance(cppLogFile, unicode):
cpplf = cppLogFile.encode('utf8')
Cpp.StartCppLogging(cpplf, 4)
Cpp.EnableCppLogStdOut()
#LOGINFO('LevelDB max-open-files is %d', TheBDM.getMaxOpenFiles())
# Also load the might-be-needed SatoshiDaemonManager
TheSDM = SatoshiDaemonManager()
# Put the import at the end to avoid circular reference problem
from armoryengine.MultiSigUtils import MultiSigLockbox
from armoryengine.Transaction import PyTx
# kate: indent-width 3; replace-tabs on;