Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.

Async kin + Typing#66

Merged
Ronserruya merged 22 commits intov2-masterfrom
async_kin
Apr 7, 2019
Merged

Async kin + Typing#66
Ronserruya merged 22 commits intov2-masterfrom
async_kin

Conversation

@Ronserruya
Copy link
Contributor

@Ronserruya Ronserruya commented Mar 24, 2019

Update the kin-sdk to work with asyncio:

Changes:

  1. Most of the methods are now async
  2. Minimum supported python version changed to 3.6
  3. User-agent on http requests changed from "kin-core-python" to "kin-sdk-python"
  4. Added type hints to all methods
  5. Builder and Horizon modules were removed since we use kin-base instead of inheriting them
  6. KinAccount doesn't throw an error when initialized with account that doesn't exist on the blockchain
  7. Monitors were changed to async generators and not generators in a different threads
  8. New parameter to set timeout between events in payment monitors
  9. MultiMonitor was changed to remove the "add/remove_address" methods, the passed set can just be changed at will.
  10. Timeout parameter removed from get_channel method, as async queue doesn't provide timeout
  11. Added context manager to the KinClient object to close the connection to the blockchain
  12. Removed "verify_kin_payment" method
  13. Transactions are now retried with the same tx, instead of building a new one each time

@codecov
Copy link

codecov bot commented Mar 24, 2019

Codecov Report

Merging #66 into v2-master will increase coverage by 6.41%.
The diff coverage is 71%.

Impacted file tree graph

@@              Coverage Diff              @@
##           v2-master      #66      +/-   ##
=============================================
+ Coverage      78.33%   84.75%   +6.41%     
=============================================
  Files             17       15       -2     
  Lines           1071      774     -297     
  Branches         115       83      -32     
=============================================
- Hits             839      656     -183     
+ Misses           200       93     -107     
+ Partials          32       25       -7
Impacted Files Coverage Δ
kin/blockchain/errors.py 100% <ø> (+0.88%) ⬆️
kin/blockchain/environment.py 100% <100%> (ø) ⬆️
kin/transactions.py 85.07% <100%> (-0.22%) ⬇️
kin/blockchain/keypair.py 93.54% <100%> (+0.95%) ⬆️
kin/config.py 100% <100%> (ø) ⬆️
kin/__init__.py 100% <100%> (ø) ⬆️
kin/version.py 100% <100%> (ø) ⬆️
kin/monitors.py 22.85% <16.66%> (+9.06%) ⬆️
kin/utils.py 41.66% <21.73%> (+3.43%) ⬆️
kin/account.py 84.54% <86.04%> (-0.42%) ⬇️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f9db0ca...779c6a2. Read the comment docs.

@Ronserruya Ronserruya requested a review from mrsegev March 24, 2019 16:08
Copy link

@mrsegev mrsegev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|Please see comments.

services:
stellar-core-1:
image: kinecosystem/stellar-core:kinecosystem-v2.0.0-stellar-v9.2.0
image: kinecosystem/stellar-core:latest
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to hardcode the version. This is the only way to be 100% sure the build is stable. Future updates might break the code.


stellar-core-2:
image: kinecosystem/stellar-core:kinecosystem-v2.0.0-stellar-v9.2.0
image: kinecosystem/stellar-core:latest
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. Let's hardcode the versions in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blockchain team removes their docker images a lot so the tests fail. latest will always be there

py==1.5.2
pytest-cov==2.5.1
pytest==3.4.0
codecov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's lock the version here.

Copy link

@yosriz yosriz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, few small comments.

return raw_tx

def get_account_tx_history(self, address, amount=10, descending=True, cursor=None, simple=True):
async def get_account_tx_history(self, address: str, amount: Optional[int] = 10, descending: Optional[bool] = True,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from a talking with @mrsegev we decided we should't expose this utill we solve history issue on the blockchain side, as it's not reliable now it can cause just troubles,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrsegev This has been exposed for months now, should I remove it then?

response = requests.get(self.environment.friendbot_url, params={'addr': address})
if response.ok:
return response.json()['hash']
response = await self.horizon._session.get(self.environment.friendbot_url, params={'addr': address})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in node-sdk, I've added also the fund option, in case of an existing account, you might want to add it as well (not necessarily now)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I will create a ticket

class ChannelPool(queue):
"""
A thread-safe queue that sets a member's status instead of pulling it in/out of the queue.
An async queue that sets a member's status instead of pulling it in/out of the queue.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity, why queue is needed? seems simple list/set will provide the same functionality? I mean there's no real use of push/pop here.
(mainly trying to understand that I'm not missing something with my node impl for that)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is really no use of any push/pop here. I used the queue class before since it helped me with thread-safety.

Now I'm inheriting it since it has asnyc methods to get/push back to the "queue". Otherwise, I would need to write my own implementation to select an item from the set asyncrounsly

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so I understood it correctly, I would suggest to call it pool instead of queue (just in case it is a public api)

:param threading.Event stop_event: an event that can be used to stop this method
"""
import json
async def single_monitor(kin_client: 'KinClient', address: str,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does the KinClinet is wrapped with ' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kin client imports "Single Monitor"

If I wanted to write KinClient without '' I would need to import KinClient in SingleMonitor, and then I would have a circular loop of importing. So instead of importing the type you can just write it as a string. That's called 'forward referencing'

https://stackoverflow.com/questions/39740632/python-type-hinting-without-cyclic-imports

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python is fun!

:raises: asyncio.TimeoutError: If too much time has passed between events (only if "timeout" is set)
"""
if not is_valid_address(address):
raise StellarAddressInvalidError('invalid address: {}'.format(address))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove Stellar from this error name?

import json
tx_data = SimplifiedTransaction(RawTransaction(tx))
except CantSimplifyError:
logger.debug("SSE transaction couldn't be simplified: ", tx)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's exactly this logger? is it controllable by the developer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a logger like any other. And yes, you can set it's level

See https://github.com/kinecosystem/migration-server/blob/master/scripts/src/init.py#L42

logger.debug("Non-payment SSE transaction skipped: ", tx_data)
continue

# Will yield twice if both of these are correct. (someone sent to himself) - which it fine
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not check both and send an only single event? (not crucial as it's an edge case, but don't see a reason to fire twice)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just how it worked so far so I didnt wanted to change it

sleep(10)
assert not monitor.thread.is_alive()
assert hash4 not in txs_found2
pass
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what exactly is broken? does it happen also on testnet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested my sse implementation manually and it works, but yes we get these 404s from time to time so I didn't want to get my test to fail because of a BC issue

@Ronserruya Ronserruya merged commit 38db287 into v2-master Apr 7, 2019
@Ronserruya Ronserruya deleted the async_kin branch April 7, 2019 11:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants