Skip to content

Commit e90e6fe

Browse files
committed
fix(provisioning-server): robustness
Properly estimate gas fees before submitting the giant transaction. Be sure to add new accounts to the needIngress list. Ensure that raised exceptions all derive from Exception.
1 parent 6f9aa68 commit e90e6fe

File tree

1 file changed

+27
-4
lines changed
  • provisioning-server/src/ag_pserver

1 file changed

+27
-4
lines changed

provisioning-server/src/ag_pserver/main.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,22 @@ def agCosmosHelper(reactor, opts, config, input, args, retries = 1):
379379
output = oj
380380
except:
381381
pass
382+
elif stderr[0:14] == b'gas estimate: ':
383+
lines = stderr.split(b'\n')
384+
oj = json.loads(lines[1].decode('utf-8'))
385+
if oj.get('type') is None:
386+
# Reformat the message into what --generate-only produces.
387+
output = {
388+
'type': 'cosmos-sdk/StdTx',
389+
'value': {
390+
'msg': oj['msgs'],
391+
'fee': oj['fee'],
392+
'signatures': None,
393+
'memo': '',
394+
}}
395+
else:
396+
output = oj
397+
code = 0
382398

383399
return code, output
384400

@@ -403,24 +419,31 @@ def doEnablePubkeys(reactor, opts, config, pkobjs):
403419
missing = False
404420
break
405421
elif code == 9:
422+
needIngress.append(pkobj)
406423
missing = True
407424
except Exception as e:
408425
missing = False
409426
print(e)
410427

411428
if missing:
412429
print('generating transaction for', pubkey)
413-
args = ['tx', 'send', config['bootstrapAddress'], pubkey, amountToken, '--generate-only']
430+
# Estimate the gas, with a little bit of padding.
431+
args = ['tx', 'send', config['bootstrapAddress'], pubkey, amountToken, '--gas=auto', '--gas-adjustment=1.05']
414432
code, output = yield agCosmosHelper(reactor, opts, config, b'', args, 1)
415433
if code == 0:
416434
txes.append(output)
417435

418436
if len(txes) > 0:
419437
tx0 = txes[0]
420438
msgs = tx0['value']['msg']
439+
# Add up all the gases.
440+
gas = int(tx0['value']['fee']['gas'])
421441
for tx in txes[1:]:
422-
for msg in tx['value']['msg']:
442+
val = tx['value']
443+
gas += int(val['fee']['gas'])
444+
for msg in val['msg']:
423445
msgs.append(msg)
446+
tx0['value']['fee']['gas'] = str(gas)
424447
# Create a temporary file that is automatically deleted.
425448
with NamedTemporaryFile() as temp:
426449
# Save the amalgamated transaction.
@@ -430,7 +453,7 @@ def doEnablePubkeys(reactor, opts, config, pkobjs):
430453
# Now the temp.name contents are available
431454
args = [
432455
'tx', 'sign', temp.name, '--from', config['bootstrapAddress'],
433-
'--yes', '--gas=auto', '--append=false',
456+
'--yes', '--append=false',
434457
]
435458

436459
# Use the temp file in the sign request.
@@ -473,7 +496,7 @@ def doEnablePubkeys(reactor, opts, config, pkobjs):
473496
raise Exception('invalid response code ' + str(resp.code))
474497
rawResp = yield treq.json_content(resp)
475498
if not rawResp.get('ok', False):
476-
raise rawResp
499+
raise Exception('response not ok ' + str(rawResp))
477500

478501
def main():
479502
o = Options()

0 commit comments

Comments
 (0)