Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/buyer/buyer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ function letsBuy (torrent) {
// Stop looking for seller
lookingForSeller = false

const endPoint = connection.endpoint
const pid = connection.pid
const sellerTerms = connection.announcedModeAndTermsFromPeer.seller.terms

let setup = makeContractAndDownloadInfoMap(endPoint, sellerTerms)
let setup = makeContractAndDownloadInfoMap(pid, sellerTerms)

torrent.startDownloading(setup.contract, setup.map, function (err) {
if (err) {
Expand All @@ -76,14 +76,14 @@ function letsBuy (torrent) {
})
}

function makeContractAndDownloadInfoMap (endPoint, sellerTerms) {
function makeContractAndDownloadInfoMap (pid, sellerTerms) {
// Fake contract
const contract = Buffer.from('01000000017b1eabe0209b1fe794124575ef807057c77ada2138ae4fa8d6c4de0398a14f3f00000000494830450221008949f0cb400094ad2b5eb399d59d01c14d73d8fe6e96df1a7150deb388ab8935022079656090d7f6bac4c9a94e0aad311a4268e082a725f8aeae0573fb12ff866a5f01ffffffff01f0ca052a010000001976a914cbc20a7664f2f69e5355aa427045bc15e7c6c77288ac00000000', 'hex')

// Download info map for one seller
const map = new Map()

map.set(endPoint, {
map.set(pid, {
index: 0,
value: 100000,
sellerTerms: sellerTerms,
Expand Down
59 changes: 31 additions & 28 deletions examples/seller/seller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ function letsSell (torrent) {
settlementFee: 5000
}

let lookingForBuyer = false

let contractSk = Buffer.from('030589ee559348bd6a7325994f9c8eff12bd5d73cc683142bd0dd1a17abc99b0', 'hex')
let finalPkHash = new Buffer(20)

Expand All @@ -44,37 +42,42 @@ function letsSell (torrent) {
}

console.log('We are in sell mode')
lookingForBuyer = true
})
})

// Wait for one suitable buyer and start uploading
torrent.on('peerPluginStatusUpdates', function (peerStatuses) {
if (!lookingForBuyer) return

let connection = pickSuitableBuyer(peerStatuses, sellerTerms)

if (!connection) return

lookingForBuyer = false

console.log('Found Suitable buyer', connection)
let buyers = new Map()

const endPoint = connection.endpoint
const buyerTerms = connection.announcedModeAndTermsFromPeer.buyer.terms

torrent.startUploading(endPoint, buyerTerms, contractSk, finalPkHash, (err) => {
if (err) {
console.log('Failed to start uploading to buyer', err)
lookingForBuyer = true
} else {
console.log('Started Selling To Buyer', connection)
}
// start uploading to matching buyers
torrent.on('peerPluginStatusUpdates', function (peerStatuses) {
console.log(peerStatuses.length, "connections")
let connections = pickSuitableBuyers(peerStatuses, sellerTerms)

if (!connections.length) return

connections.forEach((connection) => {
const pid = connection.pid
if (buyers.has(pid)) return
buyers.set(pid, true)
const buyerTerms = connection.announcedModeAndTermsFromPeer.buyer.terms

torrent.startUploading(pid, buyerTerms, contractSk, finalPkHash, (err) => {
if (err) {
console.log('Failed to start uploading to buyer', err)
buyers.delete(pid)
} else {
console.log('Started Selling To Buyer', connection)
}
})
})
})

// start uploading again to a peer if they leave
torrent.on('connectionRemoved', (pid) => buyers.delete(pid))
}

function pickSuitableBuyer (peerStatuses, sellerTerms) {
function pickSuitableBuyers (peerStatuses, sellerTerms) {
var suitableBuyers = []

for (var i in peerStatuses) {
const status = peerStatuses[i]

Expand All @@ -86,13 +89,13 @@ function pickSuitableBuyer (peerStatuses, sellerTerms) {
try {
// lazy checking for buyer
const buyerTerms = status.connection.announcedModeAndTermsFromPeer.buyer.terms
if(areTermsMatching(buyerTerms, sellerTerms)){
return status.connection
if (areTermsMatching(buyerTerms, sellerTerms)) {
suitableBuyers.push(status.connection)
}
} catch (e) {}
}

return null
return suitableBuyers
}

sellerSession.addTorrent(addTorrentParamsSeller, (err, torrent) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ class Session extends EventEmitter {
var torrent = this.torrents.get(torrentHandle.infoHash())

if (torrent) {
torrent._onConnectionAdded(alert.ip, alert.status)
torrent._onConnectionAdded(alert.pid, alert.status)
}
}

Expand All @@ -600,7 +600,7 @@ class Session extends EventEmitter {
var torrent = this.torrents.get(torrentHandle.infoHash())

if (torrent) {
torrent._onConnectionRemoved(alert.ip, alert.status)
torrent._onConnectionRemoved(alert.pid)
}
}

Expand Down
14 changes: 6 additions & 8 deletions lib/Torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ class Torrent extends EventEmitter {
//this.emit('pluginStatusUpdate', new TorrentPluginStatus(status))
}

_onConnectionAdded (endpoint, status) {
const addr = endpoint.address + ':' + endpoint.key
this.emit('connectionAdded', addr, status)
_onConnectionAdded (pid, status) {
this.emit('connectionAdded', pid, status)
//this.emit('connectionAdded', new ConnectionStatus(status))
}

_onConnectionRemoved (endpoint) {
const addr = endpoint.address + ':' + endpoint.key
this.emit('connectionRemoved', addr)
_onConnectionRemoved (pid) {
this.emit('connectionRemoved', pid)
}

// Torrent Plugin Controls
Expand Down Expand Up @@ -150,8 +148,8 @@ class Torrent extends EventEmitter {
this.plugin.pause(this.infoHash, callback)
}

startUploading (endPoint, buyerTerms, contractSk, finalPkHash, callback = () => {}) {
this.plugin.start_uploading(this.infoHash, endPoint, buyerTerms, contractSk, finalPkHash, callback)
startUploading (connectionId, buyerTerms, contractSk, finalPkHash, callback = () => {}) {
this.plugin.start_uploading(this.infoHash, connectionId, buyerTerms, contractSk, finalPkHash, callback)
}

startDownloading (contract, downloadInfoMap, callback = () => {}) {
Expand Down
5 changes: 3 additions & 2 deletions src/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Connection.hpp"
#include "libtorrent-node/utils.hpp"
#include "libtorrent-node/endpoint.hpp"
#include "libtorrent-node/peer_id.hpp"
#include "SellerTerms.hpp"
#include "BuyerTerms.hpp"
#include "OutPoint.hpp"
Expand Down Expand Up @@ -158,11 +159,11 @@ namespace connection {
return o;
}

v8::Local<v8::Object> encode(const joystream::protocol_session::status::Connection<libtorrent::tcp::endpoint>& c) {
v8::Local<v8::Object> encode(const joystream::protocol_session::status::Connection<libtorrent::peer_id>& c) {

v8::Local<v8::Object> o = Nan::New<v8::Object>();

SET_VAL(o, "endpoint", libtorrent::node::endpoint::encode(c.connectionId));
SET_VAL(o, "pid", libtorrent::node::peer_id::encode(c.connectionId));

// machine
SET_VAL(o, "innerState", encode(c.machine.innerStateTypeIndex));
Expand Down
2 changes: 1 addition & 1 deletion src/Connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace connection {
* { see above } o.payor - payor side of payment channel (only valid on buyer side)
* { see above } o.payee - payee side of payment channel (only valid on seller side)
*/
v8::Local<v8::Object> encode(const joystream::protocol_session::status::Connection<libtorrent::tcp::endpoint>& c);
v8::Local<v8::Object> encode(const joystream::protocol_session::status::Connection<libtorrent::peer_id>& c);

}
}
Expand Down
2 changes: 2 additions & 0 deletions src/PeerPluginStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "PeerPluginStatus.hpp"
#include "libtorrent-node/utils.hpp"
#include "libtorrent-node/endpoint.hpp"
#include "libtorrent-node/peer_id.hpp"
#include "BEPSupportStatus.hpp"
#include "Connection.hpp"

Expand All @@ -24,6 +25,7 @@ v8::Local<v8::Object> encode(const extension::status::PeerPlugin & s) {

v8::Local<v8::Object> o = Nan::New<v8::Object>();

SET_VAL(o, "pid", libtorrent::node::peer_id::encode(s.peerId));
SET_VAL(o, "endPoint", libtorrent::node::endpoint::encode(s.endPoint));
SET_VAL(o, "peerBEP10SupportStatus", bep_support_status::encode(s.peerBEP10SupportStatus));
SET_VAL(o, "peerBitSwaprBEPSupportStatus", bep_support_status::encode(s.peerBitSwaprBEPSupportStatus));
Expand Down
7 changes: 4 additions & 3 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "libtorrent-node/add_torrent_params.hpp"
#include "libtorrent-node/torrent_handle.h"
#include "libtorrent-node/endpoint.hpp"
#include "libtorrent-node/peer_id.hpp"
#include "libtorrent-node/error_code.hpp"

#include <extension/extension.hpp>
Expand Down Expand Up @@ -372,7 +373,7 @@ NAN_METHOD(Plugin::StartDownloading) {
ARGUMENTS_REQUIRE_DECODED(1, contractTx, Coin::Transaction, joystream::node::transaction::decode)
ARGUMENTS_REQUIRE_DECODED(2,
peerToStartDownloadInformationMap,
protocol_session::PeerToStartDownloadInformationMap<libtorrent::tcp::endpoint>,
protocol_session::PeerToStartDownloadInformationMap<libtorrent::peer_id>,
joystream::node::PeerToStartDownloadInformationMap::decode)
ARGUMENTS_REQUIRE_CALLBACK(3, managedCallback)

Expand All @@ -393,7 +394,7 @@ NAN_METHOD(Plugin::StartUploading) {
// Get validated parameters
GET_THIS_PLUGIN(plugin)
ARGUMENTS_REQUIRE_DECODED(0, infoHash, libtorrent::sha1_hash, libtorrent::node::sha1_hash::decode)
ARGUMENTS_REQUIRE_DECODED(1, endPoint, libtorrent::tcp::endpoint, libtorrent::node::endpoint::decode)
ARGUMENTS_REQUIRE_DECODED(1, peerId, libtorrent::peer_id, libtorrent::node::peer_id::decode)
ARGUMENTS_REQUIRE_DECODED(2, buyerTerms, protocol_wire::BuyerTerms, joystream::node::buyer_terms::decode)
ARGUMENTS_REQUIRE_DECODED(3, contractSk, Coin::PrivateKey, joystream::node::private_key::decode)
ARGUMENTS_REQUIRE_DECODED(4, finalPkHash, Coin::PubKeyHash, joystream::node::pubkey_hash::decode)
Expand All @@ -403,7 +404,7 @@ NAN_METHOD(Plugin::StartUploading) {

// Create request
joystream::extension::request::StartUploading request(infoHash,
endPoint,
peerId,
buyerTerms,
contractKeyPair,
finalPkHash,
Expand Down
5 changes: 3 additions & 2 deletions src/PluginAlertEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "PluginAlertEncoder.hpp"
#include "libtorrent-node/alert.hpp"
#include "libtorrent-node/endpoint.hpp"
#include "libtorrent-node/peer_id.hpp"
#include "libtorrent-node/utils.hpp"
#include "RequestResult.hpp"
#include "TorrentPluginStatus.hpp"
Expand Down Expand Up @@ -267,7 +268,7 @@ namespace PluginAlertEncoder {
v8::Local<v8::Object> encode(joystream::extension::alert::UploadStarted const * p) {
auto v = libtorrent::node::alert_types::encode(static_cast<libtorrent::torrent_alert const *>(p));

SET_VAL(v, "endPoint", libtorrent::node::endpoint::encode(p->endPoint));
SET_VAL(v, "pid", libtorrent::node::peer_id::encode(p->peerId));
SET_VAL(v, "terms", buyer_terms::encode(p->terms));
SET_VAL(v, "contractPrivateKey", private_key::encode(p->contractKeyPair.sk()));
SET_VAL(v, "finalPkHash", pubkey_hash::encode(p->finalPkHash));
Expand Down Expand Up @@ -295,7 +296,7 @@ namespace PluginAlertEncoder {
v8::Local<v8::Object> encode(joystream::extension::alert::AnchorAnnounced const * p) {
auto v = libtorrent::node::alert_types::encode(static_cast<libtorrent::torrent_alert const *>(p));

SET_VAL(v, "endPoint", libtorrent::node::endpoint::encode(p->_endPoint));
SET_VAL(v, "pid", libtorrent::node::peer_id::encode(p->_peerId));
SET_NUMBER(v, "value", p->_value);
SET_VAL(v, "outpoint", outpoint::encode(p->_anchor));
SET_VAL(v, "contractPk", public_key::encode(p->_contractPk));
Expand Down
4 changes: 2 additions & 2 deletions src/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ v8::Local<v8::Uint32> encode(const protocol_session::BuyingState & state) {
return Nan::New<v8::Uint32>(v);
}

v8::Local<v8::Object> encode(const protocol_session::status::Buying<libtorrent::tcp::endpoint> & b) {
v8::Local<v8::Object> encode(const protocol_session::status::Buying<libtorrent::peer_id> & b) {

v8::Local<v8::Object> o = Nan::New<v8::Object>();

Expand All @@ -123,7 +123,7 @@ v8::Local<v8::Object> encode(const protocol_session::status::Buying<libtorrent::
return o;
}

v8::Local<v8::Object> encode(const protocol_session::status::Session<libtorrent::tcp::endpoint> & s) {
v8::Local<v8::Object> encode(const protocol_session::status::Session<libtorrent::peer_id> & s) {

v8::Local<v8::Object> o = Nan::New<v8::Object>();

Expand Down
4 changes: 2 additions & 2 deletions src/Session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ namespace session {

v8::Local<v8::Uint32> encode(const protocol_session::BuyingState & state);

v8::Local<v8::Object> encode(const protocol_session::status::Buying<libtorrent::tcp::endpoint> & b);
v8::Local<v8::Object> encode(const protocol_session::status::Buying<libtorrent::peer_id> & b);

v8::Local<v8::Object> encode(const protocol_session::status::Session<libtorrent::tcp::endpoint> & s);
v8::Local<v8::Object> encode(const protocol_session::status::Session<libtorrent::peer_id> & s);

}
}
Expand Down
9 changes: 5 additions & 4 deletions src/StartDownloadConnectionInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "PubKeyHash.hpp"
#include <extension/Common.hpp> //std::hash<endpoint> specialization
#include "libtorrent-node/endpoint.hpp"
#include "libtorrent-node/peer_id.hpp"
#include "libtorrent-node/utils.hpp"

#define SELLER_TERMS_KEY "sellerTerms"
Expand Down Expand Up @@ -61,13 +62,13 @@ protocol_session::StartDownloadConnectionInformation decode(const v8::Local<v8::

namespace PeerToStartDownloadInformationMap {

//v8::Local<v8::Object> encode(const protocol_session::PeerToStartDownloadInformationMap<libtorrent::tcp::endpoint> & information) {
//v8::Local<v8::Object> encode(const protocol_session::PeerToStartDownloadInformationMap<libtorrent::peer_id> & information) {
//}

protocol_session::PeerToStartDownloadInformationMap<libtorrent::tcp::endpoint> decode(const v8::Local<v8::Value> & v) {
protocol_session::PeerToStartDownloadInformationMap<libtorrent::peer_id> decode(const v8::Local<v8::Value> & v) {

return std_lib_utils::decode<libtorrent::tcp::endpoint, protocol_session::StartDownloadConnectionInformation>(v,
&libtorrent::node::endpoint::decode,
return std_lib_utils::decode<libtorrent::peer_id, protocol_session::StartDownloadConnectionInformation>(v,
&libtorrent::node::peer_id::decode,
&joystream::node::StartDownloadConnectionInformation::decode);
}

Expand Down
5 changes: 3 additions & 2 deletions src/StartDownloadConnectionInformation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <protocol_session/protocol_session.hpp>
#include <libtorrent/socket.hpp>
#include <libtorrent/peer_id.hpp>

#include <nan.h>

Expand All @@ -33,8 +34,8 @@ protocol_session::StartDownloadConnectionInformation decode(const v8::Local<v8::

namespace PeerToStartDownloadInformationMap {

//v8::Local<v8::Object> encode(const protocol_session::PeerToStartDownloadInformationMap<libtorrent::tcp::endpoint> & information);
protocol_session::PeerToStartDownloadInformationMap<libtorrent::tcp::endpoint> decode(const v8::Local<v8::Value> & v);
//v8::Local<v8::Object> encode(const protocol_session::PeerToStartDownloadInformationMap<libtorrent::peer_id> & information);
protocol_session::PeerToStartDownloadInformationMap<libtorrent::peer_id> decode(const v8::Local<v8::Value> & v);

}

Expand Down