rpc: alias helper #19957

pull kallewoof wants to merge 2 commits into bitcoin:master from kallewoof:202009-rpc-alias changing 4 files +55 −28
  1. kallewoof commented at 5:42 am on September 15, 2020: member

    Introduce a new “check alias” helper for RPC commands, which checks and moves the values into the “primary” key.

    The first commit simply pushKV’s the value from the alias, if set, whereas the second commit const-casts the keys vector in UniValue and modifies that directly, i.e. “rename”. Some people (cough sipa cough) might object to the approach, so I’m splitting into two commits.

    This is partially also a cleanup of #16378.

  2. fanquake added the label RPC/REST/ZMQ on Sep 15, 2020
  3. kallewoof force-pushed on Sep 15, 2020
  4. kallewoof force-pushed on Sep 15, 2020
  5. Sjors commented at 2:26 pm on September 15, 2020: member
    Concept ACK
  6. ryanofsky requested review from MarcoFalke on Sep 15, 2020
  7. in src/rpc/util.cpp:83 in 9b2623f5d4 outdated
    78+    const std::map<std::string, std::string>& aliases)
    79+{
    80+    for (const auto& alias : aliases) {
    81+        switch (o.exists(alias.first) + o.exists(alias.second)) {
    82+        case 2: throw JSONRPCError(RPC_INVALID_PARAMETER, "May not use both " + alias.first + " and " + alias.second + " simultaneously.");
    83+        case 1: if (o.exists(alias.second)) o.pushKV(alias.first, o[alias.second]);
    


    Sjors commented at 12:16 pm on September 17, 2020:
    0        if (o.exists(alias.first) && o.exists(alias.second)) {
    1           throw JSONRPCError(RPC_INVALID_PARAMETER, "May not use both " + alias.first + " and " + alias.second + " simultaneously.");
    2        }
    3        if (o.exists(alias.second)) o.pushKV(alias.first, o[alias.second]);
    

    kallewoof commented at 0:06 am on September 18, 2020:
    Yeah, better, thanks.
  8. in src/wallet/rpcwallet.cpp:2988 in 9b2623f5d4 outdated
    2988@@ -2982,9 +2989,8 @@ void FundTransaction(CWallet* const pwallet, CMutableTransaction& tx, CAmount& f
    2989             coinControl.m_add_inputs = options["add_inputs"].get_bool();
    2990         }
    2991 
    2992-        if (options.exists("changeAddress") || options.exists("change_address")) {
    2993-            const std::string change_address_str = (options.exists("change_address") ? options["change_address"] : options["changeAddress"]).get_str();
    2994-            CTxDestination dest = DecodeDestination(change_address_str);
    2995+        if (options.exists("change_address")) {
    2996+            CTxDestination dest = DecodeDestination(options["change_address"].get_str());
    


    Sjors commented at 12:20 pm on September 17, 2020:
    Might as well make it const
  9. Sjors commented at 12:24 pm on September 17, 2020: member
    Looks good. No strong opinion on handwaving away the constness in 97e383ef829523130f192aebf67c71cc7e10d975
  10. kallewoof force-pushed on Sep 18, 2020
  11. kallewoof force-pushed on Sep 18, 2020
  12. kallewoof force-pushed on Sep 18, 2020
  13. kallewoof force-pushed on Sep 18, 2020
  14. rpc: introduce alias helper 965c760bee
  15. rpc: rename UniValue keys via const-casting 9056b07bb8
  16. kallewoof force-pushed on Sep 18, 2020
  17. Sjors commented at 10:47 am on September 18, 2020: member

    ACK 9056b07bb8dc1ef78b4df29b3b1b9e9ef5a83a22

    Travis failure should go away after #19971.

  18. promag commented at 10:59 am on September 22, 2020: member

    Concept ACK, not sure about the approach.

    I think that we could improve consistency if internally all keys are transformed like lower(key.replace('_', '')) This way alias aren’t necessary. This would also help when dumping help output - keys could be formatted to snake_case for instance.

  19. kallewoof commented at 2:07 am on September 23, 2020: member
    @promag That sounds like a good idea but people pointed out that it could be potentially riskful (use the fun-draw-transaction command on this innocent transaction and send it off and it should give you a free lottery ticket where you can potentially win a bitcoin!!). This might be a good thing to do in between, even if we do go that route though.
  20. MarcoFalke commented at 7:04 am on September 23, 2020: member
    I am wondering if the aliases can be passed in via RPCArg and the merging be done by RPCHelpMan before dispatching the request to the method.
  21. kallewoof commented at 7:24 am on September 23, 2020: member

    @MarcoFalke That sounds like a good idea. I can think of a number of ways to do that:

    1. Add an alias string var to RPCArg, and two new constructors which include alias
    2. Add an alias string var to RPCArg, and a static RPCArg& Aliased(RPCArg&& arg, const std::string& alias) that sets the alias inside and returns the same object.
    3. Make RPCArgs have new type “alias” and use e.g. ‘description’ to indicate the primary name.

    I’m leaning towards number 2 on that list but may change my mind if it turns out to be ugly.

  22. kallewoof commented at 7:44 am on September 23, 2020: member

    One issue with doing it in RPCArg is that it requires duplication e.g. for callers of FundTransaction().

    https://github.com/bitcoin/bitcoin/blob/9056b07bb8dc1ef78b4df29b3b1b9e9ef5a83a22/src/wallet/rpcwallet.cpp#L2956-L2962

  23. MarcoFalke commented at 8:32 am on September 23, 2020: member

    Oh it looks like RPCArg already has m_names (separated by |). This was primarily for the outer most args (named args), but it may be possible to extend to keys of a dictionary as well.

    it requires duplication

    The PRCArg vector or initializer list is a C++ struct, so it can be easily deduplicated and re-used. Even parts of it, which can be concatenated with our Cat vector util helper.

  24. kallewoof commented at 7:08 am on September 24, 2020: member
    @MarcoFalke The way I understood it was that you’d provide the arguments before the RPCHelper cleaned up. In the FundTransaction case, the RPCHelper has already cleaned once, so you’d have to do it twice. Which sounds kind of similar to what I’m doing here already.
  25. promag commented at 1:49 pm on September 24, 2020: member

    @promag That sounds like a good idea but people pointed out that it could be potentially riskful (use the fun-draw-transaction command on this innocent transaction and send it off and it should give you a free lottery ticket where you can potentially win a bitcoin!!). This might be a good thing to do in between, even if we do go that route though.

    The suggestion is to just transform args and json keys, not command names or arg values.

  26. kallewoof commented at 6:38 am on September 25, 2020: member
    @promag Ah, right.
  27. kallewoof commented at 8:19 am on September 25, 2020: member
    See #20013 – I believe that one is better, and it initiates what @promag suggests we do. Unless people say they prefer this approach, I will be closing this one.
  28. kallewoof closed this on Sep 25, 2020

  29. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-12-22 06:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me