This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removing unnecessary transaction on upload and removing nonce from ha…
…shX signer
- Loading branch information
Wilfred Tannr Allard
committed
Apr 26, 2018
1 parent
ae64b1b
commit fcb1e3e
Showing
13 changed files
with
214 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
30
manifest/cfcf99dabbf25f3e9cb694e25a88145eda5bbe46.batchain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters