Skip to content

Commit

Permalink
Limit requests when refreshing transfers to avoid DDOSing nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Insoleet committed Jan 30, 2016
1 parent 9b6132a commit b486904
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/sakia/models/txhistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,12 @@ def transfers(self):

async def data_received(self, transfer):
amount = transfer.metadata['amount']
deposit = await self.account.current_ref(transfer.metadata['amount'], self.community,
self.app, transfer.blockid.number)\
.diff_localized(international_system=self.app.preferences['international_system_of_units'])
try:
deposit = await self.account.current_ref(transfer.metadata['amount'], self.community,
self.app, transfer.blockid.number)\
.diff_localized(international_system=self.app.preferences['international_system_of_units'])
except NoPeerAvailable:
deposit = "Could not compute"
comment = ""
if transfer.metadata['comment'] != "":
comment = transfer.metadata['comment']
Expand All @@ -254,9 +257,12 @@ async def data_received(self, transfer):

async def data_sent(self, transfer):
amount = transfer.metadata['amount']
paiment = await self.account.current_ref(transfer.metadata['amount'], self.community,
self.app, transfer.blockid.number)\
.diff_localized(international_system=self.app.preferences['international_system_of_units'])
try:
paiment = await self.account.current_ref(transfer.metadata['amount'], self.community,
self.app, transfer.blockid.number)\
.diff_localized(international_system=self.app.preferences['international_system_of_units'])
except NoPeerAvailable:
paiment = "Could not compute"
comment = ""
if transfer.metadata['comment'] != "":
comment = transfer.metadata['comment']
Expand All @@ -278,8 +284,11 @@ async def data_sent(self, transfer):

async def data_dividend(self, dividend):
amount = dividend['amount']
deposit = await self.account.current_ref(dividend['amount'], self.community, self.app, dividend['block_number'])\
.diff_localized(international_system=self.app.preferences['international_system_of_units'])
try:
deposit = await self.account.current_ref(dividend['amount'], self.community, self.app, dividend['block_number'])\
.diff_localized(international_system=self.app.preferences['international_system_of_units'])
except NoPeerAvailable:
deposit = "Could not compute"
comment = ""
receiver = self.account.name
date_ts = dividend['time']
Expand All @@ -301,8 +310,11 @@ async def refresh_transfers(self):
transfers_data = []
if self.community:
requests_coro = []
data_list = []
count = 0
for transfer in self.transfers():
coro = None
count += 1
if type(transfer) is Transfer:
if transfer.metadata['issuer'] == self.account.pubkey:
coro = asyncio.ensure_future(self.data_sent(transfer))
Expand All @@ -312,8 +324,11 @@ async def refresh_transfers(self):
coro = asyncio.ensure_future(self.data_dividend(transfer))
if coro:
requests_coro.append(coro)
if count % 25 == 0:
gathered_list = await asyncio.gather(*requests_coro)
requests_coro = []
data_list.extend(gathered_list)

data_list = await asyncio.gather(*requests_coro)
for data in data_list:
transfers_data.append(data)
self.transfers_data = transfers_data
Expand Down

0 comments on commit b486904

Please sign in to comment.