Skip to content
Snippets Groups Projects
Commit 02ba6d72 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

nameservice: save contract ABI on disk, fix loading code

Change-Id: If171f72edc9847628cf6ee8d9f406fbab222299a
parent 544854d3
No related branches found
No related tags found
No related merge requests found
...@@ -32,8 +32,9 @@ var balance = web3.eth.getBalance(coinbase); ...@@ -32,8 +32,9 @@ var balance = web3.eth.getBalance(coinbase);
console.log(balance.toString(10)); console.log(balance.toString(10));
var REG_ADDR_FILE = "contractAddress.txt"; var REG_ADDR_FILE = "contractAddress.txt";
var REG_ADDR = "0x1b364554e859d3277d3477ef6bf21113464e2392"; var REG_ABI_FILE = "contractABI.json";
var REG_ABI = [{"constant":true,"inputs":[{"name":"_a","type":"address"}],"name":"name","outputs":[{"name":"o_name","type":"bytes32"}],"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"content","outputs":[{"name":"","type":"bytes32"}],"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"addr","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserve","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"subRegistrar","outputs":[{"name":"o_subRegistrar","type":"address"}],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_owner","type":"address"},{"name":"_a","type":"address"}],"name":"reserveFor","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_newOwner","type":"address"}],"name":"transfer","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_registrar","type":"address"}],"name":"setSubRegistrar","outputs":[],"type":"function"},{"constant":false,"inputs":[],"name":"Registrar","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_a","type":"address"},{"name":"_primary","type":"bool"}],"name":"setAddress","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_content","type":"bytes32"}],"name":"setContent","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"disown","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"register","outputs":[{"name":"","type":"address"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"}],"name":"Changed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"addr","type":"address"},{"indexed":false,"name":"owner","type":"address"}],"name":"PrimaryChanged","type":"event"}]; var REG_ADDR = "0xe53cb2ace8707526a5050bec7bcf979c57f8b44f";
var REG_ABI = [{"constant":true,"inputs":[{"name":"_a","type":"address"}],"name":"name","outputs":[{"name":"o_name","type":"bytes32"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"content","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"addr","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"subRegistrar","outputs":[{"name":"o_subRegistrar","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_a","type":"address"}],"name":"reserve","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_owner","type":"address"},{"name":"_a","type":"address"}],"name":"reserveFor","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_newOwner","type":"address"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_registrar","type":"address"}],"name":"setSubRegistrar","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"Registrar","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_a","type":"address"},{"name":"_primary","type":"bool"}],"name":"setAddress","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_content","type":"bytes32"}],"name":"setContent","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"disown","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"register","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"}],"name":"Changed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"addr","type":"address"},{"indexed":false,"name":"owner","type":"address"}],"name":"PrimaryChanged","type":"event"}];
var account; var account;
var regContract; var regContract;
...@@ -69,30 +70,39 @@ function waitForGaz(want, cb) { ...@@ -69,30 +70,39 @@ function waitForGaz(want, cb) {
function loadContract() { function loadContract() {
fs.readFile(REG_ADDR_FILE, function(err, content) { fs.readFile(REG_ADDR_FILE, function(err, content) {
if (!err) { if (err) {
REG_ADDR = content;
} else {
console.log("Can't read contract address: " + err); console.log("Can't read contract address: " + err);
} else {
REG_ADDR = String(content);
} }
console.log("Loading name contract from blockchain at " + REG_ADDR); fs.readFile(REG_ABI_FILE, function(err, abi_str){
web3.eth.getCode(REG_ADDR, function(error, result) { if (err)
//console.log("Contract code at " + REG_ADDR + ": " + result); console.log("Can't read contract ABI: " + err);
if (result == "0x") { else
console.log("Contract not found at " + REG_ADDR); REG_ABI = JSON.parse(abi_str);
initContract(); console.log("Loading name contract from blockchain at " + REG_ADDR);
} else { web3.eth.getCode(REG_ADDR, function(error, result) {
regContract = web3.eth.contract(REG_ABI); if (error)
regContract.at(REG_ADDR, function(err, result) { console.log("Error getting contract code: " + error);
console.log("Contract found and loaded from " + REG_ADDR); /*else
if(!err) { console.log("Contract code at " + REG_ADDR + ": " + result);*/
reg = result; if (!result || result == "0x") {
startServer(); console.log("Contract not found at " + REG_ADDR);
} initContract();
else { } else {
console.error("err: " + err); regContract = web3.eth.contract(REG_ABI);
} regContract.at(REG_ADDR, function(err, result) {
}); console.log("Contract found and loaded from " + REG_ADDR);
} if(!err) {
reg = result;
startServer();
}
else {
console.error("err: " + err);
}
});
}
});
}); });
}); });
} }
...@@ -108,6 +118,7 @@ function initContract() { ...@@ -108,6 +118,7 @@ function initContract() {
} }
console.log("Contract compiled, instantiating on blockchain..."); console.log("Contract compiled, instantiating on blockchain...");
REG_ABI = compiled.GlobalRegistrar.info.abiDefinition; REG_ABI = compiled.GlobalRegistrar.info.abiDefinition;
fs.writeFile(REG_ABI_FILE, JSON.stringify(REG_ABI));
regContract = web3.eth.contract(REG_ABI); regContract = web3.eth.contract(REG_ABI);
waitForGaz(3000000, function(){ waitForGaz(3000000, function(){
regContract.new({from: coinbase, data: compiled.GlobalRegistrar.code, gas: 3000000}, function(e, contract){ regContract.new({from: coinbase, data: compiled.GlobalRegistrar.code, gas: 3000000}, function(e, contract){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment