From c84cdecba4b60de59679ed459ac4f8c20fc07662 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Tue, 2 Sep 2014 19:25:03 -0400 Subject: [PATCH] Normalize the noncetrialsperbyte and payloadlengthextrabytes for each of the user's existing addresses --- src/addresses.py | 17 ++++++----------- src/class_sendDataThread.py | 2 +- src/class_sqlThread.py | 28 +++++++++++++++++----------- src/helper_bootstrap.py | 2 +- src/helper_startup.py | 2 +- src/shared.py | 2 +- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/addresses.py b/src/addresses.py index 7307c58e..f6fbbf73 100644 --- a/src/addresses.py +++ b/src/addresses.py @@ -77,6 +77,7 @@ def decodeVarint(data): Decodes an encoded varint to an integer and returns it. Per protocol v3, the encoded value must be encoded with the minimum amount of data possible or else it is malformed. + Returns a tuple: (theEncodedValue, theSizeOfTheVarintInBytes) """ if len(data) == 0: @@ -131,23 +132,17 @@ def encodeAddress(version,stream,ripe): raise Exception("Programming error in encodeAddress: The length of a given ripe hash was not 20.") ripe = ripe.lstrip('\x00') - a = encodeVarint(version) + encodeVarint(stream) + ripe + storedBinaryData = encodeVarint(version) + encodeVarint(stream) + ripe + + # Generate the checksum sha = hashlib.new('sha512') - sha.update(a) + sha.update(storedBinaryData) currentHash = sha.digest() - #print 'sha after first hashing: ', sha.hexdigest() sha = hashlib.new('sha512') sha.update(currentHash) - #print 'sha after second hashing: ', sha.hexdigest() - checksum = sha.digest()[0:4] - #print 'len(a) = ', len(a) - #print 'checksum = ', checksum.encode('hex') - #print 'len(checksum) = ', len(checksum) - asInt = int(a.encode('hex') + checksum.encode('hex'),16) - #asInt = int(checksum.encode('hex') + a.encode('hex'),16) - # print asInt + asInt = int(storedBinaryData.encode('hex') + checksum.encode('hex'),16) return 'BM-'+ encodeBase58(asInt) def decodeAddress(address): diff --git a/src/class_sendDataThread.py b/src/class_sendDataThread.py index 1d8c87b8..1d847113 100644 --- a/src/class_sendDataThread.py +++ b/src/class_sendDataThread.py @@ -96,7 +96,7 @@ class sendDataThread(threading.Thread): elif command == 'advertisepeer': self.objectHashHolderInstance.holdPeer(data) elif command == 'sendaddr': - if self.connectionIsOrWasFullyEstablished: # only send addr messages if we have send and heard a verack from the remote node + if self.connectionIsOrWasFullyEstablished: # only send addr messages if we have sent and heard a verack from the remote node numberOfAddressesInAddrMessage = len(data) payload = '' for hostDetails in data: diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index c53fb33a..24519e63 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -124,8 +124,6 @@ class sqlThread(threading.Thread): self.conn.commit() shared.config.set('bitmessagesettings', 'settingsversion', '4') - with open(shared.appdata + 'keys.dat', 'wb') as configfile: - shared.config.write(configfile) if shared.config.getint('bitmessagesettings', 'settingsversion') == 4: shared.config.set('bitmessagesettings', 'defaultnoncetrialsperbyte', str( @@ -140,9 +138,6 @@ class sqlThread(threading.Thread): shared.config.set( 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', '0') shared.config.set('bitmessagesettings', 'settingsversion', '6') - with open(shared.appdata + 'keys.dat', 'wb') as configfile: - shared.config.write(configfile) - # From now on, let us keep a 'version' embedded in the messages.dat # file so that when we make changes to the database, the database # version we are on can stay embedded in the messages.dat file. Let us @@ -258,8 +253,6 @@ class sqlThread(threading.Thread): shared.config.set('bitmessagesettings','defaultnoncetrialsperbyte', str(shared.networkDefaultProofOfWorkNonceTrialsPerByte * 2)) """ shared.config.set('bitmessagesettings', 'settingsversion', '7') - with open(shared.appdata + 'keys.dat', 'wb') as configfile: - shared.config.write(configfile) # Add a new column to the pubkeys table to store the address version. # We're going to trash all of our pubkeys and let them be redownloaded. @@ -281,9 +274,6 @@ class sqlThread(threading.Thread): shared.config.set('bitmessagesettings', 'useidenticons', 'True') if not shared.config.has_option('bitmessagesettings', 'identiconsuffix'): # acts as a salt shared.config.set('bitmessagesettings', 'identiconsuffix', ''.join(random.choice("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") for x in range(12))) # a twelve character pseudo-password to salt the identicons - # Since we've added a new config entry, let's write keys.dat to disk. - with open(shared.appdata + 'keys.dat', 'wb') as configfile: - shared.config.write(configfile) #Add settings to support no longer resending messages after a certain period of time even if we never get an ack if shared.config.getint('bitmessagesettings', 'settingsversion') == 7: @@ -332,10 +322,26 @@ class sqlThread(threading.Thread): shared.config.set('bitmessagesettings','defaultnoncetrialsperbyte', str(shared.networkDefaultProofOfWorkNonceTrialsPerByte)) shared.config.set('bitmessagesettings','defaultpayloadlengthextrabytes', str(shared.networkDefaultPayloadLengthExtraBytes)) previousTotalDifficulty = int(shared.config.getint('bitmessagesettings', 'maxacceptablenoncetrialsperbyte')) / 320 - previousSmallMessageDifficulty = int(shared.config.getint('bitmessagesettings', 'maxacceptablepayloadlengthextrabytes')) / 1400 + previousSmallMessageDifficulty = int(shared.config.getint('bitmessagesettings', 'maxacceptablepayloadlengthextrabytes')) / 14000 shared.config.set('bitmessagesettings','maxacceptablenoncetrialsperbyte', str(previousTotalDifficulty * 1000)) shared.config.set('bitmessagesettings','maxacceptablepayloadlengthextrabytes', str(previousSmallMessageDifficulty * 1000)) shared.config.set('bitmessagesettings', 'settingsversion', '9') + + # Adjust the required POW values for each of this user's addresses to conform to protocol v3 norms. + if shared.config.getint('bitmessagesettings', 'settingsversion') == 9: + for addressInKeysFile in shared.config.sections(): + try: + previousTotalDifficulty = float(shared.config.getint(addressInKeysFile, 'noncetrialsperbyte')) / 320 + previousSmallMessageDifficulty = float(shared.config.getint(addressInKeysFile, 'payloadlengthextrabytes')) / 14000 + if previousTotalDifficulty <= 2: + previousTotalDifficulty = 1 + if previousSmallMessageDifficulty < 1: + previousSmallMessageDifficulty = 1 + shared.config.set(addressInKeysFile,'noncetrialsperbyte', str(int(previousTotalDifficulty * 1000))) + shared.config.set(addressInKeysFile,'payloadlengthextrabytes', str(int(previousSmallMessageDifficulty * 1000))) + except: + continue + shared.config.set('bitmessagesettings', 'settingsversion', '10') with open(shared.appdata + 'keys.dat', 'wb') as configfile: shared.config.write(configfile) diff --git a/src/helper_bootstrap.py b/src/helper_bootstrap.py index 5c1aa77e..edeeb984 100644 --- a/src/helper_bootstrap.py +++ b/src/helper_bootstrap.py @@ -25,7 +25,7 @@ def knownNodes(): shared.knownNodes[stream][peer] = time except: shared.knownNodes = defaultKnownNodes.createDefaultKnownNodes(shared.appdata) - if shared.config.getint('bitmessagesettings', 'settingsversion') > 9: + if shared.config.getint('bitmessagesettings', 'settingsversion') > 10: print 'Bitmessage cannot read future versions of the keys file (keys.dat). Run the newer version of Bitmessage.' raise SystemExit diff --git a/src/helper_startup.py b/src/helper_startup.py index f8e592d1..0f07377e 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -57,7 +57,7 @@ def loadConfig(): # This appears to be the first time running the program; there is # no config file (or it cannot be accessed). Create config file. shared.config.add_section('bitmessagesettings') - shared.config.set('bitmessagesettings', 'settingsversion', '9') + shared.config.set('bitmessagesettings', 'settingsversion', '10') shared.config.set('bitmessagesettings', 'port', '8444') shared.config.set( 'bitmessagesettings', 'timeformat', '%%a, %%d %%b %%Y %%I:%%M %%p') diff --git a/src/shared.py b/src/shared.py index e27a2213..41ad80d4 100644 --- a/src/shared.py +++ b/src/shared.py @@ -370,7 +370,7 @@ def doCleanShutdown(): logger.info('Clean shutdown complete.') os._exit(0) -# When you want to command a sendDataThread to do something, like shutdown or send some data, this +# If you want to command all of the sendDataThreads to do something, like shutdown or send some data, this # function puts your data into the queues for each of the sendDataThreads. The sendDataThreads are # responsible for putting their queue into (and out of) the sendDataQueues list. def broadcastToSendDataQueues(data):