Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #739 from littleskunk/alloc
Browse files Browse the repository at this point in the history
increase contractCount on alloc and improve logging
  • Loading branch information
braydonf authored Oct 10, 2017
2 parents ba57616 + 806cdd0 commit 4a62a0f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
20 changes: 15 additions & 5 deletions lib/network/farmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,6 @@ FarmerInterface.prototype._handleOfferRes = function(res, contract, renter) {
item.addMetaData(renter, {});
self.storageManager.save(item, utils.noop);
self._logger.info('Offer accepted');
if (self._contractCount < Number.MAX_SAFE_INTEGER) {
self._contractCount++;
} else {
self._contractCount = 0;
}
});
};

Expand Down Expand Up @@ -776,6 +771,11 @@ FarmerInterface.prototype.handleAlloc = function(params, callback) {
return;
}

self._logger.info('handling alloc request from %s hash %s size %s',
params.contact.nodeID,
contractObj.get('data_hash'),
contractObj.get('data_size'));

this._shouldSendOffer(contractObj, function(shouldSendOffer) {
if (!shouldSendOffer) {
// TODO give back a reason
Expand Down Expand Up @@ -809,6 +809,16 @@ FarmerInterface.prototype.handleAlloc = function(params, callback) {
params.contact
);

if (self._contractCount < Number.MAX_SAFE_INTEGER) {
self._contractCount++;
} else {
self._contractCount = 0;
}

self._logger.info('Sending alloc response hash %s size %s',
contractObj.get('data_hash'),
contractObj.get('data_size'));

callback(null, { token: token, contract: contractObj.toObject() });

});
Expand Down
59 changes: 35 additions & 24 deletions test/network/farmer.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,22 @@ describe('FarmerInterface', function() {
done();
});

});

describe('#_listenForContracts', function() {

it('should call the #subscribe method with opcodes', function(done) {
var _subscribe = sinon.stub().callsArg(1);
FarmerInterface.prototype._listenForContracts.call({
subscribe: _subscribe,
_handleContractPublication: done
}, []);
});

});

describe('#handleAlloc', function() {

it('should reset contract count to 0 to prevent overflow', function(done) {
farmer = new FarmerInterface({
keyPair: KeyPair(),
Expand All @@ -968,36 +984,31 @@ describe('FarmerInterface', function() {
storagePath: tmpPath,
storageManager: new StorageManager(new RAMStorageAdapter())
});
var _load = sinon.stub(farmer.storageManager, 'load').callsArgWith(1, {});
var _save = sinon.stub(farmer.storageManager, 'save');
var _verify = sinon.stub(Contract.prototype, 'verify').returns(true);
var _shouldSendOffer = sinon.stub(farmer, '_shouldSendOffer')
.callsArgWith(1, true);
var _save = sinon.stub(farmer.storageManager, 'save')
.callsArgWith(1, null);
farmer._contractCount = Number.MAX_SAFE_INTEGER;
farmer._handleOfferRes({
result: {
contract: Contract({}).toObject()
farmer.handleAlloc({
contract: Contract({
renter_id: utils.rmd160('nodeid'),
data_hash: utils.rmd160('')
}).toObject(),
contact: {
address: '127.0.0.1',
port: 4001,
nodeID: utils.rmd160('')
}
}, new Contract(), {nodeID: 'nodeid'});
_load.restore();
_save.restore();
_verify.restore();
expect(farmer._contractCount).to.equal(0);
done();
});
});

describe('#_listenForContracts', function() {

it('should call the #subscribe method with opcodes', function(done) {
var _subscribe = sinon.stub().callsArg(1);
FarmerInterface.prototype._listenForContracts.call({
subscribe: _subscribe,
_handleContractPublication: done
}, []);
}, () => {
_shouldSendOffer.restore();
_save.restore();
expect(farmer._contractCount).to.equal(0);
done();
});
});

});


});

describe('FarmerInterface#Negotiator', function() {
Expand Down

0 comments on commit 4a62a0f

Please sign in to comment.