blockchainExplorer = function(){ // Init array var _known_blocks = null; var _liste_blocks = null; var _classes = ['bg-grey-even','bg-grey-odd']; var _cur_class = 0; var _cur_methode = 'hasard'; var _mode = 1; function _precisionRound(number) { var precision = 4; var factor = Math.pow(10, precision); return Math.round((number/100000000) * factor) / factor; } function _getblockNameFromHash(hash) { var retour = ''; if (_liste_blocks != null) _liste_blocks.forEach(function(item){ if (hash == item.hash) retour = item.name; }); return retour; } function _getblocHashFromName(name) { var retour = ''; _liste_blocks.forEach(function(item){ if (name == item.name) retour = item.hash; }); return retour; } function _addInfoForBlock(block) { var contenu = ''; var downloadingImage = new Image(); _cur_class = 1 - _cur_class; blockName = _getblockNameFromHash(block.hash); if (blockName != '') blockName = ' ( '+blockName+' )'; contenu += '
'; if (_mode != 0) { contenu += '
'; // contenu += '

block '+block.height+blockName+'

'; //contenu += ' '; contenu += ' '; //contenu += ' '; //contenu += ' '; contenu += ' '; contenu += ' '; contenu += ' '; contenu += ' '; contenu += ' '; contenu += ' '; contenu += ' '; contenu += ' '; contenu += '
hash'+block.hash+'
index'+block.block_index+'
height'+block.height+'
timestamp'+block.time+'
nonce'+block.nonce+'
nb tx'+block.n_tx+'
outputs'+_precisionRound(block.topisto_outputs).toFixed(4)+'
inputs'+_precisionRound(block.topisto_inputs).toFixed(4)+'
fees'+_precisionRound(block.topisto_fees).toFixed(4)+'
reward'+_precisionRound(block.topisto_reward).toFixed(4)+'
'; contenu += '
'; } contenu += '
'; $('#info_'+block.height).html(contenu); contenu = ''; contenu += '
'; contenu += '
'; contenu += '###BLOCK_DESC###'; contenu += '
'; contenu += '
'; return contenu; } function _addDivForBlock(block_height, onTop = false) { var lediv = document.getElementById('info_'+block_height); if (lediv === null) { var tmp; var contenu = _addDiv(block_height); var block_desc = ''; block_desc += '
'; block_desc += '

BLOCK '+block_height+' ...

'; block_desc += '
'; tmp = contenu.replace('###BLOCK_DESC###',block_desc) if (onTop) { tmp += $('#blockchain').html(); $('#blockchain').html(tmp); } else { $('#blockchain').append(tmp); } } return true; } function _addDivForVoid() { return _addDivForBlock('void'); } function _gotoBlock(block_name) { $(document).scrollTop( $("#explorer").offset().top ); // Bloquer la navigation pendant le calcul if (!flag_nav) { window.alert('A block image is currently computed, please wait ...'); return false; } flag_nav = false; if (block_name == 'NEXT') { flag_nav = true; // Supprimer un block if (cur_height.length < 3) { window.alert('No Next Block, you are at '+$('#blockSelector').val()+' block !'); return false; } _liste_blocks['PREVIOUS'] = _liste_blocks['BLOCK_'+cur_height[cur_height.length-1]]; cur_height.pop(); _toggleForwardBtn(); $('#block_'+cur_height[cur_height.length-2]).slideDown(400, function() { // Animation complete. $('#blockchain').children('div:last').remove(); _cur_class = 1 - _cur_class; }); } else { // Ajouter un block _addDivForBlock(cur_height[cur_height.length-1] - 1); // Décaler d'un block vers le haut if (cur_height.length > 1) $('#block_'+cur_height[cur_height.length-2]).slideUp(); block_hash = ''; if (block_name != 'LAST') block_hash = '?block_hash='+_liste_blocks[block_name]; $.getJSON('data/getBlockInfo.php'+block_hash, function( data ) { _liste_blocks['PREVIOUS'] = data.prev; _liste_blocks['BLOCK_'+data.height] = data.hash; _addInfoForBlock(data); _toggleForwardBtn(); }); } return true; } function _addTopBlock(data, flag = false) { if (flag) return "ajouterTopBlock"; // Maintenir la liste des blocks _liste_blocks['TOP'] = data.hash; _liste_blocks['BLOCK_'+data.height] = data.hash; // Ajouter un div _addDivForBlock(data.height,true); // Mettre les infos dans le div _addInfoForBlock(data); } function _addBottomBlock() { // Mettre les infos du block block_hash = '?block_hash='+_liste_blocks['BOTTOM']; $.getJSON('data/getBlockInfo.php'+block_hash, function( data ) { _liste_blocks['BOTTOM'] = data.prev; _liste_blocks['BLOCK_'+data.height] = data.hash; _liste_blocks['LENGTH'] += 1; // Ajouter un div _addDivForBlock(data.height, false); // Mettre les infos dans le div _addInfoForBlock(data); // Récursivité pour précharger par paquets if (_liste_blocks['LENGTH'] % 10) _addBottomBlock(); }); } function _initBlockchain(block_name) { var block_hash = ''; $(document).scrollTop( $("#explorer").offset().top ); $('#blockchain').html(''); if (block_name != 'LAST') block_hash = '?block_hash='+_getblocHashFromName(block_name); $.getJSON('data/getBlockInfo.php'+block_hash, function( data ) { _liste_blocks['BOTTOM'] = data.prev; _liste_blocks['TOP'] = data.hash; _liste_blocks['BLOCK_'+data.height] = data.hash; _liste_blocks['LENGTH'] += 1; _addDivForBlock(data.height); _addInfoForBlock(data); blockchainListener.addBlockHook(_addTopBlock); _addBottomBlock(); }); return true; } function _blockSelectorChange() { _initBlockchain($('#blockSelector').val()); } function _initBlockSelector() { // Init the selector var select = $('#blockSelector'); $.each(_liste_blocks, function (key, bloc) { select.append(new Option(bloc.name, bloc.name)); }); } function _init(mode = 1) { _mode = mode; if (_known_blocks == null) { $.getJSON('data/getKnownBlocksList.php', function( data ) { _liste_blocks = []; _liste_blocks['LENGTH'] = 0; _known_blocks = data; _known_blocks.sort(function(a,b){ // sort desc ... if (a.height < b.height) return 1; if (a.height > b.height) return -1; return 0; }); _initBlockchain('LAST'); return true; }); } else { _initBlockchain('LAST'); return true; } } return { addTopBlock: _addTopBlock, addBottomBlock: _addBottomBlock, getblocHashFromName: _getblocHashFromName, init: _init }; }();