Skip to content

Commit

Permalink
Validator improvements (#382)
Browse files Browse the repository at this point in the history
* feat(deployment): better support for external validators

Fixes #381

* feat(pserver): export genesis.json

* fix(pserver): minor nits
  • Loading branch information
michaelfig authored Jan 6, 2020
1 parent 7b9b6e0 commit ae44690
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class StartOptions(usage.Options):
class Options(usage.Options):
subCommands = [
['set-cosmos-config', None, SetConfigOptions, "Pipe output of 'ag-setup-cosmos show-config' to this command"],
['set-cosmos-genesis', None, SetConfigOptions, "Pipe output of 'ag-setup-cosmos show-genesis' to this commmand"],
['add-pubkeys', None, AddPubkeysOptions, 'Add public keys from saved database'],
['start', None, StartOptions, 'Start the HTTP server'],
]
Expand Down Expand Up @@ -75,6 +76,9 @@ def processEnded(self, reason):
def cosmosConfigFile(home):
return os.path.join(home, 'cosmos-chain.json')

def cosmosGenesisFile(home):
return os.path.join(home, 'cosmos-genesis.json')

def pubkeyDatabase(home):
return os.path.join(home, 'pubkeys.jsona')

Expand Down Expand Up @@ -309,6 +313,15 @@ def built(code):
d.addErrback(log.err)
return server.NOT_DONE_YET

class GenesisJSON(resource.Resource):
def __init__(self, o):
self.opts = o

def render_GET(self, req):
f = open(cosmosGenesisFile(self.opts['home']))
config = f.read()
req.setHeader('Content-Type', 'application/json')
return config.encode('utf-8')

class ConfigJSON(resource.Resource):
def __init__(self, o):
Expand Down Expand Up @@ -340,6 +353,7 @@ def run_server(reactor, o):

# Display the JSON config.
root.putChild(b"network-config", ConfigJSON(o))
root.putChild(b"genesis.json", GenesisJSON(o))

site = server.Site(root)
s = endpoints.serverFromString(reactor, o["listen"])
Expand Down Expand Up @@ -501,24 +515,33 @@ def doEnablePubkeys(reactor, opts, config, pkobjs):
def main():
o = Options()
o.parseOptions()
if o.subCommand == 'set-cosmos-config':
if o.subCommand.startswith('set-cosmos-'):
try:
os.mkdir(o['home'])
except FileExistsError:
pass
fname = cosmosConfigFile(o['home'])
if o.subCommand == 'set-cosmos-config':
fname = cosmosConfigFile(o['home'])
elif o.subCommand == 'set-cosmos-genesis':
fname = cosmosGenesisFile(o['home'])
print('Reading %s from stdin; hit Ctrl-D to finish' % fname)
cfgJson = sys.stdin.read()
# Check that the JSON input is properly-formatted.
json.loads(cfgJson)
# Write out the JSON.
with open(fname, 'w') as f:
f.write(cfgJson)
elif o.subCommand == 'add-pubkeys':
# Now that we have our files, add all the accounts.
f = open(cosmosConfigFile(o['home']), 'r')
config = json.loads(f.read())
with open(cosmosConfigFile(o['home']), 'r') as f:
config = json.loads(f.read())
try:
f = open(pubkeyDatabase(o['home']))
pkobjs_str = f.read().strip(', \r\n');
pkobjs = json.loads('[' + pkobjs_str + ']')
# This file is comma-terminated lines of JSON objects.
with open(pubkeyDatabase(o['home'])) as f:
# Strip the trailing newlines and comma.
pkobjs_str = f.read().rstrip().rstrip(',')
# Interpret as an array.
pkobjs = json.loads('[' + pkobjs_str + ']')
except FileNotFoundError:
return
pkobjs.reverse()
Expand Down
12 changes: 12 additions & 0 deletions packages/deployment/ansible/cosmos-validators.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
---

- hosts: "{{ STAKER_NODE }}"
user: root
gather_facts: no
vars:
- service: ag-chain-cosmos
- CHAIN_NAME: "{{ lookup('file', SETUP_HOME + '/' + service + '/chain-name.txt') }}"
- STAKER: ag-staker
- STAKER_NODE: node0
- STAKER_AMOUNT: 2000000stake
roles:
- cosmos-delegates

- hosts: "{{ service }}"
user: root
#any_errors_fatal: true
Expand Down
22 changes: 22 additions & 0 deletions packages/deployment/ansible/roles/cosmos-delegates/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- name: "Check if /home/{{ service }}/cosmos-stakers.txt exists"
stat:
path: "/home/{{ service }}/cosmos-stakers.txt"
register: stakers

- name: "Get pubkeys and amounts from /home/{{ service }}/cosmos-stakers.txt"
command: "\
sed -e 's/^\\([^:]*\\):\\([^:]*\\).*$/\\1 \\2/' \
/home/{{ service }}/cosmos-stakers.txt"
args:
warn: false
register: stakers_data
when: stakers.stat.exists

- name: "Transfer stake to delegates"
become_user: "{{ service }}"
become: true
shell: "\
echo 'mmmmmmmm' | ag-cosmos-helper tx send {{ STAKER }}-{{ STAKER_NODE }} \
{{ item }} --yes --chain-id={{ CHAIN_NAME }}"
with_items:
- "{{ stakers_data.stdout_lines }}"
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
state: directory
mode: 0755

- name: "Install {{ data | default(service + '/data') }}/../../ag-chain-cosmos/data/genesis.json"
become_user: "{{ service }}"
become: true
copy:
src: "{{ data | default(service + '/data') }}/../../ag-chain-cosmos/data/genesis.json"
dest: "/home/{{ service }}/.{{ service }}/cosmos-genesis.json"
owner: "{{ service }}"
group: "{{ service }}"
mode: 0644

- name: "Install {{ data | default(service + '/data') }}/cosmos-chain.json"
become_user: "{{ service }}"
become: true
Expand Down
4 changes: 3 additions & 1 deletion packages/deployment/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,17 +601,19 @@ or "${chalk.yellow.bold(
setSilent(true);
await chdir(SETUP_HOME);
await inited();
const [chainName, gci, rpcAddrs, bootstrapAddress] = await Promise.all(
const [chainName, gci, peers, rpcAddrs, bootstrapAddress] = await Promise.all(
[
'show-chain-name',
'show-gci',
'show-peers',
'show-rpcaddrs',
'show-bootstrap-address',
].map(cmd => needBacktick([progname, cmd].map(shellEscape).join(' '))),
);
const obj = {
chainName,
gci,
peers: peers.split(','),
rpcAddrs: rpcAddrs.split(','),
bootstrapAddress,
};
Expand Down

0 comments on commit ae44690

Please sign in to comment.