Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions magicblock-api/src/fund_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub(crate) fn fund_account_with_data(
) {
let account = if let Some(mut acc) = accountsdb.get_account(pubkey) {
acc.set_lamports(lamports);
acc.set_data(vec![0; size]);
acc
} else {
AccountSharedData::new(lamports, size, &Default::default())
Expand Down Expand Up @@ -70,16 +69,20 @@ pub(crate) fn funded_faucet(
}

pub(crate) fn fund_magic_context(accountsdb: &AccountsDb) {
const CONTEXT_LAMPORTS: u64 = u64::MAX;

fund_account_with_data(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's better to tweak the fund_account_with_data method to prevent any mutation if account already exists, all use cases assume that anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If that is the case, then it is better. Do we want to refill lamports for all of them tho?

Copy link
Collaborator

Choose a reason for hiding this comment

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

no, in case of EPHEMERAL VAULT account that would be a logic error, so we want to keep the state intact between restarts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

accountsdb,
&magic_program::MAGIC_CONTEXT_PUBKEY,
u64::MAX,
CONTEXT_LAMPORTS,
MagicContext::SIZE,
);
let mut magic_context = accountsdb
.get_account(&magic_program::MAGIC_CONTEXT_PUBKEY)
.unwrap();
.expect("magic context should have been created");
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

.expect() in production code is a MAJOR issue per project coding guidelines.

After fund_account_with_data, the account must exist — but only if the underlying insert_account succeeded. Because the result of insert_account is silently discarded on line 37 (let _ = …), a storage failure would cause this expect to panic rather than propagate a meaningful error. The function signature fn fund_magic_context(accountsdb: &AccountsDb) should return ApiResult<()> (or equivalent) so both the insertion and this retrieval can surface errors.

As per coding guidelines: "Treat any usage of .unwrap() or .expect() in production Rust code as a MAJOR issue."

🔧 Sketch of error-propagating refactor
-pub(crate) fn fund_magic_context(accountsdb: &AccountsDb) {
+pub(crate) fn fund_magic_context(accountsdb: &AccountsDb) -> ApiResult<()> {
     const CONTEXT_LAMPORTS: u64 = u64::MAX;

-    fund_account_with_data(
+    fund_account_with_data_result(
         accountsdb,
         &magic_program::MAGIC_CONTEXT_PUBKEY,
         CONTEXT_LAMPORTS,
         MagicContext::SIZE,
-    );
+    )?;
     let mut magic_context = accountsdb
         .get_account(&magic_program::MAGIC_CONTEXT_PUBKEY)
-        .expect("magic context should have been created");
+        .ok_or_else(|| /* appropriate ApiError variant */ ...)?;
     magic_context.set_delegated(true);
     magic_context.set_owner(magic_program::ID);

-    let _ = accountsdb
+    accountsdb
         .insert_account(&magic_program::MAGIC_CONTEXT_PUBKEY, &magic_context);
+    Ok(())
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@magicblock-api/src/fund_account.rs` at line 82, The code currently calls
.expect() in fund_magic_context which can panic if insert_account failed (its
Result was discarded with let _ = ...); change fund_magic_context(accountsdb:
&AccountsDb) to return ApiResult<()> (or the crate's error type), stop
discarding the Result from insert_account (use ? to propagate errors), and
replace the .expect("magic context should have been created") retrieval with a
fallible call that uses ? to return a proper error instead of panicking; update
callers to handle the ApiResult<()> accordingly (references: function
fund_magic_context, the earlier insert_account call, and the retrieval after
fund_account_with_data).

magic_context.set_delegated(true);
magic_context.set_owner(magic_program::ID);

let _ = accountsdb
.insert_account(&magic_program::MAGIC_CONTEXT_PUBKEY, &magic_context);
}
Expand Down
68 changes: 34 additions & 34 deletions test-integration/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading