add tests to getTx
This commit is contained in:
parent
22a88e94fa
commit
ea24e093c0
|
@ -400,7 +400,7 @@ CopayServer.prototype._selectUtxos = function(txp, utxos) {
|
|||
CopayServer.prototype.createTx = function(opts, cb) {
|
||||
var self = this;
|
||||
|
||||
Utils.checkRequired(opts, ['toAddress', 'amount', 'message']);
|
||||
Utils.checkRequired(opts, ['toAddress', 'amount']);
|
||||
|
||||
|
||||
// TODO?
|
||||
|
@ -569,12 +569,26 @@ CopayServer.prototype.getPendingTxs = function(opts, cb) {
|
|||
self.storage.fetchPendingTxs(self.walletId, function(err, txps) {
|
||||
if (err) return cb(err);
|
||||
|
||||
var pending = _.filter(txps, function(txp) {
|
||||
return txp.isPending();
|
||||
return cb(null, txps);
|
||||
});
|
||||
return cb(null, pending);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves pending transaction proposals in the range (maxTs-minTs)
|
||||
* @param {Object} opts.minTs (defaults to 0)
|
||||
* @param {Object} opts.maxTs (defaults to now)
|
||||
* @param {Object} opts.limit
|
||||
* @returns {TxProposal[]} Transaction proposal.
|
||||
*/
|
||||
CopayServer.prototype.getTxs = function(opts, cb) {
|
||||
var self = this;
|
||||
self.storage.fetchTxs(self.walletId, opts, function(err, txps) {
|
||||
if (err) return cb(err);
|
||||
return cb(null, txps);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = CopayServer;
|
||||
|
|
|
@ -19,12 +19,17 @@ var Storage = function(opts) {
|
|||
};
|
||||
|
||||
|
||||
// WARN: Ts is supposed to have fixed with. Y33 problem :)
|
||||
|
||||
var opKey = function(key) {
|
||||
return key ? '!' + key : '';
|
||||
};
|
||||
|
||||
var MAX_TS = '999999999999';
|
||||
var opKeyTs = function(key) {
|
||||
return key ? '!' + ('000000000000'+key).slice(-12) : '';
|
||||
};
|
||||
|
||||
|
||||
|
||||
var KEY = {
|
||||
WALLET: function(id) {
|
||||
return 'wallet::' + id;
|
||||
|
@ -36,7 +41,7 @@ var KEY = {
|
|||
return 'txp!' + walletId + opKey(txProposalId);
|
||||
},
|
||||
TXP_BY_TS: function(walletId, ts, txProposalId) {
|
||||
return 'txp-ts!' + walletId + opKey(ts) + opKey(txProposalId);
|
||||
return 'txp-ts!' + walletId + opKeyTs(ts) + opKey(txProposalId);
|
||||
},
|
||||
PENDING_TXP_BY_TS: function(walletId, ts, txProposalId) {
|
||||
return 'pending-txp-ts!' + walletId + opKey(ts) + opKey(txProposalId);
|
||||
|
@ -101,7 +106,6 @@ Storage.prototype.fetchTx = function(walletId, txProposalId, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
|
||||
Storage.prototype.fetchPendingTxs = function(walletId, cb) {
|
||||
var txs = [];
|
||||
var key = KEY.PENDING_TXP_BY_TS(walletId);
|
||||
|
@ -121,12 +125,29 @@ Storage.prototype.fetchPendingTxs = function(walletId, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
Storage.prototype.fetchTxs = function(walletId, cb) {
|
||||
/**
|
||||
* fetchTxs
|
||||
*
|
||||
* @param walletId
|
||||
* @param opts.minTs
|
||||
* @param opts.maxTs
|
||||
* @param opts.limit
|
||||
*/
|
||||
Storage.prototype.fetchTxs = function(walletId, opts, cb) {
|
||||
var txs = [];
|
||||
var key = KEY.TXP(walletId);
|
||||
opts = opts || {};
|
||||
opts.limit = opts.limit || -1;
|
||||
opts.minTs = opts.minTs || 0;
|
||||
opts.maxTs = opts.maxTs || MAX_TS;
|
||||
|
||||
var key = KEY.TXP_BY_TS(walletId, opts.minTs);
|
||||
var endkey = KEY.TXP_BY_TS(walletId, opts.maxTs);
|
||||
|
||||
this.db.createReadStream({
|
||||
gte: key,
|
||||
lt: key + '~'
|
||||
gt: key,
|
||||
lt: endkey + '~',
|
||||
reverse: true,
|
||||
limit: opts.limit,
|
||||
})
|
||||
.on('data', function(data) {
|
||||
txs.push(TxProposal.fromObj(data.value));
|
||||
|
|
|
@ -213,21 +213,19 @@ describe('Copay server', function() {
|
|||
storage = new Storage({
|
||||
db: db
|
||||
});
|
||||
CopayServer.initialize({ storage: storage });
|
||||
CopayServer.initialize({
|
||||
storage: storage
|
||||
});
|
||||
});
|
||||
|
||||
describe.skip('#getInstanceWithAuth', function() {
|
||||
beforeEach(function() {
|
||||
});
|
||||
beforeEach(function() {});
|
||||
|
||||
it('should get server instance for existing copayer', function(done) {
|
||||
});
|
||||
it('should get server instance for existing copayer', function(done) {});
|
||||
|
||||
it('should fail when requesting for non-existent copayer', function(done) {
|
||||
});
|
||||
it('should fail when requesting for non-existent copayer', function(done) {});
|
||||
|
||||
it('should fail when message signature cannot be verified', function(done) {
|
||||
});
|
||||
it('should fail when message signature cannot be verified', function(done) {});
|
||||
});
|
||||
|
||||
describe('#createWallet', function() {
|
||||
|
@ -1003,7 +1001,9 @@ describe('Copay server', function() {
|
|||
server.getPendingTxs({}, function(err, txps) {
|
||||
should.not.exist(err);
|
||||
txps.length.should.equal(0);
|
||||
server.getTx({ id: txpid }, function (err, txp) {
|
||||
server.getTx({
|
||||
id: txpid
|
||||
}, function(err, txp) {
|
||||
txp.status.should.equal('accepted');
|
||||
should.not.exist(txp.txid);
|
||||
done();
|
||||
|
@ -1028,4 +1028,97 @@ describe('Copay server', function() {
|
|||
it.skip('proposal creator should be able to delete proposal if there are no other signatures', function (done) {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getTxs', function() {
|
||||
var server, wallet, clock;
|
||||
|
||||
beforeEach(function(done) {
|
||||
console.log('\tCreating TXS...');
|
||||
clock = sinon.useFakeTimers();
|
||||
helpers.createAndJoinWallet('123', 1, 1, function(s, w) {
|
||||
server = s;
|
||||
wallet = w;
|
||||
server.createAddress({
|
||||
isChange: false,
|
||||
}, function(err, address) {
|
||||
helpers.createUtxos(server, wallet, helpers.toSatoshi(_.range(10)), function(utxos) {
|
||||
helpers.stubBlockExplorer(server, utxos);
|
||||
var txOpts = {
|
||||
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
|
||||
amount: helpers.toSatoshi(0.1),
|
||||
};
|
||||
async.eachSeries(_.range(10), function(i, next) {
|
||||
clock.tick(10000);
|
||||
server.createTx(txOpts, function(err, tx) {
|
||||
next();
|
||||
});
|
||||
},
|
||||
done
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
afterEach(function() {
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it('should pull 4 txs, down to to time 60', function(done) {
|
||||
server.getTxs({
|
||||
minTs: 60,
|
||||
limit: 8
|
||||
}, function(err, txps) {
|
||||
should.not.exist(err);
|
||||
var times = _.pluck(txps,'createdOn');
|
||||
times.should.deep.equal([90,80,70,60]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should pull the first 5 txs', function(done) {
|
||||
server.getTxs({
|
||||
maxTs: 50,
|
||||
limit: 5
|
||||
}, function(err, txps) {
|
||||
should.not.exist(err);
|
||||
var times = _.pluck(txps,'createdOn');
|
||||
times.should.deep.equal([50,40,30,20,10]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should pull the last 4 txs', function(done) {
|
||||
server.getTxs({
|
||||
limit: 4
|
||||
}, function(err, txps) {
|
||||
should.not.exist(err);
|
||||
var times = _.pluck(txps,'createdOn');
|
||||
times.should.deep.equal([90,80,70,60]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should pull all txs', function(done) {
|
||||
server.getTxs({
|
||||
}, function(err, txps) {
|
||||
should.not.exist(err);
|
||||
var times = _.pluck(txps,'createdOn');
|
||||
times.should.deep.equal([90,80,70,60,50,40,30,20,10]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should txs from times 50 to 70', function(done) {
|
||||
server.getTxs({
|
||||
minTs: 50,
|
||||
maxTs: 70,
|
||||
}, function(err, txps) {
|
||||
should.not.exist(err);
|
||||
var times = _.pluck(txps,'createdOn');
|
||||
times.should.deep.equal([70,60,50]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue