Skip to content
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

Merged
merged 33 commits into from
May 15, 2024
Merged

Conversation

mariocynicys
Copy link
Collaborator

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 a keys::Type flag.

Fixes #720

@shamardy shamardy added the in progress Changes will be made from the author label Jan 18, 2024
@mariocynicys mariocynicys changed the title [wip] Fix: Show P2PK balance [wip] fix(p2pk): Show P2PK balance Jan 18, 2024
@mariocynicys mariocynicys marked this pull request as draft January 18, 2024 16:21
@mariocynicys mariocynicys changed the title [wip] fix(p2pk): Show P2PK balance fix(p2pk): Show P2PK balance Jan 18, 2024
@shamardy shamardy changed the title fix(p2pk): Show P2PK balance fix(p2pk): Show P2PK balance Jan 18, 2024
@shamardy shamardy changed the title fix(p2pk): Show P2PK balance fix(p2pk): show P2PK balance Jan 18, 2024
@mariocynicys
Copy link
Collaborator Author

Missing unit tests for the balance addition. Verified it's working as expected with manual tests though.

@mariocynicys mariocynicys marked this pull request as ready for review January 23, 2024 15:51
Copy link
Member

@artemii235 artemii235 left a 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

mm2src/coins/utxo.rs Outdated Show resolved Hide resolved
@shamardy
Copy link
Collaborator

shamardy commented Feb 2, 2024

@mariocynicys can you please fix failing docker tests compilation by running this command cargo test --test 'docker_tests_main' --features run-docker-tests and checking the errors. You can wait for #1960 to be merged first but don't forget to check these errors after that.

Copy link
Collaborator

@shamardy shamardy left a 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.

mm2src/coins/utxo/rpc_clients.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/keys/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines +2332 to +2369
Ok(this
.scripthash_get_balances(hashes)
.compat()
.await?
.into_iter()
.fold(BigDecimal::from(0), |sum, electrum_balance| {
sum + electrum_balance.to_big_decimal(decimals)
}))
Copy link
Collaborator

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.

@shamardy
Copy link
Collaborator

shamardy commented Feb 2, 2024

@mariocynicys As discussed, we should also implement P2PK outputs spending in this PR. You should wait for the merge of the conflicting PR first for this too.

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.
@mariocynicys
Copy link
Collaborator Author

mariocynicys commented Feb 12, 2024

The changes made up till now (showing p2pk balance) are reviewable. p2pk spending isn't added yet.

update: removed the under review status so the CI doesn't complain, still reviewable though.

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)
Comment on lines 2223 to 2224
let output_script = try_f!(output_script(address));
let mut output_scripts = vec![output_script];
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
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))];

@@ -400,6 +400,7 @@ impl SlpToken {
outputs,
};
Ok((preimage, recently_spent))
//////////////////////////////////////////////////
Copy link
Member

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?
Copy link
Member

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.

Suggested change
// 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?"

Copy link
Collaborator Author

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.

Copy link
Collaborator

@dimxy dimxy Feb 26, 2024

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

Copy link
Collaborator Author

@mariocynicys mariocynicys Feb 26, 2024

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?

Comment on lines 3118 to 3121
// let signature_version = match prev_script.is_pay_to_witness_key_hash() {
// true => SignatureVersion::WitnessV0,
// _ => coin.as_ref().conf.signature_version,
// };
Copy link
Member

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
Copy link
Member

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.
@mariocynicys mariocynicys removed the in progress Changes will be made from the author label Feb 20, 2024
@mariocynicys mariocynicys requested a review from dimxy April 11, 2024 13:00
Copy link
Collaborator

@shamardy shamardy left a 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!

mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/script/src/sign.rs Show resolved Hide resolved
conf.checksum_type,
conf.address_prefixes.clone(),
conf.bech32_hrp.clone(),
)
.as_pkh()
.as_pkh_from_pk(*key_pair.public())
Copy link
Collaborator

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?

@shamardy
Copy link
Collaborator

shamardy commented May 6, 2024

@mariocynicys please fix conflicts in this PR so that we can review it.

@mariocynicys
Copy link
Collaborator Author

@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.

shamardy
shamardy previously approved these changes May 8, 2024
Copy link
Collaborator

@shamardy shamardy left a 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.

Comment on lines 139 to 140
// Do we want to mix P2PK and non-P2PK spends?
// Should we make another sweep P2PK method that spends all P2PK balance?
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

@shamardy
Copy link
Collaborator

shamardy commented May 8, 2024

@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 :)

Copy link
Collaborator

@dimxy dimxy left a comment

Choose a reason for hiding this comment

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

LGTM

@shamardy shamardy merged commit df6ab98 into KomodoPlatform:dev May 15, 2024
1 check passed
dimxy added a commit that referenced this pull request May 22, 2024
* dev:
  feat(ETH): eip1559 gas fee estimator and rpcs (#2051)
  fix(p2pk-tests): fix p2pk tests post merge (#2119)
  fix(p2pk): show and spend P2PK balance (#2053)
  fix(swap): use tmp file for swap and order files (#2118)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants