Skip to content

[test] WIP: rewrite generate() in test_node to gain determinism in test data - #19297

Closed
glowang wants to merge 4 commits into
bitcoin:masterfrom
glowang:2020/05/21/rewrite_generate_in_testnode
Closed

glowang wants to merge 4 commits into
bitcoin:masterfrom
glowang:2020/05/21/rewrite_generate_in_testnode

Conversation

@glowang

@glowang glowang commented Jun 16, 2020 •

Copy link
Copy Markdown
Contributor

This improvement was proposed by jnewbery in #19022

We want to rewrite generate() function by using an hd seed to create addresses. This allows the test to have unique but deterministic addresses to which blocks can be generated.

TODO:
Investigate why certain created keypaths do not increase incrementally/consistently:

currently, I am using a counter keypathnonce to assert every key path generated from my HD seed is unique and increment by one (maybe this was the wrong assumption to start with?).
Interestingly, when I ran tests, it seems that one particular key path,m/0'/0'/105 and m/0'/0'/106 , are always skipped. Whereas the rest of my hdkeypath increase consistently by one:

"m/0'/0'/0'
...
"m/0'/0'/101'
"m/0'/0'/102'
"m/0'/0'/103'
"m/0'/0'/104'
(skipping 105 and 106)
"m/0'/0'/107'

My assertion error

2020-06-16T15:29:57.583000Z TestFramework (ERROR): Assertion failed
Traceback (most recent call last):
  File "/Users/gwang/Documents/bitcoin/test/functional/test_framework/test_framework.py", line 116, in main
    self.run_test()
  File "test/functional/feature_dbcrash.py", line 223, in run_test
    utxo_list = create_confirmed_utxos(self.nodes[3].getnetworkinfo()['relayfee'], self.nodes[3], 5000)
  File "/Users/gwang/Documents/bitcoin/test/functional/test_framework/util.py", line 547, in create_confirmed_utxos
    node.generate(1)
  File "/Users/gwang/Documents/bitcoin/test/functional/test_framework/test_node.py", line 307, in generate
    assert_equal(hd_info["hdkeypath"], "m/0'/0'/"+str(self.hdkeypathnonce)+"'")
  File "/Users/gwang/Documents/bitcoin/test/functional/test_framework/util.py", line 46, in assert_equal
    raise AssertionError("not(%s)" % " == ".join(str(arg) for arg in (thing1, thing2) + args))
AssertionError: not(m/0'/0'/107' == m/0'/0'/105')

@maflcko

maflcko commented Jun 16, 2020

Copy link
Copy Markdown
Member

You may find the issue if you inspect the log generated by the --tracerpc -l DEBUG options

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

getnewaddress is a wallet RPC. If you want this to work when the wallet isn't compiled, you'll need to generate the HD keys in the python test framework.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks John! Would you mind giving an example of where this is done currently? I looked around in the test_framework and just the functional test dir in general, but I couldn't find any example of generating hd keys without wallet. Or maybe this is a function that doesn't exist in the functional test dir yet and we can write it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was thinking that the keys would be generated in the python test framework. I just assumed that we'd have some HD key generation code to test BIP32, but it appears I was wrong.

Marco's suggestion to use the deriveaddress seems ok, but I don't think you even need to do that. I think you can just pass the descriptor with keypath in to generatetodescriptor

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added hdkeypathnonce here to make sure a new key path is used. But I realized that incrementing the counter variable like this won't work, because many test files would call getnewaddress() outside of generate(). These new keys generated out of band also belong to the family of our HD tree, but our counter variable will not be aware of this.
(like feature_segwit.py).

Hence, although we want to make sure each time a new key path is used, it doesn't make sense to use member variable like this. Maybe the best way to check is to compare the new keypath with the existing keypool? Or do you have other suggestions? @jnewbery

@glowang

glowang commented Jun 17, 2020

Copy link
Copy Markdown
Contributor Author

You may find the issue if you inspect the log generated by the --tracerpc -l DEBUG options

Thanks Marco, really helpful!

@glowang
glowang force-pushed the 2020/05/21/rewrite_generate_in_testnode branch from 6dde62f to 799ac27 Compare June 28, 2020 22:05

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Once I generate to a new address, many tests that check whether the wallet addr number matches up start failing. I think this is because the getnewaddress is messing up the address count for some tests. But at the same time, we also don't want to generate to deterministic addresses either... I'm not sure what direction I should take here...

glowang added 2 commits July 25, 2020 11:58
TODO: investigate why created keypaths do not increase incrementally/consistently
@glowang
glowang force-pushed the 2020/05/21/rewrite_generate_in_testnode branch from 799ac27 to 86e2b7b Compare August 17, 2020 23:09
@glowang
glowang force-pushed the 2020/05/21/rewrite_generate_in_testnode branch from 86e2b7b to 671ae41 Compare August 17, 2020 23:11
@DrahtBot

DrahtBot commented Aug 20, 2020 •

Copy link
Copy Markdown
Contributor

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

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.

@glowang glowang closed this Dec 17, 2020
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants