2019-05-09 05:27:15 +00:00
|
|
|
'''
|
2019-06-12 06:44:15 +00:00
|
|
|
Onionr - Private P2P Communication
|
2019-05-09 05:27:15 +00:00
|
|
|
|
|
|
|
Upload blocks in the upload queue to peers from the communicator
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
'''
|
|
|
|
import logger
|
|
|
|
from communicatorutils import proxypicker
|
|
|
|
import onionrblockapi as block
|
2019-06-25 23:07:35 +00:00
|
|
|
from onionrutils import localcommand, stringvalidators, basicrequests
|
2019-07-10 22:38:20 +00:00
|
|
|
from communicator import onlinepeers
|
2019-05-09 05:27:15 +00:00
|
|
|
|
|
|
|
def upload_blocks_from_communicator(comm_inst):
|
|
|
|
# when inserting a block, we try to upload it to a few peers to add some deniability
|
2019-07-11 18:26:57 +00:00
|
|
|
TIMER_NAME = "upload_blocks_from_communicator"
|
|
|
|
|
2019-05-09 05:27:15 +00:00
|
|
|
triedPeers = []
|
|
|
|
finishedUploads = []
|
2019-06-25 23:07:35 +00:00
|
|
|
core = comm_inst._core
|
|
|
|
comm_inst.blocksToUpload = core._crypto.randomShuffle(comm_inst.blocksToUpload)
|
2019-05-09 05:27:15 +00:00
|
|
|
if len(comm_inst.blocksToUpload) != 0:
|
|
|
|
for bl in comm_inst.blocksToUpload:
|
2019-06-25 23:07:35 +00:00
|
|
|
if not stringvalidators.validate_hash(bl):
|
2019-07-11 18:26:57 +00:00
|
|
|
logger.warn('Requested to upload invalid block', terminal=True)
|
|
|
|
comm_inst.decrementThreadCount(TIMER_NAME)
|
2019-05-09 05:27:15 +00:00
|
|
|
return
|
|
|
|
for i in range(min(len(comm_inst.onlinePeers), 6)):
|
2019-07-10 22:38:20 +00:00
|
|
|
peer = onlinepeers.pick_online_peer(comm_inst)
|
2019-05-09 05:27:15 +00:00
|
|
|
if peer in triedPeers:
|
|
|
|
continue
|
|
|
|
triedPeers.append(peer)
|
|
|
|
url = 'http://' + peer + '/upload'
|
|
|
|
data = {'block': block.Block(bl).getRaw()}
|
|
|
|
proxyType = proxypicker.pick_proxy(peer)
|
2019-06-26 00:15:04 +00:00
|
|
|
logger.info("Uploading block to " + peer, terminal=True)
|
2019-06-25 23:07:35 +00:00
|
|
|
if not basicrequests.do_post_request(core, url, data=data, proxyType=proxyType) == False:
|
|
|
|
localcommand.local_command(core, 'waitforshare/' + bl, post=True)
|
2019-05-09 05:27:15 +00:00
|
|
|
finishedUploads.append(bl)
|
|
|
|
for x in finishedUploads:
|
|
|
|
try:
|
|
|
|
comm_inst.blocksToUpload.remove(x)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-07-11 18:26:57 +00:00
|
|
|
comm_inst.decrementThreadCount(TIMER_NAME)
|