Skip to content

Commit

Permalink
Fix running out of transaction index issue
Browse files Browse the repository at this point in the history
  • Loading branch information
learnforpractice committed Jun 7, 2022
1 parent 52bbfb8 commit 9fca2d9
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 199 deletions.
105 changes: 54 additions & 51 deletions pysrc/chainapi_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,57 +89,60 @@ async def generate_packed_transaction(self, actions, expiration, ref_block, chai
else:
expiration = int(time.time()) + expiration

tx = Transaction(expiration, ref_block, chain_id)
for a in actions:
contract, action_name, args, permissions = a
if isinstance(args, bytes):
args = args.hex()
elif isinstance(args, dict):
args = json.dumps(args)
elif isinstance(args, str):
pass
else:
tx.free()
raise Exception('Invalid args type')

if isinstance(permissions, dict):
_permissions = permissions
permissions = []
for actor in _permissions:
permissions.append({actor: _permissions[actor]})
permissions = json.dumps(permissions)
self.check_abi(contract)
tx.add_action(contract, action_name, args, permissions)

local_wallet_pub_keys = wallet.get_public_keys()
available_pub_keys = set(local_wallet_pub_keys)

ledger_pub_keys = set()
if not indexes is None:
ledger_pub_keys = ledger.get_public_keys(indexes)
available_pub_keys |= set(ledger_pub_keys)

required_keys = await self.get_sign_keys(fake_actions, list(available_pub_keys))
required_keys = set(required_keys)

signatures = set()
sign_keys = required_keys & set(local_wallet_pub_keys)
for key in sign_keys:
signatures.add(tx.sign(key))

packed_tx = tx.pack(compress, False)
sign_keys = required_keys & set(ledger_pub_keys)
if not sign_keys:
return packed_tx

packed_tx = json.loads(packed_tx)
tx_json = tx.json()
for key in sign_keys:
index = indexes[ledger_pub_keys.index(key)]
signs = ledger.sign(tx_json, [index], chain_id)
signatures |= set(signs)
packed_tx['signatures'] = list(signatures)
return json.dumps(packed_tx)
try:
tx = Transaction(expiration, ref_block, chain_id)
for a in actions:
contract, action_name, args, permissions = a
if isinstance(args, bytes):
args = args.hex()
elif isinstance(args, dict):
args = json.dumps(args)
elif isinstance(args, str):
pass
else:
tx.free()
raise Exception('Invalid args type')

if isinstance(permissions, dict):
_permissions = permissions
permissions = []
for actor in _permissions:
permissions.append({actor: _permissions[actor]})
permissions = json.dumps(permissions)
self.check_abi(contract)
tx.add_action(contract, action_name, args, permissions)

local_wallet_pub_keys = wallet.get_public_keys()
available_pub_keys = set(local_wallet_pub_keys)

ledger_pub_keys = set()
if not indexes is None:
ledger_pub_keys = ledger.get_public_keys(indexes)
available_pub_keys |= set(ledger_pub_keys)

required_keys = await self.get_sign_keys(fake_actions, list(available_pub_keys))
required_keys = set(required_keys)

signatures = set()
sign_keys = required_keys & set(local_wallet_pub_keys)
for key in sign_keys:
signatures.add(tx.sign(key))

packed_tx = tx.pack(compress, False)
sign_keys = required_keys & set(ledger_pub_keys)
if not sign_keys:
return packed_tx

packed_tx = json.loads(packed_tx)
tx_json = tx.json()
for key in sign_keys:
index = indexes[ledger_pub_keys.index(key)]
signs = ledger.sign(tx_json, [index], chain_id)
signatures |= set(signs)
packed_tx['signatures'] = list(signatures)
return json.dumps(packed_tx)
finally:
tx.free()

async def push_action(self, contract, action, args, permissions=None, compress=False, expiration=0, ref_block_id=None, indexes=None, payer=None, payer_permission="active"):
if not permissions:
Expand Down
103 changes: 53 additions & 50 deletions pysrc/chainapi_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,56 +89,59 @@ def generate_packed_transaction(self, actions, expiration, ref_block, chain_id,
else:
expiration = int(time.time()) + expiration

tx = Transaction(expiration, ref_block, chain_id)
for a in actions:
contract, action_name, args, permissions = a
if isinstance(args, bytes):
args = args.hex()
elif isinstance(args, dict):
args = json.dumps(args)
elif isinstance(args, str):
pass
else:
tx.free()
raise Exception('Invalid args type')
if isinstance(permissions, dict):
_permissions = permissions
permissions = []
for actor in _permissions:
permissions.append({actor: _permissions[actor]})
permissions = json.dumps(permissions)
self.check_abi(contract)
tx.add_action(contract, action_name, args, permissions)

local_wallet_pub_keys = wallet.get_public_keys()
available_pub_keys = set(local_wallet_pub_keys)

ledger_pub_keys = set()
if not indexes is None:
ledger_pub_keys = ledger.get_public_keys(indexes)
available_pub_keys |= set(ledger_pub_keys)

required_keys = self.get_sign_keys(fake_actions, list(available_pub_keys))
required_keys = set(required_keys)

signatures = set()
sign_keys = required_keys & set(local_wallet_pub_keys)
for key in sign_keys:
signatures.add(tx.sign(key))

packed_tx = tx.pack(compress, False)
sign_keys = required_keys & set(ledger_pub_keys)
if not sign_keys:
return packed_tx

packed_tx = json.loads(packed_tx)
tx_json = tx.json()
for key in sign_keys:
index = indexes[ledger_pub_keys.index(key)]
signs = ledger.sign(tx_json, [index], chain_id)
signatures |= set(signs)
packed_tx['signatures'] = list(signatures)
return json.dumps(packed_tx)
try:
tx = Transaction(expiration, ref_block, chain_id)
for a in actions:
contract, action_name, args, permissions = a
if isinstance(args, bytes):
args = args.hex()
elif isinstance(args, dict):
args = json.dumps(args)
elif isinstance(args, str):
pass
else:
tx.free()
raise Exception('Invalid args type')
if isinstance(permissions, dict):
_permissions = permissions
permissions = []
for actor in _permissions:
permissions.append({actor: _permissions[actor]})
permissions = json.dumps(permissions)
self.check_abi(contract)
tx.add_action(contract, action_name, args, permissions)

local_wallet_pub_keys = wallet.get_public_keys()
available_pub_keys = set(local_wallet_pub_keys)

ledger_pub_keys = set()
if not indexes is None:
ledger_pub_keys = ledger.get_public_keys(indexes)
available_pub_keys |= set(ledger_pub_keys)

required_keys = self.get_sign_keys(fake_actions, list(available_pub_keys))
required_keys = set(required_keys)

signatures = set()
sign_keys = required_keys & set(local_wallet_pub_keys)
for key in sign_keys:
signatures.add(tx.sign(key))

packed_tx = tx.pack(compress, False)
sign_keys = required_keys & set(ledger_pub_keys)
if not sign_keys:
return packed_tx

packed_tx = json.loads(packed_tx)
tx_json = tx.json()
for key in sign_keys:
index = indexes[ledger_pub_keys.index(key)]
signs = ledger.sign(tx_json, [index], chain_id)
signatures |= set(signs)
packed_tx['signatures'] = list(signatures)
return json.dumps(packed_tx)
finally:
tx.free()

def push_action(self, contract, action, args, permissions=None, compress=False, expiration=0, ref_block_id=None, indexes=None, payer=None, payer_permission="active"):
if not permissions:
Expand Down
42 changes: 20 additions & 22 deletions pysrc/chainnative.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,27 @@ def gen_transaction(self, actions, expiration, reference_block_id, chain_id):
expiration = int(time.time()) + 60
else:
expiration = int(time.time()) + expiration

tx = Transaction(expiration, reference_block_id, chain_id)
for a in actions:
contract, action_name, args, permissions = a
if isinstance(args, bytes):
args = args.hex()
elif isinstance(args, dict):
args = json.dumps(args)
elif isinstance(args, str):
pass
else:
tx.free()
raise Exception(f'Invalid args type: {type(args)}')
if isinstance(permissions, dict):
_permissions = permissions
permissions = []
for actor in _permissions:
permissions.append({actor: _permissions[actor]})
permissions = json.dumps(permissions)
self.check_abi(contract)
tx.add_action(contract, action_name, args, permissions)

try:
tx = Transaction(expiration, reference_block_id, chain_id)
for a in actions:
contract, action_name, args, permissions = a
if isinstance(args, bytes):
args = args.hex()
elif isinstance(args, dict):
args = json.dumps(args)
elif isinstance(args, str):
pass
else:
tx.free()
raise Exception(f'Invalid args type: {type(args)}')
if isinstance(permissions, dict):
_permissions = permissions
permissions = []
for actor in _permissions:
permissions.append({actor: _permissions[actor]})
permissions = json.dumps(permissions)
self.check_abi(contract)
tx.add_action(contract, action_name, args, permissions)
return tx.marshal()
finally:
tx.free()
Expand Down
Loading

0 comments on commit 9fca2d9

Please sign in to comment.