Merge pull request #207 from BeaudanBrown/p2p-json-rpc

P2p using rpc endpoint
This commit is contained in:
sachaaaaa 2019-02-22 10:04:05 +11:00 committed by GitHub
commit 3f755123e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 18 deletions

View file

@ -5,6 +5,8 @@
const nodeFetch = require('node-fetch');
const _ = require('lodash');
const endpointBase = '/v1/storage_rpc';
class HTTPError extends Error {
constructor(response) {
super(response.statusText);
@ -67,13 +69,19 @@ class LokiMessageAPI {
const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64');
const timestamp = Math.floor(Date.now() / 1000);
const p2pDetails = lokiP2pAPI.getContactP2pDetails(pubKey);
const body = {
method: 'store',
params: {
data: data64,
},
};
if (p2pDetails && (isPing || p2pDetails.isOnline)) {
try {
const port = p2pDetails.port ? `:${p2pDetails.port}` : '';
const url = `${p2pDetails.address}${port}/store`;
const url = `${p2pDetails.address}${port}${endpointBase}`;
const fetchOptions = {
method: 'POST',
body: data64,
body: JSON.stringify(body),
};
await fetch(url, fetchOptions);
@ -109,6 +117,17 @@ class LokiMessageAPI {
// Something went horribly wrong
throw err;
}
const storageParams = {
pubKey,
ttl: ttl.toString(),
nonce,
timestamp: timestamp.toString(),
};
body.params = {
...body.params,
...storageParams,
};
const completedNodes = [];
const failedNodes = [];
let successfulRequests = 0;
@ -122,17 +141,7 @@ class LokiMessageAPI {
};
const doRequest = async nodeUrl => {
const url = `${nodeUrl}${this.messageServerPort}/v1/storage_rpc`;
const body = {
method: 'store',
params: {
pubKey,
ttl: ttl.toString(),
nonce,
timestamp: timestamp.toString(),
data: data64,
},
};
const url = `${nodeUrl}${this.messageServerPort}${endpointBase}`;
const fetchOptions = {
method: 'POST',
body: JSON.stringify(body),
@ -224,7 +233,7 @@ class LokiMessageAPI {
};
const doRequest = async (nodeUrl, nodeData) => {
const url = `${nodeUrl}${this.messageServerPort}/v1/storage_rpc`;
const url = `${nodeUrl}${this.messageServerPort}${endpointBase}`;
const body = {
method: 'retrieve',
params: {

View file

@ -32,9 +32,14 @@ class LocalLokiServer extends EventEmitter {
}
// Check endpoints here
if (req.url === '/store') {
// body is a base64 encoded string
this.emit('message', body);
if (req.url === '/v1/storage_rpc') {
const bodyObject = JSON.parse(body);
if (bodyObject.method !== 'store') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Invalid endpoint!');
return;
}
this.emit('message', bodyObject.params.data);
res.statusCode = 200;
res.end();
} else {

View file

@ -56,6 +56,12 @@ describe('LocalLokiServer', () => {
it('should pass the POSTed data to the callback', async () => {
const server = new LocalLokiServer();
await server.start(8001);
const messageData = {
method: 'store',
params: {
data: 'This is data',
},
};
const promise = new Promise(res => {
server.on('message', message => {
@ -66,7 +72,7 @@ describe('LocalLokiServer', () => {
});
try {
await axios.post('http://localhost:8001/store', 'This is data');
await axios.post('http://localhost:8001/v1/storage_rpc', messageData);
} catch (error) {
assert.isNotOk(error, 'Error occured');
}