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

BLUE-130 Global txreceipt verification #80

Merged
merged 5 commits into from
Oct 25, 2024
Merged

Conversation

jairajdev
Copy link
Contributor

No description provided.

Copy link

PR Reviewer Guide 🔍

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Key issues to review

Code Complexity
The method validateArchiverReceipt has significantly increased in complexity with multiple nested conditions and loops, making it harder to maintain and understand. Consider refactoring to reduce complexity and improve readability.

Error Handling
The error handling in validateArchiverReceipt logs errors but does not propagate them or handle them in a way that allows for recovery or alternative flows. This could lead to unhandled states in production.

Possible Bug
The condition if (isValid) in the method verifyReceiptData seems incorrect. It logs an error if isValid is true, which is counterintuitive. This might be a logic error.

Code Duplication
The method verifyGlobalTxAccountChange contains duplicated logic for checking account hashes before and after a transaction. This could be abstracted into a separate method to avoid duplication and potential errors in updates.

src/Data/Collector.ts Dismissed Show dismissed Hide dismissed
@S0naliThakur S0naliThakur force-pushed the global-txreceipt-verification branch 5 times, most recently from 93334f1 to 170baf0 Compare October 18, 2024 17:22
const { signs } = appliedReceipt
// Refer to https://github.com/shardeum/shardus-core/blob/7d8877b7e1a5b18140f898a64b932182d8a35298/src/p2p/GlobalAccounts.ts#L397
let votingGroupCount = cycleShardData.shardGlobals.nodesPerConsenusGroup
if (votingGroupCount > cycleShardData.nodes.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have a lower bound to the acceptable voting group count?

Copy link
Member

Choose a reason for hiding this comment

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

addressed

if (votingGroupCount > cycleShardData.nodes.length) {
votingGroupCount = cycleShardData.nodes.length
}
let isReceiptMajority = (signs.length / votingGroupCount) * 100 >= 60
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make the percentage for majority configurable?

Copy link
Member

Choose a reason for hiding this comment

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

config requiredMajorityVotesPercentage introduced for the same

for (const sign of signs) {
const { owner: nodePubKey } = sign
// Get the node id from the public key
const node = cycleShardData.nodes.find((node) => node.publicKey === nodePubKey)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to avoid this find operation on every iteration? This loop feels suboptimal.

Copy link
Member

Choose a reason for hiding this comment

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

using Map now for reduced complexity

}
uniqueSigners.add(nodePubKey)
}
isReceiptMajority = (uniqueSigners.size / votingGroupCount) * 100 >= 60
Copy link
Contributor

Choose a reason for hiding this comment

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

Move to majority number to config here too.

Copy link
Member

Choose a reason for hiding this comment

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

done

const { signs, tx } = appliedReceipt
// Using a map to store the good signatures to avoid duplicates
const goodSignatures = new Map()
for (const sign of signs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we not need to exclude signatures from nodes not part of the consensus group here like on the previous file? Or are we filtering the signatures earlier and they are not reaching here if they are from invalid nodes?

Copy link
Contributor

Choose a reason for hiding this comment

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

100 - consensus group size
60 - required

Not part of the node list: 10
Not part of the partition group: 5
85>60

sign verify
85>60

Copy link
Member

Choose a reason for hiding this comment

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

The signatures were not getting filtered earlier, only the required no of signatures was being calculated based on the valid node count. Have introduced the filtering in this section now.

Penalty = 12,
}

export const verifyGlobalTxAccountChange = (
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we write unit tests for this function?

jairajdev and others added 2 commits October 25, 2024 13:16
… debug logs for account query

Adding global-txreceipt-verification

Added global tx receipt validation and verification

Added account verification for global tx receipt

Updated the new poqo receipt change
fix receipt verification
(signs.length / votingGroupCount) * 100 >= config.requiredMajorityVotesPercentage
if (!isReceiptMajority) {
Logger.mainLogger.error(
`Invalid receipt globalModification signs count is less than ${config.requiredMajorityVotesPercentage}% of the votingGroupCount, ${signs.length}, ${votingGroupCount}`

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.

Copilot Autofix AI 8 days ago

To fix the log injection issue, we need to sanitize the user input before logging it. Specifically, we should remove any newline characters from the config.requiredMajorityVotesPercentage value before including it in the log message. This can be done using String.prototype.replace to ensure no line endings are present in the user input.

Suggested changeset 1
src/Data/Collector.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Data/Collector.ts b/src/Data/Collector.ts
--- a/src/Data/Collector.ts
+++ b/src/Data/Collector.ts
@@ -494,3 +494,3 @@
         Logger.mainLogger.error(
-          `Invalid receipt globalModification signs count is less than ${config.requiredMajorityVotesPercentage}% of the votingGroupCount, ${signs.length}, ${votingGroupCount}`
+          `Invalid receipt globalModification signs count is less than ${config.requiredMajorityVotesPercentage.replace(/\n|\r/g, "")}% of the votingGroupCount, ${signs.length}, ${votingGroupCount}`
         )
EOF
@@ -494,3 +494,3 @@
Logger.mainLogger.error(
`Invalid receipt globalModification signs count is less than ${config.requiredMajorityVotesPercentage}% of the votingGroupCount, ${signs.length}, ${votingGroupCount}`
`Invalid receipt globalModification signs count is less than ${config.requiredMajorityVotesPercentage.replace(/\n|\r/g, "")}% of the votingGroupCount, ${signs.length}, ${votingGroupCount}`
)
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
return result
}

const nodeMap = new Map<string, P2PTypes.NodeListTypes.Node>()
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this map is only going to change once per cycle we can move this up the level for further optimisation. But for now LGTM.

arhamj
arhamj previously approved these changes Oct 25, 2024
arhamj
arhamj previously approved these changes Oct 25, 2024
@arhamj arhamj merged commit 8145e6f into dev Oct 25, 2024
6 checks passed
urnotsam pushed a commit that referenced this pull request Oct 30, 2024
* Added composite indexes ( cycle and timesamp ) on each data and added debug logs for account query

Adding global-txreceipt-verification

Added global tx receipt validation and verification

Added account verification for global tx receipt

Updated the new poqo receipt change

* fix empty cycleInfo

fix receipt verification

* enhance node validation

* shardus/types version update- 1.2.21

* fix(lint): fixed list error on votingGroupCount

---------

Co-authored-by: Sonali Thakur <[email protected]>
Co-authored-by: Arham Jain <[email protected]>
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.

3 participants