Refactored code

This commit is contained in:
Игорь Соболев 2019-06-22 23:22:55 +03:00
parent 9da7f55b50
commit 1610284b41

View File

@ -6,10 +6,10 @@ window.onload = function () {
let contractAddress = "0xeFF91455de6D4CF57C141bD8bF819E5f873c1A01"; let contractAddress = "0xeFF91455de6D4CF57C141bD8bF819E5f873c1A01";
// set ethUrl to `undefined` to use MetaMask instead of Ethereum node // set ethUrl to `undefined` to use MetaMask instead of Ethereum node
let ethUrl = "http://geth.fluence.one:8545/"; let ethUrl = "http://rinkeby.fluence.one:8545/";
// application to interact with that stored in Fluence contract // application to interact with that stored in Fluence contract
let appId = "261"; let appId = "264";
// create a session between client and backend application, and then join the game // create a session between client and backend application, and then join the game
fluence.connect(contractAddress, appId, ethUrl).then((s) => { fluence.connect(contractAddress, appId, ethUrl).then((s) => {
@ -22,316 +22,369 @@ window.onload = function () {
var contractInstance; var contractInstance;
$(document).ready(function() { $(document).ready(function() {
// window.addEventListener('load', () => {
// if (typeof Web3 !== 'undefined') {
// let web3js = new Web3(web3.currentProvider);
// } else {
// console.log('No web3? You should consider trying MetaMask!');
// let web3js = new Web3(new Web3.providers.HttpProvider('http://localhost:8080'));
// }
// });
// console.log('provider');
// console.log(web3js);
var controllerAddress = '0x1cca1f0be338c747b11a16aba8d0905251628bdf'; var controllerAddress = '0x1cca1f0be338c747b11a16aba8d0905251628bdf';
let ControllerAbi = '[\n' + let ControllerAbi = [
'\t{\n' + {
'\t\t"constant": false,\n' + "constant": true,
'\t\t"inputs": [\n' + "inputs": [],
'\t\t\t{\n' + "name": "verifier",
'\t\t\t\t"name": "id",\n' + "outputs": [
'\t\t\t\t"type": "uint256"\n' + {
'\t\t\t}\n' + "name": "",
'\t\t],\n' + "type": "address"
'\t\t"name": "finzalize",\n' + }
'\t\t"outputs": [],\n' + ],
'\t\t"payable": false,\n' + "payable": false,
'\t\t"stateMutability": "nonpayable",\n' + "stateMutability": "view",
'\t\t"type": "function"\n' + "type": "function"
'\t},\n' + },
'\t{\n' + {
'\t\t"constant": true,\n' + "constant": true,
'\t\t"inputs": [],\n' + "inputs": [],
'\t\t"name": "last5Timestamps",\n' + "name": "stake",
'\t\t"outputs": [\n' + "outputs": [
'\t\t\t{\n' + {
'\t\t\t\t"name": "result",\n' + "name": "",
'\t\t\t\t"type": "uint256[5]"\n' + "type": "uint256"
'\t\t\t}\n' + }
'\t\t],\n' + ],
'\t\t"payable": false,\n' + "payable": false,
'\t\t"stateMutability": "view",\n' + "stateMutability": "view",
'\t\t"type": "function"\n' + "type": "function"
'\t},\n' + },
'\t{\n' + {
'\t\t"constant": true,\n' + "constant": true,
'\t\t"inputs": [],\n' + "inputs": [
'\t\t"name": "verifier",\n' + {
'\t\t"outputs": [\n' + "name": "",
'\t\t\t{\n' + "type": "uint256"
'\t\t\t\t"name": "",\n' + }
'\t\t\t\t"type": "address"\n' + ],
'\t\t\t}\n' + "name": "tasks",
'\t\t],\n' + "outputs": [
'\t\t"payable": false,\n' + {
'\t\t"stateMutability": "view",\n' + "components": [
'\t\t"type": "function"\n' + {
'\t},\n' + "name": "input",
'\t{\n' + "type": "uint256[5]"
'\t\t"constant": true,\n' + }
'\t\t"inputs": [],\n' + ],
'\t\t"name": "stake",\n' + "name": "data",
'\t\t"outputs": [\n' + "type": "tuple"
'\t\t\t{\n' + },
'\t\t\t\t"name": "",\n' + {
'\t\t\t\t"type": "uint256"\n' + "components": [
'\t\t\t}\n' + {
'\t\t],\n' + "name": "a",
'\t\t"payable": false,\n' + "type": "uint256[2]"
'\t\t"stateMutability": "view",\n' + },
'\t\t"type": "function"\n' + {
'\t},\n' + "name": "b",
'\t{\n' + "type": "uint256[2][2]"
'\t\t"constant": false,\n' + },
'\t\t"inputs": [\n' + {
'\t\t\t{\n' + "name": "c",
'\t\t\t\t"components": [\n' + "type": "uint256[2]"
'\t\t\t\t\t{\n' + }
'\t\t\t\t\t\t"name": "input",\n' + ],
'\t\t\t\t\t\t"type": "uint256[5]"\n' + "name": "proof",
'\t\t\t\t\t}\n' + "type": "tuple"
'\t\t\t\t],\n' + },
'\t\t\t\t"name": "data",\n' + {
'\t\t\t\t"type": "tuple"\n' + "name": "submitter",
'\t\t\t},\n' + "type": "address"
'\t\t\t{\n' + },
'\t\t\t\t"components": [\n' + {
'\t\t\t\t\t{\n' + "name": "timestamp",
'\t\t\t\t\t\t"name": "a",\n' + "type": "uint256"
'\t\t\t\t\t\t"type": "uint256[2]"\n' + },
'\t\t\t\t\t},\n' + {
'\t\t\t\t\t{\n' + "name": "status",
'\t\t\t\t\t\t"name": "b",\n' + "type": "uint8"
'\t\t\t\t\t\t"type": "uint256[2][2]"\n' + }
'\t\t\t\t\t},\n' + ],
'\t\t\t\t\t{\n' + "payable": false,
'\t\t\t\t\t\t"name": "c",\n' + "stateMutability": "view",
'\t\t\t\t\t\t"type": "uint256[2]"\n' + "type": "function"
'\t\t\t\t\t}\n' + },
'\t\t\t\t],\n' + {
'\t\t\t\t"name": "proof",\n' + "anonymous": false,
'\t\t\t\t"type": "tuple"\n' + "inputs": [
'\t\t\t}\n' + {
'\t\t],\n' + "indexed": true,
'\t\t"name": "submit",\n' + "name": "sender",
'\t\t"outputs": [],\n' + "type": "address"
'\t\t"payable": true,\n' + },
'\t\t"stateMutability": "payable",\n' + {
'\t\t"type": "function"\n' + "indexed": false,
'\t},\n' + "name": "index",
'\t{\n' + "type": "uint256"
'\t\t"constant": true,\n' + },
'\t\t"inputs": [\n' + {
'\t\t\t{\n' + "components": [
'\t\t\t\t"name": "",\n' + {
'\t\t\t\t"type": "uint256"\n' + "components": [
'\t\t\t}\n' + {
'\t\t],\n' + "name": "input",
'\t\t"name": "tasks",\n' + "type": "uint256[5]"
'\t\t"outputs": [\n' + }
'\t\t\t{\n' + ],
'\t\t\t\t"components": [\n' + "name": "data",
'\t\t\t\t\t{\n' + "type": "tuple"
'\t\t\t\t\t\t"name": "input",\n' + },
'\t\t\t\t\t\t"type": "uint256[5]"\n' + {
'\t\t\t\t\t}\n' + "components": [
'\t\t\t\t],\n' + {
'\t\t\t\t"name": "data",\n' + "name": "a",
'\t\t\t\t"type": "tuple"\n' + "type": "uint256[2]"
'\t\t\t},\n' + },
'\t\t\t{\n' + {
'\t\t\t\t"components": [\n' + "name": "b",
'\t\t\t\t\t{\n' + "type": "uint256[2][2]"
'\t\t\t\t\t\t"name": "a",\n' + },
'\t\t\t\t\t\t"type": "uint256[2]"\n' + {
'\t\t\t\t\t},\n' + "name": "c",
'\t\t\t\t\t{\n' + "type": "uint256[2]"
'\t\t\t\t\t\t"name": "b",\n' + }
'\t\t\t\t\t\t"type": "uint256[2][2]"\n' + ],
'\t\t\t\t\t},\n' + "name": "proof",
'\t\t\t\t\t{\n' + "type": "tuple"
'\t\t\t\t\t\t"name": "c",\n' + },
'\t\t\t\t\t\t"type": "uint256[2]"\n' + {
'\t\t\t\t\t}\n' + "name": "submitter",
'\t\t\t\t],\n' + "type": "address"
'\t\t\t\t"name": "proof",\n' + },
'\t\t\t\t"type": "tuple"\n' + {
'\t\t\t},\n' + "name": "timestamp",
'\t\t\t{\n' + "type": "uint256"
'\t\t\t\t"name": "submitter",\n' + },
'\t\t\t\t"type": "address"\n' + {
'\t\t\t},\n' + "name": "status",
'\t\t\t{\n' + "type": "uint8"
'\t\t\t\t"name": "timestamp",\n' + }
'\t\t\t\t"type": "uint256"\n' + ],
'\t\t\t},\n' + "indexed": false,
'\t\t\t{\n' + "name": "task",
'\t\t\t\t"name": "status",\n' + "type": "tuple"
'\t\t\t\t"type": "uint8"\n' + }
'\t\t\t}\n' + ],
'\t\t],\n' + "name": "Submitted",
'\t\t"payable": false,\n' + "type": "event"
'\t\t"stateMutability": "view",\n' + },
'\t\t"type": "function"\n' + {
'\t},\n' + "anonymous": false,
'\t{\n' + "inputs": [
'\t\t"constant": true,\n' + {
'\t\t"inputs": [\n' + "indexed": true,
'\t\t\t{\n' + "name": "challenger",
'\t\t\t\t"name": "id",\n' + "type": "address"
'\t\t\t\t"type": "uint256"\n' + },
'\t\t\t}\n' + {
'\t\t],\n' + "indexed": false,
'\t\t"name": "taskDataById",\n' + "name": "index",
'\t\t"outputs": [\n' + "type": "uint256"
'\t\t\t{\n' + }
'\t\t\t\t"name": "data",\n' + ],
'\t\t\t\t"type": "uint256[13]"\n' + "name": "Challenged",
'\t\t\t}\n' + "type": "event"
'\t\t],\n' + },
'\t\t"payable": false,\n' + {
'\t\t"stateMutability": "view",\n' + "constant": true,
'\t\t"type": "function"\n' + "inputs": [],
'\t},\n' + "name": "tasksNum",
'\t{\n' + "outputs": [
'\t\t"constant": false,\n' + {
'\t\t"inputs": [\n' + "name": "",
'\t\t\t{\n' + "type": "uint256"
'\t\t\t\t"name": "id",\n' + }
'\t\t\t\t"type": "uint256"\n' + ],
'\t\t\t}\n' + "payable": false,
'\t\t],\n' + "stateMutability": "view",
'\t\t"name": "challenge",\n' + "type": "function"
'\t\t"outputs": [],\n' + },
'\t\t"payable": false,\n' + {
'\t\t"stateMutability": "nonpayable",\n' + "constant": false,
'\t\t"type": "function"\n' + "inputs": [
'\t},\n' + {
'\t{\n' + "components": [
'\t\t"constant": true,\n' + {
'\t\t"inputs": [],\n' + "name": "input",
'\t\t"name": "tasksNum",\n' + "type": "uint256[5]"
'\t\t"outputs": [\n' + }
'\t\t\t{\n' + ],
'\t\t\t\t"name": "",\n' + "name": "data",
'\t\t\t\t"type": "uint256"\n' + "type": "tuple"
'\t\t\t}\n' + },
'\t\t],\n' + {
'\t\t"payable": false,\n' + "components": [
'\t\t"stateMutability": "view",\n' + {
'\t\t"type": "function"\n' + "name": "a",
'\t},\n' + "type": "uint256[2]"
'\t{\n' + },
'\t\t"anonymous": false,\n' + {
'\t\t"inputs": [\n' + "name": "b",
'\t\t\t{\n' + "type": "uint256[2][2]"
'\t\t\t\t"indexed": true,\n' + },
'\t\t\t\t"name": "sender",\n' + {
'\t\t\t\t"type": "address"\n' + "name": "c",
'\t\t\t},\n' + "type": "uint256[2]"
'\t\t\t{\n' + }
'\t\t\t\t"indexed": false,\n' + ],
'\t\t\t\t"name": "index",\n' + "name": "proof",
'\t\t\t\t"type": "uint256"\n' + "type": "tuple"
'\t\t\t},\n' + }
'\t\t\t{\n' + ],
'\t\t\t\t"components": [\n' + "name": "submit",
'\t\t\t\t\t{\n' + "outputs": [],
'\t\t\t\t\t\t"components": [\n' + "payable": true,
'\t\t\t\t\t\t\t{\n' + "stateMutability": "payable",
'\t\t\t\t\t\t\t\t"name": "input",\n' + "type": "function"
'\t\t\t\t\t\t\t\t"type": "uint256[5]"\n' + },
'\t\t\t\t\t\t\t}\n' + {
'\t\t\t\t\t\t],\n' + "constant": false,
'\t\t\t\t\t\t"name": "data",\n' + "inputs": [
'\t\t\t\t\t\t"type": "tuple"\n' + {
'\t\t\t\t\t},\n' + "name": "id",
'\t\t\t\t\t{\n' + "type": "uint256"
'\t\t\t\t\t\t"components": [\n' + }
'\t\t\t\t\t\t\t{\n' + ],
'\t\t\t\t\t\t\t\t"name": "a",\n' + "name": "challenge",
'\t\t\t\t\t\t\t\t"type": "uint256[2]"\n' + "outputs": [],
'\t\t\t\t\t\t\t},\n' + "payable": false,
'\t\t\t\t\t\t\t{\n' + "stateMutability": "nonpayable",
'\t\t\t\t\t\t\t\t"name": "b",\n' + "type": "function"
'\t\t\t\t\t\t\t\t"type": "uint256[2][2]"\n' + },
'\t\t\t\t\t\t\t},\n' + {
'\t\t\t\t\t\t\t{\n' + "constant": false,
'\t\t\t\t\t\t\t\t"name": "c",\n' + "inputs": [
'\t\t\t\t\t\t\t\t"type": "uint256[2]"\n' + {
'\t\t\t\t\t\t\t}\n' + "name": "id",
'\t\t\t\t\t\t],\n' + "type": "uint256"
'\t\t\t\t\t\t"name": "proof",\n' + }
'\t\t\t\t\t\t"type": "tuple"\n' + ],
'\t\t\t\t\t},\n' + "name": "finzalize",
'\t\t\t\t\t{\n' + "outputs": [],
'\t\t\t\t\t\t"name": "submitter",\n' + "payable": false,
'\t\t\t\t\t\t"type": "address"\n' + "stateMutability": "nonpayable",
'\t\t\t\t\t},\n' + "type": "function"
'\t\t\t\t\t{\n' + },
'\t\t\t\t\t\t"name": "timestamp",\n' + {
'\t\t\t\t\t\t"type": "uint256"\n' + "constant": true,
'\t\t\t\t\t},\n' + "inputs": [
'\t\t\t\t\t{\n' + {
'\t\t\t\t\t\t"name": "status",\n' + "name": "id",
'\t\t\t\t\t\t"type": "uint8"\n' + "type": "uint256"
'\t\t\t\t\t}\n' + }
'\t\t\t\t],\n' + ],
'\t\t\t\t"indexed": false,\n' + "name": "taskDataById",
'\t\t\t\t"name": "task",\n' + "outputs": [
'\t\t\t\t"type": "tuple"\n' + {
'\t\t\t}\n' + "name": "data",
'\t\t],\n' + "type": "uint256[13]"
'\t\t"name": "Submitted",\n' + }
'\t\t"type": "event"\n' + ],
'\t},\n' + "payable": false,
'\t{\n' + "stateMutability": "view",
'\t\t"anonymous": false,\n' + "type": "function"
'\t\t"inputs": [\n' + },
'\t\t\t{\n' + {
'\t\t\t\t"indexed": true,\n' + "constant": true,
'\t\t\t\t"name": "challenger",\n' + "inputs": [],
'\t\t\t\t"type": "address"\n' + "name": "last5Timestamps",
'\t\t\t},\n' + "outputs": [
'\t\t\t{\n' + {
'\t\t\t\t"indexed": false,\n' + "name": "result",
'\t\t\t\t"name": "index",\n' + "type": "uint256[5]"
'\t\t\t\t"type": "uint256"\n' + }
'\t\t\t}\n' + ],
'\t\t],\n' + "payable": false,
'\t\t"name": "Challenged",\n' + "stateMutability": "view",
'\t\t"type": "event"\n' + "type": "function"
'\t}\n' + },
']'; {
ControllerAbi = JSON.parse(ControllerAbi); "constant": true,
//console.log(ControllerAbi); "inputs": [
{
"name": "id",
"type": "uint256"
}
],
"name": "getDataById",
"outputs": [
{
"components": [
{
"components": [
{
"name": "input",
"type": "uint256[5]"
}
],
"name": "data",
"type": "tuple"
},
{
"components": [
{
"name": "a",
"type": "uint256[2]"
},
{
"name": "b",
"type": "uint256[2][2]"
},
{
"name": "c",
"type": "uint256[2]"
}
],
"name": "proof",
"type": "tuple"
},
{
"name": "submitter",
"type": "address"
},
{
"name": "timestamp",
"type": "uint256"
},
{
"name": "status",
"type": "uint8"
}
],
"name": "task",
"type": "tuple"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
let ControllerContract = web3.eth.contract(ControllerAbi); let ControllerContract = web3.eth.contract(ControllerAbi);
//console.log(ControllerContract);
contractInstance = ControllerContract.at(controllerAddress); contractInstance = ControllerContract.at(controllerAddress);
//console.log(contractInstance);
/*
// save fluence to global variable, so it can be accessed from Developer Console
window.fluence = fluence;
// convert result to a string
window.getResultAsString = function (result) {
return result.result().then((r) => r.asString())
};
window.logResultAsString = function (result) {
return getResultAsString(result).then((r) => console.log(r))
};
*/
contractInstance.tasksNum(function (err, result) { contractInstance.tasksNum(function (err, result) {
console.log(result); console.log(result);
let maxLen = Math.min(result, 5); let maxLen = Math.min(result, 5);
for (let i = 0; i < maxLen; i++) { for (let i = 0; i < maxLen; i++) {
let data = result - 1 - i; let data = result - 1 - i;
let fluenceResponse = session.request(`{"action": "Check", "proof_id": ${data}}`); // let fluenceResponse = session.request(`{"action": "Check", "proof_id": ${data}}`);
console.log(fluenceResponse); // console.log(fluenceResponse);
$('#state-id-' + i).text(result - 1 - i); $('#state-id-' + i).text(result - 1 - i);
if (fluenceResponse.hasOwnProperty('verified')) { if (fluenceResponse.hasOwnProperty('verified')) {
if (fluenceResponse.verified) { if (fluenceResponse.verified) {
@ -346,68 +399,29 @@ $(document).ready(function() {
} }
} }
}); });
/*window.onload = function () {
// address to Fluence contract in Ethereum blockchain. Interaction with blockchain created by MetaMask or with local Ethereum node
let contractAddress = "0xeFF91455de6D4CF57C141bD8bF819E5f873c1A01";
// set ethUrl to `undefined` to use MetaMask instead of Ethereum node
let ethUrl = "http://geth.fluence.one:8545/";
// application to interact with that stored in Fluence contract
let appId = "218";
// create a session between client and backend application, and then join the game
fluence.connect(contractAddress, appId, ethUrl).then((s) => {
console.log("Session created");
window.session = s;
}).then(() => join());
// send request to list last txses
for (let i = 0; i < 5; i++) {
let id = document.getElementById('state-id-' + i).textContent;
contractInstance.giveDataFromId(id, function (err, result) {
let fluenceResponse = session.request(`{ "player_id": ${id}, "action": "GetBalance"}`);
$('#state-status-fluence-' + i).text(fluenceResponse);
if (fluenceResponse == true) {
$('challenge-' + i).prop('disabled', true);
} else {
$('challenge-' + i).text('Challenge on Ethereum!')
}
});
}
};*/
}); });
$('button').click(function () { $('button').click(function () {
//console.log($(this)[0].id.slice(-1)); let id = $(this)[0].id.slice(-1);
let data = $('#state-id-' + id)[0].textContent;
if ($(this)[0].textContent === 'Challenge on Fluence!') { if ($(this)[0].textContent === 'Challenge on Fluence!') {
let id = $(this)[0].id.slice(-1);
let data = $('#state-id-' + id)[0].textContent;
console.log(data);
//console.log(contractInstance);
contractInstance.taskDataById(data, function (err, result) { contractInstance.taskDataById(data, function (err, result) {
//console.log(err);
console.log(result);
let public_par = result.slice(0, 5); let public_par = result.slice(0, 5);
let proof = result.slice(5, 13); let proof = result.slice(5, 13);
let fluenceResponse = session.request(`{"action": "Verify", "proof_id": ${data}, "public_par": ${public_par}, "proof": ${proof}}`); let fluenceResponse = session.request(`{"action": "Verify", "proof_id": ${data}, "public_par": ${public_par}, "proof": ${proof}}`);
console.log(fluenceResponse); console.log(fluenceResponse);
$('#state-status-fluence-' + i).text(result); // let success = fluenceResponse.result === 1;
if (fluenceResponse.result) { let success = false;
if (success) {
// все хорошо - мы проверили в флюенсе // все хорошо - мы проверили в флюенсе
$('#challenge-' + i).prop('disabled', true); $('#state-status-fluence-' + id).text('TRUE by Fluence.');
$('#challenge-' + id).prop('disabled', true);
} else { } else {
// мы проверили, пруф неправильный // мы проверили, пруф неправильный
$('#challenge-' + i).text('Challenge on Ethereum!') $('#state-status-fluence-' + id).text('FALSE by Fluence.');
$('#challenge-' + id).text('Challenge on Ethereum!')
} }
}); });
} else { } else {
@ -416,6 +430,7 @@ $('button').click(function () {
}); });
function challengeEthereum(jobId) { function challengeEthereum(jobId) {
console.log(jobId);
contractInstance.challenge.sendTransaction(jobId, function (err, txHash) { contractInstance.challenge.sendTransaction(jobId, function (err, txHash) {
$('#challenge-' + jobId).text('See tx on Etherscan!').attr("href", "https://ropsten.etherscan.io/tx/" + txHash); $('#challenge-' + jobId).text('See tx on Etherscan!').attr("href", "https://ropsten.etherscan.io/tx/" + txHash);
}); });