Skip to content

Commit

Permalink
Merge pull request #3572 from devos50/one_chain
Browse files Browse the repository at this point in the history
Changed TrustChain so it's a single chain instead of multiple fragmented communities
  • Loading branch information
devos50 authored Jun 25, 2018
2 parents 39b2132 + cc8f656 commit 07892bf
Show file tree
Hide file tree
Showing 50 changed files with 735 additions and 1,006 deletions.
90 changes: 45 additions & 45 deletions Tribler/Core/APIImplementation/LaunchManyCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@
from threading import Event, enumerate as enumerate_threads
from traceback import print_exc

from twisted.internet import reactor
from twisted.internet.defer import Deferred, inlineCallbacks, DeferredList, succeed
from twisted.internet.task import LoopingCall
from twisted.internet.threads import deferToThread
from twisted.python.threadable import isInIOThread

from Tribler.community.market.wallet.dummy_wallet import DummyWallet1, DummyWallet2
from Tribler.community.market.wallet.tc_wallet import TrustchainWallet
from Tribler.Core.CacheDB.sqlitecachedb import forceDBThread
from Tribler.Core.DecentralizedTracking.dht_provider import MainlineDHTProvider
from Tribler.Core.DownloadConfig import DownloadStartupConfig, DefaultDownloadStartupConfig
from Tribler.Core.Modules.resource_monitor import ResourceMonitor
from Tribler.Core.Modules.search_manager import SearchManager
from Tribler.Core.Modules.versioncheck_manager import VersionCheckManager
from Tribler.Core.Modules.wallet.dummy_wallet import DummyWallet1, DummyWallet2
from Tribler.Core.Modules.wallet.tc_wallet import TrustchainWallet
from Tribler.Core.Modules.watch_folder import WatchFolder
from Tribler.Core.TorrentChecker.torrent_checker import TorrentChecker
from Tribler.Core.TorrentDef import TorrentDef, TorrentDefNoMetainfo
Expand All @@ -47,6 +41,11 @@
from Tribler.pyipv8.ipv8.peerdiscovery.discovery import EdgeWalk, RandomWalk
from Tribler.pyipv8.ipv8.taskmanager import TaskManager
from Tribler.pyipv8.ipv8_service import IPv8
from twisted.internet import reactor
from twisted.internet.defer import Deferred, inlineCallbacks, DeferredList, succeed
from twisted.internet.task import LoopingCall
from twisted.internet.threads import deferToThread
from twisted.python.threadable import isInIOThread


class TriblerLaunchMany(TaskManager):
Expand Down Expand Up @@ -102,7 +101,8 @@ def __init__(self):
self.tracker_manager = None
self.torrent_checker = None
self.tunnel_community = None
self.triblerchain_community = None
self.trustchain_community = None
self.wallets = {}

self.startup_deferred = Deferred()

Expand Down Expand Up @@ -243,17 +243,20 @@ def load_ipv8_overlays(self):
if not self.session.config.get_dispersy_enabled():
self.ipv8.strategies.append((RandomWalk(discovery_community), 20))

# TriblerChain Community
# TrustChain Community
if self.session.config.get_trustchain_enabled():
triblerchain_peer = Peer(self.session.trustchain_keypair)
from Tribler.pyipv8.ipv8.attestation.trustchain.community import TrustChainCommunity
trustchain_peer = Peer(self.session.trustchain_keypair)

from Tribler.community.triblerchain.community import TriblerChainCommunity
self.triblerchain_community = TriblerChainCommunity(triblerchain_peer, self.ipv8.endpoint,
self.ipv8.network,
tribler_session=self.session,
working_directory=self.session.config.get_state_dir())
self.ipv8.overlays.append(self.triblerchain_community)
self.ipv8.strategies.append((EdgeWalk(self.triblerchain_community), 20))
self.trustchain_community = TrustChainCommunity(trustchain_peer, self.ipv8.endpoint,
self.ipv8.network,
working_directory=self.session.config.get_state_dir(),
testnet=self.session.config.get_trustchain_testnet())
self.ipv8.overlays.append(self.trustchain_community)
self.ipv8.strategies.append((EdgeWalk(self.trustchain_community), 20))

tc_wallet = TrustchainWallet(self.trustchain_community)
self.wallets[tc_wallet.get_identifier()] = tc_wallet

# Tunnel Community
if self.session.config.get_tunnel_community_enabled():
Expand All @@ -265,39 +268,19 @@ def load_ipv8_overlays(self):
dht_provider=MainlineDHTProvider(
self.mainline_dht,
self.session.config.get_dispersy_port()),
triblerchain_community=self.triblerchain_community)
bandwidth_wallet=self.wallets["MB"])
self.ipv8.overlays.append(self.tunnel_community)
self.ipv8.strategies.append((RandomWalk(self.tunnel_community), 20))

# Market Community
if self.session.config.get_market_community_enabled():
wallets = {}

try:
from Tribler.community.market.wallet.btc_wallet import BitcoinWallet, BitcoinTestnetWallet
wallet_type = BitcoinTestnetWallet if self.session.config.get_btc_testnet() else BitcoinWallet
btc_wallet = wallet_type(os.path.join(self.session.config.get_state_dir(), 'wallet'))
wallets[btc_wallet.get_identifier()] = btc_wallet
except ImportError:
self._logger.error("Electrum wallet cannot be found, Bitcoin trading not available!")

mc_wallet = TrustchainWallet(self.triblerchain_community)
wallets[mc_wallet.get_identifier()] = mc_wallet

if self.session.config.get_dummy_wallets_enabled():
# For debugging purposes, we create dummy wallets
dummy_wallet1 = DummyWallet1()
wallets[dummy_wallet1.get_identifier()] = dummy_wallet1

dummy_wallet2 = DummyWallet2()
wallets[dummy_wallet2.get_identifier()] = dummy_wallet2

from Tribler.community.market.community import MarketCommunity
market_peer = Peer(self.session.tradechain_keypair)
market_peer = Peer(self.session.trustchain_keypair)

self.market_community = MarketCommunity(market_peer, self.ipv8.endpoint, self.ipv8.network,
tribler_session=self.session,
wallets=wallets,
trustchain=self.trustchain_community,
wallets=self.wallets,
working_directory=self.session.config.get_state_dir())

self.ipv8.overlays.append(self.market_community)
Expand Down Expand Up @@ -380,6 +363,23 @@ def init(self):
self.session.config.get_state_dir())
self.upnp_ports.append((self.session.config.get_mainline_dht_port(), 'UDP'))

# Wallets
try:
from Tribler.Core.Modules.wallet.btc_wallet import BitcoinWallet, BitcoinTestnetWallet
wallet_type = BitcoinTestnetWallet if self.session.config.get_btc_testnet() else BitcoinWallet
btc_wallet = wallet_type(os.path.join(self.session.config.get_state_dir(), 'wallet'))
self.wallets[btc_wallet.get_identifier()] = btc_wallet
except ImportError:
self._logger.error("Electrum wallet cannot be found, Bitcoin wallet not available!")

if self.session.config.get_dummy_wallets_enabled():
# For debugging purposes, we create dummy wallets
dummy_wallet1 = DummyWallet1()
self.wallets[dummy_wallet1.get_identifier()] = dummy_wallet1

dummy_wallet2 = DummyWallet2()
self.wallets[dummy_wallet2.get_identifier()] = dummy_wallet2

if self.ipv8:
self.load_ipv8_overlays()

Expand Down Expand Up @@ -868,10 +868,10 @@ def early_shutdown(self):

self.tracker_manager = None

if self.tunnel_community and self.triblerchain_community:
# We unload these overlays manually since the triblerchain has to be unloaded after the tunnel overlay.
if self.tunnel_community and self.trustchain_community:
# We unload these overlays manually since the TrustChain has to be unloaded after the tunnel overlay.
yield self.ipv8.unload_overlay(self.tunnel_community)
yield self.ipv8.unload_overlay(self.triblerchain_community)
yield self.ipv8.unload_overlay(self.trustchain_community)

if self.dispersy:
self._logger.info("lmc: Shutting down Dispersy...")
Expand Down
1 change: 1 addition & 0 deletions Tribler/Core/Config/config.spec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ec_keypair_filename = string(default='')
enabled = boolean(default=True)
ec_keypair_filename = string(default='')
live_edges_enabled = boolean(default=True)
testnet = boolean(default=False)

[wallets]
btc_testnet = boolean(default=False)
Expand Down
16 changes: 6 additions & 10 deletions Tribler/Core/Config/tribler_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ def set_trustchain_live_edges_enabled(self, value):
def get_trustchain_live_edges_enabled(self):
return self.config['trustchain']['live_edges_enabled']

def set_trustchain_testnet(self, value):
self.config['trustchain']['testnet'] = value

def get_trustchain_testnet(self):
return self.config['trustchain']['testnet']

def set_megacache_enabled(self, value):
self.config['general']['megacache'] = value

Expand Down Expand Up @@ -509,16 +515,6 @@ def set_is_matchmaker(self, value):
def get_is_matchmaker(self):
return self.config['market_community']['matchmaker']

def set_tradechain_permid_keypair_filename(self, keypairfilename):
self.config['market_community']['ec_keypair_filename'] = keypairfilename

def get_tradechain_permid_keypair_filename(self):
file_name = self.config['market_community']['ec_keypair_filename']
if not file_name:
file_name = os.path.join(self.get_state_dir(), 'ec_tradechain.pem')
self.set_tradechain_permid_keypair_filename(file_name)
return file_name

# Wallets

def set_btc_testnet(self, value):
Expand Down
25 changes: 12 additions & 13 deletions Tribler/Core/Modules/restapi/trustchain_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ def render_GET(self, request):
}
}
"""
triblerchain_community = self.session.lm.triblerchain_community
if not triblerchain_community:
if 'MB' not in self.session.lm.wallets:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": "triblerchain community not found"})
return json.dumps({"error": "TrustChain community not found"})

return json.dumps({'statistics': triblerchain_community.get_statistics()})
return json.dumps({'statistics': self.session.lm.wallets['MB'].get_statistics()})


class TrustchainBlocksEndpoint(TrustchainBaseEndpoint):
Expand Down Expand Up @@ -139,10 +138,10 @@ def render_GET(self, request):
}, ...]
}
"""
triblerchain_community = self.session.lm.triblerchain_community
if not triblerchain_community:
trustchain_community = self.session.lm.trustchain_community
if not trustchain_community:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": "triblerchain community not found"})
return json.dumps({"error": "trustchain community not found"})

limit_blocks = 100

Expand All @@ -156,7 +155,7 @@ def render_GET(self, request):
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "limit parameter out of range"})

blocks = triblerchain_community.persistence.get_latest_blocks(self.identity.decode("HEX"), limit_blocks)
blocks = trustchain_community.persistence.get_latest_blocks(self.identity.decode("HEX"), limit_blocks)
return json.dumps({"blocks": [dict(block) for block in blocks]})


Expand Down Expand Up @@ -196,12 +195,12 @@ def render_GET(self, request):
}
"""

triblerchain_community = self.session.lm.triblerchain_community
if not triblerchain_community:
if 'MB' not in self.session.lm.wallets:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": "triblerchain community not found"})
return json.dumps({"error": "bandwidth wallet not found"})
bandwidth_wallet = self.session.lm.wallets['MB']

available_tokens = triblerchain_community.get_bandwidth_tokens()
available_tokens = bandwidth_wallet.get_bandwidth_tokens()

if 'amount' in request.args:
try:
Expand All @@ -220,5 +219,5 @@ def render_GET(self, request):
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "Not enough bandwidth tokens available"})

result = triblerchain_community.bootstrap_new_identity(amount)
result = bandwidth_wallet.bootstrap_new_identity(amount)
return json.dumps(result)
16 changes: 8 additions & 8 deletions Tribler/Core/Modules/restapi/wallets_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def render_GET(self, request):
"""
wallets = {}
balance_deferreds = []
for wallet_id in self.session.lm.market_community.get_wallet_ids():
wallet = self.session.lm.market_community.wallets[wallet_id]
for wallet_id in self.session.lm.wallets.iterkeys():
wallet = self.session.lm.wallets[wallet_id]
wallets[wallet_id] = {
'created': wallet.created,
'unlocked': wallet.unlocked,
Expand Down Expand Up @@ -108,7 +108,7 @@ def render_PUT(self, request):
"created": True
}
"""
if self.session.lm.market_community.wallets[self.identifier].created:
if self.session.lm.wallets[self.identifier].created:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "this wallet already exists"})

Expand All @@ -125,10 +125,10 @@ def on_wallet_created(_):
if self.identifier == "BTC" or self.identifier == "TBTC": # get the password
if parameters['password'] and len(parameters['password']) > 0:
password = parameters['password'][0]
self.session.lm.market_community.wallets[self.identifier].create_wallet(password=password)\
self.session.lm.wallets[self.identifier].create_wallet(password=password)\
.addCallback(on_wallet_created)
else:
self.session.lm.market_community.wallets[self.identifier].create_wallet().addCallback(on_wallet_created)
self.session.lm.wallets[self.identifier].create_wallet().addCallback(on_wallet_created)

return NOT_DONE_YET

Expand Down Expand Up @@ -171,7 +171,7 @@ def on_balance(balance):
request.write(json.dumps({"balance": balance}))
request.finish()

self.session.lm.market_community.wallets[self.identifier].get_balance().addCallback(on_balance)
self.session.lm.wallets[self.identifier].get_balance().addCallback(on_balance)

return NOT_DONE_YET

Expand Down Expand Up @@ -220,7 +220,7 @@ def on_transactions(transactions):
request.write(json.dumps({"transactions": transactions}))
request.finish()

self.session.lm.market_community.wallets[self.identifier].get_transactions().addCallback(on_transactions)
self.session.lm.wallets[self.identifier].get_transactions().addCallback(on_transactions)

return NOT_DONE_YET

Expand Down Expand Up @@ -262,7 +262,7 @@ def render_POST(self, request):
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "currently, currency transfers using the API is only supported for Bitcoin"})

wallet = self.session.lm.market_community.wallets[self.identifier]
wallet = self.session.lm.wallets[self.identifier]

if not wallet.created:
request.setResponseCode(http.BAD_REQUEST)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,49 @@
GENESIS_SEQ, GENESIS_HASH


class TriblerChainBlock(TrustChainBlock):
class TriblerBandwidthBlock(TrustChainBlock):
"""
Container for TriblerChain block information
Container for bandwidth block information
"""

@classmethod
def create(cls, transaction, database, public_key, link=None, link_pk=None):
def create(cls, block_type, transaction, database, public_key, link=None, link_pk=None):
"""
Create an empty next block.
:param database: the database to use as information source
:param block_type: the type of the block to be created
:param transaction: the transaction to use in this block
:param database: the database to use as information source
:param public_key: the public key to use for this block
:param link: optionally create the block as a linked block to this block
:param link_pk: the public key of the counterparty in this transaction
:return: A newly created block
"""
blk = database.get_latest(public_key)
latest_bw_block = database.get_latest(public_key, block_type='tribler_bandwidth')
latest_block = database.get_latest(public_key)
ret = cls()
if link:
ret.type = link.type
ret.transaction["up"] = link.transaction["down"] if "down" in link.transaction else 0
ret.transaction["down"] = link.transaction["up"] if "up" in link.transaction else 0
ret.link_public_key = link.public_key
ret.link_sequence_number = link.sequence_number
else:
ret.type = block_type
ret.transaction["up"] = transaction["up"] if "up" in transaction else 0
ret.transaction["down"] = transaction["down"] if "down" in transaction else 0
ret.link_public_key = link_pk

if blk:
ret.transaction["total_up"] = blk.transaction["total_up"] + ret.transaction["up"]
ret.transaction["total_down"] = blk.transaction["total_down"] + ret.transaction["down"]
ret.sequence_number = blk.sequence_number + 1
ret.previous_hash = blk.hash
if latest_bw_block:
ret.transaction["total_up"] = latest_bw_block.transaction["total_up"] + ret.transaction["up"]
ret.transaction["total_down"] = latest_bw_block.transaction["total_down"] + ret.transaction["down"]
else:
ret.transaction["total_up"] = ret.transaction["up"]
ret.transaction["total_down"] = ret.transaction["down"]

if latest_block:
ret.sequence_number = latest_block.sequence_number + 1
ret.previous_hash = latest_block.hash

ret.public_key = public_key
ret.signature = EMPTY_SIG

Expand Down Expand Up @@ -72,8 +78,8 @@ def err(reason):

blk = database.get(self.public_key, self.sequence_number)
link = database.get_linked(self)
prev_blk = database.get_block_before(self)
next_blk = database.get_block_after(self)
prev_blk = database.get_block_before(self, block_type='tribler_bandwidth')
next_blk = database.get_block_after(self, block_type='tribler_bandwidth')

is_genesis = self.sequence_number == GENESIS_SEQ or self.previous_hash == GENESIS_HASH
if is_genesis:
Expand Down
Loading

0 comments on commit 07892bf

Please sign in to comment.