From a791ead809242594edead8e6530926f47308272c Mon Sep 17 00:00:00 2001 From: Matt Marcha Date: Tue, 12 Jun 2018 23:10:16 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20pages=20de=20liste=20des=20blocs?= =?UTF-8?q?=20de=20de=20d=C3=A9tails=20d'un=20bloc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/stylesheets/style.css | 2 +- routes/index.js | 127 +++++++++++++++++++++++++++++++++-- views/block.pug | 19 ++++++ views/blockslist.pug | 20 ++++++ views/index.pug | 22 +++++- views/layout.pug | 1 + 6 files changed, 182 insertions(+), 9 deletions(-) create mode 100644 views/block.pug create mode 100644 views/blockslist.pug diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 9453385..832349b 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -1,6 +1,6 @@ body { padding: 50px; - font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; + font: 11px "Lucida Grande", Helvetica, Arial, sans-serif; } a { diff --git a/routes/index.js b/routes/index.js index 2b348a2..c7b582a 100644 --- a/routes/index.js +++ b/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; diff --git a/views/block.pug b/views/block.pug new file mode 100644 index 0000000..addaad5 --- /dev/null +++ b/views/block.pug @@ -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 + + diff --git a/views/blockslist.pug b/views/blockslist.pug new file mode 100644 index 0000000..f420ba5 --- /dev/null +++ b/views/blockslist.pug @@ -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++ + diff --git a/views/index.pug b/views/index.pug index cac1442..beb60d3 100644 --- a/views/index.pug +++ b/views/index.pug @@ -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]} + + diff --git a/views/layout.pug b/views/layout.pug index 15af079..958ddbc 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -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