-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(p2pk): show P2PK balance #2053
Conversation
P2PK
balanceP2PK
balance
P2PK
balanceP2PK
balance
Missing unit tests for the balance addition. Verified it's working as expected with manual tests though. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docker tests fail to compile with these changes, they can be run using
cargo test --test 'docker_tests_main' --features run-docker-tests
Though, some of them are known to fail, which is partially fixed in another PR.
cc @shamardy
@mariocynicys can you please fix failing docker tests compilation by running this command |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great start! A few comments/notes for now but will do a complete review after #1960 is merged and conflicts are resolved.
Ok(this | ||
.scripthash_get_balances(hashes) | ||
.compat() | ||
.await? | ||
.into_iter() | ||
.fold(BigDecimal::from(0), |sum, electrum_balance| { | ||
sum + electrum_balance.to_big_decimal(decimals) | ||
})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit of an overhead to send 2 requests to electrums to get both P2PK
and P2PKH
balances when most addresses will not have P2PK
outputs but I don't see a way around it. Just opening this for discussions in case you or anybody else has a better idea.
P.S. we opted for this solution as any other solution we could find would lead to a new coin for P2PK
addresses, some explorers do the same as it was pointed out in a note in the code changes but others don't. A complete solution would be to have only segwit addresses for any coin that supports it and include all kind of outputs in the balance, but this is outside the scope of this issue/PR and more related to this #2050. This solution would lead to having balance on addresses completely different from explorers though.
@mariocynicys As discussed, we should also implement |
ccd6147
to
0a24680
Compare
This adds a new `.pubkey` field `Address` of type `Option<Public>`. Using this, we have a way to keep the pubkey of our address so that we can pull p2pk balance if any.
0a24680
to
ce267a9
Compare
The changes made up till now (showing p2pk balance) are reviewable. p2pk spending isn't added yet. update: removed the |
instead of having one output script per address. This facilitates spending outputs of different types in the same transaction (e.g. spending p2pk outputs along with p2pkh outputs in the same transaction. note that p2pk output script is different than p2pkh one)
mm2src/coins/utxo/rpc_clients.rs
Outdated
let output_script = try_f!(output_script(address)); | ||
let mut output_scripts = vec![output_script]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
let output_script = try_f!(output_script(address)); | |
let mut output_scripts = vec![output_script]; | |
let mut output_scripts = vec![try_f!(output_script(address))]; |
mm2src/coins/utxo/slp.rs
Outdated
@@ -400,6 +400,7 @@ impl SlpToken { | |||
outputs, | |||
}; | |||
Ok((preimage, recently_spent)) | |||
////////////////////////////////////////////////// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess a leftover
@@ -420,6 +420,7 @@ impl Script { | |||
_ => unreachable!(), // because we are relying on script_type() checks here | |||
}) | |||
.map(|public| { | |||
// DISCUSS: Should we really reduce this to pkh? what is it used for? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we will merge this note, it would be great if we can change this to TODO
. I often grep this placeholder in the entire codebase to see what we left behind.
// DISCUSS: Should we really reduce this to pkh? what is it used for? | |
// TODO: Discussion needed: "Should we really reduce this to pkh? what is it used for?" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intend to get this resolved before the merger of the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we really reduce this to pkh?
btw you can check the original ExtractDestination in the daemon code, I can see it works in the similar way as basically used to get the address from P2PK utxos, to add the utxo to the wallet balance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw you can check the original ExtractDestination in the daemon code, I can see it works in the similar way as basically used to get the address from P2PK utxos, to add the utxo to the wallet balance
iirc, yeah, it's used somewhere to extract the address the coins are sent to, but they are just used for comparison and not stored anymore (basically, is this output sent our address?). the optional pubkey
field doesn't contribute to that comparison (see).
maybe we can check if this output belongs to us by comparing output scripts instead?
mm2src/coins/utxo/utxo_common.rs
Outdated
// let signature_version = match prev_script.is_pay_to_witness_key_hash() { | ||
// true => SignatureVersion::WitnessV0, | ||
// _ => coin.as_ref().conf.signature_version, | ||
// }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a leftover or needed for future work/ref? If we are going to merge this part too, we need some notes explaining why we keeping this.
@@ -85,7 +78,19 @@ pub fn p2pk_spend( | |||
) -> UtxoSignWithKeyPairResult<TransactionInput> { | |||
let unsigned_input = get_input(signer, input_index)?; | |||
|
|||
// DISCUSS: Why are we doing this check? Solely to check whether we are spending |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same (about the TODO
thing) here
…ctionInput` instead This allows us to get rid of the assumingly never triggered `UtxoSignWithKeyPairError::InputIndexOutOfBound` error. `mm2src/mm2_bitcoin/script/src/sign.rs` will need a similar refactor since it still uses indices and not unsigned inputs directly.
…edTransactionInput` instead" Revert the above commit since it introduces panics (`Vec[index]` this indexing technique might panic!). This reverts commit 926bc46.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Next review iteration!
@mariocynicys please note that the below tests are failing here and not in dev, after fixing those I will do the final review round :)
mm2_tests::mm2_tests_inner::test_sign_raw_transaction_p2wpkh
mm2_tests::mm2_tests_inner::test_sign_raw_transaction_rick
@dimxy please resolve any comments that were fixed!
conf.checksum_type, | ||
conf.address_prefixes.clone(), | ||
conf.bech32_hrp.clone(), | ||
) | ||
.as_pkh() | ||
.as_pkh_from_pk(*key_pair.public()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this done? should HD PRs be merged first?
@mariocynicys please fix conflicts in this PR so that we can review it. |
@shamardy, sorry, missed the last suggestions. Done now. There seems to be some of this pattern in this test file though. Let me try to handle these as well in this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for your patience during this PR review! Only non-blockers left!
@dimxy please check if all your comments are resolved so that I can merge this.
mm2src/coins/utxo/utxo_withdraw.rs
Outdated
// Do we want to mix P2PK and non-P2PK spends? | ||
// Should we make another sweep P2PK method that spends all P2PK balance? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not do that in this PR. Please open an issue for this so that we can consider it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, forgot about this one, will put fixmes next time.
The logic right now mixes p2pk and non-p2pk (e.g. p2pkh, p2wpkh) and signs each input accordingly. So we should get rid of this comment.
@smk762 if you have time, can you please check if your comment here #720 (comment) is resolved? If I merged it before that, you can check this in dev :) |
9cc7d16
to
2ecd5e4
Compare
2ecd5e4
to
fb1df6a
Compare
by adding -Segwit to coin name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This makes it so we don't auto-convert P2PK addresses to P2PKH addresses, thus we can query them for balances if they have any.
Also refactors
utxo::output_script()
to auto detect the address type without the need for akeys::Type
flag.Fixes #720