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

Multi send #1445

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Multi send #1445

wants to merge 1 commit into from

Conversation

levoncrypto
Copy link
Contributor

@levoncrypto levoncrypto commented Jun 2, 2024

This update enhances multi-sending capabilities. Previously, sending from a transparent address was limited to either another transparent address or a private address. Now, you can send Firo from a transparent address to a private and transparent address. Transaction fees will be evenly distributed across all transactions. When the fee is intended to be deducted from the sent amount, it will be deducted from the total transaction, not from any specific transaction. Sending Firo has now become more versatile and functional.

Copy link

coderabbitai bot commented Jun 2, 2024

Walkthrough

The changes primarily focus on enhancing the functionality and validation of Spark transactions, including minting, sending, and spending Spark tokens. The updates introduce new parameters, improve transaction handling, and refine the logic for validating and processing transactions. Additionally, modifications in the UI components reflect these backend changes, ensuring seamless integration and user experience.

Changes

Files/Groups Change Summaries
qa/rpc-tests/llmq-is-spark.py Added loop for multiple transactions, validation for transaction values, and instant lock checks.
qa/rpc-tests/spark_mint.py Expanded minting amounts, updated address handling, and adjusted verification logic.
qa/rpc-tests/spark_setmintstatus_validation.py Renamed variables and added new minting logic with additional address parameters.
qa/rpc-tests/spark_spend_gettransaction.py Updated variable names and added logic for minting and spending Spark tokens.
src/qt/sendcoinsdialog.cpp Modified conditions related to transaction preparation based on fAnonymousMode and sparkAddressCount.
src/qt/sparkmodel.cpp Added include for wallet header and refactored MintAndStoreSpark function call.
src/qt/walletmodel.cpp Added new vector for recipients and refactored address validation and data handling logic.
src/qt/walletmodeltransaction.cpp Changed iteration from recipients to transaction outputs in reassignAmounts function.
src/spark/sparkwallet.cpp Enhanced transaction amount handling and output adjustments in CreateSparkMintTransactions.
src/spark/sparkwallet.h Added parameter to CreateSparkMintTransactions function.
src/test/evospork_tests.cpp Modified MintAndStoreSpark function call to include vecSend parameter.
src/test/fixtures.cpp Introduced new vector for recipients in GenerateMints method.
src/wallet/rpcwallet.cpp Enhanced mintspark function to support various address types and parameters.
src/wallet/test/spark_tests.cpp Updated MintAndStoreSpark calls in test cases to include vecSend parameter.
src/wallet/wallet.cpp Added logic related to vecSend and subtractFeeFromAmount in MintAndStoreSpark and CreateSparkMintTransactions.
src/wallet/wallet.h Updated CreateSparkMintTransactions and MintAndStoreSpark function signatures to include vecSend.

Poem

In the realm of Spark, where tokens gleam,
We've added loops and checks supreme.
With minting magic, values soar,
Transactions flow like never before.
From wallets deep to tests so bright,
Our code now shines with Spark's new light.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 9

Outside diff range and nitpick comments (1)
src/qt/walletmodel.cpp (1)

Line range hint 1358-1412: Refactor prepareMintSparkTransaction to improve readability and maintainability.

The prepareMintSparkTransaction method has multiple responsibilities, including validation, transaction creation, and error handling. Consider breaking this method into smaller, more focused methods. For example, separate the validation logic into a method validateRecipients, and the transaction creation logic into createMintSparkTransaction. This will make the code easier to maintain and test.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cf19353 and 0af162e.

Files selected for processing (16)
  • qa/rpc-tests/llmq-is-spark.py (1 hunks)
  • qa/rpc-tests/spark_mint.py (1 hunks)
  • qa/rpc-tests/spark_setmintstatus_validation.py (2 hunks)
  • qa/rpc-tests/spark_spend_gettransaction.py (1 hunks)
  • src/qt/sendcoinsdialog.cpp (4 hunks)
  • src/qt/sparkmodel.cpp (2 hunks)
  • src/qt/walletmodel.cpp (3 hunks)
  • src/qt/walletmodeltransaction.cpp (2 hunks)
  • src/spark/sparkwallet.cpp (9 hunks)
  • src/spark/sparkwallet.h (1 hunks)
  • src/test/evospork_tests.cpp (1 hunks)
  • src/test/fixtures.cpp (2 hunks)
  • src/wallet/rpcwallet.cpp (3 hunks)
  • src/wallet/test/spark_tests.cpp (9 hunks)
  • src/wallet/wallet.cpp (7 hunks)
  • src/wallet/wallet.h (2 hunks)
Files not reviewed due to errors (6)
  • qa/rpc-tests/spark_setmintstatus_validation.py (no review received)
  • src/qt/walletmodeltransaction.cpp (no review received)
  • qa/rpc-tests/spark_spend_gettransaction.py (no review received)
  • qa/rpc-tests/llmq-is-spark.py (no review received)
  • src/qt/sparkmodel.cpp (no review received)
  • qa/rpc-tests/spark_mint.py (no review received)
Additional context used
Ruff
qa/rpc-tests/spark_setmintstatus_validation.py

3-3: from test_framework.util import * used; unable to detect undefined names


13-13: enable_mocktime may be undefined, or defined from star imports


14-14: start_nodes may be undefined, or defined from star imports


65-65: assert_raises may be undefined, or defined from star imports


65-65: JSONRPCException may be undefined, or defined from star imports


66-66: assert_raises may be undefined, or defined from star imports


66-66: JSONRPCException may be undefined, or defined from star imports


67-67: assert_raises may be undefined, or defined from star imports


67-67: JSONRPCException may be undefined, or defined from star imports


68-68: assert_raises may be undefined, or defined from star imports


68-68: JSONRPCException may be undefined, or defined from star imports


69-69: assert_raises may be undefined, or defined from star imports


69-69: JSONRPCException may be undefined, or defined from star imports

qa/rpc-tests/spark_spend_gettransaction.py

2-2: from decimal import * used; unable to detect undefined names


5-5: from test_framework.util import * used; unable to detect undefined names


15-15: enable_mocktime may be undefined, or defined from star imports


16-16: start_nodes may be undefined, or defined from star imports


29-29: Local variable valid_address is assigned to but never used


55-55: Decimal may be undefined, or defined from star imports


65-65: Decimal may be undefined, or defined from star imports


77-77: assert_raises may be undefined, or defined from star imports


77-77: JSONRPCException may be undefined, or defined from star imports

qa/rpc-tests/llmq-is-spark.py

6-6: from test_framework.mininode import * used; unable to detect undefined names


8-8: test_framework.util.sync_blocks imported but unused


8-8: test_framework.util.set_node_times imported but unused


9-9: test_framework.util.isolate_node imported but unused


9-9: test_framework.util.reconnect_isolated_node imported but unused


9-9: test_framework.util.set_mocktime imported but unused


9-9: test_framework.util.get_mocktime imported but unused


10-10: test_framework.util.assert_equal imported but unused


11-11: test_framework.util.bitcoind_processes imported but unused


11-11: test_framework.util.start_nodes imported but unused


11-11: test_framework.util.start_node imported but unused


11-11: test_framework.util.connect_nodes_bi imported but unused


43-43: Statement ends with an unnecessary semicolon


56-56: Statement ends with an unnecessary semicolon

Additional comments not posted (19)
src/spark/sparkwallet.h (1)

117-117: The addition of vecSend parameter in CreateSparkMintTransactions method aligns with the PR's multi-send feature.

src/test/fixtures.cpp (1)

Line range hint 362-373: The initialization of vecSend and its use in MintAndStoreSpark correctly reflects the changes made in CSparkWallet. This ensures that the test setup aligns with the new functionality.

src/wallet/test/spark_tests.cpp (1)

79-80: The introduction of vecSend in various test cases correctly reflects the updated method signature in MintAndStoreSpark. The tests are well-structured to check the new functionality.

Also applies to: 185-186, 335-336, 426-427, 477-477

src/qt/sendcoinsdialog.cpp (4)

533-533: Confirm the handling of Spark transactions in non-anonymous mode.

Verification successful

Let's proceed by examining the implementation of the mintSparkCoins method in walletmodel.cpp to ensure it handles Spark transactions correctly.


Let's try a different approach to locate and review the implementation of the mintSparkCoins method in walletmodel.cpp.


The implementation of the mintSparkCoins method in walletmodel.cpp has been successfully retrieved. The method appears to handle Spark transactions by committing transactions and processing recipients' messages.

  • The method locks the wallet and main chain.
  • It iterates over the transactions and commits each one.
  • It handles messages from recipients and serializes the transaction.

This confirms that the mintSparkCoins method is designed to handle Spark transactions properly when fAnonymousMode is false, sparkAddressCount is greater than 0, and spark::IsSparkAllowed() returns true.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that Spark transactions are handled correctly when `fAnonymousMode` is false and `sparkAddressCount` is greater than 0.

# Test: Search for the method usage. Expect: Proper transaction handling.
rg --type cpp $'mintSparkCoins'

Length of output: 421



Script:

#!/bin/bash
# Description: Retrieve the implementation of the mintSparkCoins method in walletmodel.cpp to verify proper handling of Spark transactions.

# Extract the implementation of mintSparkCoins from walletmodel.cpp
ast-grep --lang cpp --pattern $'WalletModel::SendCoinsReturn WalletModel::mintSparkCoins($_, $_, $_) {
  $$$
}' 

Length of output: 112



Script:

#!/bin/bash
# Description: Retrieve the implementation of the mintSparkCoins method in walletmodel.cpp to verify proper handling of Spark transactions.

# Extract the implementation of mintSparkCoins from walletmodel.cpp using ripgrep to locate the function definition.
rg --type cpp --context 20 'WalletModel::SendCoinsReturn WalletModel::mintSparkCoins'

Length of output: 2834


491-491: Check the total amount calculation logic for various transaction scenarios.

Verification successful

The getTotalTransactionAmount method in walletmodeltransaction.cpp sums up the amounts from the recipients list. This logic appears straightforward and correct for calculating the total transaction amount.

The usage of this method in sendcoinsdialog.cpp and lelantusdialog.cpp also seems consistent with its purpose.

  • sendcoinsdialog.cpp:

    • mintSparkAmount += transaction.getTotalTransactionAmount();
    • totalAmount = currentTransaction.getTotalTransactionAmount() + txFee;
  • lelantusdialog.cpp:

    • allAmount += wtx.getTotalTransactionAmount();

These usages indicate that the total transaction amount is being correctly calculated and used in different contexts.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the total amount is calculated correctly for different transaction scenarios, especially when `fAnonymousMode` is toggled.

# Test: Search for the method usage. Expect: Correct total amount calculations.
rg --type cpp $'getTotalTransactionAmount'

Length of output: 492



Script:

#!/bin/bash
# Retrieve the implementation of getTotalTransactionAmount in walletmodeltransaction.cpp
ast-grep --lang cpp --pattern $'CAmount WalletModelTransaction::getTotalTransactionAmount() {
  $$$
}' --json

# Retrieve the usage of getTotalTransactionAmount in sendcoinsdialog.cpp
rg --type cpp 'getTotalTransactionAmount' src/qt/sendcoinsdialog.cpp

# Retrieve the usage of getTotalTransactionAmount in lelantusdialog.cpp
rg --type cpp 'getTotalTransactionAmount' src/qt/lelantusdialog.cpp

Length of output: 454



Script:

#!/bin/bash
# Retrieve the implementation of getTotalTransactionAmount in walletmodeltransaction.cpp
rg --type cpp 'CAmount WalletModelTransaction::getTotalTransactionAmount' src/qt/walletmodeltransaction.cpp

Length of output: 169



Script:

#!/bin/bash
# Retrieve the full implementation of getTotalTransactionAmount in walletmodeltransaction.cpp
rg --type cpp -A 10 -B 10 'CAmount WalletModelTransaction::getTotalTransactionAmount' src/qt/walletmodeltransaction.cpp

Length of output: 588


465-465: Validate the calculation of transaction fees and sizes for Spark transactions.


335-335: Ensure correct handling of Spark transactions when fAnonymousMode is false.

Verification successful

Ensure correct handling of Spark transactions when fAnonymousMode is false.

The prepareMintSparkTransaction method in src/qt/walletmodel.cpp correctly handles Spark transactions, including checks for valid addresses, amounts, and balance. The method prepares the transaction data and ensures that the transaction is created successfully, emitting error messages if any issues arise.

  • src/qt/walletmodel.cpp: Implementation of prepareMintSparkTransaction method.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the `prepareMintSparkTransaction` method correctly handles transactions when `fAnonymousMode` is false and `sparkAddressCount` is greater than 0.

# Test: Search for the method usage. Expect: Proper handling of transactions.
rg --type cpp $'prepareMintSparkTransaction'

Length of output: 571



Script:

#!/bin/bash
# Extract the implementation of the `prepareMintSparkTransaction` method from `src/qt/walletmodel.cpp`.
ast-grep --lang cpp --pattern $'SendCoinsReturn WalletModel::prepareMintSparkTransaction($_, $_, $_, $_, $_) {
  $$$
}' src/qt/walletmodel.cpp

Length of output: 143



Script:

#!/bin/bash
# Extract the implementation of the `prepareMintSparkTransaction` method from `src/qt/walletmodel.cpp` with a more flexible pattern.
ast-grep --lang cpp --pattern $'SendCoinsReturn WalletModel::prepareMintSparkTransaction($_, $_, $_, $_, $_) {
  $$$
}' src/qt/walletmodel.cpp

Length of output: 143



Script:

#!/bin/bash
# Search for the definition of `prepareMintSparkTransaction` in `src/qt/walletmodel.cpp` and extract the surrounding lines to get the full implementation.
rg --context 20 'SendCoinsReturn WalletModel::prepareMintSparkTransaction' src/qt/walletmodel.cpp

Length of output: 1482



Script:

#!/bin/bash
# Search for the definition of `prepareMintSparkTransaction` in `src/qt/walletmodel.cpp` and extract more surrounding lines to get the full implementation.
rg --context 50 'SendCoinsReturn WalletModel::prepareMintSparkTransaction' src/qt/walletmodel.cpp

Length of output: 3279



Script:

#!/bin/bash
# Search for the definition of `prepareMintSparkTransaction` in `src/qt/walletmodel.cpp` and extract even more surrounding lines to get the full implementation.
rg --context 100 'SendCoinsReturn WalletModel::prepareMintSparkTransaction' src/qt/walletmodel.cpp

Length of output: 7304

src/wallet/wallet.h (2)

1041-1041: Ensure the new method signature is consistent with its implementation and usage across the project.

Verification successful

The method signature for CreateSparkMintTransactions is consistent across the project. The header file, source file implementations, and usages all align with the new signature.

  • Header File: src/wallet/wallet.h
  • Source File Implementations: src/wallet/wallet.cpp, src/spark/sparkwallet.cpp
  • Usages: src/wallet/wallet.cpp, src/qt/walletmodel.cpp
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the new method signature for `CreateSparkMintTransactions` is used consistently across the project.

# Test: Search for the method usage. Expect: Only occurrences of the new signature.
ast-grep --lang cpp --pattern $'class CWallet {
  $$$
  CreateSparkMintTransactions($_, $_, $_, $_, $_, $_, $_, $_, $_, $_) {
    $$$
  }
  $$$  
}'

Length of output: 494



Script:

#!/bin/bash
# Description: Verify the method signature for `CreateSparkMintTransactions` in the header file and its implementation and usage across the project.

# Step 1: Search for the method signature in the header file.
echo "Searching for method signature in header files..."
ast-grep --lang cpp --pattern 'CreateSparkMintTransactions(const std::vector<CRecipient>&, $_, $_, $_, $_, $_, $_, $_, $_)' > header_signature.txt

# Step 2: Search for the method implementation in the source files.
echo "Searching for method implementation in source files..."
ast-grep --lang cpp --pattern 'CreateSparkMintTransactions(const std::vector<CRecipient>&, $_, $_, $_, $_, $_, $_, $_, $_) {' > source_implementation.txt

# Step 3: Search for the method usage across the project.
echo "Searching for method usage across the project..."
rg 'CreateSparkMintTransactions\(' > method_usage.txt

# Combine results for review.
echo "Combining results..."
cat header_signature.txt source_implementation.txt method_usage.txt

Length of output: 1634


1092-1092: Ensure the new method signature is consistent with its implementation and usage across the project.

src/spark/sparkwallet.cpp (2)

753-753: Ensure proper error handling for CreateSparkMintTransactions.


1267-1270: Verify logic for handling remaining transaction value.

src/wallet/rpcwallet.cpp (2)

3642-3658: Ensure the help text and examples are clear and accurate.

Verification successful

The help text and examples for the mintspark command in src/wallet/rpcwallet.cpp appear to be accurate and consistent with the function's implementation. The function definition and the help text both describe the command's usage, parameters, and expected results clearly.

  • The help text specifies the format for the mintspark command, including the address, amount, memo, and subtractfee parameters.
  • The examples provided in the help text demonstrate typical use cases for the mintspark command, including sending amounts to transparent and private addresses.

Based on the extracted implementation and the provided help text, no discrepancies were found.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the accuracy of the help text and examples for the `mintspark` command.

# Test: Search for the `mintspark` command usage in the documentation and compare with the help text.
rg --type markdown $'mintspark'

Length of output: 30



Script:

#!/bin/bash
# Description: Locate the implementation of the `mintspark` command in the codebase and review its parameters and usage.

# Search for the `mintspark` command implementation in the codebase.
ast-grep --lang cpp --pattern $'void mintspark($_) {
  $$$
}'

Length of output: 62



Script:

#!/bin/bash
# Description: Search for the `mintspark` keyword in the codebase to locate its definition and usage.

# Search for the `mintspark` keyword in the codebase.
rg 'mintspark'

Length of output: 4136



Script:

#!/bin/bash
# Description: Extract the implementation of the `mintspark` function from `src/wallet/rpcwallet.cpp`.

# Extract the implementation of the `mintspark` function.
ast-grep --lang cpp --pattern $'UniValue mintspark(const JSONRPCRequest& request) {
  $$$
}' src/wallet/rpcwallet.cpp

Length of output: 118



Script:

#!/bin/bash
# Description: Search for the `mintspark` function definition in `src/wallet/rpcwallet.cpp`.

# Search for the `mintspark` function definition.
rg 'UniValue mintspark' src/wallet/rpcwallet.cpp

Length of output: 100



Script:

#!/bin/bash
# Description: Extract the full implementation of the `mintspark` function from `src/wallet/rpcwallet.cpp`.

# Extract the full implementation of the `mintspark` function.
rg -A 50 'UniValue mintspark' src/wallet/rpcwallet.cpp

Length of output: 4071


3675-3755: Review the logic for handling Spark and Bitcoin addresses, validating parameters, and preparing data for transaction creation.

Verification successful

The verification process has confirmed the presence and implementation of the functions and classes involved in the logic for handling Spark and Bitcoin addresses, validating parameters, and preparing data for transaction creation. The following points summarize the findings:

  • The decode method for Spark addresses is implemented in src/libspark/keys.cpp.
  • The CBitcoinAddress class and its IsValid method are implemented in src/base58.cpp.
  • The AmountFromValue function is implemented in src/rpc/server.cpp.
  • The MintAndStoreSpark method is implemented in src/wallet/wallet.cpp.

These implementations confirm that the logic in the provided code snippet is supported by the necessary functions and classes.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the logic for handling Spark and Bitcoin addresses, validating parameters, and preparing data for transaction creation.

# Test: Search for similar logic in other parts of the codebase and compare for consistency.
rg --type cpp $'address handling'

Length of output: 34



Script:

#!/bin/bash
# Description: Gather information about the implementation of sAddress.decode, CBitcoinAddress, AmountFromValue, and MintAndStoreSpark.

# Find the implementation of sAddress.decode
ast-grep --lang cpp --pattern $'class Address {
  $$$
  decode($_) {
    $$$
  }
  $$$
}'

# Find the implementation of CBitcoinAddress
ast-grep --lang cpp --pattern $'class CBitcoinAddress {
  $$$
  IsValid() {
    $$$
  }
  $$$
}'

# Find the implementation of AmountFromValue
ast-grep --lang cpp --pattern $'AmountFromValue($_) {
  $$$
}'

# Find the implementation of MintAndStoreSpark
ast-grep --lang cpp --pattern $'class CWallet {
  $$$
  MintAndStoreSpark($_, $_, $_, $_) {
    $$$
  }
  $$$
}'

Length of output: 1401



Script:

#!/bin/bash
# Description: Gather information about the implementation of sAddress.decode, CBitcoinAddress, AmountFromValue, and MintAndStoreSpark with refined patterns.

# Find the implementation of sAddress.decode
ast-grep --lang cpp --pattern $'class $_ {
  $$$
  unsigned char decode($_) {
    $$$
  }
  $$$
}'

# Find the implementation of CBitcoinAddress
ast-grep --lang cpp --pattern $'class CBitcoinAddress {
  $$$
  bool IsValid() const {
    $$$
  }
  $$$
}'

# Find the implementation of AmountFromValue
ast-grep --lang cpp --pattern $'CAmount AmountFromValue(const UniValue& value) {
  $$$
}'

# Find the implementation of MintAndStoreSpark
ast-grep --lang cpp --pattern $'class CWallet {
  $$$
  std::string MintAndStoreSpark(const std::vector<CRecipient>& recipients, const std::vector<spark::MintedCoinData>& outputs, std::vector<std::pair<CWalletTx, CAmount>>& wtxAndFee, bool subtractFeeFromAmount) {
    $$$
  }
  $$$
}'

Length of output: 1621



Script:

#!/bin/bash
# Description: Gather information about the implementation of sAddress.decode, CBitcoinAddress, AmountFromValue, and MintAndStoreSpark using rg.

# Search for the implementation of sAddress.decode
rg 'decode' --type cpp

# Search for the implementation of CBitcoinAddress
rg 'CBitcoinAddress' --type cpp

# Search for the implementation of AmountFromValue
rg 'AmountFromValue' --type cpp

# Search for the implementation of MintAndStoreSpark
rg 'MintAndStoreSpark' --type cpp

Length of output: 62035

src/wallet/wallet.cpp (6)

Line range hint 1-1: LGTM! Simple and straightforward addition operation.


5612-5612: LGTM! Updated function signature aligns with the PR's objectives.


5646-5646: LGTM! Proper use of the updated CreateSparkMintTransactions function.


8176-8176: LGTM! Consistent update to function signature.


8187-8187: LGTM! Correct usage of the updated CreateSparkMintTransactions.


5927-5927: Verify the intention behind using empty vectors in MintAndStoreSpark.

Verification successful

Verify the context and intention behind using empty vectors in MintAndStoreSpark by examining the function definition and relevant test cases.


Correct the regex pattern in the rg command and re-run the ast-grep command to gather the necessary information.


The use of empty vectors {} in MintAndStoreSpark is intentional and validated through test cases. The tests check for expected outcomes, indicating that this usage is correct and purposeful.

  • [src/wallet/test/spark_tests.cpp]
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check for other occurrences of MintAndStoreSpark to ensure consistency.

# Test: Search for other uses of MintAndStoreSpark. Expect: Consistent usage or clear documentation when empty vectors are passed.
rg --type cpp $'MintAndStoreSpark'

Length of output: 2187



Script:

#!/bin/bash
# Description: Retrieve the definition of MintAndStoreSpark and relevant test cases to understand the usage of empty vectors.

# Retrieve the function definition from wallet.cpp
ast-grep --lang cpp --pattern $'std::string CWallet::MintAndStoreSpark($_, $_, $_, $_, $_, $_, $_) {
  $$$
}' src/wallet/wallet.cpp

# Retrieve the relevant test cases from spark_tests.cpp
rg --type cpp --context 10 $'MintAndStoreSpark({}, {}, wtxAndFee, false, true)' src/wallet/test/spark_tests.cpp

Length of output: 388



Script:

#!/bin/bash
# Description: Retrieve the definition of MintAndStoreSpark and relevant test cases to understand the usage of empty vectors.

# Retrieve the function definition from wallet.cpp
ast-grep --lang cpp --pattern $'std::string CWallet::MintAndStoreSpark($_, $_, $_, $_, $_, $_, $_) {
  $$$
}' src/wallet/wallet.cpp

# Retrieve the relevant test cases from spark_tests.cpp with corrected regex pattern
rg --type cpp --context 10 'MintAndStoreSpark\(\{\}, \{\}, wtxAndFee, false, true\)' src/wallet/test/spark_tests.cpp

Length of output: 1581

Comment on lines +606 to +608
std::vector<CRecipient> vecSend;
std::vector<spark::MintedCoinData> mints{{address, 50*COIN, ""}};
std::string error = pwalletMain->MintAndStoreSpark(mints, wtxAndFee, false);
std::string error = pwalletMain->MintAndStoreSpark(vecSend, mints, wtxAndFee, false);
Copy link

Choose a reason for hiding this comment

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

Consider adding error handling for MintAndStoreSpark.

The function MintAndStoreSpark is called without handling potential errors. It's good practice to handle or assert expected outcomes to ensure robustness, especially in test environments. Consider adding error handling or assertions to check the return value.

Comment on lines +765 to +774
CAmount nValue = 0;
for (const auto& recipient : vecSend)
{
if (nValue < 0 || recipient.nAmount < 0)
{
strFailReason = _("Transaction amounts must not be negative");
return false;
}
nValue += recipient.nAmount;
}
Copy link

Choose a reason for hiding this comment

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

Check for negative transaction amounts.

The check for negative transaction amounts is crucial to prevent erroneous or malicious inputs. Ensure that this validation is robust and consider adding more detailed error messages or handling specific edge cases.

@@ -774,11 +784,14 @@
assert(txNew.nLockTime <= (unsigned int) chainActive.Height());
assert(txNew.nLockTime < LOCKTIME_THRESHOLD);
std::vector<spark::MintedCoinData> outputs_ = outputs;
std::vector<CRecipient> sendOutputs = vecSend;
Copy link

Choose a reason for hiding this comment

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

Refactor to improve clarity and maintainability.

Consider renaming valueToSendInTx to valueToProcessInTx to better reflect its usage in the context of both sending and processing transactions.

Also applies to: 825-825

Comment on lines +837 to +846
sendValue = valueToSendInTx;
if (subtractFeeFromAmount)
nValueToSelect = mintedValue;
nValueToSelect = sendValue;
else
nValueToSelect = mintedValue + nFeeRet;
nValueToSelect = sendValue + nFeeRet;

// if no enough coins in this group then subtract fee from mint
if (nValueToSelect > itr->first && !subtractFeeFromAmount) {
nValueToSelect = mintedValue;
mintedValue -= nFeeRet;
nValueToSelect = sendValue;
sendValue -= nFeeRet;
Copy link

Choose a reason for hiding this comment

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

Optimize fee calculation logic.

The fee calculation logic is repeated multiple times. Consider extracting this into a separate method to reduce redundancy and improve code maintainability.

}

if (!MoneyRange(mintedValue) || mintedValue == 0) {
if (!MoneyRange(sendValue) || sendValue == 0) {
Copy link

Choose a reason for hiding this comment

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

Handle potential division by zero in fee distribution.

There is a potential risk of division by zero when calculating singleFee. Ensure that totalOutputs is never zero before performing the division.

Comment on lines +911 to +939
size_t totalOutputs = singleTxOutputs.size() + singleTxSendOutputs.size();
CAmount singleFee = nFeeRet / totalOutputs;
CAmount remainder = nFeeRet % totalOutputs;

for (size_t i = 0; i < singleTxOutputs.size(); ++i) {
if (singleTxOutputs[i].v <= singleFee) {
remainder += singleTxOutputs[i].v - singleFee;
singleTxOutputs.erase(singleTxOutputs.begin() + i);
reminder += singleTxOutputs[i].v - singleFee;
--i;
} else {
singleTxOutputs[i].v -= singleFee;
if (remainder > 0 && singleTxOutputs[i].v > remainder) {
singleTxOutputs[i].v -= remainder;
remainder = 0;
}
}
singleTxOutputs[i].v -= singleFee;
if (reminder > 0 && singleTxOutputs[i].v > nFeeRet % singleTxOutputs.size()) {// first receiver pays the remainder not divisible by output count
singleTxOutputs[i].v -= reminder;
reminder = 0;
}

for (size_t i = 0; i < singleTxSendOutputs.size(); ++i) {
if (singleTxSendOutputs[i].nAmount <= singleFee) {
remainder += singleTxSendOutputs[i].nAmount - singleFee;
singleTxSendOutputs.erase(singleTxSendOutputs.begin() + i);
--i;
} else {
singleTxSendOutputs[i].nAmount -= singleFee;
if (remainder > 0 && singleTxSendOutputs[i].nAmount > remainder) {
singleTxSendOutputs[i].nAmount -= remainder;
remainder = 0;
}
Copy link

Choose a reason for hiding this comment

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

Refine fee subtraction logic for clarity.

The logic for subtracting fees from transaction outputs is complex and could be simplified. Consider breaking down the operations into smaller, more manageable functions or adding more comments to clarify the steps involved.

src/spark/sparkwallet.cpp Show resolved Hide resolved
src/wallet/wallet.cpp Show resolved Hide resolved
src/wallet/rpcwallet.cpp Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant