/
skale-miner.js
41 lines (35 loc) · 1.48 KB
/
skale-miner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const BN = require("bn.js");
const crypto = require("crypto")
const DIFFICULTY = new BN(1);
async function mineGasForTransaction(web3, tx) {
if(tx.from === undefined || tx.nonce === undefined) {
throw new Error("Not enough fields for mining gas (from, nonce)")
}
if (!tx.gas) {
tx.gas = await web3.eth.estimateGas(tx)
}
let address = tx.from
let nonce = web3.utils.isHex(tx.nonce) ? web3.utils.hexToNumber(tx.nonce) : tx.nonce;
let gas = web3.utils.isHex(tx.gas) ? web3.utils.hexToNumber(tx.gas) : tx.gas;
tx.gasPrice = mineFreeGas(gas, address, nonce, web3);
}
function mineFreeGas(gasAmount, address, nonce, web3) {
console.log('Mining free gas: ', gasAmount);
let nonceHash = new BN(web3.utils.soliditySha3(nonce).slice(2), 16)
let addressHash = new BN(web3.utils.soliditySha3(address).slice(2), 16)
let nonceAddressXOR = nonceHash.xor(addressHash)
let maxNumber = new BN(2).pow(new BN(256)).sub(new BN(1));
let divConstant = maxNumber.div(DIFFICULTY);
let candidate;
while (true){
candidate = new BN(crypto.randomBytes(32).toString('hex'), 16);
let candidateHash = new BN(web3.utils.soliditySha3(candidate).slice(2), 16);
let resultHash = nonceAddressXOR.xor(candidateHash);
let externalGas = divConstant.div(resultHash).toNumber();
if (externalGas >= gasAmount) {
break;
}
}
return candidate.toString();
}
exports.mineGasForTransaction = mineGasForTransaction