From 0f684277785deac6204c7be93a9273060f21f79f Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Thu, 28 Sep 2017 22:47:50 -0400 Subject: [PATCH 1/3] Adding 'confirmations' property to TX getJSON Adds a 'confirmations' property to the JSON returned by cli tx [hash] as well as the REST '/tx/:hash' call by subtracting TX height from chain height --- lib/http/server.js | 6 +++--- lib/primitives/txmeta.js | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/http/server.js b/lib/http/server.js index e6b558a5..f2ff9332 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -210,7 +210,7 @@ HTTPServer.prototype.initRouter = function initRouter() { const view = await this.node.getMetaView(meta); - res.send(200, meta.getJSON(this.network, view)); + res.send(200, meta.getJSON(this.network, view, this.chain.height)); }); // TX by address @@ -226,7 +226,7 @@ HTTPServer.prototype.initRouter = function initRouter() { for (const meta of metas) { const view = await this.node.getMetaView(meta); - result.push(meta.getJSON(this.network, view)); + result.push(meta.getJSON(this.network, view, this.chain.height)); } res.send(200, result); @@ -245,7 +245,7 @@ HTTPServer.prototype.initRouter = function initRouter() { for (const meta of metas) { const view = await this.node.getMetaView(meta); - result.push(meta.getJSON(this.network, view)); + result.push(meta.getJSON(this.network, view, this.chain.height)); } res.send(200, result); diff --git a/lib/primitives/txmeta.js b/lib/primitives/txmeta.js index ec6e7f7f..6685e222 100644 --- a/lib/primitives/txmeta.js +++ b/lib/primitives/txmeta.js @@ -151,12 +151,14 @@ TXMeta.prototype.toJSON = function toJSON() { * @returns {Object} */ -TXMeta.prototype.getJSON = function getJSON(network, view) { +TXMeta.prototype.getJSON = function getJSON(network, view, chainheight) { const json = this.tx.getJSON(network, view, null, this.index); json.mtime = this.mtime; json.height = this.height; json.block = this.block ? util.revHex(this.block) : null; json.time = this.time; + if (chainheight) + json.confirmations = chainheight - this.height; return json; }; From c2cef86296d2f7e36ff120d5345680651fe6ddad Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Thu, 28 Sep 2017 23:41:13 -0400 Subject: [PATCH 2/3] Adding 'confirmations' property to block getJSON Adds a 'confirmations' property to the JSON returned by `cli tx [hash]` as well as the Rest `/tx/:hash` call by subtracting block height from chain height --- lib/http/server.js | 3 ++- lib/primitives/block.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/http/server.js b/lib/http/server.js index f2ff9332..23fb3d97 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -279,8 +279,9 @@ HTTPServer.prototype.initRouter = function initRouter() { } const height = await this.chain.getHeight(hash); + const confirmations = this.chain.height - height; - res.send(200, block.getJSON(this.network, view, height)); + res.send(200, block.getJSON(this.network, view, height, confirmations)); }); // Mempool snapshot diff --git a/lib/primitives/block.js b/lib/primitives/block.js index ae9c8918..9be79213 100644 --- a/lib/primitives/block.js +++ b/lib/primitives/block.js @@ -575,11 +575,12 @@ Block.prototype.toJSON = function toJSON() { * @returns {Object} */ -Block.prototype.getJSON = function getJSON(network, view, height) { +Block.prototype.getJSON = function getJSON(network, view, height, confirmations) { network = Network.get(network); return { hash: this.rhash(), height: height, + confirmations: confirmations, version: this.version, prevBlock: util.revHex(this.prevBlock), merkleRoot: util.revHex(this.merkleRoot), From 4f317992f5f2a3bd9a81f0870dd27db92fe82bde Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Mon, 2 Oct 2017 00:58:48 -0700 Subject: [PATCH 3/3] Fixes to TX & Block `confirmations` property TX `confirmations` property set to 0 for unconfirmed transactions. Block `confirmations` property set to -1 for orphaned blocks. Fixing to add +1 to `confirmations` to match bitcoind behavior and corresponding bcoin rpc methods - block or tx at tip of main chain starts with 1 confirmation (not zero). --- lib/http/server.js | 3 +-- lib/primitives/txmeta.js | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/http/server.js b/lib/http/server.js index 23fb3d97..374ee2e5 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -279,8 +279,7 @@ HTTPServer.prototype.initRouter = function initRouter() { } const height = await this.chain.getHeight(hash); - const confirmations = this.chain.height - height; - + res.send(200, block.getJSON(this.network, view, height, confirmations)); }); diff --git a/lib/primitives/txmeta.js b/lib/primitives/txmeta.js index 6685e222..9f9e92dc 100644 --- a/lib/primitives/txmeta.js +++ b/lib/primitives/txmeta.js @@ -157,8 +157,10 @@ TXMeta.prototype.getJSON = function getJSON(network, view, chainheight) { json.height = this.height; json.block = this.block ? util.revHex(this.block) : null; json.time = this.time; - if (chainheight) - json.confirmations = chainheight - this.height; + if (this.block === null) + json.confirmations = 0; + else + json.confirmations = chainheight - this.height + 1; return json; };