Skip to content

Commit

Permalink
Merge pull request #4 from cyberway/fix-getRandomAccount
Browse files Browse the repository at this point in the history
Fix getRandomAccount to support None returned from getAccount cyberway/commun.contracts#590
  • Loading branch information
afalaleev authored Mar 13, 2020
2 parents 54aac8d + 3679abb commit 70ea6a9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def formatError(self, error):
if isinstance(error, CleosException):
print(' ', error.output.replace('\n', '\n '), sep='')
else:
print(' ', error.replace('\n', '\n '), sep='')
print(' ', str(error).replace('\n', '\n '), sep='')

def _processItem(self, mItem, items, predicat, data, mapping):
subitem = items[mItem.index]
Expand Down
18 changes: 10 additions & 8 deletions testnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ def _cleos(arguments, *, output=False, retry=None, logger=None):
try:
return subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
except subprocess.CalledProcessError as e:
if logger: logger.formatError(e)
import sys
(exception, traceback) = (e, sys.exc_info()[2])

if retry is None or retry <= 1:
raise CleosException.fromCalledProcessError(exception).with_traceback(traceback)
err = CleosException.fromCalledProcessError(exception).with_traceback(traceback)
if logger: logger.formatError(err)
raise err
else: retry -= 1


Expand Down Expand Up @@ -354,7 +355,7 @@ def createAccount(creator, account, owner_auth, active_auth=None, **kwargs):

def getAccount(account, **kwargs):
try:
acc = json.loads(cleos('get account -j {acc}'.format(acc=account), **kwargs))
acc = json.loads(cleos('get account -j {acc}'.format(acc=account), **kwargs, output=False))
perm = {}
for p in acc['permissions']:
perm[p['perm_name']] = p
Expand Down Expand Up @@ -407,19 +408,20 @@ def randomText(length):
return ''.join(random.choice(letters) for i in range(length))

def createKey():
data = cleos("create key --to-console")
data = cleos("create key --to-console", output=False)
m = re.search('Private key: ([a-zA-Z0-9]+)\nPublic key: ([a-zA-Z0-9]+)', data)
return (m.group(1), m.group(2))

def importPrivateKey(private):
cleos("wallet import --name test --private-key %s" % private)

def getRandomAccount():
while True:
retry = 32
while retry:
name = randomName()
try: getAccount(name)
except: break
return name
if getAccount(name) is None: return name
retry -= 1
raise BaseException("Can't create random account: Retries exceeded")

def createRandomAccount(owner_auth, active_auth=None, *, creator='tech', **kwargs):
name = getRandomAccount()
Expand Down

0 comments on commit 70ea6a9

Please sign in to comment.