Skip to content

Commit 7b11677

Browse files
afk11dcousens
authored andcommitted
Transaction and block versions are signed integers
1 parent 3de754a commit 7b11677

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/block.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ Block.fromBuffer = function (buffer) {
3131
return i
3232
}
3333

34+
function readInt32 () {
35+
var i = buffer.readInt32LE(offset)
36+
offset += 4
37+
return i
38+
}
39+
3440
var block = new Block()
35-
block.version = readUInt32()
41+
block.version = readInt32()
3642
block.prevHash = readSlice(32)
3743
block.merkleRoot = readSlice(32)
3844
block.timestamp = readUInt32()
@@ -93,12 +99,16 @@ Block.prototype.toBuffer = function (headersOnly) {
9399
offset += slice.length
94100
}
95101

102+
function writeInt32 (i) {
103+
buffer.writeInt32LE(i, offset)
104+
offset += 4
105+
}
96106
function writeUInt32 (i) {
97107
buffer.writeUInt32LE(i, offset)
98108
offset += 4
99109
}
100110

101-
writeUInt32(this.version)
111+
writeInt32(this.version)
102112
writeSlice(this.prevHash)
103113
writeSlice(this.merkleRoot)
104114
writeUInt32(this.timestamp)

src/transaction.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
3232
return i
3333
}
3434

35+
function readInt32 () {
36+
var i = buffer.readInt32LE(offset)
37+
offset += 4
38+
return i
39+
}
40+
3541
function readUInt64 () {
3642
var i = bufferutils.readUInt64LE(buffer, offset)
3743
offset += 8
@@ -49,7 +55,7 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
4955
}
5056

5157
var tx = new Transaction()
52-
tx.version = readUInt32()
58+
tx.version = readInt32()
5359

5460
var vinLen = readVarInt()
5561
for (var i = 0; i < vinLen; ++i) {
@@ -261,10 +267,11 @@ Transaction.prototype.toBuffer = function (buffer, initialOffset) {
261267
var offset = initialOffset || 0
262268
function writeSlice (slice) { offset += slice.copy(buffer, offset) }
263269
function writeUInt32 (i) { offset = buffer.writeUInt32LE(i, offset) }
270+
function writeInt32 (i) { offset = buffer.writeInt32LE(i, offset) }
264271
function writeUInt64 (i) { offset = bufferutils.writeUInt64LE(buffer, i, offset) }
265272
function writeVarInt (i) { offset += bufferutils.writeVarInt(buffer, i, offset) }
266273

267-
writeUInt32(this.version)
274+
writeInt32(this.version)
268275
writeVarInt(this.ins.length)
269276

270277
this.ins.forEach(function (txIn) {

test/block.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ var Block = require('../src/block')
66
var fixtures = require('./fixtures/block')
77

88
describe('Block', function () {
9+
describe('version', function () {
10+
it('should be interpreted as an int32le', function () {
11+
var blockHex = 'ffffffff0000000000000000000000000000000000000000000000000000000000000000414141414141414141414141414141414141414141414141414141414141414101000000020000000300000000'
12+
var block = Block.fromHex(blockHex)
13+
assert.equal(-1, block.version)
14+
assert.equal(1, block.timestamp)
15+
})
16+
})
17+
918
describe('calculateTarget', function () {
1019
fixtures.targets.forEach(function (f) {
1120
it('returns ' + f.expected + ' for 0x' + f.bits, function () {

test/transaction.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ describe('Transaction', function () {
8787
})
8888
})
8989

90+
describe('transactionVersion', function () {
91+
it('should be interpreted as an int32le', function () {
92+
var txHex = 'ffffffff0000ffffffff'
93+
var tx = Transaction.fromHex(txHex)
94+
assert.equal(-1, tx.version)
95+
assert.equal(0xffffffff, tx.locktime)
96+
})
97+
})
98+
9099
describe('addInput', function () {
91100
var prevTxHash
92101
beforeEach(function () {

0 commit comments

Comments
 (0)