sed -i 's/ var / const /', with const->let fixes

This commit is contained in:
Daniel Cousens 2018-06-25 16:37:45 +10:00
parent 91b8823aa8
commit a5db0a4e44
46 changed files with 776 additions and 769 deletions

View file

@ -19,25 +19,25 @@ function Block () {
Block.fromBuffer = function (buffer) {
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)')
var offset = 0
let offset = 0
function readSlice (n) {
offset += n
return buffer.slice(offset - n, offset)
}
function readUInt32 () {
var i = buffer.readUInt32LE(offset)
const i = buffer.readUInt32LE(offset)
offset += 4
return i
}
function readInt32 () {
var i = buffer.readInt32LE(offset)
const i = buffer.readInt32LE(offset)
offset += 4
return i
}
var block = new Block()
const block = new Block()
block.version = readInt32()
block.prevHash = readSlice(32)
block.merkleRoot = readSlice(32)
@ -48,22 +48,22 @@ Block.fromBuffer = function (buffer) {
if (buffer.length === 80) return block
function readVarInt () {
var vi = varuint.decode(buffer, offset)
const vi = varuint.decode(buffer, offset)
offset += varuint.decode.bytes
return vi
}
function readTransaction () {
var tx = Transaction.fromBuffer(buffer.slice(offset), true)
const tx = Transaction.fromBuffer(buffer.slice(offset), true)
offset += tx.byteLength()
return tx
}
var nTransactions = readVarInt()
const nTransactions = readVarInt()
block.transactions = []
for (var i = 0; i < nTransactions; ++i) {
var tx = readTransaction()
const tx = readTransaction()
block.transactions.push(tx)
}
@ -91,7 +91,7 @@ Block.prototype.getId = function () {
}
Block.prototype.getUTCDate = function () {
var date = new Date(0) // epoch
const date = new Date(0) // epoch
date.setUTCSeconds(this.timestamp)
return date
@ -99,9 +99,9 @@ Block.prototype.getUTCDate = function () {
// TODO: buffer, offset compatibility
Block.prototype.toBuffer = function (headersOnly) {
var buffer = Buffer.allocUnsafe(this.byteLength(headersOnly))
const buffer = Buffer.allocUnsafe(this.byteLength(headersOnly))
var offset = 0
let offset = 0
function writeSlice (slice) {
slice.copy(buffer, offset)
offset += slice.length
@ -129,7 +129,7 @@ Block.prototype.toBuffer = function (headersOnly) {
offset += varuint.encode.bytes
this.transactions.forEach(function (tx) {
var txSize = tx.byteLength() // TODO: extract from toBuffer?
const txSize = tx.byteLength() // TODO: extract from toBuffer?
tx.toBuffer(buffer, offset)
offset += txSize
})
@ -142,9 +142,9 @@ Block.prototype.toHex = function (headersOnly) {
}
Block.calculateTarget = function (bits) {
var exponent = ((bits & 0xff000000) >> 24) - 3
var mantissa = bits & 0x007fffff
var target = Buffer.alloc(32, 0)
const exponent = ((bits & 0xff000000) >> 24) - 3
const mantissa = bits & 0x007fffff
const target = Buffer.alloc(32, 0)
target.writeUInt32BE(mantissa, 28 - exponent)
return target
}
@ -153,7 +153,7 @@ Block.calculateMerkleRoot = function (transactions) {
typeforce([{ getHash: types.Function }], transactions)
if (transactions.length === 0) throw TypeError('Cannot compute merkle root for zero transactions')
var hashes = transactions.map(function (transaction) {
const hashes = transactions.map(function (transaction) {
return transaction.getHash()
})
@ -163,13 +163,13 @@ Block.calculateMerkleRoot = function (transactions) {
Block.prototype.checkMerkleRoot = function () {
if (!this.transactions) return false
var actualMerkleRoot = Block.calculateMerkleRoot(this.transactions)
const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions)
return this.merkleRoot.compare(actualMerkleRoot) === 0
}
Block.prototype.checkProofOfWork = function () {
var hash = this.getHash().reverse()
var target = Block.calculateTarget(this.bits)
const hash = this.getHash().reverse()
const target = Block.calculateTarget(this.bits)
return hash.compare(target) <= 0
}