Block view.

This commit is contained in:
Simon Lindh
2019-11-12 16:39:59 +08:00
parent 2dbfa323fa
commit 4de8384708
24 changed files with 455 additions and 126 deletions

View File

@@ -5,9 +5,12 @@ export interface AbstractBitcoinApi {
getRawMempool(): Promise<ITransaction['txid'][]>;
getRawTransaction(txId: string): Promise<ITransaction>;
getBlockCount(): Promise<number>;
getBlock(hash: string): Promise<IBlock>;
getBlockAndTransactions(hash: string): Promise<IBlock>;
getBlockHash(height: number): Promise<string>;
getBlock(hash: string): Promise<IBlock>;
getBlockTransactions(hash: string): Promise<IBlock>;
getBlockTransactionsFromIndex(hash: string, index: number): Promise<IBlock>;
getBlocks(): Promise<string>;
getBlocksFromHeight(height: number): Promise<string>;
}

View File

@@ -59,7 +59,7 @@ class BitcoindApi implements AbstractBitcoinApi {
});
}
getBlock(hash: string, verbosity: 1 | 2 = 1): Promise<IBlock> {
getBlockAndTransactions(hash: string, verbosity: 1 | 2 = 1): Promise<IBlock> {
return new Promise((resolve, reject) => {
this.client.getBlock(hash, verbosity, (err: Error, block: IBlock) => {
if (err) {
@@ -81,6 +81,10 @@ class BitcoindApi implements AbstractBitcoinApi {
});
}
getBlock(hash: string): Promise<IBlock> {
throw new Error('Method not implemented.');
}
getBlocks(): Promise<string> {
throw new Error('Method not implemented.');
}
@@ -88,6 +92,14 @@ class BitcoindApi implements AbstractBitcoinApi {
getBlocksFromHeight(height: number): Promise<string> {
throw new Error('Method not implemented.');
}
getBlockTransactions(hash: string): Promise<IBlock> {
throw new Error('Method not implemented.');
}
getBlockTransactionsFromIndex(hash: string, index: number): Promise<IBlock> {
throw new Error('Method not implemented.');
}
}
export default BitcoindApi;

View File

@@ -65,7 +65,7 @@ class EsploraApi implements AbstractBitcoinApi {
});
}
getBlock(hash: string): Promise<IBlock> {
getBlockAndTransactions(hash: string): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
try {
const blockInfo: AxiosResponse = await this.client.get('/block/' + hash);
@@ -116,6 +116,39 @@ class EsploraApi implements AbstractBitcoinApi {
}
});
}
getBlock(hash: string): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
try {
const blockInfo: AxiosResponse = await this.client.get('/block/' + hash);
resolve(blockInfo.data);
} catch (error) {
reject(error);
}
});
}
getBlockTransactions(hash: string): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
try {
const blockInfo: AxiosResponse = await this.client.get('/block/' + hash + '/txs');
resolve(blockInfo.data);
} catch (error) {
reject(error);
}
});
}
getBlockTransactionsFromIndex(hash: string, index: number): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
try {
const blockInfo: AxiosResponse = await this.client.get('/block/' + hash + '/txs/' + index);
resolve(blockInfo.data);
} catch (error) {
reject(error);
}
});
}
}
export default EsploraApi;

View File

@@ -56,7 +56,7 @@ class Blocks {
block = storedBlock;
} else {
const blockHash = await bitcoinApi.getBlockHash(this.currentBlockHeight);
block = await bitcoinApi.getBlock(blockHash);
block = await bitcoinApi.getBlockAndTransactions(blockHash);
const coinbase = await memPool.getRawTransaction(block.tx[0], true);
if (coinbase && coinbase.totalOut) {

View File

@@ -120,7 +120,7 @@ class MempoolSpace {
console.log('Found block by looking in local cache');
client['blockHeight'] = foundBlock.height;
} else {
const theBlock = await bitcoinApi.getBlock(tx.blockhash);
const theBlock = await bitcoinApi.getBlockAndTransactions(tx.blockhash);
if (theBlock) {
client['blockHeight'] = theBlock.height;
}
@@ -269,6 +269,9 @@ class MempoolSpace {
.get(config.API_ENDPOINT + 'explorer/blocks', routes.getBlocks)
.get(config.API_ENDPOINT + 'explorer/blocks/:height', routes.getBlocks)
.get(config.API_ENDPOINT + 'explorer/tx/:id', routes.getRawTransaction)
.get(config.API_ENDPOINT + 'explorer/block/:hash', routes.getBlock)
.get(config.API_ENDPOINT + 'explorer/block/:hash/tx', routes.getBlockTransactions)
.get(config.API_ENDPOINT + 'explorer/block/:hash/tx/:index', routes.getBlockTransactionsFromIndex)
;
}

View File

@@ -99,6 +99,33 @@ class Routes {
res.status(500).send(e.message);
}
}
public async getBlock(req, res) {
try {
const result = await bitcoinApi.getBlock(req.params.hash);
res.send(result);
} catch (e) {
res.status(500).send(e.message);
}
}
public async getBlockTransactions(req, res) {
try {
const result = await bitcoinApi.getBlockTransactions(req.params.hash);
res.send(result);
} catch (e) {
res.status(500).send(e.message);
}
}
public async getBlockTransactionsFromIndex(req, res) {
try {
const result = await bitcoinApi.getBlockTransactionsFromIndex(req.params.hash, req.params.index);
res.send(result);
} catch (e) {
res.status(500).send(e.message);
}
}
}
export default new Routes();