Service now responds to availability requests

This commit is contained in:
Richard Ramos 2018-08-24 13:40:21 -04:00
parent c6b3c226c4
commit 34cc2eca89
1 changed files with 40 additions and 42 deletions

View File

@ -59,50 +59,30 @@ events.on('setup:complete', async (settings) => {
// Verifying relayer balance
await verifyBalance();
shhOptions.symKeyId = await web3.shh.addSymKey(config.node.whisper.symKey);
shhOptions.kId = await web3.shh.newKeyPair();
const symKeyID = await web3.shh.addSymKey(config.node.whisper.symKey);
const pubKey = await web3.shh.getPublicKey(shhOptions.kId);
// Listening to whisper
// Individual subscriptions due to https://github.com/ethereum/web3.js/issues/1361
// once this is fixed, we'll be able to use an array of topics and a single subs for symkey and a single subs for privKey
console.info(`Sym Key: ${config.node.whisper.symKey}`);
console.info(`Relayer Public Key: ${pubKey}`);
console.info("Topics Available:");
for(let contract in settings.contracts) {
console.info("- %s: %s [%s]", settings.getContractByTopic(contract).name, contract, Object.keys(settings.getContractByTopic(contract).allowedFunctions).join(', '));
shhOptions.topics = [contract];
events.emit('server:listen', shhOptions, settings);
// Listen to public channel - Used for reporting availability
events.emit('server:listen', Object.assign({symKeyID}, shhOptions), settings);
// Listen to private channel - Individual transactions
events.emit('server:listen', Object.assign({privateKeyID: shhOptions.kId}, shhOptions), settings);
}
/*
if(config.heartbeat.enabled){
web3.shh.addSymKey(config.heartbeat.symKey)
.then(heartbeatSymKeyId => {
for(let tokenAddress in settings.getTokens()){
let heartbeatPayload = settings.getToken(tokenAddress);
heartbeatPayload.address = tokenAddress;
setInterval(() => {
web3.shh.post({
symKeyID: heartbeatSymKeyId,
sig: keyId,
ttl: config.node.whisper.ttl,
powTarget:config.node.whisper.minPow,
powTime: config.node.whisper.powTime,
topic: web3.utils.toHex("relay-heartbeat-" + heartbeatPayload.symbol).slice(0, 10),
payload: web3.utils.toHex(JSON.stringify(heartbeatPayload))
}).catch((err) => {
console.error(err);
process.exit(-1);
});
}, 60000);
}
});
}*/
});
const reply = (message) => (text, receipt) => {
const replyFunction = (message) => (text, receipt) => {
if(message.sig !== undefined){
console.log(text);
web3.shh.post({
@ -121,9 +101,7 @@ const extractInput = (message) => {
let obj = {
contract: null,
address: null,
functionName: null,
functionParameters: null,
payload: null
action: null
};
try {
@ -131,9 +109,15 @@ const extractInput = (message) => {
let parsedObj = JSON.parse(msg);
obj.contract = parsedObj.contract;
obj.address = parsedObj.address;
obj.functionName = parsedObj.encodedFunctionCall.slice(0, 10);
obj.functionParameters = "0x" + parsedObj.encodedFunctionCall.slice(10);
obj.payload = parsedObj.encodedFunctionCall;
obj.action = parsedObj.action;
if(obj.action == 'transaction'){
obj.functionName = parsedObj.encodedFunctionCall.slice(0, 10);
obj.functionParameters = "0x" + parsedObj.encodedFunctionCall.slice(10);
obj.payload = parsedObj.encodedFunctionCall;
} else if(obj.action == 'availability') {
obj.token = parsedObj.token;
obj.gasPrice = parsedObj.gasPrice;
}
} catch(err){
console.error("Couldn't parse " + message);
}
@ -152,9 +136,23 @@ events.on('server:listen', (shhOptions, settings) => {
verifyBalance(true);
processor.process(settings.getContractByTopic(message.topic),
extractInput(message),
reply(message));
const input = extractInput(message);
const reply = replyFunction(message);
switch(input.action){
case 'transaction':
processor.process(settings.getContractByTopic(message.topic),
input,
reply);
break;
case 'availability':
reply("available");
break;
default:
reply("unknown-action");
}
});
});