Files
bch-js/test/unit/blockchain.js
T

498 lines
14 KiB
JavaScript
Raw Normal View History

2021-01-02 15:41:12 -08:00
const assert = require('assert')
const assert2 = require('chai').assert
const axios = require('axios')
const BCHJS = require('../../src/bch-js')
2020-07-27 21:32:51 -07:00
const bchjs = new BCHJS()
2021-01-02 15:41:12 -08:00
const sinon = require('sinon')
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
const mockData = require('./fixtures/blockchain-mock')
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
describe('#Blockchain', () => {
describe('#getBestBlockHash', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
2021-01-02 15:41:12 -08:00
it('should get best block hash', done => {
const resolved = new Promise(resolve =>
resolve({
2020-07-27 21:32:51 -07:00
data:
2021-01-02 15:41:12 -08:00
'0000000000000000005f1f550d3d8b142b684277016ebd00fa29c668606ae52d'
2020-07-27 21:32:51 -07:00
})
)
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getBestBlockHash()
.then(result => {
const hash =
2021-01-02 15:41:12 -08:00
'0000000000000000005f1f550d3d8b142b684277016ebd00fa29c668606ae52d'
assert.strictEqual(hash, result)
2020-07-27 21:32:51 -07:00
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getBlock', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = {
2021-01-02 15:41:12 -08:00
hash: '00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09',
2020-07-27 21:32:51 -07:00
confirmations: 526807,
size: 216,
height: 1000,
version: 1,
2021-01-02 15:41:12 -08:00
versionHex: '00000001',
2020-07-27 21:32:51 -07:00
merkleroot:
2021-01-02 15:41:12 -08:00
'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33',
tx: ['fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33'],
2020-07-27 21:32:51 -07:00
time: 1232346882,
mediantime: 1232344831,
nonce: 2595206198,
2021-01-02 15:41:12 -08:00
bits: '1d00ffff',
2020-07-27 21:32:51 -07:00
difficulty: 1,
chainwork:
2021-01-02 15:41:12 -08:00
'000000000000000000000000000000000000000000000000000003e903e903e9',
2020-07-27 21:32:51 -07:00
previousblockhash:
2021-01-02 15:41:12 -08:00
'0000000008e647742775a230787d66fdf92c46a48c896bfbc85cdc8acc67e87d',
2020-07-27 21:32:51 -07:00
nextblockhash:
2021-01-02 15:41:12 -08:00
'00000000a2887344f8db859e372e7e4bc26b23b9de340f725afbf2edb265b4c6'
2020-07-27 21:32:51 -07:00
}
2021-01-02 15:41:12 -08:00
it('should get block by hash', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getBlock(
2021-01-02 15:41:12 -08:00
'00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09'
2020-07-27 21:32:51 -07:00
)
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getBlockchainInfo', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = {
2021-01-02 15:41:12 -08:00
chain: 'main',
2020-07-27 21:32:51 -07:00
blocks: 527810,
headers: 527810,
bestblockhash:
2021-01-02 15:41:12 -08:00
'000000000000000001d127592d091d4c45062504663c9acab27a1b16c028e3c0',
2020-07-27 21:32:51 -07:00
difficulty: 576023394804.6666,
mediantime: 1524878499,
verificationprogress: 0.9999990106793685,
chainwork:
2021-01-02 15:41:12 -08:00
'00000000000000000000000000000000000000000096da5b040913fa09249b4e',
2020-07-27 21:32:51 -07:00
pruned: false,
softforks: [
2021-01-02 15:41:12 -08:00
{ id: 'bip34', version: 2, reject: [Object] },
{ id: 'bip66', version: 3, reject: [Object] },
{ id: 'bip65', version: 4, reject: [Object] }
2020-07-27 21:32:51 -07:00
],
bip9_softforks: {
csv: {
2021-01-02 15:41:12 -08:00
status: 'active',
2020-07-27 21:32:51 -07:00
startTime: 1462060800,
timeout: 1493596800,
since: 419328
}
}
}
2021-01-02 15:41:12 -08:00
it('should get blockchain info', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getBlockchainInfo()
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getBlockCount', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = 527810
2021-01-02 15:41:12 -08:00
it('should get block count', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getBlockCount()
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getBlockHash', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data =
2021-01-02 15:41:12 -08:00
'000000000000000001d127592d091d4c45062504663c9acab27a1b16c028e3c0'
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should get block hash by height', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getBlockHash(527810)
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getBlockHeader', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = {
2021-01-02 15:41:12 -08:00
hash: '000000000000000001d127592d091d4c45062504663c9acab27a1b16c028e3c0',
2020-07-27 21:32:51 -07:00
confirmations: 1,
height: 527810,
version: 536870912,
2021-01-02 15:41:12 -08:00
versionHex: '20000000',
2020-07-27 21:32:51 -07:00
merkleroot:
2021-01-02 15:41:12 -08:00
'9298432bbebe4638456aa19cb7ef91639da87668a285d88d0ecd6080424d223b',
2020-07-27 21:32:51 -07:00
time: 1524881438,
mediantime: 1524878499,
nonce: 3326843941,
2021-01-02 15:41:12 -08:00
bits: '1801e8a5',
2020-07-27 21:32:51 -07:00
difficulty: 576023394804.6666,
chainwork:
2021-01-02 15:41:12 -08:00
'00000000000000000000000000000000000000000096da5b040913fa09249b4e',
2020-07-27 21:32:51 -07:00
previousblockhash:
2021-01-02 15:41:12 -08:00
'000000000000000000b33251708bc7a7b4540e61880d8c376e8e2db6a19a4789'
2020-07-27 21:32:51 -07:00
}
2021-01-02 15:41:12 -08:00
it('should get block header by hash', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getBlockHeader(
2021-01-02 15:41:12 -08:00
'000000000000000001d127592d091d4c45062504663c9acab27a1b16c028e3c0',
2020-07-27 21:32:51 -07:00
true
)
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getDifficulty', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
2021-01-02 15:41:12 -08:00
const data = '577528469277.1339'
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should get difficulty', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getDifficulty()
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getMempoolAncestors', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
2021-01-02 15:41:12 -08:00
const data = 'Transaction not in mempool'
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should get mempool ancestors', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getMempoolAncestors(
2021-01-02 15:41:12 -08:00
'daf58932cb91619304dd4cbd03c7202e89ad7d6cbd6e2209e5f64ce3b6ed7c88',
2020-07-27 21:32:51 -07:00
true
)
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getMempoolDescendants', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = {
2021-01-02 15:41:12 -08:00
result: 'Transaction not in mempool'
2020-07-27 21:32:51 -07:00
}
2021-01-02 15:41:12 -08:00
it('should get mempool descendants', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getMempoolDescendants(
2021-01-02 15:41:12 -08:00
'daf58932cb91619304dd4cbd03c7202e89ad7d6cbd6e2209e5f64ce3b6ed7c88',
2020-07-27 21:32:51 -07:00
true
)
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getMempoolEntry', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = {
2021-01-02 15:41:12 -08:00
result: 'Transaction not in mempool'
2020-07-27 21:32:51 -07:00
}
2021-01-02 15:41:12 -08:00
it('should get mempool entry', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getMempoolEntry(
2021-01-02 15:41:12 -08:00
'daf58932cb91619304dd4cbd03c7202e89ad7d6cbd6e2209e5f64ce3b6ed7c88'
2020-07-27 21:32:51 -07:00
)
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getMempoolInfo', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = {
result: {
size: 317,
bytes: 208583,
usage: 554944,
maxmempool: 300000000,
mempoolminfee: 0
}
}
2021-01-02 15:41:12 -08:00
it('should get mempool info', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getMempoolInfo()
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getRawMempool', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = {
result: {
transactions: [
{
txid:
2021-01-02 15:41:12 -08:00
'ab36d68dd0a618592fe34e4a898e8beeeb4049133547dbb16f9338384084af96',
2020-07-27 21:32:51 -07:00
size: 191,
fee: 0.00047703,
modifiedfee: 0.00047703,
time: 1524883317,
height: 527811,
startingpriority: 5287822727.272727,
currentpriority: 5287822727.272727,
descendantcount: 1,
descendantsize: 191,
descendantfees: 47703,
ancestorcount: 1,
ancestorsize: 191,
ancestorfees: 47703,
depends: []
}
]
}
}
2021-01-02 15:41:12 -08:00
it('should get mempool info', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.getRawMempool()
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#getTxOut', () => {
2020-07-27 21:32:51 -07:00
// TODO finish this test
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
// const data = {
// result: {}
// }
2021-01-02 15:41:12 -08:00
it('should throw an error for improper txid.', async () => {
2020-07-27 21:32:51 -07:00
try {
2021-01-02 15:41:12 -08:00
await bchjs.Blockchain.getTxOut('badtxid')
2020-07-27 21:32:51 -07:00
} catch (err) {
2021-01-02 15:41:12 -08:00
assert2.include(
err.message,
'txid needs to be a proper transaction ID'
)
2020-07-27 21:32:51 -07:00
}
})
2021-01-02 15:41:12 -08:00
it('should throw an error if no vout value is provided.', async () => {
2020-07-27 21:32:51 -07:00
try {
await bchjs.Blockchain.getTxOut(
2021-01-02 15:41:12 -08:00
'daf58932cb91619304dd4cbd03c7202e89ad7d6cbd6e2209e5f64ce3b6ed7c88'
2020-07-27 21:32:51 -07:00
)
} catch (err) {
2021-01-02 15:41:12 -08:00
assert2.include(err.message, 'n must be an integer')
2020-07-27 21:32:51 -07:00
}
})
2021-01-02 15:41:12 -08:00
it('should throw an error if include_mempool is not a boolean', async () => {
2020-07-27 21:32:51 -07:00
try {
await bchjs.Blockchain.getTxOut(
2021-01-02 15:41:12 -08:00
'daf58932cb91619304dd4cbd03c7202e89ad7d6cbd6e2209e5f64ce3b6ed7c88',
2020-07-27 21:32:51 -07:00
0,
2021-01-02 15:41:12 -08:00
'bad value'
2020-07-27 21:32:51 -07:00
)
} catch (err) {
assert2.include(
err.message,
2021-01-02 15:41:12 -08:00
'includeMempool input must be of type boolean'
2020-07-27 21:32:51 -07:00
)
}
})
2021-01-02 15:41:12 -08:00
it('should get information on an unspent tx', async () => {
sandbox.stub(axios, 'post').resolves({ data: mockData.txOutUnspent })
2020-07-27 21:32:51 -07:00
const result = await bchjs.Blockchain.getTxOut(
2021-01-02 15:41:12 -08:00
'62a3ea958a463a372bc0caf2c374a7f60be9c624be63a0db8db78f05809df6d8',
2020-07-27 21:32:51 -07:00
0,
true
)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert2.hasAllKeys(result, [
2021-01-02 15:41:12 -08:00
'bestblock',
'confirmations',
'value',
'scriptPubKey',
'coinbase'
2020-07-27 21:32:51 -07:00
])
})
2021-01-02 15:41:12 -08:00
it('should get information on a spent tx', async () => {
sandbox.stub(axios, 'post').resolves({ data: null })
2020-07-27 21:32:51 -07:00
const result = await bchjs.Blockchain.getTxOut(
2021-01-02 15:41:12 -08:00
'87380e52d151856b23173d6d8a3db01b984c6b50f77ea045a5a1cf4f54497871',
2020-07-27 21:32:51 -07:00
0,
true
)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert2.equal(result, null)
})
})
2021-01-02 15:41:12 -08:00
describe('#preciousBlock', () => {
2020-07-27 21:32:51 -07:00
// TODO finish this test
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = {
result: {}
}
2021-01-02 15:41:12 -08:00
it('should get TODO', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.preciousBlock()
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#pruneBlockchain', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
2021-01-02 15:41:12 -08:00
const data = 'Cannot prune blocks because node is not in prune mode.'
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should prune blockchain', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'post').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.pruneBlockchain(507)
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#verifyChain', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = true
2021-01-02 15:41:12 -08:00
it('should verify blockchain', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Blockchain.verifyChain(3, 6)
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
2021-01-02 15:41:12 -08:00
describe('#verifyTxOutProof', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
const data = "proof must be hexadecimal string (not '')"
2021-01-02 15:41:12 -08:00
it('should verify utxo proof', done => {
2020-07-27 21:32:51 -07:00
const resolved = new Promise(r => r({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
bchjs.Blockchain.verifyTxOutProof('3')
2020-07-27 21:32:51 -07:00
.then(result => {
assert.deepEqual(data, result)
})
.then(done, done)
})
})
})