# Buggy code
def received_trustchain_statistics(self, statistics):
    if not statistics:
        return
    statistics = statistics["statistics"]
    total_up = 0
    total_down = 0
    if "latest_block" in statistics:
        total_up = statistics["latest_block"]["transaction"]["total_up"]
        total_down = statistics["latest_block"]["transaction"]["total_down"]

    self.window().trust_contribution_amount_label.setText(
        "%s MBytes" % (total_up / self.byte_scale)
    )
    self.window().trust_consumption_amount_label.setText(
        "%s MBytes" % (total_down / self.byte_scale)
    )

    self.window().trust_people_helped_label.setText(
        "%d" % statistics["peers_that_pk_helped"]
    )
    self.window().trust_people_helped_you_label.setText(
        "%d" % statistics["peers_that_helped_pk"]
    )

    # Fetch the latest blocks of this user
    self.public_key = statistics["id"]
    self.request_mgr = TriblerRequestManager()
    self.request_mgr.perform_request(
        "ipv8/trustchain/users/%s/blocks" % self.public_key,
        self.received_trustchain_blocks,
    )


# Traceback (TypeError)
ERROR   1538645830.67              rest_manager:53   (RESTRequest)  [Failure instance: Traceback: <type 'exceptions.TypeError'>: 'NoneType' object is not iterable
/home/sandip/.local/lib/python2.7/site-packages/twisted/protocols/basic.py:572:dataReceived
/home/sandip/.local/lib/python2.7/site-packages/twisted/web/http.py:2099:lineReceived
/home/sandip/.local/lib/python2.7/site-packages/twisted/web/http.py:2190:allContentReceived
/home/sandip/.local/lib/python2.7/site-packages/twisted/web/http.py:917:requestReceived
--- <exception caught here> ---
/home/sandip/.local/lib/python2.7/site-packages/twisted/web/server.py:198:process
/home/sandip/.local/lib/python2.7/site-packages/twisted/web/server.py:258:render
/home/sandip/.local/lib/python2.7/site-packages/twisted/web/resource.py:250:render
/home/sandip/tudelft/tribler/Tribler/Core/Modules/restapi/trustchain_endpoint.py:85:render_GET
/home/sandip/tudelft/tribler/Tribler/pyipv8/ipv8/util.py:49:helper
/home/sandip/tudelft/tribler/Tribler/pyipv8/ipv8/util.py:61:blockingCallFromThread
/home/sandip/tudelft/tribler/Tribler/Core/Modules/wallet/tc_wallet.py:172:get_statistics
/home/sandip/tudelft/tribler/Tribler/pyipv8/ipv8/attestation/trustchain/database.py:156:get_latest
/home/sandip/tudelft/tribler/Tribler/pyipv8/ipv8/attestation/trustchain/database.py:78:_get
]
Traceback (most recent call last):
File "/home/sandip/.local/lib/python2.7/site-packages/twisted/web/server.py", line 198, in process
self.render(resrc)
File "/home/sandip/.local/lib/python2.7/site-packages/twisted/web/server.py", line 258, in render
body = resrc.render(self)
File "/home/sandip/.local/lib/python2.7/site-packages/twisted/web/resource.py", line 250, in render
return m(request)
File "/home/sandip/tudelft/tribler/Tribler/Core/Modules/restapi/trustchain_endpoint.py", line 85, in render_GET
return json.dumps({'statistics': self.session.lm.wallets['MB'].get_statistics()})
File "/home/sandip/tudelft/tribler/Tribler/pyipv8/ipv8/util.py", line 49, in helper
return blockingCallFromThread(reactor, func, *args, **kargs)
File "/home/sandip/tudelft/tribler/Tribler/pyipv8/ipv8/util.py", line 61, in blockingCallFromThread
return f(*args, **kwargs)
File "/home/sandip/tudelft/tribler/Tribler/Core/Modules/wallet/tc_wallet.py", line 172, in get_statistics
latest_block = self.trustchain.persistence.get_latest(public_key, block_type='tribler_bandwidth')
File "/home/sandip/tudelft/tribler/Tribler/pyipv8/ipv8/attestation/trustchain/database.py", line 156, in get_latest
(database_blob(public_key), block_type, database_blob(public_key), block_type))
File "/home/sandip/tudelft/tribler/Tribler/pyipv8/ipv8/attestation/trustchain/database.py", line 78, in _get
db_result = list(self.execute(self.get_sql_header() + query, params, fetch_all=False))
TypeError: 'NoneType' object is not iterable

# Fixed code
def received_trustchain_statistics(self, statistics):
    if not statistics:
        return
    statistics = statistics["statistics"]
    self.public_key = statistics["id"]
    total_up = 0
    total_down = 0
    if "latest_block" in statistics:
        total_up = statistics["latest_block"]["transaction"]["total_up"]
        total_down = statistics["latest_block"]["transaction"]["total_down"]

    self.window().trust_contribution_amount_label.setText(
        "%s MBytes" % (total_up / self.byte_scale)
    )
    self.window().trust_consumption_amount_label.setText(
        "%s MBytes" % (total_down / self.byte_scale)
    )

    self.window().trust_people_helped_label.setText(
        "%d" % statistics["peers_that_pk_helped"]
    )
    self.window().trust_people_helped_you_label.setText(
        "%d" % statistics["peers_that_helped_pk"]
    )