def CreateQRMatrix(dataToEncode, errLevel=QRErrorCorrectLevel.L): dataLen = len(dataToEncode) baseSz = 4 if errLevel == QRErrorCorrectLevel.L else \ 5 if errLevel == QRErrorCorrectLevel.M else \ 6 if errLevel == QRErrorCorrectLevel.Q else \ 7 # errLevel = QRErrorCorrectLevel.H sz = baseSz if dataLen < 70 else 5 + (dataLen - 70) / 30 qrmtrx = [[]] while sz<20: try: errCorrectEnum = getattr(QRErrorCorrectLevel, errLevel.upper()) qr = QRCode(sz, errCorrectEnum) qr.addData(dataToEncode) qr.make() success=True break except TypeError: sz += 1 if not success: LOGERROR('Unsuccessful attempt to create QR code') LOGERROR('Data to encode: (Length: %s, isAscii: %s)', \ len(dataToEncode), isASCII(dataToEncode)) return [[0]], 1 qrmtrx = [] modCt = qr.getModuleCount() for r in range(modCt): tempList = [0]*modCt for c in range(modCt): # The matrix is transposed by default, from what we normally expect tempList[c] = 1 if qr.isDark(c,r) else 0 qrmtrx.append(tempList) return [qrmtrx, modCt]
def stopBitcoind(self): LOGINFO('Called stopBitcoind') if self.bitcoind == False: self.bitcoind = None return try: if not self.isRunningBitcoind(): LOGINFO('...but bitcoind is not running, to be able to stop') return from armoryengine.BDM import TheBDM cookie = TheBDM.getCookie() TheBDM.bdv().shutdownNode(cookie) #poll the pid until it's gone, for as long as 2 minutes total = 0 while self.bitcoind.poll() == None: time.sleep(0.1) total += 1 if total > 1200: LOGERROR( "bitcoind failed to shutdown in less than 2 minutes." " Terminating.") return self.bitcoind = None except Exception as e: LOGERROR(e) return
def extractSignedDataFromVersionsDotTxt(wholeFile, doVerify=True): """ This method returns a pair: a dictionary to lookup link by OS, and a formatted string that is sorted by OS, and re-formatted list that will hash the same regardless of original format or ordering """ msgBegin = wholeFile.find('# -----BEGIN-SIGNED-DATA-') msgBegin = wholeFile.find('\n', msgBegin + 1) + 1 msgEnd = wholeFile.find('# -----SIGNATURE---------') sigBegin = wholeFile.find('\n', msgEnd + 1) + 3 sigEnd = wholeFile.find('# -----END-SIGNED-DATA---') MSGRAW = wholeFile[msgBegin:msgEnd] SIGHEX = wholeFile[sigBegin:sigEnd].strip() if -1 in [msgBegin, msgEnd, sigBegin, sigEnd]: LOGERROR('No signed data block found') return '' if doVerify: Pub = SecureBinaryData(hex_to_binary(ARMORY_INFO_SIGN_PUBLICKEY)) Msg = SecureBinaryData(MSGRAW) Sig = SecureBinaryData(hex_to_binary(SIGHEX)) isVerified = CryptoECDSA().VerifyData(Msg, Sig, Pub) if not isVerified: LOGERROR('Signed data block failed verification!') return '' else: LOGINFO('Signature on signed data block is GOOD!') return MSGRAW
def stopBitcoind(self): LOGINFO('Called stopGroestlcoind') try: if not self.isRunningBitcoind(): LOGINFO( '...but groestlcoind is not running, to be able to stop') return #signal groestlcoind to stop self.proxy.stop() #poll the pid until it's gone, for as long as 2 minutes total = 0 while self.groestlcoind.poll() == None: time.sleep(0.1) total += 1 if total > 1200: LOGERROR( "groestlcoind failed to shutdown in less than 2 minutes." " Terminating.") return self.groestlcoind = None except Exception as e: LOGERROR(e) return
def callJSON(self, func, *args): state = self.getSDMState() if not state in ('BitcoindReady', 'BitcoindSynchronizing'): LOGERROR('Called callJSON(%s, %s)', func, str(args)) LOGERROR('Current SDM state: %s', state) raise self.BitcoindError, 'callJSON while %s'%state return self.proxy.__getattr__(func)(*args)
def inner(*args, **kwargs): jsonPrefix = "jsonrpc_" rv = None errTB = None errFrame = None errDepth = 0 # Just call the funct. If no errs are thrown, or if errs are caught before # returning, we'll get through this. Otherwise, get the error info and # place it in the log. try: rv = func(*args, **kwargs) except: # get the error info and ignore the 1st frame (i.e., this funct). (errType, errVal, errTB) = sys.exc_info() errFrame = errTB.tb_frame.f_back # Save basic info on the trace. rv = {} errStr = 'An error occurred in %s' % func.__name__[len(jsonPrefix ):] errTypeStr = 'Error Type = \'%s\'' % errType.__name__ errValStr = 'Error Value = \'%s\'' % errVal rv['Error'] = errStr rv['Error Type'] = errType.__name__ rv['Error Value'] = str( errVal) # If type has no val, this'll be blank LOGRAWDATA(errStr) LOGRAWDATA(errTypeStr) LOGRAWDATA(errValStr) #LOGERROR(errStr) #LOGERROR(errTypeStr) #LOGERROR(errValStr) # Log each error line but don't return to the user. The user really # doesn't need to see the trace. Also, unless directly called, the # JSON functs will just lead back to where the JSON server started. # The trace can go almost 30 levels deep! So, we'll limit the reported # levels to 10 so that the logs aren't crushed. while errFrame != None and errDepth < 10: errFile = errFrame.f_code.co_filename errFileStr = 'Error File = \'%s\'' % errFile errFileNumStr = 'Error Line Number = \'%d\'' % errFrame.f_lineno LOGERROR(errFileStr) LOGERROR(errFileNumStr) errFrame = errFrame.f_back errDepth += 1 if errFrame != None and errDepth == 10: LOGERROR('Trace stopped so as to not overwhelm the logs') finally: # Delete circular references and return the error dict. if errTB: del errTB del errFrame # Not totally sure this is necessary. Just in case.... return rv
def startBitcoind(self, callback): self.btcOut, self.btcErr = None, None if self.disabled: LOGERROR('SDM was disabled, must be re-enabled before starting') return LOGINFO('Called startGroestlcoind') if self.isRunningBitcoind() or TheTDM.getTDMState() == 'Downloading': raise self.BitcoindError, 'Looks like we have already started theSDM' if not os.path.exists(self.executable): raise self.BitcoindError, 'Could not find groestlcoind' chk1 = os.path.exists(self.useTorrentFile) chk2 = self.shouldTryBootstrapTorrent() chk3 = TheTDM.getTDMState() == 'ReadyToStart' if chk1 and chk2 and chk3: TheTDM.startDownload() else: self.launchBitcoindAndGuardian() #New backend code: we wont be polling the SDM state in the main thread #anymore, instead create a thread at groestlcoind start to poll the SDM state #and notify the main thread once groestlcoind is ready, then terminates self.pollBitcoindState(callback, async=True)
def readTimer(self, timerName): if not self.timerMap.has_key(timerName): LOGERROR('Requested read timer that does not exist! (%s)' % timerName) return timerEntry = self.timerMap[timerName] return timerEntry[0] + (RightNow() - timerEntry[2])
def __eq__(self, aso2): if not isinstance(aso2, self.__class__): return False # Expect three lists of comparables to be present (static or otherwise) # self.EQ_ATTRS_SIMPLE # self.EQ_ATTRS_LISTS # self.EQ_ATTRS_MAPS #### Regular compares if hasattr(self, 'EQ_ATTRS_SIMPLE'): for attr in self.EQ_ATTRS_SIMPLE: if not getattr(self, attr) == getattr(aso2, attr): LOGERROR('Compare failed for attribute: %s' % attr) LOGERROR(' self: %s' % str(getattr(self,attr))) LOGERROR(' other: %s' % str(getattr(aso2,attr))) return False #### List iteration compares if hasattr(self, 'EQ_ATTRS_LISTS'): for attr in self.EQ_ATTRS_LISTS: selfList = getattr(self, attr) otherList = getattr(aso2, attr) if not len(selfList)==len(otherList): LOGERROR('List size compare failed for %s' % attr) return False i = -1 for a,b in zip(selfList, otherList): i+=1 if not a==b: LOGERROR('Failed list compare for attr %s, index %d' % (attr,i)) return False #### Map iteration compares if hasattr(self, 'EQ_ATTRS_MAPS'): for attr in self.EQ_ATTRS_MAPS: selfMap = getattr(self, attr) otherMap = getattr(aso2, attr) if not len(selfMap)==len(otherMap): LOGERROR('Map size compare failed for %s' % attr) return False for key,val in selfMap.iteritems(): if not key in otherMap: LOGERROR('First map has key not in second map: "%s"' % key) return False if not val==otherMap[key]: LOGERROR('Value for attr=%s, key=%s does not match' % (attr,key)) return False return True
def unserializeAscii(self, ustxBlock, skipMagicCheck=False): headStr,rawData = readAsciiBlock(ustxBlock, self.BLKSTRING) if rawData is None: LOGERROR('Expected str "%s", got "%s"' % (self.BLKSTRING, headStr)) raise UnserializeError('Unexpected BLKSTRING') expectID = headStr.split('-')[-1] return self.unserialize(rawData, expectID, skipMagicCheck)
def torrentFailed(): # Not sure there's actually anything we need to do here... bootsz = '<Unknown>' if os.path.exists(bootfile): bootsz = bytesToHumanSize(os.path.getsize(bootfile)) LOGERROR('Torrent failed; size of %s is %s', torrentPath, bootsz) self.launchBitcoindAndGuardian()
def startBitcoind(self): self.btcOut, self.btcErr = None, None if self.disabled: LOGERROR('SDM was disabled, must be re-enabled before starting') return LOGINFO('Called startBitcoind') if self.isRunningBitcoind(): raise self.BitcoindError, 'Looks like we have already started theSDM' if not os.path.exists(self.executable): raise self.BitcoindError, 'Could not find bitcoind' self.launchBitcoindAndGuardian()
def getGuardianPath(self): if OS_WINDOWS: armoryInstall = os.path.dirname(inspect.getsourcefile(SatoshiDaemonManager)) # This should return a zip file because of py2exe if armoryInstall.endswith('.zip'): armoryInstall = os.path.dirname(armoryInstall) gpath = os.path.join(armoryInstall, 'guardian.exe') else: theDir = os.path.dirname(inspect.getsourcefile(SatoshiDaemonManager)) gpath = os.path.join(theDir, 'guardian.py') if not os.path.exists(gpath): LOGERROR('Could not find guardian script: %s', gpath) raise FileExistsError return gpath
def startBitcoind(self): self.btcOut, self.btcErr = None, None if self.disabled: LOGERROR('SDM was disabled, must be re-enabled before starting') return LOGINFO('Called startBitcoind') if self.isRunningBitcoind() or TheTDM.getTDMState() == 'Downloading': raise self.BitcoindError, 'Looks like we have already started theSDM' if not os.path.exists(self.executable): raise self.BitcoindError, 'Could not find bitcoind' chk1 = os.path.exists(self.useTorrentFile) chk2 = self.shouldTryBootstrapTorrent() chk3 = TheTDM.getTDMState() == 'ReadyToStart' if chk1 and chk2 and chk3: TheTDM.startDownload() else: self.launchBitcoindAndGuardian()
def startBitcoind(self, callback): self.btcOut, self.btcErr = None, None if self.disabled: LOGERROR('SDM was disabled, must be re-enabled before starting') return LOGINFO('Called startBitcoind') if self.isRunningBitcoind(): raise self.BitcoindError, 'Looks like we have already started theSDM' if not os.path.exists(self.executable): raise self.BitcoindError, 'Could not find bitcoind' self.launchBitcoindAndGuardian() # wait for user and pass from cookie file after bitcoind has started. Should be very quick self.readCookieFile() #New backend code: we wont be polling the SDM state in the main thread #anymore, instead create a thread at bitcoind start to poll the SDM state #and notify the main thread once bitcoind is ready, then terminates self.pollBitcoindState(callback, async=True)
def readBitcoinConf(self, makeIfDNE=False): LOGINFO('Reading groestlcoin.conf file') bitconf = os.path.join(self.satoshiRoot, 'groestlcoin.conf') if not os.path.exists(bitconf): if not makeIfDNE: raise self.BitcoinDotConfError, 'Could not find groestlcoin.conf' else: LOGINFO('No groestlcoin.conf available. Creating it...') touchFile(bitconf) # Guarantee that bitcoin.conf file has very strict permissions if OS_WINDOWS: if OS_VARIANT[0].lower() == 'xp': LOGERROR('Cannot set permissions correctly in XP!') LOGERROR('Please confirm permissions on the following file ') LOGERROR('are set to exclusive access only for your user ') LOGERROR( '(it usually is, but Groestlcoin Armory cannot guarantee it ' ) LOGERROR('on XP systems):') LOGERROR(' %s', bitconf) else: LOGINFO('Setting permissions on groestlcoin.conf') import ctypes username_u16 = ctypes.create_unicode_buffer(u'\0', 512) str_length = ctypes.c_int(512) ctypes.windll.Advapi32.GetUserNameW(ctypes.byref(username_u16), ctypes.byref(str_length)) CLI_OPTIONS.disableConfPermis = True #!!!GRS if not CLI_OPTIONS.disableConfPermis: import win32process LOGINFO('Setting permissions on groestlcoin.conf') cmd_icacls = [ u'icacls', bitconf, u'/inheritance:r', u'/grant:r', u'%s:F' % username_u16.value ] kargs = {} kargs['shell'] = True kargs['creationflags'] = win32process.CREATE_NO_WINDOW icacls_out = subprocess_check_output(cmd_icacls, **kargs) LOGINFO('icacls returned: %s', icacls_out) else: LOGWARN( 'Skipped setting permissions on groestlcoin.conf file') else: if not CLI_OPTIONS.disableConfPermis: LOGINFO('Setting permissions on groestlcoin.conf') os.chmod(bitconf, stat.S_IRUSR | stat.S_IWUSR) else: LOGWARN('Skipped setting permissions on groestlcoin.conf file') with open(bitconf, 'r') as f: # Find the last character of the each line: either a newline or '#' endchr = lambda line: line.find('#') if line.find( '#') > 1 else len(line) # Reduce each line to a list of key,value pairs separated with '=' allconf = [l[:endchr(l)].strip().split('=') for l in f.readlines()] # Need to convert to (x[0],x[1:]) in case the password has '=' in it allconfPairs = [[x[0], '='.join(x[1:])] for x in allconf if len(x) > 1] # Convert the list of pairs to a dictionary self.bitconf = dict(allconfPairs) # Look for rpcport, use default if not there self.bitconf['rpcport'] = int( self.bitconf.get('rpcport', BITCOIN_RPC_PORT)) # We must have a username and password. If not, append to file if not self.bitconf.has_key('rpcuser'): LOGDEBUG('No rpcuser: creating one') with open(bitconf, 'a') as f: f.write('\n') f.write('rpcuser=generated_by_armory\n') self.bitconf['rpcuser'] = '******' if not self.bitconf.has_key('rpcpassword'): LOGDEBUG('No rpcpassword: creating one') with open(bitconf, 'a') as f: randBase58 = SecureBinaryData().GenerateRandom(32).toBinStr() randBase58 = binary_to_base58(randBase58) f.write('\n') f.write('rpcpassword=%s' % randBase58) self.bitconf['rpcpassword'] = randBase58 if not isASCII(self.bitconf['rpcuser']): LOGERROR('Non-ASCII character in bitcoin.conf (rpcuser)!') if not isASCII(self.bitconf['rpcpassword']): LOGERROR('Non-ASCII character in bitcoin.conf (rpcpassword)!') self.bitconf['host'] = '127.0.0.1'
def findBitcoind(self, extraSearchPaths=[]): self.foundExe = [] searchPaths = list(extraSearchPaths) # create a copy if OS_WINDOWS: # Making sure the search path argument comes with /daemon and /Bitcoin on Windows searchPaths.extend( [os.path.join(sp, 'Groestlcoin') for sp in searchPaths]) searchPaths.extend( [os.path.join(sp, 'daemon') for sp in searchPaths]) possBaseDir = [] from platform import machine if '64' in machine(): possBaseDir.append(os.getenv("ProgramW6432")) possBaseDir.append(os.getenv('PROGRAMFILES(X86)')) else: possBaseDir.append(os.getenv('PROGRAMFILES')) # check desktop for links home = os.path.expanduser('~') desktop = os.path.join(home, 'Desktop') if os.path.exists(desktop): dtopfiles = os.listdir(desktop) for path in [os.path.join(desktop, fn) for fn in dtopfiles]: if 'groestlcoin' in path.lower() and path.lower().endswith( '.lnk'): import win32com.client shell = win32com.client.Dispatch('WScript.Shell') targ = shell.CreateShortCut(path).Targetpath targDir = os.path.dirname(targ) LOGINFO('Found Groestlcoin-Qt link on desktop: %s', targDir) possBaseDir.append(targDir) # Also look in default place in ProgramFiles dirs # Now look at a few subdirs of the searchPaths.extend(possBaseDir) searchPaths.extend([ os.path.join(p, 'Groestlcoin', 'daemon') for p in possBaseDir ]) searchPaths.extend( [os.path.join(p, 'daemon') for p in possBaseDir]) searchPaths.extend( [os.path.join(p, 'Groestlcoin') for p in possBaseDir]) for p in searchPaths: testPath = os.path.join(p, 'groestlcoind.exe') if os.path.exists(testPath): self.foundExe.append(testPath) else: # In case this was a downloaded copy, make sure we traverse to bin/64 dir searchPaths.extend( [os.path.join(p, 'bin') for p in extraSearchPaths]) if SystemSpecs.IsX64: searchPaths.extend( [os.path.join(p, 'bin/64') for p in extraSearchPaths]) else: searchPaths.extend( [os.path.join(p, 'bin/32') for p in extraSearchPaths]) searchPaths.extend(['/usr/lib/groestlcoin/']) searchPaths.extend(os.getenv("PATH").split(':')) for p in searchPaths: testPath = os.path.join(p, 'groestlcoind') if os.path.exists(testPath): self.foundExe.append(testPath) try: locs = subprocess_check_output(['whereis', 'groestlcoind']).split() if len(locs) > 1: locs = filter( lambda x: os.path.basename(x) == 'groestlcoind', locs) LOGINFO('"whereis" returned: %s', str(locs)) self.foundExe.extend(locs) except: LOGEXCEPT('Error executing "whereis" command') # For logging purposes, check that the first answer matches one of the # extra search paths. There should be some kind of notification that # their supplied search path was invalid and we are using something else. if len(self.foundExe) > 0 and len(extraSearchPaths) > 0: foundIt = False for p in extraSearchPaths: if self.foundExe[0].startswith(p): foundIt = True if not foundIt: LOGERROR( 'Groestlcoind could not be found in the specified installation:' ) for p in extraSearchPaths: LOGERROR(' %s', p) LOGERROR('Groestlcoind is being started from:') LOGERROR(' %s', self.foundExe[0]) return self.foundExe
def resetTimer(self, timerName): if not self.timerMap.has_key(timerName): LOGERROR('Requested reset timer that does not exist! (%s)' % timerName) # Even if it didn't exist, it will be created now self.timerMap[timerName] = [0, 0, 0, False]
def tryToSetupTorrentDL(self, torrentPath): if self.torrentDisabled: LOGWARN('Tried to setup torrent download mgr but we are disabled') return False if not torrentPath or not os.path.exists(torrentPath): self.useTorrentFinalAnswer = False return False bootfile = os.path.join(self.satoshiHome, 'bootstrap.dat') bootfilePart = bootfile + '.partial' bootfileOld = bootfile + '.old' # cleartorrent.flag means we should remove any pre-existing files delTorrentFlag = os.path.join(ARMORY_HOME_DIR, 'cleartorrent.flag') if os.path.exists(delTorrentFlag): LOGWARN('Flag found to delete any pre-existing torrent files') if os.path.exists(bootfile): os.remove(bootfile) if os.path.exists(bootfilePart): os.remove(bootfilePart) if os.path.exists(bootfileOld): os.remove(bootfileOld) if os.path.exists(delTorrentFlag): os.remove(delTorrentFlag) TheTDM.setupTorrent(torrentPath, bootfile) if not TheTDM.getTDMState() == 'ReadyToStart': LOGERROR('Unknown error trying to start torrent manager') self.useTorrentFinalAnswer = False return False # We will tell the TDM to write status updates to the log file, and only # every 90 seconds. After it finishes (or fails), simply launch groestlcoind # as we would've done without the torrent ##### def torrentLogToFile(dpflag=Event(), fractionDone=None, timeEst=None, downRate=None, upRate=None, activity=None, statistics=None, **kws): statStr = '' if fractionDone: statStr += ' Done: %0.1f%% ' % (fractionDone * 100) if downRate: statStr += ' / DLRate: %0.1f/sec' % (downRate / 1024.) if timeEst: statStr += ' / TLeft: %s' % secondsToHumanTime(timeEst) if statistics: statStr += ' / Seeds: %d' % (statistics.numSeeds) statStr += ' / Peers: %d' % (statistics.numPeers) if len(statStr) == 0: statStr = 'No torrent info available' LOGINFO('Torrent: %s' % statStr) ##### def torrentFinished(): bootsz = '<Unknown>' if os.path.exists(bootfile): bootsz = bytesToHumanSize(os.path.getsize(bootfile)) LOGINFO('Torrent finished; size of %s is %s', torrentPath, bootsz) LOGINFO('Remove the core btc databases before doing bootstrap') deleteBitcoindDBs() self.launchBitcoindAndGuardian() ##### def warnUserHashFail(): from PyQt4.QtGui import QMessageBox QMessageBox.warning( self, tr('Hash Failure'), tr("""The torrent download is currently encountering too many packet hash failures to allow it to progress properly. As a result, the torrent engine has been halted. You should report this incident to the Groestlcoin Armory team and turn off this feature until further notice."""), QMessageBox.Ok) ##### def torrentFailed(errMsg=''): # Not sure there's actually anything we need to do here... if errMsg == 'hashFail': warnUserHashFail() bootsz = '<Unknown>' if os.path.exists(bootfile): bootsz = bytesToHumanSize(os.path.getsize(bootfile)) LOGERROR('Torrent failed; size of %s is %s', torrentPath, bootsz) self.launchBitcoindAndGuardian() TheTDM.setSecondsBetweenUpdates(90) TheTDM.setCallback('displayFunc', torrentLogToFile) TheTDM.setCallback('finishedFunc', torrentFinished) TheTDM.setCallback('failedFunc', torrentFailed) LOGINFO('Bootstrap file is %s' % bytesToHumanSize(TheTDM.torrentSize)) self.useTorrentFinalAnswer = True self.useTorrentFile = torrentPath return True
def dataReceived(self, data): """ Called by the reactor when data is received over the connection. This method will do nothing if we don't receive a full message. """ #print '\n\nData Received:', #pprintHex(binary_to_hex(data), withAddr=False) # Put the current buffer into an unpacker, process until empty self.recvData += data buf = BinaryUnpacker(self.recvData) messages = [] while True: try: # recvData is only modified if the unserialize succeeds # Had a serious issue with references, so I had to convert # messages to strings to guarantee that copies were being # made! (yes, hacky...) thisMsg = PyMessage().unserialize(buf) messages.append(thisMsg.serialize()) self.recvData = buf.getRemainingString() except NetworkIDError: LOGERROR('Message for a different network!') if BLOCKCHAINS.has_key(self.recvData[:4]): LOGERROR('(for network: %s)', BLOCKCHAINS[self.recvData[:4]]) # Before raising the error, we should've finished reading the msg # So pop it off the front of the buffer self.recvData = buf.getRemainingString() return except UnpackerError: # Expect this error when buffer isn't full enough for a whole msg break # We might've gotten here without anything to process -- if so, bail if len(messages) == 0: return # Finally, we have some message to process, let's do it for msgStr in messages: msg = PyMessage().unserialize(msgStr) cmd = msg.cmd # Log the message if netlog option if CLI_OPTIONS.netlog: LOGDEBUG('DataReceived: %s', msg.payload.command) if msg.payload.command == 'tx': LOGDEBUG('\t' + binary_to_hex(msg.payload.tx.thisHash)) elif msg.payload.command == 'block': LOGDEBUG('\t' + msg.payload.header.getHashHex()) elif msg.payload.command == 'inv': for inv in msg.payload.invList: LOGDEBUG(('\tBLOCK: ' if inv[0]==2 else '\tTX : ') + \ binary_to_hex(inv[1])) # We process version and verackk only if we haven't yet if cmd == 'version' and not self.sentVerack: self.peerInfo = {} self.peerInfo['version'] = msg.payload.version self.peerInfo['subver'] = msg.payload.subver self.peerInfo['time'] = msg.payload.time self.peerInfo['height'] = msg.payload.height0 LOGINFO('Received version message from peer:') LOGINFO(' Version: %s', str(self.peerInfo['version'])) LOGINFO(' SubVersion: %s', str(self.peerInfo['subver'])) LOGINFO(' TimeStamp: %s', str(self.peerInfo['time'])) LOGINFO(' StartHeight: %s', str(self.peerInfo['height'])) self.sentVerack = True self.sendMessage(PayloadVerack()) elif cmd == 'verack': self.gotVerack = True self.factory.handshakeFinished(self) #self.startHeaderDL() #################################################################### # Don't process any other messages unless the handshake is finished if self.gotVerack and self.sentVerack: self.processMessage(msg)
def connectionFailed(self, protoObj, reason): LOGERROR( '***Initial connection to Satoshi client failed! Retrying...') ReconnectingClientFactory.connectionFailed(self, protoObj, reason)
def clientConnectionLost(self, connector, reason): LOGERROR( '***Connection to Satoshi client LOST! Attempting to reconnect...' ) self.func_loseConnect() ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def readBitcoinConf(self, makeIfDNE=False): LOGINFO('Reading bitcoin.conf file') bitconf = os.path.join( self.satoshiHome, 'bitcoin.conf' ) if not os.path.exists(bitconf): if not makeIfDNE: raise self.BitcoinDotConfError, 'Could not find bitcoin.conf' else: LOGINFO('No bitcoin.conf available. Creating it...') touchFile(bitconf) # Guarantee that bitcoin.conf file has very strict permissions if OS_WINDOWS: if OS_VARIANT[0].lower()=='xp': LOGERROR('Cannot set permissions correctly in XP!') LOGERROR('Please confirm permissions on the following file ') LOGERROR('are set to exclusive access only for your user ') LOGERROR('(it usually is, but Armory cannot guarantee it ') LOGERROR('on XP systems):') LOGERROR(' %s', bitconf) else: LOGINFO('Setting permissions on bitcoin.conf') import win32api username = win32api.GetUserName() LOGINFO('Setting permissions on bitcoin.conf') cmd_icacls = ['icacls',bitconf,'/inheritance:r','/grant:r', '%s:F' % username] icacls_out = subprocess_check_output(cmd_icacls, shell=True) LOGINFO('icacls returned: %s', icacls_out) else: LOGINFO('Setting permissions on bitcoin.conf') os.chmod(bitconf, stat.S_IRUSR | stat.S_IWUSR) with open(bitconf,'r') as f: # Find the last character of the each line: either a newline or '#' endchr = lambda line: line.find('#') if line.find('#')>1 else len(line) # Reduce each line to a list of key,value pairs separated with '=' allconf = [l[:endchr(l)].strip().split('=') for l in f.readlines()] # Need to convert to (x[0],x[1:]) in case the password has '=' in it allconfPairs = [[x[0], '='.join(x[1:])] for x in allconf if len(x)>1] # Convert the list of pairs to a dictionary self.bitconf = dict(allconfPairs) # Look for rpcport, use default if not there self.bitconf['rpcport'] = int(self.bitconf.get('rpcport', BITCOIN_RPC_PORT)) # We must have a username and password. If not, append to file if not self.bitconf.has_key('rpcuser'): LOGDEBUG('No rpcuser: creating one') with open(bitconf,'a') as f: f.write('\n') f.write('rpcuser=generated_by_armory\n') self.bitconf['rpcuser'] = '******' if not self.bitconf.has_key('rpcpassword'): LOGDEBUG('No rpcpassword: creating one') with open(bitconf,'a') as f: randBase58 = SecureBinaryData().GenerateRandom(32).toBinStr() randBase58 = binary_to_base58(randBase58) f.write('\n') f.write('rpcpassword=%s' % randBase58) self.bitconf['rpcpassword'] = randBase58 if not isASCII(self.bitconf['rpcuser']): LOGERROR('Non-ASCII character in bitcoin.conf (rpcuser)!') if not isASCII(self.bitconf['rpcpassword']): LOGERROR('Non-ASCII character in bitcoin.conf (rpcpassword)!') self.bitconf['host'] = '127.0.0.1'
def get(self, varType, sz=0, endianness=LITTLEENDIAN): """ First argument is the data-type: UINT32, VAR_INT, etc. If BINARY_CHUNK, need to supply a number of bytes to read, as well """ def sizeCheck(sz): if self.getRemainingSize() < sz: raise UnpackerError E = endianness pos = self.pos if varType == UINT32: sizeCheck(4) value = unpack(E + 'I', self.binaryStr[pos:pos + 4])[0] self.advance(4) return value elif varType == UINT64: sizeCheck(8) value = unpack(E + 'Q', self.binaryStr[pos:pos + 8])[0] self.advance(8) return value elif varType == UINT8: sizeCheck(1) value = unpack(E + 'B', self.binaryStr[pos:pos + 1])[0] self.advance(1) return value elif varType == UINT16: sizeCheck(2) value = unpack(E + 'H', self.binaryStr[pos:pos + 2])[0] self.advance(2) return value elif varType == INT32: sizeCheck(4) value = unpack(E + 'i', self.binaryStr[pos:pos + 4])[0] self.advance(4) return value elif varType == INT64: sizeCheck(8) value = unpack(E + 'q', self.binaryStr[pos:pos + 8])[0] self.advance(8) return value elif varType == INT8: sizeCheck(1) value = unpack(E + 'b', self.binaryStr[pos:pos + 1])[0] self.advance(1) return value elif varType == INT16: sizeCheck(2) value = unpack(E + 'h', self.binaryStr[pos:pos + 2])[0] self.advance(2) return value elif varType == VAR_INT: sizeCheck(1) [value, nBytes] = unpackVarInt(self.binaryStr[pos:pos + 9]) self.advance(nBytes) return value elif varType == VAR_STR: sizeCheck(1) [value, nBytes] = unpackVarInt(self.binaryStr[pos:pos + 9]) binOut = self.binaryStr[pos + nBytes:pos + nBytes + value] self.advance(nBytes + value) return binOut elif varType == FLOAT: sizeCheck(4) value = unpack(E + 'f', self.binaryStr[pos:pos + 4])[0] self.advance(4) return value elif varType == BINARY_CHUNK: sizeCheck(sz) binOut = self.binaryStr[pos:pos + sz] self.advance(sz) return binOut LOGERROR('Var Type not recognized! VarType = %d', varType) raise UnpackerError, "Var type not recognized! VarType=" + str( varType)
def tryToSetupTorrentDL(self, torrentPath): if self.torrentDisabled: LOGWARN('Tried to setup torrent download mgr but we are disabled') return False if not torrentPath or not os.path.exists(torrentPath): self.useTorrentFinalAnswer = False return False bootfile = os.path.join(self.satoshiHome, 'bootstrap.dat') TheTDM.setupTorrent(torrentPath, bootfile) if not TheTDM.getTDMState()=='ReadyToStart': LOGERROR('Unknown error trying to start torrent manager') self.useTorrentFinalAnswer = False return False # We will tell the TDM to write status updates to the log file, and only # every 90 seconds. After it finishes (or fails), simply launch bitcoind # as we would've done without the torrent ##### def torrentLogToFile(dpflag=Event(), fractionDone=None, timeEst=None, downRate=None, upRate=None, activity=None, statistics=None, **kws): statStr = '' if fractionDone: statStr += ' Done: %0.1f%% ' % (fractionDone*100) if downRate: statStr += ' / DLRate: %0.1f/sec' % (downRate/1024.) if timeEst: statStr += ' / TLeft: %s' % secondsToHumanTime(timeEst) if statistics: statStr += ' / Seeds: %d' % (statistics.numSeeds) statStr += ' / Peers: %d' % (statistics.numPeers) if len(statStr)==0: statStr = 'No torrent info available' LOGINFO('Torrent: %s' % statStr) ##### def torrentFinished(): bootsz = '<Unknown>' if os.path.exists(bootfile): bootsz = bytesToHumanSize(os.path.getsize(bootfile)) LOGINFO('Torrent finished; size of %s is %s', torrentPath, bootsz) LOGINFO('Remove the core btc databases before doing bootstrap') deleteBitcoindDBs() self.launchBitcoindAndGuardian() ##### def torrentFailed(): # Not sure there's actually anything we need to do here... bootsz = '<Unknown>' if os.path.exists(bootfile): bootsz = bytesToHumanSize(os.path.getsize(bootfile)) LOGERROR('Torrent failed; size of %s is %s', torrentPath, bootsz) self.launchBitcoindAndGuardian() TheTDM.setSecondsBetweenUpdates(90) TheTDM.setCallback('displayFunc', torrentLogToFile) TheTDM.setCallback('finishedFunc', torrentFinished) TheTDM.setCallback('failedFunc', torrentFailed) LOGINFO('Bootstrap file is %s' % bytesToHumanSize(TheTDM.torrentSize)) self.useTorrentFinalAnswer = True self.useTorrentFile = torrentPath return True
def readBitcoinConf(self): LOGINFO('Reading bitcoin.conf file') bitconf = os.path.join(self.satoshiRoot, 'bitcoin.conf') if os.path.exists(bitconf): # Guarantee that bitcoin.conf file has very strict permissions if OS_WINDOWS: if OS_VARIANT[0].lower() == 'xp': LOGERROR('Cannot set permissions correctly in XP!') LOGERROR( 'Please confirm permissions on the following file ') LOGERROR('are set to exclusive access only for your user ') LOGERROR('(it usually is, but Armory cannot guarantee it ') LOGERROR('on XP systems):') LOGERROR(' %s', bitconf) else: LOGINFO('Setting permissions on bitcoin.conf') import ctypes username_u16 = ctypes.create_unicode_buffer(u'\0', 512) str_length = ctypes.c_int(512) ctypes.windll.Advapi32.GetUserNameW( ctypes.byref(username_u16), ctypes.byref(str_length)) if not CLI_OPTIONS.disableConfPermis: import win32process LOGINFO('Setting permissions on bitcoin.conf') cmd_icacls = [ u'icacls', bitconf, u'/inheritance:r', u'/grant:r', u'%s:F' % username_u16.value ] kargs = {} kargs['shell'] = True kargs['creationflags'] = win32process.CREATE_NO_WINDOW icacls_out = subprocess_check_output( cmd_icacls, **kargs) LOGINFO('icacls returned: %s', icacls_out) else: LOGWARN( 'Skipped setting permissions on bitcoin.conf file') else: if not CLI_OPTIONS.disableConfPermis: LOGINFO('Setting permissions on bitcoin.conf') os.chmod(bitconf, stat.S_IRUSR | stat.S_IWUSR) else: LOGWARN('Skipped setting permissions on bitcoin.conf file') with open(bitconf, 'r') as f: # Find the last character of the each line: either a newline or '#' endchr = lambda line: line.find('#') if line.find( '#') > 1 else len(line) # Reduce each line to a list of key,value pairs separated with '=' allconf = [ l[:endchr(l)].strip().split('=') for l in f.readlines() ] # Need to convert to (x[0],x[1:]) in case the password has '=' in it allconfPairs = [[x[0], '='.join(x[1:])] for x in allconf if len(x) > 1] # Convert the list of pairs to a dictionary self.bitconf = dict(allconfPairs) # If there is no password, use cookie auth if not self.bitconf.has_key('rpcpassword'): LOGDEBUG('No rpcpassword: Using cookie Auth') self.readCookieFile() # defaults self.bitconf['host'] = '127.0.0.1' self.bitconf['rpcport'] = BITCOIN_RPC_PORT