351 lines
9.7 KiB
JavaScript
Raw Normal View History

2019-06-15 11:54:30 +03:00
import "bootstrap/dist/css/bootstrap.min.css";
2019-06-15 17:53:21 +03:00
let state = {};
2019-06-16 16:06:11 +03:00
let url = "http://salmon.fluence.one:8080/";
let wsUrl = "ws://salmon.fluence.one:8080/";
2019-06-16 14:01:00 +03:00
2019-06-15 11:54:30 +03:00
window.onload = function () {
2019-06-16 16:06:11 +03:00
getApps();
2019-06-15 13:13:56 +03:00
let list = document.getElementById("zoneList");
let coolZone = {
name: "Cool Zone",
nodesNumber: 12,
2019-06-15 17:11:40 +03:00
lastHeight: 49771,
2019-06-15 13:13:56 +03:00
binaryLink: "#"
};
2019-06-15 17:11:40 +03:00
let coolStatus = {
status: "in-progress",
height: 3545
};
2019-06-16 16:13:37 +03:00
// appendNewZone("cool-zone", coolZone, coolStatus);
2019-06-15 13:13:56 +03:00
let iris = {
name: "Iris",
nodesNumber: 57,
lastHeight: 12567,
binaryLink: "#"
};
2019-06-15 17:11:40 +03:00
let irisStatus = {
status: "success"
};
2019-06-16 16:13:37 +03:00
// appendNewZone("iris", iris, irisStatus);
2019-06-15 13:13:56 +03:00
2019-06-16 14:01:00 +03:00
let badZone = {
name: "Bad Zone",
2019-06-15 13:13:56 +03:00
nodesNumber: 4,
lastHeight: 2566,
binaryLink: "#"
};
2019-06-15 17:11:40 +03:00
let nameStatus = {
status: "error"
};
2019-06-16 14:01:00 +03:00
let bzId = "bad-zone";
state[bzId] = {};
state[bzId].errorInfo = JSON.parse(`{
"height": "10",
"txs": null,
"signatures": [
{
"validator_address": "7869301611744D2F6A5578EB001DB18BCC0AC705",
"signature": "ruuvN8mPiKXniLc0JkblK4ol1oUHx3RuLDykik/DN8aiWXnWzPOPiLZrOLE0XbranEJpdErGalZnzShblUixAw=="
}
]
}`);
2019-06-16 16:06:11 +03:00
2019-06-16 16:13:37 +03:00
// appendNewZone(bzId, badZone, nameStatus);
2019-06-15 17:11:40 +03:00
let buttonEl = document.getElementById("start-check");
2019-06-15 23:10:31 +03:00
buttonEl.addEventListener("click", function() {
startCheckEvent()
});
2019-06-16 16:06:11 +03:00
document.getElementById("name-service-hlp").onclick = function() {
document.getElementById("node-ip").value = "207.154.210.117";
document.getElementById("node-port").value = "26657";
document.getElementById("binary-url").value = "QmQ69JoPDaKFpPSbWvvUGRGG5E83u6nTP2hRegmCoa6aW5";
document.getElementById("node-name").value = "nameservice";
};
document.getElementById("commercio-hlp").onclick = function() {
document.getElementById("node-ip").value = "188.166.71.251 ";
document.getElementById("node-port").value = "26657";
document.getElementById("binary-url").value = "QmepV645qHM9XR97KiK7Bvd2jjK3MiKTGbq8cYm7izRyQK";
document.getElementById("node-name").value = "commercionetwork";
};
2019-06-15 11:54:30 +03:00
};
2019-06-15 17:11:40 +03:00
function startCheckEvent() {
2019-06-16 14:01:00 +03:00
let nodeIp = document.getElementById("node-ip").value;
let port = document.getElementById("node-port").value;
let binaryUrl = document.getElementById("binary-url").value;
let name = document.getElementById("node-name").value;
let params = {
nodeIp: nodeIp,
port: port,
binaryUrl: binaryUrl,
name: name
};
2019-06-15 17:53:21 +03:00
2019-06-16 16:06:11 +03:00
let app = state.apps.find((r) => {
return r.name === name
});
2019-06-15 17:53:21 +03:00
2019-06-16 16:06:11 +03:00
let someInfo;
if (app) {
someInfo = {
name: name,
nodesNumber: 13,
lastHeight: app.consensusHeight,
binaryLink: app.binaryHash
};
} else {
// mock
someInfo = {
name: name,
nodesNumber: 34,
lastHeight: 48,
binaryLink: "#"
};
}
2019-06-16 11:11:50 +03:00
2019-06-16 14:01:00 +03:00
startCheck(params, someInfo.name, someInfo);
2019-06-15 17:11:40 +03:00
}
2019-06-15 13:13:56 +03:00
2019-06-15 23:10:31 +03:00
function appendNewZone(id, info, status) {
2019-06-16 14:01:00 +03:00
if (state[id]) {
state[id].info = info;
state[id].status = status;
} else {
state[id] = {
info: info,
status: status
};
}
2019-06-15 17:11:40 +03:00
let list = document.getElementById("zoneList");
2019-06-15 23:10:31 +03:00
if (!status) {
status = {
status: "in-progress",
height: 0
};
}
2019-06-15 17:11:40 +03:00
let zoneInfo = zoneElement(id, info, status);
2019-06-15 23:10:31 +03:00
list.innerHTML = zoneInfo + list.innerHTML;
if (status.status === "error") {
2019-06-16 14:01:00 +03:00
setError(id, status.height)
2019-06-15 23:10:31 +03:00
}
2019-06-15 17:11:40 +03:00
}
function setHeightAndWidth(el, currentHeight, lastHeight) {
let percent = currentHeight / lastHeight * 100;
el.style.width = `${percent}%`;
el.innerHTML = `Height: ${currentHeight}`;
}
2019-06-15 13:13:56 +03:00
2019-06-15 17:11:40 +03:00
function changeHeight(el, currentHeight) {
el.innerHTML = `Height: ${currentHeight}`;
}
2019-06-15 23:10:31 +03:00
function getInfo(id) {
let el = document.getElementById(id);
2019-06-16 16:06:11 +03:00
return el.getElementsByClassName("error-info")[0];
2019-06-15 23:10:31 +03:00
}
function showError(id, btn) {
let infoDiv = getInfo(id);
2019-06-16 14:01:00 +03:00
let jsonInfo;
console.log("ID === " + state[id].errorInfo);
if (state[id].errorInfo) {
jsonInfo = state[id].errorInfo;
} else {
let block = state[id].block;
let signatures = block.last_commit.precommits.map((p) => {
return {
validator_address: p.validator_address,
signature: p.signature
}
});
jsonInfo = {
height: block.header.height,
txs: block.data.txs,
signatures: signatures
};
}
infoDiv.innerHTML = `<pre>${JSON.stringify(jsonInfo, null, 2)}</pre>`;
2019-06-15 19:25:55 +03:00
btn.innerHTML = "Hide Error";
}
2019-06-15 23:10:31 +03:00
function hideError(id, btn) {
let infoDiv = getInfo(id);
2019-06-15 19:25:55 +03:00
infoDiv.innerHTML = "";
btn.innerHTML = "Show Error";
}
2019-06-15 23:10:31 +03:00
function errorAction(btn, id) {
showError(id, btn);
}
function unerrorAction(btn, id) {
hideError(id, btn);
2019-06-15 19:25:55 +03:00
}
2019-06-15 23:10:31 +03:00
function genErrorButton(id) {
let newBtn = document.createElement("button");
newBtn.type = "button";
newBtn.id = id + "-btn";
2019-06-16 16:06:11 +03:00
newBtn.style.width = "20%";
2019-06-15 23:10:31 +03:00
newBtn.classList.add("btn", "btn-block", "btn-danger");
newBtn.innerHTML = "Get Error Info";
return newBtn;
2019-06-15 19:25:55 +03:00
}
2019-06-16 14:01:00 +03:00
function setError(id, erroredHeight) {
2019-06-15 23:10:31 +03:00
let el = getProgressBar(id);
2019-06-16 14:01:00 +03:00
console.log("ERROR!!!!!!!!!!! " + erroredHeight);
getBlock(id, erroredHeight).then((resp) => {
return resp.text()
}).then((r) => {
console.log(JSON.parse(r));
state[id].block = JSON.parse(r).result.block;
});
2019-06-15 17:53:21 +03:00
el.classList.add("progress-error");
2019-06-16 16:06:11 +03:00
let claimBtn = document.getElementById(id).getElementsByClassName("claim-btn")[0];
claimBtn.style.display = "block";
2019-06-15 23:10:31 +03:00
let btn = genErrorButton(id);
state[id].swap = false;
state[id].errorButton = btn;
state[id].errorFunc = function(e) {
console.log("error button clicked = " + id);
if (state[id].swap) {
unerrorAction(btn, id);
state[id].swap = false;
2019-06-15 19:25:55 +03:00
} else {
2019-06-15 23:10:31 +03:00
errorAction(btn, id);
state[id].swap = true;
2019-06-15 19:25:55 +03:00
}
};
2019-06-15 23:10:31 +03:00
el.parentElement.appendChild(btn);
// to avoid `onclick` erasure for some reasons
Object.keys(state).forEach(function(key) {
if (state[key].errorButton) {
document.getElementById(key).onclick = state[key].errorFunc
}
});
2019-06-15 17:11:40 +03:00
}
function zoneElement(id, info, status) {
2019-06-15 13:13:56 +03:00
let barStatus;
2019-06-16 16:06:11 +03:00
2019-06-15 17:11:40 +03:00
let percent = "100";
let currentHeight = info.lastHeight;
if (status.status === "success") {
2019-06-15 13:13:56 +03:00
barStatus = "progress-bar"
2019-06-15 17:11:40 +03:00
} else if (status.status === "error") {
2019-06-15 13:13:56 +03:00
barStatus = "progress-bar progress-error"
2019-06-15 17:11:40 +03:00
} else if (status.status === "in-progress") {
barStatus = "progress-bar";
currentHeight = status.height;
percent = currentHeight / info.lastHeight * 100;
2019-06-15 13:13:56 +03:00
}
2019-06-15 23:10:31 +03:00
2019-06-16 16:13:37 +03:00
let valNumber = 4;
if (id === "nameservice") {
valNumber = 1;
}
2019-06-16 14:01:00 +03:00
return `<div class="row border border-dark p-2" id="${id}">
2019-06-16 16:06:11 +03:00
<div class="col-9 list-info">
2019-06-16 16:13:37 +03:00
<p>Name: ${info.name}</p><p>Validator Number: ${valNumber}</p><p>Consensus Height: ${info.lastHeight}</p><p>Binary in IPFS: ${info.binaryLink}</p>
2019-06-16 16:06:11 +03:00
</div>
<div class="text-right col-3"><button type="button" class="btn btn-info btn-block claim-btn pull-right" style="display: none;">Send Fraud Proof</button></div>
2019-06-15 13:13:56 +03:00
<div class="col-12">
2019-06-16 16:06:11 +03:00
2019-06-15 13:13:56 +03:00
<div class="progress">
2019-06-15 23:10:31 +03:00
<div class="${barStatus}" role="progressbar" style="width: ${percent}%;" aria-valuenow="${percent}" aria-valuemin="0" aria-valuemax="100">Height: ${currentHeight}</div>
2019-06-15 13:13:56 +03:00
</div>
2019-06-16 16:06:11 +03:00
<div class="bg-white error-info"></div>
2019-06-15 13:13:56 +03:00
</div>
2019-06-15 23:10:31 +03:00
</div>`;
}
function getProgressBar(id) {
return document.getElementById(id).getElementsByClassName("progress-bar")[0];
2019-06-15 13:13:56 +03:00
}
2019-06-16 14:01:00 +03:00
function startCheck(params, zoneId, info) {
2019-06-15 11:54:30 +03:00
2019-06-15 23:10:31 +03:00
if (state[zoneId]) {
return;
}
2019-06-16 14:01:00 +03:00
createCheck(params).then((sa) => {
2019-06-16 16:06:11 +03:00
let socket = new WebSocket(wsUrl + "websocket/" + params.name);
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
socket.onopen = function(e) {
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
console.log(e);
state[zoneId] = {
info: info
};
appendNewZone(zoneId, info);
2019-06-15 23:10:31 +03:00
};
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
let k = 0;
socket.onmessage = function(event) {
let st = state[zoneId];
let data = JSON.parse(event.data);
2019-06-16 14:01:00 +03:00
if (data.height) {
state[zoneId].currentHeight = data.height
}
2019-06-16 11:11:50 +03:00
let el = getProgressBar(zoneId);
k++;
if (data.correct) {
if (st.info.lastHeight < 1000 || k % 100 === 0) {
setHeightAndWidth(el, data.height, st.info.lastHeight);
} else {
changeHeight(el, data.height)
}
} else {
2019-06-16 16:06:11 +03:00
setHeightAndWidth(el, st.info.lastHeight, st.info.lastHeight);
2019-06-16 17:13:03 +03:00
changeHeight(el, data.height - 1);
2019-06-16 14:01:00 +03:00
let erroredHeight = state[zoneId].currentHeight;
2019-06-16 16:06:11 +03:00
setError(zoneId, erroredHeight - 1);
2019-06-16 11:11:50 +03:00
socket.close()
}
};
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
socket.onclose = function(event) {
console.log("Websocket was closed. Cause: " + JSON.stringify(event))
};
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
socket.onerror = function(error) {
console.log("Error: " + JSON.stringify(error))
};
});
2019-06-15 17:11:40 +03:00
}
2019-06-16 14:01:00 +03:00
2019-06-16 16:06:11 +03:00
async function getApps() {
fetch(`${url}apps`).then((r) => {
return r.text()
}).then((r) => {
state.apps = JSON.parse(r)
})
2019-06-16 14:01:00 +03:00
}
async function getBlock(id, height) {
2019-06-16 16:06:11 +03:00
return fetch(`${url}block/${id}/${height}`)
2019-06-16 14:01:00 +03:00
}
async function createCheck(params) {
2019-06-16 16:06:11 +03:00
let createUrl = `${url}create/${params.name}/${params.nodeIp}/${params.port}/${params.binaryUrl}`;
2019-06-16 14:01:00 +03:00
fetch(createUrl);
return Promise.resolve()
}