-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Implement balance checks for L2PS transactions to ensure suffic… #680
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
Open
Shitikyan
wants to merge
1
commit into
testnet
Choose a base branch
from
fix-l2ps-balance-check
base: testnet
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| import type { BlockContent, L2PSTransaction, RPCResponse } from "@kynesyslabs/demosdk/types" | ||
| import type { BlockContent, L2PSTransaction, RPCResponse, INativePayload } from "@kynesyslabs/demosdk/types" | ||
| import Chain from "src/libs/blockchain/chain" | ||
| import Transaction from "src/libs/blockchain/transaction" | ||
| import { emptyResponse } from "../../server_rpc" | ||
|
|
||
| import { L2PS, L2PSEncryptedPayload } from "@kynesyslabs/demosdk/l2ps" | ||
| import ParallelNetworks from "@/libs/l2ps/parallelNetworks" | ||
| import L2PSMempool from "@/libs/blockchain/l2ps_mempool" | ||
| import L2PSTransactionExecutor from "@/libs/l2ps/L2PSTransactionExecutor" | ||
| import L2PSTransactionExecutor, { L2PS_TX_FEE } from "@/libs/l2ps/L2PSTransactionExecutor" | ||
| import log from "@/utilities/logger" | ||
|
|
||
| /** | ||
|
|
@@ -72,6 +72,38 @@ async function decryptAndValidate( | |
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Check sender balance before mempool insertion. | ||
| * Returns an error message if balance is insufficient, null if OK. | ||
| */ | ||
| async function checkSenderBalance(decryptedTx: Transaction): Promise<string | null> { | ||
| const sender = decryptedTx.content.from as string | ||
| if (!sender) return "Missing sender address in decrypted transaction" | ||
|
|
||
| // Extract amount from native payload | ||
| let amount = 0 | ||
| if (decryptedTx.content.type === "native" && Array.isArray(decryptedTx.content.data)) { | ||
| const nativePayload = decryptedTx.content.data[1] as INativePayload | ||
| if (nativePayload?.nativeOperation === "send") { | ||
| const [, sendAmount] = nativePayload.args as [string, number] | ||
| amount = sendAmount || 0 | ||
| } | ||
| } | ||
|
|
||
| const totalRequired = amount + L2PS_TX_FEE | ||
| try { | ||
| const balance = await L2PSTransactionExecutor.getBalance(sender) | ||
| if (balance < BigInt(totalRequired)) { | ||
| return `Insufficient balance: need ${totalRequired} (${amount} + ${L2PS_TX_FEE} fee) but have ${balance}` | ||
| } | ||
| } catch (error) { | ||
| return `Balance check failed: ${error instanceof Error ? error.message : "Unknown error"}` | ||
| } | ||
|
Comment on lines
+80
to
+102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. L2ps fee/balance check inconsistent The new L2PS balance pre-checks always add L2PS_TX_FEE (even when the executor does not charge any fee for that tx type), and they don’t validate sendAmount like the executor does. This can incorrectly reject L2PS transactions that would otherwise execute successfully (e.g., non-send native ops, or non-native txs with only gcr_edits) and can also produce misleading errors/behavior when the amount is malformed. Agent Prompt
|
||
|
|
||
| return null | ||
| } | ||
|
|
||
| export default async function handleL2PS( | ||
| l2psTx: L2PSTransaction, | ||
| ): Promise<RPCResponse> { | ||
|
|
@@ -111,6 +143,13 @@ export default async function handleL2PS( | |
| return createErrorResponse(response, 400, `Decrypted transaction hash mismatch: expected ${originalHash}, got ${decryptedTx.hash}`) | ||
| } | ||
|
|
||
| // Pre-check sender balance BEFORE mempool insertion | ||
| const balanceError = await checkSenderBalance(decryptedTx) | ||
| if (balanceError) { | ||
| log.error(`[handleL2PS] Balance pre-check failed: ${balanceError}`) | ||
| return createErrorResponse(response, 400, balanceError) | ||
| } | ||
|
|
||
| // Process Valid Transaction | ||
| return await processValidL2PSTransaction(response, l2psUid, l2psTx, decryptedTx, originalHash) | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
1. Logging can throw on failure
🐞 Bug⛯ ReliabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools