Skip to content
Snippets Groups Projects
Commit 774aa9e0 authored by Asad Salman's avatar Asad Salman Committed by Adrien Béraud
Browse files

nameservice: added batch name adding script

also modified read_names.js to write a list of objs instead
of writing on object with all names as key/value pairs

extended write_names.js to parallelize batch registry,
cleanup and more error checking
Change-Id: I2d6b509cc65b453f4efc88f997b2c5faf99d8b02
parent 7b212b04
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,11 @@
"optimizer": {
"enabled": true,
"runs": 500
}
},
"outputSelection": {
"*": {
"*": [ "abi", "evm.bytecode" ]
}
}
}
}
\ No newline at end of file
......@@ -138,7 +138,7 @@ function initContract() {
} else {
console.log("Contract mined! Address: " + contract.address);
regAddress = contract.address;
fs.writeFile(REG_ADDR_FILE, regAddress);
fs.writeFileSync(REG_ADDR_FILE, regAddress);
reg = contract;
startServer();
}
......
......@@ -7,10 +7,11 @@
"author": "Adrien Béraud <adrien.beraud@savoirfairelinux.com>",
"license": "GPL-3.0",
"dependencies": {
"web3" : ">=0.15" ,
"express" : ">=4.1.0",
"body-parser" : ">=1.8.1",
"async": "^2.6.1",
"bignumber.js": ">=2.0.0",
"minimist" : ">=1.0.0"
"body-parser": ">=1.8.1",
"express": ">=4.1.0",
"minimist": ">=1.0.0",
"web3": ">=0.15"
}
}
\ No newline at end of file
}
......@@ -11,8 +11,7 @@ web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var REG_ADDR_FILE = "contractAddress.txt";
var REG_ADDR = "0xe53cb2ace8707526a5050bec7bcf979c57f8b44f";
var REG_ABI_registerFor = ['bytes32', 'address', 'address'];
var NAME_MAP = {};
var NAME_LIST = [];
function readContractAddress() {
fs.readFile(REG_ADDR_FILE, function(err, content) {
if (err) {
......@@ -41,7 +40,7 @@ function getAllNames() {
var p = web3.SolidityCoder.decodeParams(REG_ABI_registerFor, tr.input.substr(10));
var n = web3.toUtf8(p[0]);
console.log("Entry: " + n + " -> " + p[1] + " " + p[2]);
NAME_MAP[n] = {"addr":p[2], "owner":p[1]};
NAME_LIST.push({"name": n,"addr":p[2], "owner":p[1]});
} else {
console.log("Wrong contract: " + tr.to + " expected " + REG_ADDR);
}
......@@ -53,8 +52,8 @@ function getAllNames() {
if (nextBlock < totalBlocks)
web3.eth.getBlock(nextBlock++, true, cb);
if (rem == 0) {
console.log("Found " + Object.keys(NAME_MAP).length + " name mappings");
fs.writeFile("names.json", JSON.stringify(NAME_MAP));
console.log("Found " + NAME_LIST.length + " name mappings");
fs.writeFile("names.json", JSON.stringify(NAME_LIST));
} else if (!error && block && block.transactions.length) {
console.log("Listing names: " + Math.round(100-100*rem/totalBlocks) + "%, " + rem + " remaining... ");
}
......
var async = require("async");
var BigNumber = require('bignumber.js');
var fs = require('fs');
var Web3 = require('web3');
var web3 = new Web3();
var path = require('path');
var fs = require('fs');
var argv = require('minimist')(process.argv.slice(2));
if(argv['_'] < 1)
throw ("Specify Batch Input File as: nodejs " + path.basename(__filename) + " <filename>" );
function validateFile(filename){
if ( path.isAbsolute(filename) && fs.existsSync(filename) )
return filename
else if ( !path.isAbsolute(filename) && fs.existsSync("./" +filename))
return path.resolve(filename)
return false
}
var providedPath = String(argv['_'][0])
batchInputFile = validateFile(providedPath);
if(!batchInputFile)
throw "File " + providedPath + " does not exist"
Object.getPrototypeOf(web3.eth).awaitConsensus = function(txhash, mined_cb) {
var ethP = this;
var tries = 5;
var filter = this.filter('latest');
filter.watch(function(error, result) {
if (error)
console.log("watch error: " + error);
var receipt = ethP.getTransactionReceipt(txhash);
if (receipt && receipt.transactionHash == txhash) {
filter.stopWatching();
mined_cb();
} else if (!--tries) {
mined_cb("Transaction timeout..");
}
});
}
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var coinbase = web3.eth.coinbase;
console.log("Coinbase: "+ coinbase);
var balance = web3.eth.getBalance(coinbase);
console.log("Balance:" +balance.toString(10));
var REG_FILE = __dirname + "/contract/registrar.out.json";
var REG_ADDR_FILE = __dirname + "/contractAddress.txt";
var NAME_VALIDATOR = new RegExp('^[a-z0-9-_]{3,32}$');
var account;
var regAddress = "0xe53cb2ace8707526a5050bec7bcf979c57f8b44f";
var regData;
var regContract;
var reg;
function unlockAccount() {
web3.personal.unlockAccount(coinbase, "toto");
}
function getRemainingGaz() {
return web3.eth.getBalance(coinbase) / web3.eth.gasPrice;
}
function loadContract(onContractLoaded) {
fs.readFile(REG_ADDR_FILE, function(err, content) {
if (err) {
throw "Contract Address issues, run server to initialize contract address first time";
} else {
regAddress = String(content).trim();
}
fs.readFile(REG_FILE, function(err, data){
if (err) {
console.log("Can't read contract ABI: " + err);
throw err;
}
regData = JSON.parse(data).contracts.registrar.GlobalRegistrar;
regContract = web3.eth.contract(regData.abi);
console.log("Loading name contract from blockchain at " + regAddress);
web3.eth.getCode(regAddress, function(error, result) {
if (error)
console.log("Error getting contract code: " + error);
if (!result || result == "0x") {
console.log("Contract not found at " + regAddress);
throw "Contract Address issues, run server to initialize contract address"
} else {
regContract.at(regAddress, function(err, result) {
console.log("Contract found and loaded from " + regAddress);
if(!err) {
reg = result;
onContractLoaded();
}
else {
console.error("err: " + err);
}
});
}
});
});
});
}
function checkName(name) {
try {
return Boolean(name.match(NAME_VALIDATOR));
} catch (e) {
return false;
}
}
function formatName(name) {
return '0x' + new Buffer(name, 'utf8').toString('hex');
}
function isHashZero(h) {
return !h || h == "0x" || h == "0x0" || h == "0x0000000000000000000000000000000000000000";
}
function parseString(s) {
return s ? web3.toUtf8(s) : s;
}
function formatAddress(address) {
if (address) {
var s = address.trim();
try {
if (s.startsWith("ring:"))
s = s.substr(5);
if (!s.startsWith("0x"))
s = "0x" + s;
if (new BigNumber(s.substr(2), 16) == 0)
return undefined;
return s;
} catch (err) {}
}
return undefined;
}
function registerName(addressparam, nameparam, ownerparam, mined_cb){
try {
var addr = formatAddress(addressparam);
if (!addr) {
console.log("Error parsing input address "+ addressparam);
return;
}
try {
ownerparam = formatAddress(ownerparam);
if (!ownerparam)
throw "no owner";
} catch (err) {
console.log("Error parsing input: " + err);
return;
}
if (!checkName(nameparam)) {
console.log("error: invalid name "+ nameparam);
return;
}
console.log("Got reg request (" + nameparam + " -> " + addr + ") from " + ownerparam);
reg.owner(nameparam, function(err, owner) {
if (owner == 0) {
reg.name(addr, function(err, res) {
try {
if (err)
console.log("Error checking name: " + err);
var name = parseString(res);
if (name) {
console.log("Address " + addr + " already registered with name: " + name);
return;
} else {
console.log("Remaing gaz: " + getRemainingGaz());
unlockAccount();
reg.reserveFor.sendTransaction(formatName(nameparam), ownerparam, addr, {
from: coinbase,
gas: 3000000
}, function(terr, reg_c) {
if (terr) {
console.log("Transaction error " + JSON.stringify(terr));
} else {
console.log("Transaction sent " + reg_c);
// Send answer as soon as the transaction is queued
console.log("Success!")
web3.eth.awaitConsensus(reg_c, function(error) {
mined_cb(reg_c)
if (error) {
console.log(error);
return;
}
});
}
});
}
} catch (err) {
console.log("Address registration exception: " + err);
}
});
} else {
if (owner == ownerparam) {
reg.addr(nameparam, function(err, reg_addr) {
if (reg_addr == addr) {
console.log("Success!");
return;
} else {
console.log("Error!" + err);
return;
}
});
} else {
console.log("Error!");;
console.log("Owner "+ owner);
console.log("Ownerparam "+ ownerparam);
return;
}
}
});
} catch (err) {
console.log("Address registration exception: " + err);
}
}
function startWrites(){
var NAME_LIST = JSON.parse(fs.readFileSync(batchInputFile, 'utf8'));
console.log(String(NAME_LIST.length) + " inserts to do");
//create parallel queue that does 256 registerNames parallely
var q = async.queue(function(task, callback) {
registerName(task['addr'], task['name'], task['owner'], callback);
}, 256);
for (var i = 0; i < NAME_LIST.length; i++) {
q.push(NAME_LIST[i], function(c){console.log("Mined registry: "+ c)});
console.log("Queued " + i);
}
}
unlockAccount();
loadContract(startWrites);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment