Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving code from shared, easy part #1623

Merged
merged 5 commits into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 0 additions & 66 deletions src/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,69 +274,3 @@ def addBMIfNotPresent(address):
"""Prepend BM- to an address if it doesn't already have it"""
address = str(address).strip()
return address if address[:3] == 'BM-' else 'BM-' + address


# TODO: make test case
if __name__ == "__main__":
from pyelliptic import arithmetic

print(
'\nLet us make an address from scratch. Suppose we generate two'
' random 32 byte values and call the first one the signing key'
' and the second one the encryption key:'
)
privateSigningKey = \
'93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665'
privateEncryptionKey = \
'4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a'
print(
'\nprivateSigningKey = %s\nprivateEncryptionKey = %s' %
(privateSigningKey, privateEncryptionKey)
)
print(
'\nNow let us convert them to public keys by doing'
' an elliptic curve point multiplication.'
)
publicSigningKey = arithmetic.privtopub(privateSigningKey)
publicEncryptionKey = arithmetic.privtopub(privateEncryptionKey)
print(
'\npublicSigningKey = %s\npublicEncryptionKey = %s' %
(publicSigningKey, publicEncryptionKey)
)

print(
'\nNotice that they both begin with the \\x04 which specifies'
' the encoding type. This prefix is not send over the wire.'
' You must strip if off before you send your public key across'
' the wire, and you must add it back when you receive a public key.'
)

publicSigningKeyBinary = \
arithmetic.changebase(publicSigningKey, 16, 256, minlen=64)
publicEncryptionKeyBinary = \
arithmetic.changebase(publicEncryptionKey, 16, 256, minlen=64)

ripe = hashlib.new('ripemd160')
sha = hashlib.new('sha512')
sha.update(publicSigningKeyBinary + publicEncryptionKeyBinary)

ripe.update(sha.digest())
addressVersionNumber = 2
streamNumber = 1
print(
'\nRipe digest that we will encode in the address: %s' %
hexlify(ripe.digest())
)
returnedAddress = \
encodeAddress(addressVersionNumber, streamNumber, ripe.digest())
print('Encoded address: %s' % returnedAddress)
status, addressVersionNumber, streamNumber, data = \
decodeAddress(returnedAddress)
print(
'\nAfter decoding address:\n\tStatus: %s'
'\n\taddressVersionNumber %s'
'\n\tstreamNumber %s'
'\n\tlength of data (the ripe hash): %s'
'\n\tripe data: %s' %
(status, addressVersionNumber, streamNumber, len(data), hexlify(data))
)
17 changes: 9 additions & 8 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import shared
import shutdown
import state
import threads
from addresses import (
addBMIfNotPresent,
calculateInventoryHash,
Expand Down Expand Up @@ -1206,7 +1207,7 @@ def HandleDisseminatePreEncryptedMsg(self, params):
len(encryptedPayload) + requiredPayloadLengthExtraBytes + 8
) * requiredAverageProofOfWorkNonceTrialsPerByte
)
with shared.printLock:
with threads.printLock:
print(
'(For msg message via API) Doing proof of work.'
'Total required difficulty:',
Expand All @@ -1221,7 +1222,7 @@ def HandleDisseminatePreEncryptedMsg(self, params):
powStartTime = time.time()
initialHash = hashlib.sha512(encryptedPayload).digest()
trialValue, nonce = proofofwork.run(target, initialHash)
with shared.printLock:
with threads.printLock:
print '(For msg message via API) Found proof of work', trialValue, 'Nonce:', nonce
try:
print(
Expand All @@ -1240,7 +1241,7 @@ def HandleDisseminatePreEncryptedMsg(self, params):
objectType, toStreamNumber, encryptedPayload,
int(time.time()) + TTL, ''
)
with shared.printLock:
with threads.printLock:
print 'Broadcasting inv for msg(API disseminatePreEncryptedMsg command):', hexlify(inventoryHash)
queues.invQueue.put((toStreamNumber, inventoryHash))

Expand Down Expand Up @@ -1294,7 +1295,7 @@ def HandleDissimatePubKey(self, params):
Inventory()[inventoryHash] = (
objectType, pubkeyStreamNumber, payload, int(time.time()) + TTL, ''
)
with shared.printLock:
with threads.printLock:
print 'broadcasting inv within API command disseminatePubkey with hash:', hexlify(inventoryHash)
queues.invQueue.put((pubkeyStreamNumber, inventoryHash))

Expand Down Expand Up @@ -1347,15 +1348,15 @@ def HandleClientStatus(self, params):
connections_num = len(network.stats.connectedHostsList())
if connections_num == 0:
networkStatus = 'notConnected'
elif shared.clientHasReceivedIncomingConnections:
elif state.clientHasReceivedIncomingConnections:
networkStatus = 'connectedAndReceivingIncomingConnections'
else:
networkStatus = 'connectedButHaveNotReceivedIncomingConnections'
return json.dumps({
'networkConnections': connections_num,
'numberOfMessagesProcessed': shared.numberOfMessagesProcessed,
'numberOfBroadcastsProcessed': shared.numberOfBroadcastsProcessed,
'numberOfPubkeysProcessed': shared.numberOfPubkeysProcessed,
'numberOfMessagesProcessed': state.numberOfMessagesProcessed,
'numberOfBroadcastsProcessed': state.numberOfBroadcastsProcessed,
'numberOfPubkeysProcessed': state.numberOfPubkeysProcessed,
'networkStatus': networkStatus,
'softwareName': 'PyBitmessage',
'softwareVersion': softwareVersion
Expand Down
7 changes: 4 additions & 3 deletions src/bitmessagecurses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import queues
import shared
import shutdown
import state

from addresses import addBMIfNotPresent, decodeAddress
from bmconfigparser import BMConfigParser
Expand Down Expand Up @@ -274,11 +275,11 @@ def drawtab(stdscr):
# Uptime and processing data
stdscr.addstr(6, 35, "Since startup on " + l10n.formatTimestamp(startuptime, False))
stdscr.addstr(7, 40, "Processed " + str(
shared.numberOfMessagesProcessed).ljust(4) + " person-to-person messages.")
state.numberOfMessagesProcessed).ljust(4) + " person-to-person messages.")
stdscr.addstr(8, 40, "Processed " + str(
shared.numberOfBroadcastsProcessed).ljust(4) + " broadcast messages.")
state.numberOfBroadcastsProcessed).ljust(4) + " broadcast messages.")
stdscr.addstr(9, 40, "Processed " + str(
shared.numberOfPubkeysProcessed).ljust(4) + " public keys.")
state.numberOfPubkeysProcessed).ljust(4) + " public keys.")

# Inventory data
stdscr.addstr(11, 35, "Inventory lookups per second: " + str(inventorydata).ljust(3))
Expand Down
53 changes: 14 additions & 39 deletions src/bitmessagemain.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
from bmconfigparser import BMConfigParser
from debug import logger # this should go before any threads
from helper_startup import (
isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections,
start_proxyconfig
)
adjustHalfOpenConnectionsLimit, start_proxyconfig)
from inventory import Inventory
from knownnodes import readKnownNodes
# Network objects and threads
Expand All @@ -51,30 +49,8 @@
from singleinstance import singleinstance
# Synchronous threads
from threads import (
set_thread_name, addressGenerator, objectProcessor, singleCleaner,
singleWorker, sqlThread
)


def connectToStream(streamNumber):
"""Connect to a stream"""
state.streamsInWhichIAmParticipating.append(streamNumber)

if isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections():
# Some XP and Vista systems can only have 10 outgoing connections
# at a time.
state.maximumNumberOfHalfOpenConnections = 9
else:
state.maximumNumberOfHalfOpenConnections = 64
try:
# don't overload Tor
if BMConfigParser().get(
'bitmessagesettings', 'socksproxytype') != 'none':
state.maximumNumberOfHalfOpenConnections = 4
except:
pass

BMConnectionPool().connectToStream(streamNumber)
set_thread_name, printLock,
addressGenerator, objectProcessor, singleCleaner, singleWorker, sqlThread)


def _fixSocket():
Expand Down Expand Up @@ -157,7 +133,7 @@ def signal_handler(signum, frame):
logger.error("Got signal %i", signum)
# there are possible non-UI variants to run bitmessage
# which should shutdown especially test-mode
if shared.thisapp.daemon or not state.enableGUI:
if state.thisapp.daemon or not state.enableGUI:
shutdown.doCleanShutdown()
else:
print('# Thread: %s(%d)' % (thread.name, thread.ident))
Expand All @@ -175,6 +151,7 @@ def start(self):
"""Start main application"""
# pylint: disable=too-many-statements,too-many-branches,too-many-locals
_fixSocket()
adjustHalfOpenConnectionsLimit()

config = BMConfigParser()
daemon = config.safeGetBoolean('bitmessagesettings', 'daemon')
Expand Down Expand Up @@ -233,10 +210,10 @@ def start(self):
' \'-c\' as a commandline argument.'
)
# is the application already running? If yes then exit.
shared.thisapp = singleinstance("", daemon)
state.thisapp = singleinstance("", daemon)

if daemon:
with shared.printLock:
with printLock:
print('Running as a daemon. Send TERM signal to end.')
self.daemonize()

Expand Down Expand Up @@ -333,7 +310,7 @@ def start(self):
# start network components if networking is enabled
if state.enableNetwork:
start_proxyconfig()
BMConnectionPool()
BMConnectionPool().connectToStream(1)
asyncoreThread = BMNetworkThread()
asyncoreThread.daemon = True
asyncoreThread.start()
Expand All @@ -357,8 +334,6 @@ def start(self):
state.uploadThread.daemon = True
state.uploadThread.start()

connectToStream(1)

if config.safeGetBoolean('bitmessagesettings', 'upnp'):
import upnp
upnpThread = upnp.uPnPThread()
Expand Down Expand Up @@ -413,7 +388,7 @@ def daemonize():
try:
if os.fork():
# unlock
shared.thisapp.cleanup()
state.thisapp.cleanup()
# wait until grandchild ready
while True:
time.sleep(1)
Expand All @@ -423,7 +398,7 @@ def daemonize():
pass
else:
parentPid = os.getpid()
shared.thisapp.lock() # relock
state.thisapp.lock() # relock

os.umask(0)
try:
Expand All @@ -434,7 +409,7 @@ def daemonize():
try:
if os.fork():
# unlock
shared.thisapp.cleanup()
state.thisapp.cleanup()
# wait until child ready
while True:
time.sleep(1)
Expand All @@ -443,8 +418,8 @@ def daemonize():
# fork not implemented
pass
else:
shared.thisapp.lock() # relock
shared.thisapp.lockPid = None # indicate we're the final child
state.thisapp.lock() # relock
state.thisapp.lockPid = None # indicate we're the final child
sys.stdout.flush()
sys.stderr.flush()
if not sys.platform.startswith('win'):
Expand Down Expand Up @@ -483,7 +458,7 @@ def usage():
@staticmethod
def stop():
"""Stop main application"""
with shared.printLock:
with printLock:
print('Stopping Bitmessage Deamon.')
shutdown.doCleanShutdown()

Expand Down
Loading