Skip to content

Conversation

dougEfresh
Copy link
Contributor

@dougEfresh dougEfresh commented Oct 20, 2021

Add fee response in BTC to getrawtransaction #23264

For Reviewers

bitcoin-cli -chain=test getrawtransaction 9ae533f7da9be4a34997db78343a8d8d6d6186b6bba3959e56f416a5c70e7de4 2 000000000000001d442e556146d5f2841d85150c200e8d8b8a4b5005b13878f6
  "in_active_chain": true,
  "txid": "9ae533f7da9be4a34997db78343a8d8d6d6186b6bba3959e56f416a5c70e7de4",
  "hash": "7f23e3f3a0a256ddea1d35ffd43e9afdd67cc68389ef1a804bb20c76abd6863e",
 ....
  "vin": [
    {
      "txid": "23fc75d6d74f6f97e225839af69ff36a612fe04db58a4414ec4828d1749a05a0",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "prevout": {
        "generated": false,
        "height": 2099486,
        "value": 0.00017764,
        "scriptPubKey": {
          "asm": "0 7846ce1ced3253d8bd43008db2ca364cc722f5a2",
          "hex": "00147846ce1ced3253d8bd43008db2ca364cc722f5a2",
          "address": "tb1q0prvu88dxffa302rqzxm9j3kfnrj9adzk49mlp",
          "type": "witness_v0_keyhash"
        }
      },
      "sequence": 4294967295
    },
...
 "fee": 0.00000762
}

@fanquake fanquake linked an issue Oct 20, 2021 that may be closed by this pull request
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@dougEfresh dougEfresh force-pushed the 23264-fee-getrawtransaction branch from 2730b68 to 2598983 Compare October 20, 2021 12:39
@dougEfresh dougEfresh force-pushed the 23264-fee-getrawtransaction branch from 2598983 to d3f084a Compare October 20, 2021 13:51
Copy link
Contributor

@lsilva01 lsilva01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK.

It would be interesting to add a functional test case.

@dougEfresh dougEfresh force-pushed the 23264-fee-getrawtransaction branch from d3f084a to 2e8ce71 Compare October 20, 2021 18:47
@maflcko
Copy link
Member

maflcko commented Oct 20, 2021

I think this has been said on IRC already: Shouldn't this be hidden behind a flag like the RPC it is copied from?

@dougEfresh
Copy link
Contributor Author

Concept ACK.

It would be interesting to add a functional test case.

Agreed, wanted to discuss the implementation before diving in.
Anything special I should know about testing ./test/functional/rpc_rawtransaction.py ? This is my 1st time doing functional test.

@dougEfresh
Copy link
Contributor Author

I think this has been said on IRC already: Shouldn't this be hidden behind a flag like the RPC it is copied from?

To confirm we want the second (hybrid) parameter to have three verbosity levels:

  • 0 or false - hex encoded
  • 1 or true - returns an Object with information about txid
  • 2 - adds fee field to Object

@maflcko
Copy link
Member

maflcko commented Oct 20, 2021

Yes, if that is the behavior of the other RPC, it makes sense to copy it.

@sipa
Copy link
Member

sipa commented Oct 20, 2021

You might want to also add the UTXOs being spent by the transaction. You're looking those up anyway.

@maflcko
Copy link
Member

maflcko commented Oct 21, 2021

The tests can also be taken from the other RPC: https://github.com/bitcoin/bitcoin/pull/22918/files

@dougEfresh dougEfresh force-pushed the 23264-fee-getrawtransaction branch 2 times, most recently from 6c61582 to 83e5f6c Compare October 21, 2021 20:31
@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 25, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK aureleoules, hernanmarino, pablomartin4btc, achow101
Concept ACK MarcoFalke, lsilva01, jonatack
Stale ACK stickies-v, rajarshimaitra

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #26039 (refactor: Run type check against RPCArgs (1/2) by MarcoFalke)
  • #19262 (rpc: Replace OMITTED_NAMED_ARG with OMITTED by MarcoFalke)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@dougEfresh
Copy link
Contributor Author

You might want to also add the UTXOs being spent by the transaction. You're looking those up anyway.

@sipa Can you clarify a bit more on add the UTXOs being spent ? Specifically, what you like to add to the response body?

@sipa
Copy link
Member

sipa commented Oct 28, 2021

@dougEfresh The scriptPubKey and amount being spent for each input (together with information about the scriptPubKey like the output already has).

@maflcko
Copy link
Member

maflcko commented Oct 29, 2021

Showing the utxos being spent is also being done by the other RPC. Any questions you might have should be answered by looking at the code changes of https://github.com/bitcoin/bitcoin/pull/22918/files

@dougEfresh dougEfresh force-pushed the 23264-fee-getrawtransaction branch 2 times, most recently from 2051110 to 2c56d72 Compare October 29, 2021 11:42
@dougEfresh dougEfresh changed the title rpc: Return fee in getrawtransaction rpc: Return fee and prevout (utxos) to getrawtransaction Oct 29, 2021
@dougEfresh
Copy link
Contributor Author

Showing the utxos being spent is also being done by the other RPC. Any questions you might have should be answered by looking at the code changes of https://github.com/bitcoin/bitcoin/pull/22918/files

thanks, added utxos with TxVerbosity::SHOW_DETAILS_AND_PREVOUT

My latest change includes

  • test for prevout (utxo)
  • release documentation
  • consolidate if statement in ./src/rpc/rawtransaction.cpp getrawtransaction
    if (verbosity == 1 || !blockindex || IsBlockPruned(blockindex) ....
    

assert_equal(gottx['in_active_chain'], True)
if v == 2:
# fee is in verbosity 2 only
assert_equal(gottx['fee'], Decimal('0.00002820'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is supposed to guarantee this is correct?

Copy link
Contributor Author

@dougEfresh dougEfresh Oct 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value of fee is not important. I want to see that the field fee exists. Change it to assert('fee' in gottxt) ?

There are another tests (outside the scope of this PR) which validate the fee amount.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do tx_fee = -self.nodes[2].gettransaction(tx)['fee'] immediately after sending and compare to that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No fee field in gettransaction .
I added assert('fee' in gottxt) but kept assert_equal(gottx['fee'], Decimal('0.00002820'))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is fee in getransaction. see https://developer.bitcoin.org/reference/rpc/gettransaction.html

I also agree with @luke-jr . There is no guarantee that fee calculation will always remain same, and thus better not to assert against hard coded values.

Copy link
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strong ReACK eae8f8e

Its been almost an year since this PR is standing and its quite useful for wallet devs for obvious reasons.. Thanks to the PR author for chugging along..

I did a re-pass and have following non blocking observation regarding a specific blockhash- verbosity-txindex combination.

const bool is_block_pruned{WITH_LOCK(cs_main, return chainman.m_blockman.IsBlockPruned(blockindex))};

if (verbosity == 1 || tx->IsCoinBase() ||
!blockindex || is_block_pruned ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems if verbosity==2 and !blockindex this will trigger and we won't get the fee and prevout fields even when g_txindex is available.. Is this expected behavior?

Copy link
Contributor Author

@dougEfresh dougEfresh Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. I have refactored the code to detect if blockhash was sent as a parameter.
What should we expect if the blockhash is not sent and the tx is in mempool? Should the fee field be available? (I would think so)


static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry, Chainstate& active_chainstate)
static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry, Chainstate& active_chainstate, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_TXID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry, Chainstate& active_chainstate, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_TXID)
static void TxToJSON(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, Chainstate& active_chainstate, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_TXID)

@dougEfresh dougEfresh force-pushed the 23264-fee-getrawtransaction branch 3 times, most recently from 845913f to 226b0b7 Compare October 10, 2022 07:58
Copy link

@vostrnad vostrnad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiled and manually tested.

The RPC help result object could use some improvement. Right now the section is rendered like this:

"prevout" : {                  (json object)
  "generated" : true|false,    (boolean)
  "value" : n,                 (numeric) The value in BTC
  "height" : n,                (numeric) previous block height
  "scriptPubKey" : {           (json object)
    "asm" : "str",             (string) the asm
    "desc" : "str",            (string) desc
    "hex" : "str",             (string) the hex
    "address" : "str",         (string, optional) The Bitcoin address (only if a well-defined address exists)
    "type" : "str"             (string) The type, eg 'pubkeyhash'
  }
}

I guess there's no reason it should look much different to what's in getblock for verbosity=3? For reference:

"prevout" : {                  (json object) (Only if undo information is available)
  "generated" : true|false,    (boolean) Coinbase or not
  "height" : n,                (numeric) The height of the prevout
  "value" : n,                 (numeric) The value in BTC
  "scriptPubKey" : {           (json object)
    "asm" : "str",             (string) Disassembly of the public key script
    "desc" : "str",            (string) Inferred descriptor for the output
    "hex" : "hex",             (string) The raw public key script bytes, hex-encoded
    "address" : "str",         (string, optional) The Bitcoin address (only if a well-defined address exists)
    "type" : "str"             (string) The type (one of: nonstandard, pubkey, pubkeyhash, scripthash, multisig, nulldata, witness_v0_scripthash, witness_v0_keyhash, witness_v1_taproot, witness_unknown)
  }
}

@dougEfresh dougEfresh force-pushed the 23264-fee-getrawtransaction branch from 226b0b7 to 6e5e745 Compare October 30, 2022 07:22
@dougEfresh
Copy link
Contributor Author

Compiled and manually tested.

The RPC help result object could use some improvement. Right now the section is rendered like this:

I updated the RPC help to reflect getblock

* Add optional fee response in BTC to getrawtransaction
* Add optional prevout(s) response to getrawtransaction showing utxos being spent
* Add getrawtransaction_verbosity functional test to validate fields
@dougEfresh dougEfresh force-pushed the 23264-fee-getrawtransaction branch from 6e5e745 to f866971 Compare October 30, 2022 12:06
}
// If request is verbosity >= 1 but no blockhash was given, then look up the blockindex
if (request.params[2].isNull()) {
LOCK(cs_main);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to avoid this lock, suggestions welcome.

Copy link
Contributor

@aureleoules aureleoules left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f866971

Copy link
Contributor

@hernanmarino hernanmarino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re ACK f866971

Copy link
Member

@pablomartin4btc pablomartin4btc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-tACK f866971

@achow101
Copy link
Member

ACK f866971

@achow101 achow101 merged commit daf881d into bitcoin:master Dec 13, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Dec 14, 2022
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thx. Left some doc nits.

},
RPCResult{"if verbose is set to true",
RPCResult{"if verbosity is set to 1",
// When updating this documentation, update `decoderawtransaction` in the same way.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is this dev comment still accurate, given that DecodeTxDoc was split out for this reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoFalke Fixed in #26734

@bitcoin bitcoin locked and limited conversation to collaborators Dec 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return fee in getrawtransaction