Skip to content

Commit

Permalink
fix(provisioning-server): robustness
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaelfig committed Nov 8, 2019
1 parent 6f9aa68 commit e90e6fe
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions provisioning-server/src/ag_pserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,22 @@ def agCosmosHelper(reactor, opts, config, input, args, retries = 1):
output = oj
except:
pass
elif stderr[0:14] == b'gas estimate: ':
lines = stderr.split(b'\n')
oj = json.loads(lines[1].decode('utf-8'))
if oj.get('type') is None:
# Reformat the message into what --generate-only produces.
output = {
'type': 'cosmos-sdk/StdTx',
'value': {
'msg': oj['msgs'],
'fee': oj['fee'],
'signatures': None,
'memo': '',
}}
else:
output = oj
code = 0

return code, output

Expand All @@ -403,24 +419,31 @@ def doEnablePubkeys(reactor, opts, config, pkobjs):
missing = False
break
elif code == 9:
needIngress.append(pkobj)
missing = True
except Exception as e:
missing = False
print(e)

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

if len(txes) > 0:
tx0 = txes[0]
msgs = tx0['value']['msg']
# Add up all the gases.
gas = int(tx0['value']['fee']['gas'])
for tx in txes[1:]:
for msg in tx['value']['msg']:
val = tx['value']
gas += int(val['fee']['gas'])
for msg in val['msg']:
msgs.append(msg)
tx0['value']['fee']['gas'] = str(gas)
# Create a temporary file that is automatically deleted.
with NamedTemporaryFile() as temp:
# Save the amalgamated transaction.
Expand All @@ -430,7 +453,7 @@ def doEnablePubkeys(reactor, opts, config, pkobjs):
# Now the temp.name contents are available
args = [
'tx', 'sign', temp.name, '--from', config['bootstrapAddress'],
'--yes', '--gas=auto', '--append=false',
'--yes', '--append=false',
]

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

def main():
o = Options()
Expand Down

0 comments on commit e90e6fe

Please sign in to comment.