Merge pull request #1324 from zmanian/SetLockTimeSeqNumber

Fix SequenceNumber for nLockTime transactions
This commit is contained in:
Braydon Fuller 2015-08-28 16:30:26 -04:00
commit 721f54f965
3 changed files with 51 additions and 0 deletions

View File

@ -13,6 +13,7 @@ var Output = require('../output');
var DEFAULT_SEQNUMBER = 0xFFFFFFFF;
var DEFAULT_LOCKTIME_SEQNUMBER = 0x00000000;
function Input(params) {
if (!(this instanceof Input)) {
@ -24,6 +25,7 @@ function Input(params) {
}
Input.DEFAULT_SEQNUMBER = DEFAULT_SEQNUMBER;
Input.DEFAULT_LOCKTIME_SEQNUMBER = DEFAULT_LOCKTIME_SEQNUMBER;
Object.defineProperty(Input.prototype, 'script', {
configurable: false,

View File

@ -414,6 +414,13 @@ Transaction.prototype.lockUntilDate = function(time) {
if (_.isDate(time)) {
time = time.getTime() / 1000;
}
for (var i = 0; i < this.inputs.length; i++) {
if (this.inputs[i].sequenceNumber === Input.DEFAULT_SEQNUMBER){
this.inputs[i].sequenceNumber = Input.DEFAULT_LOCKTIME_SEQNUMBER;
}
}
this.nLockTime = time;
return this;
};
@ -433,6 +440,14 @@ Transaction.prototype.lockUntilBlockHeight = function(height) {
if (height < 0) {
throw new errors.Transaction.NLockTimeOutOfRange();
}
for (var i = 0; i < this.inputs.length; i++) {
if (this.inputs[i].sequenceNumber === Input.DEFAULT_SEQNUMBER){
this.inputs[i].sequenceNumber = Input.DEFAULT_LOCKTIME_SEQNUMBER;
}
}
this.nLockTime = height;
return this;
};

View File

@ -748,6 +748,40 @@ describe('Transaction', function() {
return new Transaction().lockUntilBlockHeight(-1);
}).to.throw(errors.Transaction.NLockTimeOutOfRange);
});
it('has a non-max sequenceNumber for effective date locktime tx', function() {
var transaction = new Transaction()
.from(simpleUtxoWith1BTC)
.lockUntilDate(date);
transaction.inputs[0].sequenceNumber
.should.equal(Transaction.Input.DEFAULT_LOCKTIME_SEQNUMBER);
});
it('has a non-max sequenceNumber for effective blockheight locktime tx', function() {
var transaction = new Transaction()
.from(simpleUtxoWith1BTC)
.lockUntilBlockHeight(blockHeight);
transaction.inputs[0].sequenceNumber
.should.equal(Transaction.Input.DEFAULT_LOCKTIME_SEQNUMBER);
});
it('should serialize correctly for date locktime ', function() {
var transaction= new Transaction()
.from(simpleUtxoWith1BTC)
.lockUntilDate(date);
var serialized_tx = transaction.uncheckedSerialize();
var copy = new Transaction(serialized_tx);
serialized_tx.should.equal(copy.uncheckedSerialize());
copy.inputs[0].sequenceNumber
.should.equal(Transaction.Input.DEFAULT_LOCKTIME_SEQNUMBER)
});
it('should serialize correctly for a block height locktime', function() {
var transaction= new Transaction()
.from(simpleUtxoWith1BTC)
.lockUntilBlockHeight(blockHeight);
var serialized_tx = transaction.uncheckedSerialize();
var copy = new Transaction(serialized_tx);
serialized_tx.should.equal(copy.uncheckedSerialize());
copy.inputs[0].sequenceNumber
.should.equal(Transaction.Input.DEFAULT_LOCKTIME_SEQNUMBER)
});
});
it('handles anyone-can-spend utxo', function() {