Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
Removing unnecessary transaction on upload and removing nonce from ha…
Browse files Browse the repository at this point in the history
…shX signer
  • Loading branch information
Wilfred Tannr Allard committed Apr 26, 2018
1 parent ae64b1b commit fcb1e3e
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 42 deletions.
70 changes: 30 additions & 40 deletions batnode.js
Expand Up @@ -128,39 +128,35 @@ class BatNode {

sendShardToNode(nodeInfo, shard, shards, shardIdx, storedShardName, distinctIdx, manifestPath) {
fs.readFile(`./shards/${storedShardName}`, (err, fileData) => {
crypto.randomBytes(32, (err, randomKey) => {
let nonce = randomKey;
let hashedDataAndNonce = fileUtils.sha1HashData(fileData, nonce);
let shaPreimage = Buffer.from(hashedDataAndNonce, 'hex');
let shaSignerKey = crypto.createHash('sha256').update(shaPreimage).digest('hex');
let stellarPrivateKey = fileUtils.getStellarSecretSeed();
this.createEscrowAccount(stellarPrivateKey, shaSignerKey, (escrowKeypair) => {
let { port, host } = nodeInfo;
let client = this.connect(port, host, () => {
console.log('connected to target batnode')
});

let message = {
messageType: "STORE_FILE",
fileName: shard,
fileContent: fileData,
escrow: escrowKeypair.publicKey(),
nonce
};

if (shardIdx < shards.length - 1){
this.getClosestBatNodeToShard(shards[shardIdx + 1], (batNode, kadNode) => {
this.sendShardToNode(batNode, shards[shardIdx + 1], shards, shardIdx + 1, storedShardName, distinctIdx, manifestPath)
})
} else {
this.distributeCopies(distinctIdx + 1, manifestPath)
}
let hashedData = fileUtils.sha1HashData(fileData);
let shaPreimage = Buffer.from(hashedData, 'hex');
let shaSignerKey = crypto.createHash('sha256').update(shaPreimage).digest('hex');
let stellarPrivateKey = fileUtils.getStellarSecretSeed();
this.createEscrowAccount(stellarPrivateKey, shaSignerKey, (escrowKeypair) => {
let { port, host } = nodeInfo;
let client = this.connect(port, host, () => {
console.log('connected to target batnode')
});

let message = {
messageType: "STORE_FILE",
fileName: shard,
fileContent: fileData,
escrow: escrowKeypair.publicKey(),
};

if (shardIdx < shards.length - 1){
this.getClosestBatNodeToShard(shards[shardIdx + 1], (batNode, kadNode) => {
this.sendShardToNode(batNode, shards[shardIdx + 1], shards, shardIdx + 1, storedShardName, distinctIdx, manifestPath)
})
} else {
this.distributeCopies(distinctIdx + 1, manifestPath)
}


client.write(JSON.stringify(message), () => {
console.log('Sending shard to a peer node...')
});
})

client.write(JSON.stringify(message), () => {
console.log('Sending shard to a peer node...')
});
})
})
}
Expand All @@ -181,15 +177,9 @@ class BatNode {
const shardsOfManifest = Object.keys(manifest.chunks)
if (distinctIdx < shardsOfManifest.length) {
let copiesOfCurrentShard = manifest.chunks[shardsOfManifest[distinctIdx]]

this.getClosestBatNodeToShard(copiesOfCurrentShard[copyIdx], (batNode, kadNode) => {
this.kadenceNode.getOtherNodeStellarAccount(kadNode, (error, accountId) => {
console.log("Sending payment to a peer node's Stellar account...")
this.sendPaymentFor(accountId, (paymentResult) => {
this.sendShardToNode(batNode, copiesOfCurrentShard[copyIdx], copiesOfCurrentShard, copyIdx, shardsOfManifest[distinctIdx], distinctIdx, manifestPath)
})
})
});
this.sendShardToNode(batNode, copiesOfCurrentShard[copyIdx], copiesOfCurrentShard, copyIdx, shardsOfManifest[distinctIdx], distinctIdx, manifestPath)
})
} else {
console.log("Uploading shards and copies completed! You can safely remove the files under shards folder from your end now.")
}
Expand Down
153 changes: 153 additions & 0 deletions local-start.js
@@ -0,0 +1,153 @@
const bunyan = require('bunyan');
const levelup = require('levelup');
const leveldown = require('leveldown');
const encoding = require('encoding-down');
const kad = require('@kadenceproject/kadence');
const BatNode = require('./batnode.js').BatNode;
const kad_bat = require('./kadence_plugin').kad_bat;
const stellar_account = require('./kadence_plugin').stellar_account;
const seed = require('./constants').SEED_NODE;
const cliServer = require('./constants').CLI_SERVER;
const batNodePort = require('./constants').BATNODE_SERVER_PORT
const kadNodePort = require('./constants').KADNODE_PORT
const publicIp = require('public-ip');
const fs = require('fs');
const fileUtils = require('./utils/file').fileSystem;
const JSONStream = require('JSONStream');
const backoff = require('backoff');
const crypto = require('crypto');
const base32 = require('base32');



const kademliaNode = new kad.KademliaNode({
transport: new kad.HTTPTransport(),
storage: levelup(encoding(leveldown('./dbbb'))),
contact: {hostname: '0.0.0.0', port: 1705}
})

kademliaNode.plugin(kad_bat)
kademliaNode.plugin(stellar_account)
kademliaNode.listen(1705)
const batNode = new BatNode(kademliaNode)
kademliaNode.batNode = batNode

const nodeConnectionCallback = (serverConnection) => {
serverConnection.on('end', () => {
console.log('end')
})

const stream = JSONStream.parse();
serverConnection.pipe(stream);

stream.on('data', (receivedData, error) => {
if (error) { throw error; }
console.log("received data: ", receivedData)

if (receivedData.messageType === "RETRIEVE_FILE") {
batNode.readFile(`./hosted/${receivedData.fileName}`, (err, data) => {
serverConnection.write(data)
})
} else if (receivedData.messageType === "STORE_FILE"){
let fileName = receivedData.fileName
let nonce = Buffer.from(receivedData.nonce);
let fileContent = Buffer.from(receivedData.fileContent)
let preimage = fileUtils.sha1HashData(fileContent, nonce)
let escrowAccountId = receivedData.escrow;
batNode.acceptPayment(preimage, escrowAccountId)

batNode.kadenceNode.iterativeStore(fileName, [batNode.kadenceNode.identity.toString(), batNode.kadenceNode.contact], (err, stored) => {
console.log('nodes who stored this value: ', stored)
batNode.writeFile(`./hosted/${fileName}`, fileContent, (writeErr) => {
if (writeErr) {
throw writeErr;
}
serverConnection.write(JSON.stringify({messageType: "SUCCESS"}))
})
})
} else if (receivedData.messageType === "AUDIT_FILE") {
fs.exists(`./hosted/${receivedData.fileName}`, (doesExist) => {
if (doesExist) {
fs.readFile(`./hosted/${receivedData.fileName}`, (err, data) => {
const shardSha1 = fileUtils.sha1HashData(data);
serverConnection.write(shardSha1);
});
} else {
serverConnection.write("Shard not found")
}
})
}
})
}

const nodeCLIConnectionCallback = (serverConnection) => {

const sendAuditDataWhenFinished = (exponentialBackoff) => {
exponentialBackoff.failAfter(10);
exponentialBackoff.on('backoff', function(number, delay) {
console.log(number + ' ' + delay + 'ms');
});
exponentialBackoff.on('ready', function() {
if (!batNode.audit.ready) {
exponentialBackoff.backoff();
} else {
serverConnection.write(JSON.stringify(batNode.audit));
return;
}
});
exponentialBackoff.on('fail', function() {
console.log('Timeout: failed to complete audit');
});
exponentialBackoff.backoff();
}

serverConnection.on('data', (data) => {
let receivedData = JSON.parse(data);

if (receivedData.messageType === "CLI_UPLOAD_FILE") {
let filePath = receivedData.filePath;

batNode.uploadFile(filePath, 0, () => {
serverConnection.write("File has finished uploading")
})
} else if (receivedData.messageType === "CLI_DOWNLOAD_FILE") {
let filePath = receivedData.filePath;

batNode.retrieveFile(filePath);
} else if (receivedData.messageType === "CLI_AUDIT_FILE") {
let filePath = receivedData.filePath;
let exponentialBackoff = backoff.exponential({
randomisationFactor: 0,
initialDelay: 20,
maxDelay: 10000
});

batNode.auditFile(filePath);
// post audit cleanup
serverConnection.on('close', () => {
batNode.audit.ready = false;
batNode.audit.data = null;
batNode.audit.passed = false;
batNode.audit.failed = [];
});

// Exponential backoff until file audit finishes
sendAuditDataWhenFinished(exponentialBackoff);

} else if (receivedData.messageType === "CLI_PATCH_FILE") {
const { manifestPath, siblingShardId, failedShaId, copiesToRemoveFromManifest } = receivedData;
batNode.patchFile(manifestPath, failedShaId, siblingShardId, copiesToRemoveFromManifest)
}
})
}

batNode.createCLIServer(cliServer.port, cliServer.host, nodeCLIConnectionCallback);
batNode.createServer(batNodePort, '0.0.0.0', nodeConnectionCallback)


kademliaNode.join(seed, () => {
console.log('you have joined the network! Ready to accept commands from the CLI!')
})



30 changes: 30 additions & 0 deletions manifest/cfcf99dabbf25f3e9cb694e25a88145eda5bbe46.batchain
@@ -0,0 +1,30 @@
{
"fileName": "p2p.jpg.crypt",
"fileSize": 16496,
"chunks": {
"dac309f6841b749246aae6df24b0d88cbe432f11": [
"dd8505c3912eee0da8ea4f03055bd9ae55cbee44"
],
"f1adda9e59aa20a53fdc2d2cbd79e6161279c838": [
"7cbc3ff53b00ca30cd716ae8c365c1a1b9177334"
],
"2f3364a56d53183f59cb3c74a7bcfb4baaf744b1": [
"2a7cd328b526123bcccaed2865a63c125e1a5e5e"
],
"7957f10bfbe5cbf43a54ca58eb930af456073e30": [
"fab3d366110381ea297756bab4f068a4bd460a10"
],
"daaacc649f739ec771290f679954b4bb45d4e557": [
"7fb0a733fe53e0e1ef0b01e4fdb0318f58e17e16"
],
"dbf4d74183ce720408e7f69c9ebf32fbc07d105d": [
"0cf68cd903be106c24bd33e64415532e53ab99bd"
],
"35b375be203413091a248a794eebdcda380ece1b": [
"cfb848cb10d909c3e4b1514b221b2f1f2d10b2d8"
],
"eb4bad849a6ba6849f721eb52cbfb952b5da2941": [
"dbc7e1998b475d72cc5a71893af05ff2e5abdd62"
]
}
}
Binary file added personal/decrypted-p2p.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added shards/2f3364a56d53183f59cb3c74a7bcfb4baaf744b1
Binary file not shown.
Binary file added shards/35b375be203413091a248a794eebdcda380ece1b
Binary file not shown.
Binary file added shards/7957f10bfbe5cbf43a54ca58eb930af456073e30
Binary file not shown.
Binary file added shards/daaacc649f739ec771290f679954b4bb45d4e557
Binary file not shown.
Binary file added shards/dac309f6841b749246aae6df24b0d88cbe432f11
Binary file not shown.
Binary file added shards/dbf4d74183ce720408e7f69c9ebf32fbc07d105d
Binary file not shown.
Binary file added shards/eb4bad849a6ba6849f721eb52cbfb952b5da2941
Binary file not shown.
Binary file added shards/f1adda9e59aa20a53fdc2d2cbd79e6161279c838
Binary file not shown.
3 changes: 1 addition & 2 deletions start.js
Expand Up @@ -56,9 +56,8 @@ publicIp.v4().then(ip => {
});
} else if (receivedData.messageType === "STORE_FILE"){
let fileName = receivedData.fileName
let nonce = Buffer.from(receivedData.nonce);
let fileContent = Buffer.from(receivedData.fileContent)
let preimage = fileUtils.sha1HashData(fileContent, nonce)
let preimage = fileUtils.sha1HashData(fileContent)
let escrowAccountId = receivedData.escrow;
batNode.acceptPayment(preimage, escrowAccountId)

Expand Down

0 comments on commit fcb1e3e

Please sign in to comment.