Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ethereum.routing.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ethereum.status:
path: '/admin/reports/ethereum'
path: '/admin/reports/ethereum/{server_id}'
defaults:
_controller: 'Drupal\ethereum\Controller\EthereumController::status'
_title: 'Ethereum'
server_id: NULL
requirements:
_permission: 'administer site configuration'

Expand Down
53 changes: 42 additions & 11 deletions src/Controller/EthereumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Ethereum\DataType\EthBlockParam;
use Ethereum\DataType\EthB;
use Drupal\Core\Render\Markup;
use Symfony\Component\HttpFoundation\Request;

/**
* Controller routines for Ethereum routes.
Expand All @@ -22,7 +23,7 @@ class EthereumController extends ControllerBase {
*/
protected $web3;

// @todo Doesn't seem to be propagetad to the library anymore.
// @todo Doesn't seem to be propagated to the library anymore.
private $debug = TRUE;

/**
Expand Down Expand Up @@ -63,18 +64,50 @@ public function debug($clear = FALSE) {
/**
* Displays the ethereum status report page.
*
* This page provides a overview about Ethereum functions and usage.
* This page provides an overview of Ethereum functions and usage.
*
* @throws \Exception
* @param string|null $server_id
* The id of the server to report on.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return array
* Render array. Table with current status of the ethereum node.
* Render array. Table with current status of the ethereum node. Or just
* the server select form after initial submission.
*
* @throws \Exception
* If the server can not be loaded.
*/
public function status() {
// Default server.
$server = \Drupal::service('ethereum.manager')->getCurrentServer();
public function status($server_id = NULL, Request $request) {
// Used to tell if the server select form was submitted.
$form_id = $request->request->get('form_id');
// If the server select form was submitted, we don't need to build the
// report because it will be built once the form redirect is complete.
// But we do need to build the form again otherwise it doesn't process.
if ($form_id && $form_id == 'ethereum_status_server_select_form') {
$server_id = $request->attributes->get('server_id');
return ['choose_server' => \Drupal::formBuilder()->getForm('Drupal\ethereum\Form\EthereumStatusServerSelectForm', $server_id)];
}
if ($server_id) {
try {
$server = \Drupal::service('ethereum.manager')->getServer($server_id);
$host = $server->get('url');
$this->web3 = new Ethereum($host);
}
catch (\Exception $e) {
\Drupal::logger('ethereum')->error($e->getMessage());
return [
'#markup' => $e->getMessage(),
];
}
}
else {
// Load the server.
$server = \Drupal::service('ethereum.manager')->getCurrentServer();
}

// Validate active server.
// Validate the server.
$liveStatus = $server->validateConnection();
$this->messenger()->addMessage(
$liveStatus['message'], ($liveStatus['error']) ? 'error' : 'status'
Expand Down Expand Up @@ -133,11 +166,9 @@ public function status() {
]
];


$random_rows[] = [$this->t('<b>JsonRPC standard Methods</b>'), $this->t('Read more about <a href="https://github.com/ethereum/wiki/wiki/JSON-RPC">Ethereum JsonRPC-API</a> implementation.')];
$random_rows[] = [$this->t('<b>Ethereum-PHP</b>'), $this->t('Ethereum <a href="http://ethereum-php.org/">Web3 PHP API reference</a> and <a href="https://github.com/digitaldonkey/ethereum-php">codebase</a>.')];


// Blocks.
$random_rows[] = [$this->t("<b>Block info</b>"), ''];
$block_latest = $this->web3->eth_getBlockByNumber(new EthBlockParam('latest'), new EthB(FALSE));
Expand Down Expand Up @@ -176,7 +207,6 @@ public function status() {
$high_block,
];


// More.

// Ethereum sha3 != standardized sha3, but a "Keccak-256"
Expand Down Expand Up @@ -206,6 +236,7 @@ public function status() {
// $this->debug();

return [
'choose_server' => \Drupal::formBuilder()->getForm('Drupal\ethereum\Form\EthereumStatusServerSelectForm', $server_id),
'server_info' => $serverInfo,
'server_live_status' => $serverLiveInfo,
'random_stuff' => $serverRandomRows,
Expand Down
16 changes: 16 additions & 0 deletions src/EthereumManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ public function getCurrentServer() {
return $server;
}

/**
* {@inheritdoc}
*/
public function getServer($server_id) {
/** @var \Drupal\ethereum\EthereumServerInterface $server */
$server = $this->serverStorage->load($server_id);
if (!$server) {
throw new \RuntimeException('Server (' . $server_id . ') does not exist.');
}
if (!$server->status()) {
throw new \RuntimeException('Server is not enabled.');
}

return $server;
}

/**
* {@inheritdoc}
*/
Expand Down
11 changes: 11 additions & 0 deletions src/EthereumManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ interface EthereumManagerInterface {
*/
public function getCurrentServer();

/**
* Returns the requested backend server.
*
* @param string $server_id
* The id of the server.
*
* @return \Drupal\ethereum\EthereumServerInterface
* An ethereum_server object.
*/
public function getServer($server_id);

/**
* Returns the frontend server.
*
Expand Down
89 changes: 89 additions & 0 deletions src/Form/EthereumStatusServerSelectForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Drupal\ethereum\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ethereum\EthereumManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Defines a form to select one of the active Ethereum network servers.
*/
class EthereumStatusServerSelectForm extends FormBase {

/**
* The Ethereum manager service.
*
* @var \Drupal\ethereum\EthereumManagerInterface
*/
protected $ethereumManager;

/**
* Constructs a new EthereumStatusServerSelectForm.
*
* @param \Drupal\ethereum\EthereumManagerInterface $ethereum_manager
* The Ethereum Manager service.
*/
public function __construct(EthereumManagerInterface $ethereum_manager) {
$this->ethereumManager = $ethereum_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('ethereum.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'ethereum_status_server_select_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $server_id = NULL) {
$config = $this->config('ethereum.settings');
$enabled_servers = $this->ethereumManager->getServersAsOptions(TRUE);

$form['#title'] = $this->t('Check the status of any active server');

$form['server'] = [
'#type' => 'select',
'#title' => $this->t('Reporting on:'),
'#required' => TRUE,
'#description' => $this->t('Choose a server to report on. Only enabled servers are listed.'),
'#options' => $enabled_servers,
'#default_value' => $server_id ?: $config->get('current_server'),
];

$form['actions'] = [
'#type' => 'actions',
];

$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];

return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state->getValue('server')) {
$form_state->setRedirect(
'ethereum.status',
['server_id' => $form_state->getValue('server')]
);
}
}

}