예제 #1
0
 def start_full(self, path, p2p_address, http_port, p2p_port):
     try:
         nodepath = join(path, self.folder_scheme + 'genesis')
         if not os.path.isdir(nodepath):
             os.makedirs(nodepath)
         os.chdir(nodepath)
         config = ConfigurationParser()
         config.read(join(self.parent_dir, 'config/template_config.ini'))
         config.set('blocks-dir', join(nodepath, 'blocks'))
         config.set('http-server-address', '0.0.0.0:' + http_port)
         config.set('p2p-listen-endpoint', '0.0.0.0:' + p2p_port)
         config.set('p2p-server-address', '%s:%s' % (p2p_address, p2p_port))
         config.set('enable-stale-production', True)
         config.set('producer-name', 'eosio')
         pair = self.wallet.create_import()
         self.edit_new_genesis(pair.public)
         config.set('signature-provider', self.create_sig_provider(pair))
         plugins = [
             'eosio::http_plugin', 'eosio::chain_plugin',
             'eosio::chain_api_plugin', 'eosio::history_plugin',
             'eosio::history_api_plugin', 'eosio::net_plugin',
             'eosio::net_api_plugin', 'eosio::producer_plugin'
         ]
         config.append('plugin', plugins)
         config.write(join(nodepath, 'config.ini'))
         copyfile(join(self.parent_dir, 'config/genesis.json'),
                  join(nodepath, "genesis.json"))
         node = Node('genesis', nodepath)
         node.start(3.0)
         self.update_node_state(node)
         self.save()
     except FileNotFoundError as e:
         print(e)
예제 #2
0
 def create_producer_node(self, account, path, p2p_address):
     try:
         nodepath = join(path, self.folder_scheme + account.name)
         if not os.path.isdir(nodepath):
             os.makedirs(nodepath)
         os.chdir(nodepath)
         config = ConfigurationParser()
         config.read(join(self.parent_dir, 'config/template_config.ini'))
         config.set('blocks-dir', join(nodepath, 'blocks'))
         config.set('http-server-address',
                    '0.0.0.0:' + str(self.get_open_port()))
         p2p_port = str(self.get_open_port())
         config.set('p2p-listen-endpoint', '0.0.0.0:' + p2p_port)
         config.set('p2p-server-address',
                    '%s:%s' % (str(p2p_address), str(p2p_port)))
         config.set('producer-name', account.name)
         config.set('signature-provider',
                    self.create_sig_provider(account.keypair))
         plugins = ['eosio::producer_plugin']
         config.append('plugin', plugins)
         config.write(join(nodepath, 'config.ini'))
         copyfile(join(self.parent_dir, 'config/genesis.json'),
                  join(nodepath, "genesis.json"))
         node = Node(account.name, nodepath)
         return node
     except FileNotFoundError as e:
         print(e)
예제 #3
0
class Node:
    def __init__(self, name, path):
        self.config = ConfigurationParser()
        self.path = os.path.abspath(path)
        self.name = name
        self.config.read(join(path, 'config.ini'))
        self.pid = self.get_pid()

    def is_running(self):
        try:
            pid = self.get_pid()
            if pid != -1:
                return psutil.pid_exists(pid)
            return False
        except OSError as e:
            print(e)
            return False

    def get_pid(self):
        path = join(self.path, 'node.pid')
        if os.path.isfile(path):
            pid = int(file_get_contents(path))
            self.pid = pid
            return pid
        return -1

    def start(self, delay_time=1.0):
        if not os.path.isdir(self.path):
            os.makedirs(self.path)
        cmd = NodeFactory.nodeos_dir + ' --config-dir %s --genesis-json %s --delete-all-blocks'
        genesis_dir = join(self.path, 'genesis.json')
        print('Starting node: %s' % self.name)
        start_background_proc(cmd % (self.path, genesis_dir),
                              log_file(join(self.path, 'stderr.txt')),
                              join(self.path, 'node.pid'))
        sleep(delay_time)

    def restart(self, delay_time=1.0):
        if self.is_running():
            self.stop()
        if os.path.isdir(self.path):
            cmd = NodeFactory.nodeos_dir + ' --config-dir %s --hard-replay-blockchain'
            start_background_proc(cmd % self.path,
                                  log_file(join(self.path, 'stderr.txt')),
                                  join(self.path, 'node.pid'))
            sleep(delay_time)
        else:
            print('nodeos folder path: %s does not exist' % self.path)

    def stop(self):
        try:
            pid = self.get_pid()
            if pid != -1 and self.is_running():
                p = psutil.Process(pid)
                p.terminate()
        except OSError as e:
            print(e)

    def show_output(self):
        tail(join(self.path, 'stderr.txt'))

    def get_ports(self):
        return [self.get_p2p_port(), self.get_http_port()]

    def get_p2p_port(self):
        p2p = self.config.get('p2p-listen-endpoint')
        return int(p2p[p2p.index(':') + 1:len(p2p) + 1])

    def get_http_port(self):
        http = self.config.get('http-server-address')
        return int(http[http.index(':') + 1:len(http) + 1])

    def get_endpoints(self):
        return self.config.get('p2p-server-address')

    def set_peers(self, peers):
        tmp = peers.copy()
        self.remove_self(tmp)

        endpoints = []
        for name in tmp:
            endpoints.append(tmp[name])

        self.config.append('p2p-peer-address', endpoints)
        self.config.write(join(self.path, 'config.ini'))

    def remove_self(self, peers):
        if self.name in peers:
            del peers[self.name]

    def get_info(self):
        return {
            'name': self.name,
            'pid': self.get_pid(),
            'path': self.path,
            'ports': self.get_ports()
        }

    def __str__(self):
        info = self.get_info()
        return json.dumps(info, indent=4, sort_keys=True)