mirror of
https://github.com/Permissionless-Software-Foundation/bch-js.git
synced 2026-09-21 16:51:59 -07:00
Merge pull request #139 from Permissionless-Software-Foundation/dh-getblock
feat(getBlock): Added getBlock endpoint to bch-js
This commit is contained in:
+32
-22
@@ -68,8 +68,8 @@ class Blockchain {
|
|||||||
* @apiName getBlock
|
* @apiName getBlock
|
||||||
* @apiGroup Blockchain
|
* @apiGroup Blockchain
|
||||||
* @apiDescription
|
* @apiDescription
|
||||||
* If verbose is false, returns a string that is serialized, hex-encoded data for block 'hash'. If verbose is true, returns an Object with information about block hash.
|
* If verbose is 0, returns a string that is serialized, hex-encoded data for block 'hash'. If verbose is 1, returns an Object with information about block hash.
|
||||||
*
|
* If verbose is 2, returns an Object with information about block hash and information about tx.
|
||||||
* @apiExample Example usage:
|
* @apiExample Example usage:
|
||||||
* (async () => {
|
* (async () => {
|
||||||
* try {
|
* try {
|
||||||
@@ -80,28 +80,38 @@ class Blockchain {
|
|||||||
* }
|
* }
|
||||||
* })()
|
* })()
|
||||||
*
|
*
|
||||||
* // { hash: '00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09',
|
* // {
|
||||||
* // confirmations: 528236,
|
* // hash: '00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09',
|
||||||
* // size: 216,
|
* // confirmations: 528236,
|
||||||
* // height: 1000,
|
* // size: 216,
|
||||||
* // version: 1,
|
* // height: 1000,
|
||||||
* // versionHex: '00000001',
|
* // version: 1,
|
||||||
* // merkleroot: 'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33',
|
* // versionHex: '00000001',
|
||||||
* // tx:
|
* // merkleroot: 'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33',
|
||||||
* // [ 'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33' ],
|
* // tx:
|
||||||
* // time: 1232346882,
|
* // [ 'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33' ],
|
||||||
* // mediantime: 1232344831,
|
* // time: 1232346882,
|
||||||
* // nonce: 2595206198,
|
* // mediantime: 1232344831,
|
||||||
* // bits: '1d00ffff',
|
* // nonce: 2595206198,
|
||||||
* // difficulty: 1,
|
* // bits: '1d00ffff',
|
||||||
* // chainwork: '000000000000000000000000000000000000000000000000000003e903e903e9',
|
* // difficulty: 1,
|
||||||
* // previousblockhash: '0000000008e647742775a230787d66fdf92c46a48c896bfbc85cdc8acc67e87d',
|
* // chainwork: '000000000000000000000000000000000000000000000000000003e903e903e9',
|
||||||
* // nextblockhash: '00000000a2887344f8db859e372e7e4bc26b23b9de340f725afbf2edb265b4c6' }
|
* // previousblockhash: '0000000008e647742775a230787d66fdf92c46a48c896bfbc85cdc8acc67e87d',
|
||||||
|
* // nextblockhash: '00000000a2887344f8db859e372e7e4bc26b23b9de340f725afbf2edb265b4c6'
|
||||||
|
* // }
|
||||||
*/
|
*/
|
||||||
async getBlock (blockhash, verbose = true) {
|
async getBlock (blockhash, verbosity = 1) {
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(
|
// Input validation
|
||||||
`${this.restURL}blockchain/getBlock/${blockhash}?verbose=${verbose}`,
|
if (!blockhash || typeof blockhash !== 'string') {
|
||||||
|
throw new Error('blockhash must be a string')
|
||||||
|
}
|
||||||
|
const response = await axios.post(
|
||||||
|
`${this.restURL}blockchain/getblock`,
|
||||||
|
{
|
||||||
|
blockhash,
|
||||||
|
verbosity
|
||||||
|
},
|
||||||
_this.axiosOptions
|
_this.axiosOptions
|
||||||
)
|
)
|
||||||
return response.data
|
return response.data
|
||||||
|
|||||||
@@ -276,6 +276,43 @@ describe('#blockchain', () => {
|
|||||||
assert.equal(result, null)
|
assert.equal(result, null)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('#getBlock', () => {
|
||||||
|
it('should get block information with default verbosity', async () => {
|
||||||
|
const blockhash =
|
||||||
|
'0000000000000000008e8d83cba6d45a9314bc2ef4538d4e0577c6bed8593536'
|
||||||
|
const result = await bchjs.Blockchain.getBlock(blockhash)
|
||||||
|
|
||||||
|
assert.hasAllKeys(result, [
|
||||||
|
'hash',
|
||||||
|
'confirmations',
|
||||||
|
'size',
|
||||||
|
'height',
|
||||||
|
'version',
|
||||||
|
'versionHex',
|
||||||
|
'merkleroot',
|
||||||
|
'tx',
|
||||||
|
'time',
|
||||||
|
'mediantime',
|
||||||
|
'nonce',
|
||||||
|
'bits',
|
||||||
|
'difficulty',
|
||||||
|
'chainwork',
|
||||||
|
'nTx',
|
||||||
|
'previousblockhash',
|
||||||
|
'nextblockhash'
|
||||||
|
])
|
||||||
|
assert.isArray(result.tx)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should get block information with verbosity 0', async () => {
|
||||||
|
const blockhash =
|
||||||
|
'0000000000000000008e8d83cba6d45a9314bc2ef4538d4e0577c6bed8593536'
|
||||||
|
const verbosity = 0
|
||||||
|
const result = await bchjs.Blockchain.getBlock(blockhash, verbosity)
|
||||||
|
|
||||||
|
assert.isString(result)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
function sleep (ms) {
|
function sleep (ms) {
|
||||||
|
|||||||
+28
-7
@@ -61,16 +61,40 @@ describe('#Blockchain', () => {
|
|||||||
|
|
||||||
it('should get block by hash', done => {
|
it('should get block by hash', done => {
|
||||||
const resolved = new Promise(resolve => resolve({ data: data }))
|
const resolved = new Promise(resolve => resolve({ data: data }))
|
||||||
sandbox.stub(axios, 'get').returns(resolved)
|
sandbox.stub(axios, 'post').returns(resolved)
|
||||||
|
|
||||||
bchjs.Blockchain.getBlock(
|
const blockhash =
|
||||||
'00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09'
|
'00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09'
|
||||||
)
|
bchjs.Blockchain.getBlock(blockhash)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
assert.deepStrictEqual(data, result)
|
assert.deepStrictEqual(data, result)
|
||||||
})
|
})
|
||||||
.then(done, done)
|
.then(done, done)
|
||||||
})
|
})
|
||||||
|
it('should throw error if blockhash is not provided', async () => {
|
||||||
|
try {
|
||||||
|
await bchjs.Blockchain.getBlock()
|
||||||
|
assert2.fail('Unexpected result')
|
||||||
|
} catch (err) {
|
||||||
|
assert2.include(err.message, 'blockhash must be a string')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
it('should handle response error', async () => {
|
||||||
|
try {
|
||||||
|
const error = new Error()
|
||||||
|
error.response = {
|
||||||
|
data: 'Test Error'
|
||||||
|
}
|
||||||
|
sandbox.stub(axios, 'post').throws(error)
|
||||||
|
|
||||||
|
const blockhash =
|
||||||
|
'00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09'
|
||||||
|
await bchjs.Blockchain.getBlock(blockhash)
|
||||||
|
assert2.fail('Unexpected result')
|
||||||
|
} catch (err) {
|
||||||
|
assert2.include(err, 'Test Error')
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('#getBlockchainInfo', () => {
|
describe('#getBlockchainInfo', () => {
|
||||||
@@ -355,10 +379,7 @@ describe('#Blockchain', () => {
|
|||||||
try {
|
try {
|
||||||
await bchjs.Blockchain.getTxOut('badtxid')
|
await bchjs.Blockchain.getTxOut('badtxid')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
assert2.include(
|
assert2.include(err.message, 'txid needs to be a proper transaction ID')
|
||||||
err.message,
|
|
||||||
'txid needs to be a proper transaction ID'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user