getblockchaininfo: make bip9_softforks an object, not an array. #7863

pull rustyrussell wants to merge 2 commits into bitcoin:master from rustyrussell:bip9-status-as-object changing 4 files +11 −17
  1. rustyrussell commented at 6:25 AM on April 12, 2016: contributor

    We can't change "softforks", but it seems far more logical to use tags in an object rather than using an "id" field in an array.

    For example, to get the csv status before, you need to iterate the array to find the entry with 'id' field equal to "csv":

    jq '.bip9_softforks | map(select(.id == "csv"))[] | .status'

    Now: jq '.bip9_softforks.csv.status'

    There is no issue with fork names being incompatible with JSON tags, since we're selecting them ourselves.

    Signed-off-by: Rusty Russell rusty@rustcorp.com.au

  2. NicolasDorier commented at 6:28 AM on April 12, 2016: contributor

    it probably broke py tests

  3. dcousens commented at 6:28 AM on April 12, 2016: contributor

    The array also implies that id may not be unique.

    utACK bc1ebf5.

  4. NicolasDorier commented at 6:30 AM on April 12, 2016: contributor

    I don't strongly oppose, but using an 'id' is easier for parsers who use class reflection (every statically typed language does it) for parsing the JSON. (In .NET for example, you don't have to parse by hand, you just create your class with an "id" field and that's it)

  5. rustyrussell commented at 6:47 AM on April 12, 2016: contributor

    Thanks @NicolasDorier it did break tests. It's exactly this kind of code which is why I think the format is ins... sub-optimal:

    -    for row in info['bip9_softforks']:
    -        if row['id'] == key:
    -            return row
    -    raise IndexError ('key:"%s" not found' % key)
    +    return info['bip9_softforks'][key]
    
  6. rustyrussell force-pushed on Apr 12, 2016
  7. dcousens commented at 6:58 AM on April 12, 2016: contributor

    @rustyrussell agreed, for selecting it changes the lookup complexity to O(1) (not that performance matters, but, it certainly looks/feels nicer) @NicolasDorier surely those languages can derive their field from the key, maybe through a custom constructor?

  8. NicolasDorier commented at 7:03 AM on April 12, 2016: contributor

    @dcousens static languages derive their field the shape of the JSON. Changing it that way will mean there will be one different field for each fork proposal instead of a simple non typed array.

    But by thinking about it is in reality it is not so much a problem, because the list of bip9 is static anyway, so it makes sense for a static language to have a different field for each different fork proposal.

    So now I'm rather neutral about this change.

  9. dcousens commented at 7:49 AM on April 12, 2016: contributor

    @NicolasDorier keeping the id field seems like it would entirely be a special case just for libraries that automatically parse the JSON into class objects. For those that might access the JSON using a more traditional approach json["key"]["sub-key"] (etc), it just becomes a tedium.

    I've only ever used the RPC using Python/Javascript and C++, all of which that traditional approach ruled supreme, and with which searching the JSON Array would be a tedium.

  10. MarcoFalke commented at 7:59 AM on April 12, 2016: member

    Travis error unrelated: #7846

  11. NicolasDorier commented at 8:46 AM on April 12, 2016: contributor

    @dcousens well, when one solution improve the life of some people, it makes more difficult the life of other. Should we make life easier for hackish tool to parse json or make easier for parser library for static language is a question without responses.

    What you call "traditional approach" is different for both of us, as we code in different environments.

    But by thinking about it I don't think it makes life much difficult for parser libs with generated class objects after all.

  12. in qa/rpc-tests/test_framework/util.py:None in 76968db9b3 outdated
     547 | @@ -548,7 +548,4 @@ def create_lots_of_big_transactions(node, txouts, utxos, fee):
     548 |  
     549 |  def get_bip9_status(node, key):
     550 |      info = node.getblockchaininfo()
     551 | -    for row in info['bip9_softforks']:
     552 | -        if row['id'] == key:
     553 | -            return row
     554 | -    raise IndexError ('key:"%s" not found' % key)
     555 | +    return info['bip9_softforks'][key]
    


    btcdrak commented at 10:43 AM on April 12, 2016:

    Could you change this to preserve the IndexError exception on key not found, null.


    MarcoFalke commented at 11:27 AM on April 12, 2016:

    It should throw a KeyError, which is fine. Am I missing something?


    btcdrak commented at 11:53 AM on April 12, 2016:

    @MarcoFalke even better. The point is to preserve the previous behaviour of throwing a meaningful exception rather than leaving it to Python which results in Unexpected exception caught during testing: 'xxxx'.


    MarcoFalke commented at 2:00 PM on April 12, 2016:

    Oh, actually this is not the issue of the code here. To get a better error message, you'd have to change str into repr in this line: https://github.com/bitcoin/bitcoin/blob/514993554c370f4cf30a109ac28d5d64893dbf0a/qa/rpc-tests/test_framework/test_framework.py#L146

  13. getblockchaininfo: make bip9_softforks an object, not an array.
    We can't change "softforks", but it seems far more logical to use tags
    in an object rather than using an "id" field in an array.
    
    For example, to get the csv status before, you need to iterate the
    array to find the entry with 'id' field equal to "csv":
    
       jq '.bip9_softforks | map(select(.id == "csv"))[] | .status'
    
    Now:
       jq '.bip9_softforks.csv.status'
    
    There is no issue with fork names being incompatible with JSON tags,
    since we're selecting them ourselves.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    85c807c9ea
  14. rpc-tests: handle KeyError nicely in test_framework.py
    btcdrak wrote this for me.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    d12760b16a
  15. rustyrussell force-pushed on Apr 13, 2016
  16. rustyrussell commented at 7:34 AM on April 13, 2016: contributor

    OK, found another place in the tests which cut & paste the same function. And added BTCDrak's error cleanup as suggested by @MarcoFalke as a separate commit on top.

  17. btcdrak commented at 7:40 AM on April 13, 2016: contributor

    tACK d12760b

  18. MarcoFalke commented at 9:38 AM on April 13, 2016: member

    utACK

  19. in qa/rpc-tests/bip9-softforks.py:None in d12760b16a
      78 | @@ -79,11 +79,7 @@ def generate_blocks(self, number, version, test_blocks = []):
      79 |  
      80 |      def get_bip9_status(self, key):
    


    MarcoFalke commented at 9:42 AM on April 13, 2016:

    found another place in the tests which cut & paste the same function.

    I'd rather have this duplicate code removed... You change stuff in one branch of the code and later notice you have missed to apply the patch to all other copies of the code.


    btcdrak commented at 3:26 PM on April 13, 2016:

    Yes, this can go since the function was moved to util.py @rustyrussell

  20. laanwj commented at 4:04 PM on April 13, 2016: member

    Concept ACK

  21. laanwj added the label RPC/REST/ZMQ on Apr 13, 2016
  22. laanwj added the label Needs backport on Apr 13, 2016
  23. laanwj merged this on Apr 14, 2016
  24. laanwj closed this on Apr 14, 2016

  25. laanwj referenced this in commit 72c54e3883 on Apr 14, 2016
  26. MarcoFalke referenced this in commit 777334a126 on Jun 9, 2016
  27. MarcoFalke referenced this in commit da03f574d1 on Jun 22, 2016
  28. MarcoFalke referenced this in commit feaa4ded1c on Jun 22, 2016
  29. MarcoFalke removed the label Needs backport on Jul 11, 2016
  30. MarcoFalke commented at 1:18 PM on July 11, 2016: member

    Removing label 'Needs backport'. Please discuss in #8186 about the backport.

  31. sickpig referenced this in commit e12268daeb on Mar 31, 2017
  32. codablock referenced this in commit eef608b1bd on Sep 16, 2017
  33. codablock referenced this in commit 1116d9bee1 on Sep 19, 2017
  34. codablock referenced this in commit cc3a06bf53 on Dec 20, 2017
  35. DrahtBot locked this on Sep 8, 2021

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: 2026-04-13 15:15 UTC

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