Skip to content

Conversation

theStack
Copy link
Contributor

gruve-p reported that the signet miner doesn't work anymore (see #24501 (comment)), failing with the following error of the walletprocesspsbt RPC:

error code: -22
error message:
Specified sighash value does not match value stored in PSBT
.....
subprocess.CalledProcessError: Command '['bitcoin-cli', '-signet', '-stdin', 'walletprocesspsbt']' returned non-zero exit status 22

PSBT signing was changed to use SIGHASH_DEFAULT by default in #22514. The signet miner script sets the sighash type of the created PSBT to SIGHASH_ALL (3 is the per-input type PSBT_IN_SIGHASH_TYPE, following a little-endian 32 unsigned integer of the sighash type):

psbt.i = [ PSBTMap( {0: spendme.serialize(),
3: bytes([1,0,0,0])})

hence this leads to a sighash mismatch when the walletprocesspsbt RPC is called. Fix this by explicitly passing the correct sighash type. The same change was needed in one of our functional tests, see commit d399266.

Note that instead of feeding the PSBT via -stdin it is directly passed as parameter, as I couldn't figure out a way to pass multiple parameters otherwise (separating by newline also didn't work).

@theStack
Copy link
Contributor Author

pinging @achow101 @gruve-p

@gruve-p
Copy link
Contributor

gruve-p commented Mar 13, 2022

Just tested and this fixes the issue

@fanquake fanquake requested review from ajtowns and kallewoof March 14, 2022 05:47
@achow101
Copy link
Member

Code Review ACK 4e1503d

@ajtowns
Copy link
Contributor

ajtowns commented Mar 14, 2022

This doesn't seem right? The walletprocesspsbt docs say

  1. sighashtype (string, optional, default="DEFAULT for Taproot, ALL otherwise") The signature hash type to sign with if not specified by the PSBT.

but in this case the signature hash type is specified by the PSBT, so we're just getting a redundant error?

If we do have to specify the sighash type via RPC as well as the PSBT, you don't need to remove the -stdin to do it; this works:

-        psbt_signed = json.loads(args.bcli("-stdin", "walletprocesspsbt", input=psbt.encode('utf8')))
+        psbt_signed = json.loads(args.bcli("-stdin", "walletprocesspsbt", input=(psbt.encode('utf8') + b"\ntrue\nALL")))

(Passing the psbt via stdin is necessary, as otherwise the block size is limited by your shell's maximum argument length)

PSBT signing was changed to use SIGHASH_DEFAULT by default in bitcoin#22514.
The signet miner script sets the sighash type of the created PSBT to
SIGHASH_ALL, hence this leads to a sighash mismatch when the
`walletprocesspsbt` RPC is called. Fix this by explicitly passing the
correct sighash type.

Note that the same change was needed in one of our functional tests,
see commit d399266.

Reported by gruve-p.
@theStack theStack force-pushed the 202203-contrib-fix_signet_miner_sighash_mismatch branch from 4e1503d to 12cc020 Compare March 14, 2022 11:03
@theStack
Copy link
Contributor Author

theStack commented Mar 14, 2022

This doesn't seem right? The walletprocesspsbt docs say

  1. sighashtype (string, optional, default="DEFAULT for Taproot, ALL otherwise") The signature hash type to sign with if not specified by the PSBT.

but in this case the signature hash type is specified by the PSBT, so we're just getting a redundant error?

Good point. Looking at the code, the sighash passed for the walletprocesspsbt (or, the default value SIGHASH_DEFAULT) is always compared to each input's sighash if it is specified in the PSBT:

if (sign && input.sighash_type != std::nullopt && *input.sighash_type != sighash_type) {
return TransactionError::SIGHASH_MISMATCH;
}

So it seems the help text should be changed?

If we do have to specify the sighash type via RPC as well as the PSBT, you don't need to remove the -stdin to do it; this works:

-        psbt_signed = json.loads(args.bcli("-stdin", "walletprocesspsbt", input=psbt.encode('utf8')))
+        psbt_signed = json.loads(args.bcli("-stdin", "walletprocesspsbt", input=(psbt.encode('utf8') + b"\ntrue\nALL")))

(Passing the psbt via stdin is necessary, as otherwise the block size is limited by your shell's maximum argument length)

Thanks, that works indeed. I think yesterday when I tried this I accidentially swapped the argument order of "walletprocesspsbt" and "-stdin" and wondered why it doesn't work - d'oh! Done; used os.linesep instead of \n directly in order to be more portable (OTOH I don't know if the mining script even works on a non-unixoid system).

@ajtowns
Copy link
Contributor

ajtowns commented Mar 14, 2022

#24563 has a patch to make walleprocesspsbt work the way I thought it was meant to, which seems like it's also enough to get contrib/signet/miner working again

Copy link
Contributor

@kallewoof kallewoof left a comment

Choose a reason for hiding this comment

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

ACK 12cc020

@gruve-p
Copy link
Contributor

gruve-p commented Mar 16, 2022

Do I need to retest this or do we prefer the patch approach on #24563 (in any case either needs a milestone 23.0 and backport label) as the signet miner is currently broken on the rc

Also does #24559 superseed this PR? In that case this one can get closed?

Copy link
Contributor

@ajtowns ajtowns left a comment

Choose a reason for hiding this comment

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

ACK 12cc020 ; code review only

Fine to merge this whether walletprocesspsbt behaviour is changed or not.

@maflcko maflcko merged commit 74f8c55 into bitcoin:master Mar 17, 2022
@maflcko maflcko added this to the 23.0 milestone Mar 17, 2022
@theStack theStack deleted the 202203-contrib-fix_signet_miner_sighash_mismatch branch March 17, 2022 07:24
@gruve-p
Copy link
Contributor

gruve-p commented Mar 17, 2022

Post-merge ACK 12cc020

@theStack
Copy link
Contributor Author

Also does #24559 superseed this PR? In that case this one can get closed?

#24559 adds a functional test for the signet mining script, in order to find potential bugs automatically in the future, rather than by accident months after its introduction (thanks again for reporting!). Including this PR's commit in #24559 was necessary to make the CI tests pass; now that the bugfix is merged, #24559 is rebased on master, solely consisting of the commit adding the test. Review/testing would of course be much appreciated!

@laanwj
Copy link
Member

laanwj commented Mar 17, 2022

Note that instead of feeding the PSBT via -stdin it is directly passed as parameter, as I couldn't figure out a way to pass multiple parameters otherwise (separating by newline also didn't work).

It's strange that newline didn't work. This is the supposed way to pass multiple arguments using -stdin. E.g. see the documentation:

Read extra arguments from standard input, one per line until EOF/Ctrl-D

If it doesn't work work anymore, that's a bug. Note that it only works for positional arguments, not named ones.

Edit: oh, I see you did get it to work, but didn't update the OP that ended up in the commit message.

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Mar 18, 2022
12cc020 contrib: fix signet miner (sighash mismatch) (Sebastian Falbesoner)

Pull request description:

  gruve-p reported that the signet miner doesn't work anymore (see bitcoin#24501 (comment)), failing with the following error of the `walletprocesspsbt` RPC:

  ```
  error code: -22
  error message:
  Specified sighash value does not match value stored in PSBT
  .....
  subprocess.CalledProcessError: Command '['bitcoin-cli', '-signet', '-stdin', 'walletprocesspsbt']' returned non-zero exit status 22
  ```

  PSBT signing was changed to use SIGHASH_DEFAULT by default in bitcoin#22514. The signet miner script sets the sighash type of the created PSBT to SIGHASH_ALL (3 is the per-input type PSBT_IN_SIGHASH_TYPE, following a little-endian 32 unsigned integer of the sighash type):

  https://github.com/bitcoin/bitcoin/blob/e04720ec3336e3df7fce522e3b1da972aa65ff62/contrib/signet/miner#L169-L170

  hence this leads to a sighash mismatch when the `walletprocesspsbt` RPC is called. Fix this by explicitly passing the correct sighash type. The same change was needed in one of our functional tests, see commit d399266.

  Note that instead of feeding the PSBT via `-stdin` it is directly passed as parameter, as I couldn't figure out a way to pass multiple parameters otherwise (separating by newline also didn't work).

ACKs for top commit:
  kallewoof:
    ACK 12cc020
  ajtowns:
    ACK 12cc020 ; code review only

Tree-SHA512: 8509e768e96f85e28c0ca0dc2d35874aa29623febddc46bf90472ec38f38cb3a1b5407c563fd9101d07088775d0fdb18e9137cc38955e847885b83c16591c736
@jonatack
Copy link
Member

Backported to v23.0 in #24512.

jonatack pushed a commit to jonatack/bitcoin that referenced this pull request Mar 28, 2022
PSBT signing was changed to use SIGHASH_DEFAULT by default in bitcoin#22514.
The signet miner script sets the sighash type of the created PSBT to
SIGHASH_ALL, hence this leads to a sighash mismatch when the
`walletprocesspsbt` RPC is called. Fix this by explicitly passing the
correct sighash type.

Note that the same change was needed in one of our functional tests,
see commit d399266.

Reported by gruve-p.

Github-Pull: bitcoin#24553
Rebased-From: 12cc020
@jonatack jonatack mentioned this pull request Mar 28, 2022
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Mar 31, 2022
PSBT signing was changed to use SIGHASH_DEFAULT by default in bitcoin#22514.
The signet miner script sets the sighash type of the created PSBT to
SIGHASH_ALL, hence this leads to a sighash mismatch when the
`walletprocesspsbt` RPC is called. Fix this by explicitly passing the
correct sighash type.

Note that the same change was needed in one of our functional tests,
see commit d399266.

Reported by gruve-p.

Github-Pull: bitcoin#24553
Rebased-From: 12cc020
jonatack pushed a commit to jonatack/bitcoin that referenced this pull request Mar 31, 2022
PSBT signing was changed to use SIGHASH_DEFAULT by default in bitcoin#22514.
The signet miner script sets the sighash type of the created PSBT to
SIGHASH_ALL, hence this leads to a sighash mismatch when the
`walletprocesspsbt` RPC is called. Fix this by explicitly passing the
correct sighash type.

Note that the same change was needed in one of our functional tests,
see commit d399266.

Reported by gruve-p.

Github-Pull: bitcoin#24553
Rebased-From: 12cc020
fanquake added a commit that referenced this pull request Mar 31, 2022
174af33 util: Add inotify_rm_watch to syscall sandbox (AllowFileSystem) (Hennadii Stepanov)
ded10fe build: Fix Boost.Process test for Boost 1.78 (Hennadii Stepanov)
26c2f23 build: Fix Boost.Process detection on macOS arm64 (Hennadii Stepanov)
85f85c7 util: add linkat to syscall sandbox (AllowFileSystem) (fanquake)
eaa0419 contrib: fix signet miner (sighash mismatch) (Sebastian Falbesoner)
235b042 rpc: Exclude descriptor when address is excluded (MarcoFalke)
b05a59b ci: Temporarily use clang-13 to work around clang-14 TSan bug (MarcoFalke)
65b9667 doc, init: add links to doc/cjdns.md (Jon Atack)
7a553d4 doc: update i2p.md with cjdns, improve local addresses section (Jon Atack)
4148396 doc: update tor.md with cjdns and getnodeaddresses, fix tor grep, (Jon Atack)
4690e8a doc: create initial doc/cjdns.md for cjdns how-to documentation (Jon Atack)
5d24f61 Clarify in -maxtimeadjustment that only outbound peers influence time data (Jon Atack)
b1646f1 test: set segwit height back to 0 on regtest (Martin Zumsande)
ef6a37b rpc: rename getdeploymentinfo status-next to status_next (Jon Atack)
2a6fcf9 init, doc: improve -onlynet help and tor/i2p documentation (Jon Atack)

Pull request description:

  Backport the following to 23.x:

  - #24468
  - #24528
  - #24527
  - #24609
  - #24555
  - #24663
  - #24572
  - #24636
  - #24553
  - #24659
  - #24521
  - #24523
  - #24690
  - #24710

  Possibly also:
  - #24579
  - #24691

ACKs for top commit:
  laanwj:
    List-of-commits ACK 174af33, I think we should merge this and move forward with rc3..
  hebasto:
    ACK 174af33

Tree-SHA512: 5a493e1652b780b527767d6ca9e67012abd2fa5573496e85e0d8aa4bed3eb332bfcd72610b8dfb954ff274d42450623233c96c479de2085b9c8344ba5abf1935
laanwj added a commit that referenced this pull request Apr 14, 2022
038d2a6 test: add test for signet miner script (Sebastian Falbesoner)
449b96e test: add `is_bitcoin_util_compiled` helper (Sebastian Falbesoner)
dde33ec test: determine path to `bitcoin-util` in test framework (Sebastian Falbesoner)

Pull request description:

  This PR adds a very basic test for the signet miner script (contrib/signet/miner). ~~It was based on #24553 (merged by now) which fixes a bug (and was also the motivation to write this test).~~

  The test roughly follows the steps from https://en.bitcoin.it/wiki/Signet#Custom_Signet, except that the challenge key-pair is created solely with the test framework. Calibration is also skipped, the difficulty is simply set to the first mainnet target `0x1d00ffff` (see also https://bitcoin.stackexchange.com/a/57186).

ACKs for top commit:
  laanwj:
    re-ACK 038d2a6

Tree-SHA512: 150698a3c0cda3679661b47688e3b932c9761e777fdd284776b867b485db6a8895960177bd02a53f838a4c9b9bbe6a9beea8d7a5b14825b38e4e43b3177821b3
@bitcoin bitcoin locked and limited conversation to collaborators Mar 28, 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.

10 participants