Skip to content

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kesar committed Jul 15, 2018
1 parent 0408d3b commit 6a12125
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 78 deletions.
3 changes: 1 addition & 2 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ services:

api.vote_service:
class: AppBundle\Services\VoteService
factory: ['@doctrine.orm.default_entity_manager', 'getRepository']
arguments:
- AppBundle\Entity\Vote
- "@doctrine.orm.entity_manager"
lazy: true
public: true

Expand Down
137 changes: 137 additions & 0 deletions src/AppBundle/Command/MissedBlocksCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MissedBlocksCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('eostracker:missed-blocks')
->setDescription('Check Missed blocks')
->addOption('scheduling', '', InputOption::VALUE_OPTIONAL);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$scheduling = $input->getOption('scheduling') ?? 1;
$em = $this->getContainer()->get("doctrine.orm.entity_manager");

$output->writeln('scheduling: '.$scheduling);


$sql = "SELECT block_number, producer FROM blocks WHERE version = $scheduling ORDER BY block_number ASC LIMIT 1";
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$block = $stmt->fetchAll();
$firstBlock = $block[0]['block_number'];

$sql = "SELECT block_number FROM blocks WHERE version = $scheduling ORDER BY block_number DESC LIMIT 1";
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$lastBlock = $stmt->fetchColumn();

$sql = "SELECT new_producers FROM blocks WHERE block_number < $firstBlock AND version = ($scheduling-1) AND new_producers IS NOT NULL ORDER BY block_number DESC LIMIT 1";
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$listProducers = json_decode($stmt->fetchColumn(), true);
$orderedProducers = [];

foreach ($listProducers as $producer) {
$orderedProducers[] = $producer['producer_name'];
}

$output->writeln('Blocks: '.($lastBlock - $firstBlock));
$output->writeln('First Block: '.$firstBlock);
$output->writeln('Last Block: '.$lastBlock);
$output->writeln('List Producers: '.json_encode($orderedProducers));

$blocksPerQuery = 10000;
$blocksPerRound = 12;
$currentBlockInRoundForBP = 1;
$currentBP = $block[0]['producer'];
$currentBlock = $firstBlock - 2;

$missedBlocks = [];

while ($currentBlock < $lastBlock) {
$sql = "SELECT block_number, producer, version FROM blocks WHERE block_number BETWEEN $currentBlock AND ($currentBlock+$blocksPerQuery) ORDER BY block_number ASC";
$stmt = $em->getConnection()->prepare($sql);
//$output->writeln($sql);
$stmt->execute();
$blocks = $stmt->fetchAll();

foreach ($blocks as $block) {
if ($block['version'] != $scheduling) {
continue;
}

if ($this->isCurrentRound($currentBlockInRoundForBP, $blocksPerRound)) {
if ($block['producer'] !== $currentBP) {
$blocksMissed = $blocksPerRound - $currentBlockInRoundForBP;
$output->writeln('Missed Blocks ('.$blocksMissed .') at: '.$block['block_number'] . ' for: '. $currentBP);
$missedBlocks[$currentBP] = isset($missedBlocks[$currentBP]) ? $missedBlocks[$currentBP] + $blocksMissed : $blocksMissed;

list($currentBP, $currentBlockInRoundForBP, $missedBlocks) = $this->startNewRound(
$output, $orderedProducers, $currentBP, $block, $missedBlocks, $blocksPerRound
);
} else {
$currentBlockInRoundForBP++;
}
} else {
list($currentBP, $currentBlockInRoundForBP, $missedBlocks) = $this->startNewRound(
$output, $orderedProducers, $currentBP, $block, $missedBlocks, $blocksPerRound
);
}
}

$currentBlock = $currentBlock + $blocksPerQuery + 1;
}

$output->writeln('Missed Blocks: '.json_encode($missedBlocks));
}


private function nextBP(array $orderedProducers, string $currentProducer): string
{
foreach ($orderedProducers as $key => $producer) {
if ($currentProducer == $producer) {
if (isset($orderedProducers[$key + 1])) {
return $orderedProducers[$key + 1];
} else {
return $orderedProducers[0];
}
}
}
}

private function isCurrentRound(int $currentBlockInRoundForBP, int $blocksPerRound): bool
{
return $currentBlockInRoundForBP < $blocksPerRound;
}

private function startNewRound($output, $orderedProducers, $currentBP, $block, $missedBlocks, $blocksPerRound): array
{

// $output->writeln('new Round: '. $block['producer']. ' ' .$block['block_number']);

$nextBP = $this->nextBP($orderedProducers, $currentBP);
if ($nextBP !== $block['producer']) {
$output->writeln('Missed Round (12) at: '.$block['block_number'] . ' for: '. $nextBP);
$missedBlocks[$nextBP] = isset($missedBlocks[$nextBP]) ? $missedBlocks[$nextBP] + $blocksPerRound : $blocksPerRound;
}
$currentBP = $block['producer'];
$currentBlockInRoundForBP = 1;

return [
$currentBP,
$currentBlockInRoundForBP,
$missedBlocks,
];
}
}
2 changes: 1 addition & 1 deletion src/AppBundle/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function statsAction()
$this->get('api.block_service')->count([]),
$this->get('api.transaction_service')->count([]),
$this->get('api.account_service')->count([]),
$this->get('api.action_service')->count([]),
$this->get('api.action_service')->count(['parentId' => 0]),
];
$result->set($data)->expiresAfter(new \DateInterval('PT15S'));
$this->get('cache.app')->save($result);
Expand Down
13 changes: 7 additions & 6 deletions src/AppBundle/Entity/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Action
private $account;
private $transaction;
private $name;
private $createdAt;
private $data;
private $authorizations;
private $seq;
private $parentId;

public function __construct(int $id, Account $account, Transaction $transaction, string $name, array $data,\DateTime $createdAt, int $seq = 0)
public function __construct(int $id, Account $account, Transaction $transaction, string $name, array $data, int $seq = 0, int $parentId = 0)
{
$this->id = $id;
$this->account = $account;
Expand All @@ -22,17 +22,17 @@ public function __construct(int $id, Account $account, Transaction $transaction,
$this->data = $data;
$this->authorizations = [];
$this->seq = $seq;
$this->createdAt = $createdAt;
$this->parentId = $parentId;
}

public function id(): int
{
return $this->id;
}

public function createdAt(): \DateTime
public function parentId(): int
{
return $this->createdAt;
return $this->parentId;
}

public function seq(): int
Expand Down Expand Up @@ -73,7 +73,8 @@ public function toArray(): array
'account' => $this->account()->name(),
'transaction' => $this->transaction()->id(),
'blockId' => $this->transaction()->blockId(),
'createdAt' => $this->createdAt()->getTimestamp(),
'parentId' => $this->parentId(),
'createdAt' => $this->transaction()->createdAt()->getTimestamp(),
'name' => $this->name(),
'data' => $this->data(),
'authorizations' => $this->authorizationsToArray()
Expand Down
6 changes: 3 additions & 3 deletions src/AppBundle/Entity/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
\DateTime $timestamp,
string $transactionMerkleRoot,
string $actionMerkleRoot,
Account $producer,
string $producer,
int $numTransactions,
int $confirmed
) {
Expand Down Expand Up @@ -88,7 +88,7 @@ public function actionMerkleRoot(): string
return $this->actionMerkleRoot;
}

public function producer(): Account
public function producer(): string
{
return $this->producer;
}
Expand All @@ -113,7 +113,7 @@ public function toArray(): array
'timestamp' => $this->timestamp()->getTimestamp(),
'transactionMerkleRoot' => $this->transactionMerkleRoot(),
'actionMerkleRoot' => $this->actionMerkleRoot(),
'producer' => $this->producer()->name(),
'producer' => $this->producer(),
'version' => $this->version(),
'newProducers' => $this->newProducers(),
'numTransactions' => $this->numTransactions(),
Expand Down
33 changes: 0 additions & 33 deletions src/AppBundle/Entity/Vote.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/AppBundle/Resources/config/doctrine/Action.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ AppBundle\Entity\Action:
nullable: true
name:
type: string
createdAt:
type: datetime
seq:
type: integer
parentId:
type: integer
manyToOne:
account:
targetEntity: AppBundle\Entity\Account
Expand Down
8 changes: 2 additions & 6 deletions src/AppBundle/Resources/config/doctrine/Block.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ AppBundle\Entity\Block:
type: string
actionMerkleRoot:
type: string
producer:
type: string
numTransactions:
type: integer
version:
Expand All @@ -27,9 +29,3 @@ AppBundle\Entity\Block:
nullable: true
confirmed:
type: integer
manyToOne:
producer:
targetEntity: AppBundle\Entity\Account
joinColumn:
name: producer
referencedColumnName: name
13 changes: 0 additions & 13 deletions src/AppBundle/Resources/config/doctrine/Vote.orm.yml

This file was deleted.

7 changes: 4 additions & 3 deletions src/AppBundle/Services/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function get(int $page = 1, int $limit = 30)
{
return $this->getEntityManager()->createQuery(<<<DQL
SELECT a
FROM AppBundle\Entity\ACCOUNT a
FROM AppBundle\Entity\Account a
ORDER BY a.createdAt DESC
DQL
)
Expand All @@ -36,9 +36,10 @@ public function producers(\DateTime $since)

public function withPublicKey(string $publicKey): ?array
{
/*
$sql = " SELECT account FROM accounts_keys WHERE public_key = '".$publicKey."' LIMIT 1";
$stmt = $this->getEntityManager()->getConnection()->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
$stmt->execute(); */ // TODO: fix
return [];
}
}
3 changes: 2 additions & 1 deletion src/AppBundle/Services/ActionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function get(int $page = 1, int $limit = 30)
FROM AppBundle\Entity\Action a
JOIN a.transaction att
JOIN a.account acc
WHERE a.parentId = 0
ORDER BY a.id DESC
DQL
)
Expand Down Expand Up @@ -44,7 +45,7 @@ public function getFromAccount(Account $account, int $page = 1, int $limit = 30)
public function countLast(int $hours): int
{
$datetime = new \DateTime('-'.$hours.' hours');
$sql = "SELECT count(a.id) as cant FROM actions a JOIN transactions t ON a.transaction_id = t.id WHERE t.created_at > '".date_format($datetime, 'Y-m-d H:i:s')."' LIMIT 1";
$sql = "SELECT count(a.id) as cant FROM actions a JOIN transactions t ON a.transaction_id = t.id WHERE a.parent_id = 0 AND t.created_at > '".date_format($datetime, 'Y-m-d H:i:s')."' LIMIT 1";
$stmt = $this->getEntityManager()->getConnection()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
Expand Down
13 changes: 11 additions & 2 deletions src/AppBundle/Services/TwitterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ public function __construct(EntityManager $entityManager)

public function all(int $page = 0): ?array
{
$sql = "SELECT a.id, UNIX_TIMESTAMP(a.created_at) AS created_at, aa.actor, JSON_UNQUOTE(data->\"$.msg\") AS msg FROM actions a JOIN actions_accounts aa ON a.id = aa.action_id WHERE account = 'decentwitter' AND name='tweet' ORDER BY a.id DESC LIMIT 50 OFFSET ".$page * 50;
$sql = "SELECT a.id, a.transaction_id, a.seq, UNIX_TIMESTAMP(a.created_at) AS created_at, aa.actor, JSON_UNQUOTE(data->\"$.msg\") AS msg FROM actions a JOIN actions_accounts aa ON a.id = aa.action_id WHERE account = 'decentwitter' AND name='tweet' ORDER BY a.id DESC LIMIT 50 OFFSET ".$page * 50;
$stmt = $this->entityManager->getConnection()->prepare($sql);
$stmt->execute();

return $stmt->fetchAll();
}

public function replies(array $tweetIds): ?array
{
$sql = "SELECT a.id, a.transaction_id, a.seq, UNIX_TIMESTAMP(a.created_at) AS created_at, aa.actor, JSON_UNQUOTE(data->\"$.msg\") AS msg FROM actions a JOIN actions_accounts aa ON a.id = aa.action_id WHERE account = 'decentwitter' AND name='tweet' ORDER BY a.id DESC LIMIT 50 OFFSET ".$page * 50;
$stmt = $this->entityManager->getConnection()->prepare($sql);
$stmt->execute();

Expand All @@ -25,7 +34,7 @@ public function all(int $page = 0): ?array

public function forUser(string $account, int $page = 0): ?array
{
$sql = "SELECT a.id, UNIX_TIMESTAMP(a.created_at) AS created_at, aa.actor, JSON_UNQUOTE(data->\"$.msg\") AS msg FROM actions a JOIN actions_accounts aa ON a.id = aa.action_id WHERE account = 'decentwitter' AND aa.actor='".$account."' AND name='tweet' ORDER BY a.id DESC LIMIT 50 OFFSET ".$page * 50;
$sql = "SELECT a.id, a.transaction_id, a.seq, UNIX_TIMESTAMP(a.created_at) AS created_at, aa.actor, JSON_UNQUOTE(data->\"$.msg\") AS msg FROM actions a JOIN actions_accounts aa ON a.id = aa.action_id WHERE account = 'decentwitter' AND aa.actor='".$account."' AND name='tweet' ORDER BY a.id DESC LIMIT 50 OFFSET ".$page * 50;
$stmt = $this->entityManager->getConnection()->prepare($sql);
$stmt->execute();

Expand Down

0 comments on commit 6a12125

Please sign in to comment.