Ajout des pages de liste des blocs de de détails d'un bloc
This commit is contained in:
parent
0a093b1bd2
commit
a791ead809
|
@ -1,6 +1,6 @@
|
|||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
font: 11px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
a {
|
||||
|
|
127
routes/index.js
127
routes/index.js
|
@ -6,20 +6,133 @@ var Web3 = require('web3');
|
|||
router.get('/', function(req, res, next) {
|
||||
//Connexion au noeud de la blockchain pour récupérer les infos
|
||||
var web3 = new Web3();
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
||||
|
||||
var blockNb = 1;
|
||||
|
||||
|
||||
// On détermine un objet qui contiendra les infos à utiliser dans la vue
|
||||
function Blockchain(web3Instance) {
|
||||
this.wallets = web3Instance.eth.getAccounts();
|
||||
this.nbBlocks = web3Instance.eth.getBlockNumber();
|
||||
this.montant = web3Instance.eth.getBalance("0x5421c79d465a288c28e10aa43f9b7dff1b313c8e");
|
||||
function Blockchain(account, blockNb) {
|
||||
this.wallets = web3.eth.getAccounts();
|
||||
this.nbBlocks = web3.eth.getBlockNumber();
|
||||
this.montant = web3.eth.getBalance(account);
|
||||
this.sync = web3.eth.isSyncing();
|
||||
}
|
||||
infos = new Blockchain(web3);
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
||||
var node1 = new Blockchain("0x5421c79d465a288c28e10aa43f9b7dff1b313c8e", blockNb);
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1402"));
|
||||
var node2 = new Blockchain("0xef816528949bda3b87e19b86848fb28767156232", blockNb);
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://5.51.59.70:1403"));
|
||||
var node3 = new Blockchain("0x869abc2DadD7E23c8B38F054276813A67D8131A7");
|
||||
|
||||
|
||||
//On fait un Promise all pour récupérer toutes les valeurs et les envoyer dans la vue... y a surement plus propre mais pour l'instant j'ai pas la bonne logique.
|
||||
Promise.all([infos.wallets, infos.nbBlocks, infos.montant]).then(values=>{ res.render('index', { title: 'Moniteur de Blockchain', infos: values });});
|
||||
Promise.all([node1.wallets, node1.nbBlocks, node1.montant, node1.sync, node2.wallets, node2.nbBlocks, node2.montant, node2.sync,node3.wallets, node3.nbBlocks, node3.montant, node3.sync]).then(values=>{
|
||||
res.render('index', { title: 'Moniteur de Blockchain', infos: values, block: blockNb });
|
||||
});
|
||||
//TODO : Trouver un moyen de récuéprer les valeurs des Promises pour les traiter et ensuite els envoyer à la vue -> permettra de lister transactions ?
|
||||
//TODO : Actualiser valeurs sur la page sans refresh
|
||||
})
|
||||
|
||||
.get('/blockslist/:page?', function(req, res, next) {
|
||||
//Connexion au noeud de la blockchain pour récupérer les infos
|
||||
var web3 = new Web3();
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
||||
|
||||
//On récupère la page donnée en URL
|
||||
if (parseInt(req.params.page) > 0) {
|
||||
var page = parseInt(req.params.page);
|
||||
}
|
||||
else {
|
||||
page = 0;
|
||||
}
|
||||
//Nombre d'entrées ?
|
||||
var max = page+100;
|
||||
|
||||
|
||||
// On détermine un objet qui contiendra les infos à utiliser dans la vue
|
||||
function Blockchain(account) {
|
||||
this.amounts = [];
|
||||
this.blockTransactions = [];
|
||||
|
||||
//Pour chauqe blocs
|
||||
for (var b = page; b < max ; b++) {
|
||||
//Total du montant du compte
|
||||
this.amounts.push(
|
||||
web3.eth.getBalance(account, b)
|
||||
.then(data=>{return data;})
|
||||
.catch(err=>{return err;})
|
||||
);
|
||||
|
||||
// total des transactions
|
||||
this.blockTransactions.push(
|
||||
web3.eth.getBlockTransactionCount(b)
|
||||
.then(data=>{return data;})
|
||||
.catch(err=>{return err;})
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
||||
var node1 = new Blockchain("0x5421c79d465a288c28e10aa43f9b7dff1b313c8e")
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1402"));
|
||||
var node2 = new Blockchain("0xef816528949bda3b87e19b86848fb28767156232");
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://5.51.59.70:1403"));
|
||||
var node3 = new Blockchain("0x869abc2DadD7E23c8B38F054276813A67D8131A7");
|
||||
|
||||
//On fait un Promise all pour récupérer toutes les valeurs et les envoyer dans la vue... y a surement plus propre mais pour l'instant j'ai pas la bonne logique.
|
||||
Promise.all(node3.amounts)
|
||||
.then(amounts=>{
|
||||
Promise.all(node3.blockTransactions)
|
||||
.then(blockTrans=>{
|
||||
res.render('blockslist', { title: 'Liste de blocs', page: page, max: max, amounts: amounts, trans: blockTrans})
|
||||
})
|
||||
})
|
||||
})
|
||||
/****
|
||||
** Page infos bloc seul *
|
||||
****/
|
||||
.get('/block/:numBloc', function(req, res, next) {
|
||||
//Connexion au noeud de la blockchain pour récupérer les infos
|
||||
var web3 = new Web3();
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
||||
|
||||
//On récupère le num de blocs donné en URL
|
||||
if (parseInt(req.params.numBloc) > 0) {
|
||||
var numBloc = parseInt(req.params.numBloc);
|
||||
}
|
||||
else {
|
||||
numBloc = 0;
|
||||
}
|
||||
|
||||
|
||||
// On détermine un objet qui contiendra les infos à utiliser dans la vue
|
||||
function Blockchain() {
|
||||
this.transactions = [];
|
||||
}
|
||||
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
||||
var node1 = new Blockchain()
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1402"));
|
||||
var node2 = new Blockchain();
|
||||
web3.setProvider(new web3.providers.HttpProvider("http://5.51.59.70:1403"));
|
||||
var node3 = new Blockchain();
|
||||
|
||||
//On fait un Promise all pour récupérer toutes les valeurs et les envoyer dans la vue... y a surement plus propre mais pour l'instant j'ai pas la bonne logique.
|
||||
web3.eth.getBlockTransactionCount(numBloc)
|
||||
.then(count=>{
|
||||
for (var i = 0; i < count; i++) {
|
||||
node3.transactions.push(web3.eth.getTransactionFromBlock(numBloc, i)
|
||||
.then(data=>{return data;})
|
||||
.catch(err=>{return err;})
|
||||
);
|
||||
}
|
||||
Promise.all(node3.transactions)
|
||||
.then(trans=>{
|
||||
res.render('block', { title: 'Détails du bloc', num: numBloc, transactions: trans})
|
||||
})
|
||||
.catch(err=>{return err;})
|
||||
})
|
||||
.catch(err=>{return err;})
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
|
19
views/block.pug
Normal file
19
views/block.pug
Normal file
|
@ -0,0 +1,19 @@
|
|||
extends layout
|
||||
|
||||
block content
|
||||
h1 #{title} #{num}
|
||||
h2 Transactions
|
||||
table(class="table")
|
||||
thead
|
||||
tr
|
||||
th(scope="col") N° transaction
|
||||
th(scope="col") Détails
|
||||
tbody
|
||||
each transaction in transactions
|
||||
tr
|
||||
th(scope="row")= transaction["transactionIndex"]
|
||||
td
|
||||
each attr, desc in transaction
|
||||
p= desc + " : " + attr
|
||||
|
||||
|
20
views/blockslist.pug
Normal file
20
views/blockslist.pug
Normal file
|
@ -0,0 +1,20 @@
|
|||
extends layout
|
||||
|
||||
block content
|
||||
h1= title
|
||||
h2 Liste des blocs #{page} à #{max}
|
||||
table(class="table")
|
||||
thead
|
||||
tr
|
||||
th(scope="col") N° bloc
|
||||
th(scope="col") Montant total du compte
|
||||
th(scope="col") Nombre de transactions du compte
|
||||
tbody
|
||||
while page < max
|
||||
tr
|
||||
th(scope="row")
|
||||
a(href="/block/"+page)= page
|
||||
td= amounts[page]
|
||||
td= trans[page]
|
||||
- page++
|
||||
|
|
@ -2,8 +2,28 @@ extends layout
|
|||
|
||||
block content
|
||||
h1= title
|
||||
h2 Node 1
|
||||
p Number of accounts : #{infos[0].length}
|
||||
each account in infos[0]
|
||||
pre= account
|
||||
p Number of blocks : #{infos[1]}
|
||||
p Montant du compte n°1 : #{infos[2]}
|
||||
p Montant du compte lié au noeud : #{infos[2]}
|
||||
p Info de synchronisation : #{infos[3]}
|
||||
|
||||
h2 Node 2
|
||||
p Number of accounts : #{infos[4].length}
|
||||
each account in infos[4]
|
||||
pre= account
|
||||
p Number of blocks : #{infos[5]}
|
||||
p Montant du compte lié au noeud : #{infos[6]}
|
||||
p Info de synchronisation : #{infos[7]}
|
||||
|
||||
h2 Node 3
|
||||
p Number of accounts : #{infos[8].length}
|
||||
each account in infos[8]
|
||||
pre= account
|
||||
p Number of blocks : #{infos[9]}
|
||||
p Montant du compte lié au noeud : #{infos10}
|
||||
p Info de synchronisation : #{infos[11]}
|
||||
|
||||
|
||||
|
|
|
@ -3,5 +3,6 @@ html
|
|||
head
|
||||
title= title
|
||||
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||
link(rel='stylesheet', href='https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css')
|
||||
body
|
||||
block content
|
||||
|
|
Loading…
Reference in a new issue