Calculating last clock value

This commit is contained in:
Richard Ramos 2018-11-08 12:00:40 -04:00
parent 396edd041c
commit ac1281fe48
1 changed files with 46 additions and 12 deletions

View File

@ -13,9 +13,13 @@ const CONTACT_DISCOVERY_TOPIC = '0xf8946aac';
const CONTACT_CODE_REGEXP = /^(0x)?[0-9a-f]{130}$/i; const CONTACT_CODE_REGEXP = /^(0x)?[0-9a-f]{130}$/i;
function createStatusPayload(content, messageType, isJson) { function createStatusPayload(content, messageType, clockValue, isJson) {
const tag = '~#c4'; const tag = '~#c4';
const clockValue = (new Date().getTime()) * 100; if(!clockValue){
clockValue = (new Date().getTime()) * 100;
}
const contentType = (isJson ? 'content/json' : 'text/plain'); const contentType = (isJson ? 'content/json' : 'text/plain');
const timestamp = new Date().getTime(); const timestamp = new Date().getTime();
@ -59,6 +63,7 @@ class StatusJS {
this.channels[channelName] = { this.channels[channelName] = {
channelName, channelName,
channelKey, channelKey,
lastClockValue: 0,
channelCode: Web3.utils.sha3(channelName).slice(0, 10) channelCode: Web3.utils.sha3(channelName).slice(0, 10)
} }
if (cb) cb(); if (cb) cb();
@ -66,7 +71,8 @@ class StatusJS {
async addContact(contactCode, cb) { async addContact(contactCode, cb) {
this.contacts[contactCode] = { this.contacts[contactCode] = {
'username': utils.generateUsernameFromSeed(contactCode) username: utils.generateUsernameFromSeed(contactCode),
lastClockValue: 0
} }
if (cb) cb(); if (cb) cb();
} }
@ -103,6 +109,13 @@ class StatusJS {
topics: [this.channels[channelName].channelCode] topics: [this.channels[channelName].channelCode]
}).on('data', (data) => { }).on('data', (data) => {
let username = utils.generateUsernameFromSeed(data.sig); let username = utils.generateUsernameFromSeed(data.sig);
const payloadArray = JSON.parse(hexToAscii(data.payload));
if(this.channels[channelName].lastClockValue < payloadArray[1][3]){
this.channels[channelName].lastClockValue = payloadArray[1][3];
}
cb(null, {payload: hexToAscii(data.payload), data: data, username: username}); cb(null, {payload: hexToAscii(data.payload), data: data, username: username});
}).on('error', (err) => { }).on('error', (err) => {
cb(err); cb(err);
@ -115,22 +128,34 @@ class StatusJS {
privateKeyID: this.sig, privateKeyID: this.sig,
topics: [CONTACT_DISCOVERY_TOPIC] topics: [CONTACT_DISCOVERY_TOPIC]
}).on('data', (data) => { }).on('data', (data) => {
let username = utils.generateUsernameFromSeed(data.sig); if(!this.contacts[data.sig]){
if(!this.contacts[data.sig]) this.contacts[data.sig] = {}; this.addContact(data.sig);
this.contacts[data.sig].username = username; }
cb(null, {payload: hexToAscii(data.payload), data: data, username: username});
const payloadArray = JSON.parse(hexToAscii(data.payload));
if(this.contacts[data.sig].lastClockValue < payloadArray[1][3]){
this.contacts[data.sig].lastClockValue = payloadArray[1][3];
}
cb(null, {payload: hexToAscii(data.payload), data: data, username: this.contacts[data.sig].username});
}).on('error', (err) => { }).on('error', (err) => {
cb(err); cb(err);
}); });
} }
sendUserMessage(contactCode, msg, cb) { sendUserMessage(contactCode, msg, cb) {
if(!this.contacts[contactCode]){
this.addContact(contactCode);
}
this.contacts[contactCode].lastClockValue++;
this.shh.post({ this.shh.post({
pubKey: contactCode, pubKey: contactCode,
sig: this.sig, sig: this.sig,
ttl: TTL, ttl: TTL,
topic: CONTACT_DISCOVERY_TOPIC, topic: CONTACT_DISCOVERY_TOPIC,
payload: createStatusPayload(msg, USER_MESSAGE), payload: createStatusPayload(msg, USER_MESSAGE, this.contacts[contactCode].lastClockValue),
powTime: POW_TIME, powTime: POW_TIME,
powTarget: POW_TARGET powTarget: POW_TARGET
}).then(() => { }).then(() => {
@ -148,12 +173,14 @@ class StatusJS {
return cb("unknown channel: " + channelName); return cb("unknown channel: " + channelName);
} }
this.channels[channelName].lastClockValue++;
this.shh.post({ this.shh.post({
symKeyID: this.channels[channelName].channelKey, symKeyID: this.channels[channelName].channelKey,
sig: this.sig, sig: this.sig,
ttl: TTL, ttl: TTL,
topic: this.channels[channelName].channelCode, topic: this.channels[channelName].channelCode,
payload: createStatusPayload(msg, GROUP_MESSAGE), payload: createStatusPayload(msg, GROUP_MESSAGE, this.channels[channelName].lastClockValue ),
powTime: POW_TIME, powTime: POW_TIME,
powTarget: POW_TARGET powTarget: POW_TARGET
}).then(() => { }).then(() => {
@ -167,12 +194,17 @@ class StatusJS {
sendJsonMessage(destination, msg, cb) { sendJsonMessage(destination, msg, cb) {
if (CONTACT_CODE_REGEXP.test(destination)) { if (CONTACT_CODE_REGEXP.test(destination)) {
if(!this.contacts[destination]){
this.addContact(destination);
}
this.contacts[destination].lastClockValue++;
this.shh.post({ this.shh.post({
pubKey: destination, pubKey: destination,
sig: this.sig, sig: this.sig,
ttl: TTL, ttl: TTL,
topic: CONTACT_DISCOVERY_TOPIC, topic: CONTACT_DISCOVERY_TOPIC,
payload: createStatusPayload(msg, USER_MESSAGE), payload: createStatusPayload(msg, USER_MESSAGE, this.contacts[destination].lastClockValue, true),
powTime: POW_TIME, powTime: POW_TIME,
powTarget: POW_TARGET powTarget: POW_TARGET
}).then(() => { }).then(() => {
@ -183,12 +215,14 @@ class StatusJS {
cb(e, false); cb(e, false);
}); });
} else { } else {
this.channels[destination].lastClockValue++;
this.shh.post({ this.shh.post({
symKeyID: this.channels[destination].channelKey, symKeyID: this.channels[destination].channelKey,
sig: this.sig, sig: this.sig,
ttl: TTL, ttl: TTL,
topic: this.channels[destination].channelCode, topic: this.channels[destination].channelCode,
payload: createStatusPayload(JSON.stringify(msg), GROUP_MESSAGE, true), payload: createStatusPayload(JSON.stringify(msg), GROUP_MESSAGE, this.channels[destination].lastClockValue, true),
powTime: POW_TIME, powTime: POW_TIME,
powTarget: POW_TARGET powTarget: POW_TARGET
}).then(() => { }).then(() => {