Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix on pre-fork block hash. Extracted config to network #5

Merged
merged 2 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 44 additions & 18 deletions src/block_gold.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// https://github.com/BTCGPU/BTCGPU/wiki/Technical-Spec

var Buffer = require('safe-buffer').Buffer
var bcrypto = require('./crypto')
var varuint = require('varuint-bitcoin')
var networks = require('./networks')
var eq = require('equihashjs-verify')

var Transaction = require('./transaction')
Expand All @@ -20,7 +22,6 @@ function BlockGold () {
this.solutionLength = 0
this.solution = null
this.transactions = []
this.btgForkHeight = 491407
}

BlockGold.prototype = Object.create(Block.prototype)
Expand Down Expand Up @@ -85,9 +86,15 @@ BlockGold.fromBuffer = function (buffer) {
return block
}

BlockGold.prototype.byteLength = function (headersOnly) {
// Solution can have different size, for regtest/testnet is arround 140-170, for mainnet 1400-1500
var headerSize = 140 + varuint.encodingLength(this.solutionLength) + this.solution.length
BlockGold.prototype.byteLength = function (headersOnly, useLegacyFormat) {
var headerSize = 0
if (useLegacyFormat) {
headerSize = 80
} else {
// Solution can have different size, for regtest/testnet is arround 140-170, for mainnet 1400-1500
headerSize = 140 + varuint.encodingLength(this.solutionLength) + this.solution.length
}

if (headersOnly || !this.transactions) return headerSize

return headerSize + varuint.encodingLength(this.transactions.length) + this.transactions.reduce(function (a, x) {
Expand All @@ -99,9 +106,21 @@ BlockGold.fromHex = function (hex) {
return BlockGold.fromBuffer(Buffer.from(hex, 'hex'))
}

BlockGold.prototype.getHash = function (network) {
network = network || networks.bitcoingold
var useLegacyFormat = false

// Pre-Fork blocks
if (this.height < network.forkHeight) {
useLegacyFormat = true
}

return bcrypto.hash256(this.toBuffer(true, useLegacyFormat))
}

// TODO: buffer, offset compatibility
BlockGold.prototype.toBuffer = function (headersOnly) {
var buffer = Buffer.allocUnsafe(this.byteLength(headersOnly))
BlockGold.prototype.toBuffer = function (headersOnly, useLegacyFormat) {
var buffer = Buffer.allocUnsafe(this.byteLength(headersOnly, useLegacyFormat))

var offset = 0
function writeSlice (slice) {
Expand All @@ -121,14 +140,20 @@ BlockGold.prototype.toBuffer = function (headersOnly) {
writeInt32(this.version)
writeSlice(this.prevHash)
writeSlice(this.merkleRoot)
writeInt32(this.height)
writeSlice(this.reserved)
writeUInt32(this.timestamp)
writeUInt32(this.bits)
writeSlice(this.nonce)
varuint.encode(this.solutionLength, buffer, offset)
offset += varuint.encode.bytes
writeSlice(this.solution)
if (useLegacyFormat) {
writeUInt32(this.timestamp)
writeUInt32(this.bits)
writeUInt32(this.nonce.slice(0, 4).readUInt32LE())
} else {
writeInt32(this.height)
writeSlice(this.reserved)
writeUInt32(this.timestamp)
writeUInt32(this.bits)
writeSlice(this.nonce)
varuint.encode(this.solutionLength, buffer, offset)
offset += varuint.encode.bytes
writeSlice(this.solution)
}

if (headersOnly || !this.transactions) return buffer

Expand All @@ -144,11 +169,12 @@ BlockGold.prototype.toBuffer = function (headersOnly) {
return buffer
}

BlockGold.prototype.toHex = function (headersOnly) {
return this.toBuffer(headersOnly).toString('hex')
BlockGold.prototype.toHex = function (headersOnly, useLegacyFormat) {
return this.toBuffer(headersOnly, useLegacyFormat).toString('hex')
}

BlockGold.prototype.checkProofOfWork = function (validateSolution, network) {
network = network || networks.bitcoingold
var hash = this.getHash().reverse()
var target = Block.calculateTarget(this.bits)
var validTarget = hash.compare(target) <= 0
Expand All @@ -157,9 +183,9 @@ BlockGold.prototype.checkProofOfWork = function (validateSolution, network) {
return false
}

if (validateSolution && this.height >= this.btgForkHeight) {
if (validateSolution && this.height >= network.forkHeight) {
var header = this.toHex(true)
var equihash = new eq.Equihash(network || eq.networks.bitcoingold)
var equihash = new eq.Equihash(network.equihash || eq.networks.bitcoingold)
return equihash.verify(Buffer.from(header, 'hex'), this.solution)
} else {
return true
Expand Down
30 changes: 28 additions & 2 deletions src/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ module.exports = {
},
pubKeyHash: 0x26,
scriptHash: 0x17,
wif: 0x80
wif: 0x80,
forkHeight: 491407,
equihash: {
n: 200,
k: 9
}
},
bitcoingoldtestnet: {
messagePrefix: '\x1DBitcoin Gold Signed Message:\n',
Expand All @@ -22,7 +27,28 @@ module.exports = {
},
pubKeyHash: 0x6f,
scriptHash: 0xc4,
wif: 0xef
wif: 0xef,
forkHeight: 1,
equihash: {
n: 200,
k: 9
}
},
bitcoingoldregtest: {
messagePrefix: '\x1DBitcoin Gold Signed Message:\n',
bech32: 'tbtg',
bip32: {
public: 0x043587cf,
private: 0x04358394
},
pubKeyHash: 0x6f,
scriptHash: 0xc4,
wif: 0xef,
forkHeight: 2000,
equihash: {
n: 48,
k: 5
}
},
bitcoin: {
messagePrefix: '\x18Bitcoin Signed Message:\n',
Expand Down
16 changes: 16 additions & 0 deletions test/block_gold.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var assert = require('assert')
var BlockGold = require('../src/block_gold')
var networks = require('../src/networks')

var fixtures = require('./fixtures/block_gold')

Expand Down Expand Up @@ -71,4 +72,19 @@ describe('BlockGold', function () {
})
})
})

describe('getHash', function () {
fixtures.valid.forEach(function (f) {
var block

beforeEach(function () {
block = BlockGold.fromHex(f.hex)
})

it('returns ' + f.hash + ' for ' + f.description, function () {
var network = networks[f.network]
assert.strictEqual(block.getHash(network).reverse().toString('hex'), f.hash)
})
})
})
})
Loading