|
| 1 | +/** |
| 2 | + * Viz Magic — History Source |
| 3 | + * Read-only source layer for old public chain history. |
| 4 | + * VIZ RPC remains the live source; optional archive mirrors can serve old |
| 5 | + * blocks when a public node no longer keeps enough history. |
| 6 | + */ |
| 7 | +var HistorySource = (function() { |
| 8 | + 'use strict'; |
| 9 | + |
| 10 | + function _makeError(message) { |
| 11 | + try { return new Error(message); } catch (e) { return { message: message }; } |
| 12 | + } |
| 13 | + |
| 14 | + function _archiveMirrors() { |
| 15 | + if (typeof VizMagicConfig === 'undefined' || !VizMagicConfig.HISTORY_ARCHIVE_MIRRORS) { |
| 16 | + return []; |
| 17 | + } |
| 18 | + return VizMagicConfig.HISTORY_ARCHIVE_MIRRORS; |
| 19 | + } |
| 20 | + |
| 21 | + function _normalizeMirror(mirror) { |
| 22 | + if (typeof mirror === 'string') { |
| 23 | + return { url: mirror }; |
| 24 | + } |
| 25 | + return mirror || {}; |
| 26 | + } |
| 27 | + |
| 28 | + function _mirrorUrl(mirror, blockNum) { |
| 29 | + var pattern = mirror.url || mirror.blockUrl || ''; |
| 30 | + if (!pattern) return ''; |
| 31 | + if (pattern.indexOf('{block}') !== -1) { |
| 32 | + return pattern.replace('{block}', encodeURIComponent(String(blockNum))); |
| 33 | + } |
| 34 | + return pattern.replace(/\/$/, '') + '/' + encodeURIComponent(String(blockNum)) + '.json'; |
| 35 | + } |
| 36 | + |
| 37 | + function _extractBlockFromMirrorPayload(payload) { |
| 38 | + if (!payload) return null; |
| 39 | + if (payload.previous && payload.timestamp && payload.transactions) return payload; |
| 40 | + if (payload.block && payload.block.previous && payload.block.transactions) return payload.block; |
| 41 | + if (payload.result && payload.result.previous && payload.result.transactions) return payload.result; |
| 42 | + if (payload.data && payload.data.block && payload.data.block.transactions) return payload.data.block; |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + function _requestJson(url, timeoutMs, callback) { |
| 47 | + if (typeof XMLHttpRequest === 'undefined') { |
| 48 | + callback(_makeError('HTTP archive fetch is unavailable')); |
| 49 | + return; |
| 50 | + } |
| 51 | + var xhr = new XMLHttpRequest(); |
| 52 | + var completed = false; |
| 53 | + var timer = setTimeout(function() { |
| 54 | + if (completed) return; |
| 55 | + completed = true; |
| 56 | + try { xhr.abort(); } catch (e) {} |
| 57 | + callback(_makeError('Archive mirror timeout')); |
| 58 | + }, timeoutMs || 6000); |
| 59 | + |
| 60 | + xhr.onreadystatechange = function() { |
| 61 | + if (xhr.readyState !== 4 || completed) return; |
| 62 | + completed = true; |
| 63 | + clearTimeout(timer); |
| 64 | + if (xhr.status < 200 || xhr.status >= 300) { |
| 65 | + callback(_makeError('Archive mirror HTTP ' + xhr.status)); |
| 66 | + return; |
| 67 | + } |
| 68 | + try { |
| 69 | + callback(null, JSON.parse(xhr.responseText)); |
| 70 | + } catch (parseErr) { |
| 71 | + callback(parseErr); |
| 72 | + } |
| 73 | + }; |
| 74 | + try { |
| 75 | + xhr.open('GET', url, true); |
| 76 | + xhr.send(null); |
| 77 | + } catch (err) { |
| 78 | + if (completed) return; |
| 79 | + completed = true; |
| 80 | + clearTimeout(timer); |
| 81 | + callback(err); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + function _getBlockFromMirrors(blockNum, index, callback) { |
| 86 | + var mirrors = _archiveMirrors(); |
| 87 | + if (!mirrors.length || index >= mirrors.length) { |
| 88 | + callback(_makeError('Block unavailable from VIZ RPC and archive mirrors')); |
| 89 | + return; |
| 90 | + } |
| 91 | + var mirror = _normalizeMirror(mirrors[index]); |
| 92 | + var url = _mirrorUrl(mirror, blockNum); |
| 93 | + if (!url) { |
| 94 | + _getBlockFromMirrors(blockNum, index + 1, callback); |
| 95 | + return; |
| 96 | + } |
| 97 | + _requestJson(url, mirror.timeoutMs || 6000, function(err, payload) { |
| 98 | + var block = err ? null : _extractBlockFromMirrorPayload(payload); |
| 99 | + if (block) { |
| 100 | + callback(null, block); |
| 101 | + return; |
| 102 | + } |
| 103 | + _getBlockFromMirrors(blockNum, index + 1, callback); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + function getBlock(blockNum, callback) { |
| 108 | + callback = callback || function() {}; |
| 109 | + if (!blockNum || blockNum <= 0) { |
| 110 | + callback(_makeError('Invalid block number')); |
| 111 | + return; |
| 112 | + } |
| 113 | + if (typeof viz === 'undefined' || !viz.api || !viz.api.getBlock) { |
| 114 | + _getBlockFromMirrors(blockNum, 0, callback); |
| 115 | + return; |
| 116 | + } |
| 117 | + viz.api.getBlock(blockNum, function(err, block) { |
| 118 | + if (!err && block) { |
| 119 | + callback(null, block); |
| 120 | + return; |
| 121 | + } |
| 122 | + _getBlockFromMirrors(blockNum, 0, callback); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + function getAccountProtocol(account, protocol, callback) { |
| 127 | + callback = callback || function() {}; |
| 128 | + if (!account || !protocol) { |
| 129 | + callback(_makeError('Account and protocol are required')); |
| 130 | + return; |
| 131 | + } |
| 132 | + if (typeof VizAccount === 'undefined' || !VizAccount.getAccountProtocol) { |
| 133 | + callback(_makeError('Account protocol lookup is unavailable')); |
| 134 | + return; |
| 135 | + } |
| 136 | + VizAccount.getAccountProtocol(account, protocol, callback); |
| 137 | + } |
| 138 | + |
| 139 | + function getAccountActions(account, protocol, options, callback) { |
| 140 | + if (typeof options === 'function') { |
| 141 | + callback = options; |
| 142 | + options = {}; |
| 143 | + } |
| 144 | + options = options || {}; |
| 145 | + callback = callback || function() {}; |
| 146 | + if (protocol !== VizMagicConfig.PROTOCOLS.VM) { |
| 147 | + callback(null, []); |
| 148 | + return; |
| 149 | + } |
| 150 | + if (typeof VMProtocol === 'undefined' || !VMProtocol.traverseChain) { |
| 151 | + callback(_makeError('VM protocol traversal is unavailable')); |
| 152 | + return; |
| 153 | + } |
| 154 | + VMProtocol.traverseChain(account, options.limit || 5000, callback, HistorySource); |
| 155 | + } |
| 156 | + |
| 157 | + function getCapabilities(callback) { |
| 158 | + callback = callback || function() {}; |
| 159 | + if (typeof VizConnection !== 'undefined' && VizConnection.checkHistoryCapability) { |
| 160 | + VizConnection.checkHistoryCapability(callback); |
| 161 | + return; |
| 162 | + } |
| 163 | + callback(null, { |
| 164 | + live: false, |
| 165 | + recentBlocks: false, |
| 166 | + historicalBlocks: false, |
| 167 | + checkedAt: Date.now(), |
| 168 | + node: '' |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + return { |
| 173 | + getBlock: getBlock, |
| 174 | + getAccountProtocol: getAccountProtocol, |
| 175 | + getAccountActions: getAccountActions, |
| 176 | + getCapabilities: getCapabilities |
| 177 | + }; |
| 178 | +})(); |
0 commit comments