Skip to content

Commit b9dbae4

Browse files
bmuddhaDodecahedr0x
authored andcommitted
feat: fail transaction if account commit failed (#796)
1 parent fe13970 commit b9dbae4

File tree

19 files changed

+421
-194
lines changed

19 files changed

+421
-194
lines changed

Cargo.lock

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ resolver = "2"
3939

4040
[workspace.package]
4141
# Solana Version (2.2.x)
42-
version = "0.5.1"
42+
version = "0.5.2"
4343
authors = ["MagicBlock Maintainers <[email protected]>"]
4444
repository = "https://github.com/magicblock-labs/ephemeral-validator"
4545
homepage = "https://www.magicblock.xyz"
@@ -144,7 +144,7 @@ serde_json = "1.0"
144144
serde_with = "3.16"
145145
serial_test = "3.2"
146146
sha3 = "0.10.8"
147-
solana-account = { git = "https://github.com/magicblock-labs/solana-account.git", rev = "57158728" }
147+
solana-account = { git = "https://github.com/magicblock-labs/solana-account.git", rev = "2246929" }
148148
solana-account-decoder = { version = "2.2" }
149149
solana-account-decoder-client-types = { version = "2.2" }
150150
solana-account-info = { version = "2.2" }
@@ -235,7 +235,7 @@ features = ["lz4"]
235235
# some solana dependencies have solana-storage-proto as dependency
236236
# we need to patch them with our version, because they use protobuf-src v1.1.0
237237
# and we use protobuf-src v2.1.1. Otherwise compilation fails
238-
solana-account = { git = "https://github.com/magicblock-labs/solana-account.git", rev = "57158728" }
238+
solana-account = { git = "https://github.com/magicblock-labs/solana-account.git", rev = "2246929" }
239239
solana-storage-proto = { path = "./storage-proto" }
240240
solana-svm = { git = "https://github.com/magicblock-labs/magicblock-svm.git", rev = "3e9456ec4" }
241241
# Fork is used to enable `disable_manual_compaction` usage

magicblock-accounts-db/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ impl From<lmdb::Error> for AccountsDbError {
2626
#[macro_export]
2727
macro_rules! log_err {
2828
($msg: expr) => {
29-
|err| ::log::warn!("{} error: {err}", $msg)
29+
|err| ::log::error!("{} error: {err}", $msg)
3030
};
3131
($msg: expr, $($ctx:expr),* $(,)?) => {
32-
|err| ::log::warn!("{} error: {err}", format!($msg, $($ctx),*))
32+
|err| ::log::error!("{} error: {err}", format!($msg, $($ctx),*))
3333
};
3434
}

magicblock-accounts-db/src/index.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ impl AccountsDbIndex {
168168
pubkey: &Pubkey,
169169
owner: &Pubkey,
170170
allocation: Allocation,
171+
txn: &mut RwTransaction,
171172
) -> AccountsDbResult<Option<ExistingAllocation>> {
172173
let Allocation { offset, blocks, .. } = allocation;
173174

174-
let mut txn = self.env.begin_rw_txn()?;
175175
let mut dealloc = None;
176176

177177
// merge offset and block count into one single u64 and cast it to [u8; 8]
@@ -181,23 +181,21 @@ impl AccountsDbIndex {
181181

182182
// optimisitically try to insert account to index, assuming that it doesn't exist
183183
let inserted =
184-
self.accounts
185-
.put_if_not_exists(&mut txn, pubkey, index_value)?;
184+
self.accounts.put_if_not_exists(txn, pubkey, index_value)?;
186185
// if the account does exist, then it already occupies space in main storage
187186
if !inserted {
188187
// in which case we just move the account to a new allocation
189188
// adjusting all of the offsets and cleaning up the older ones
190189
let previous =
191-
self.reallocate_account(pubkey, &mut txn, &index_value)?;
190+
self.reallocate_account(pubkey, txn, &index_value)?;
192191
dealloc.replace(previous);
193192
};
194193

195194
// track the account via programs' index as well
196-
self.programs.put(&mut txn, owner, offset_and_pubkey)?;
195+
self.programs.put(txn, owner, offset_and_pubkey)?;
197196
// track the reverse relation between account and its owner
198-
self.owners.put(&mut txn, pubkey, owner)?;
197+
self.owners.put(txn, pubkey, owner)?;
199198

200-
txn.commit()?;
201199
Ok(dealloc)
202200
}
203201

@@ -267,9 +265,9 @@ impl AccountsDbIndex {
267265
&self,
268266
pubkey: &Pubkey,
269267
owner: &Pubkey,
268+
txn: &mut RwTransaction,
270269
) -> AccountsDbResult<()> {
271-
let txn = self.env.begin_ro_txn()?;
272-
let old_owner = match self.owners.get(&txn, pubkey)? {
270+
let old_owner = match self.owners.get(txn, pubkey)? {
273271
// if current owner matches with that stored in index, then we are all set
274272
Some(val) if owner.as_ref() == val => {
275273
return Ok(());
@@ -278,23 +276,21 @@ impl AccountsDbIndex {
278276
// if they don't match, then we have to remove old entries and create new ones
279277
Some(val) => Pubkey::try_from(val).ok(),
280278
};
281-
let allocation = self.get_allocation(&txn, pubkey)?;
282-
let mut txn = self.env.begin_rw_txn()?;
279+
let allocation = self.get_allocation(txn, pubkey)?;
283280
// cleanup `programs` and `owners` index
284281
self.remove_programs_index_entry(
285282
pubkey,
286283
old_owner,
287-
&mut txn,
284+
txn,
288285
allocation.offset,
289286
)?;
290287
// track new owner of the account via programs' index
291288
let offset_and_pubkey =
292289
bytes!(#pack, allocation.offset, Offset, *pubkey, Pubkey);
293-
self.programs.put(&mut txn, owner, offset_and_pubkey)?;
290+
self.programs.put(txn, owner, offset_and_pubkey)?;
294291
// track the reverse relation between account and its owner
295-
self.owners.put(&mut txn, pubkey, owner)?;
296-
297-
txn.commit().map_err(Into::into)
292+
self.owners.put(txn, pubkey, owner)?;
293+
Ok(())
298294
}
299295

300296
fn remove_programs_index_entry(
@@ -379,9 +375,9 @@ impl AccountsDbIndex {
379375
pub(crate) fn try_recycle_allocation(
380376
&self,
381377
space: Blocks,
378+
txn: &mut RwTransaction,
382379
) -> AccountsDbResult<ExistingAllocation> {
383-
let mut txn = self.env.begin_rw_txn()?;
384-
let mut cursor = self.deallocations.cursor_rw(&mut txn)?;
380+
let mut cursor = self.deallocations.cursor_rw(txn)?;
385381
// this is a neat lmdb trick where we can search for entry with matching
386382
// or greater key since we are interested in any allocation of at least
387383
// `blocks` size or greater, this works perfectly well for this case
@@ -402,9 +398,6 @@ impl AccountsDbIndex {
402398
cursor.put(&remainder.to_le_bytes(), &index_value, WEMPTY)?;
403399
}
404400

405-
drop(cursor);
406-
txn.commit()?;
407-
408401
Ok(ExistingAllocation { offset, blocks })
409402
}
410403

@@ -438,6 +431,11 @@ impl AccountsDbIndex {
438431
};
439432
self.deallocations.entries(&txn)
440433
}
434+
435+
/// Initiate RW Transaction
436+
pub(crate) fn rwtxn(&self) -> lmdb::Result<RwTransaction<'_>> {
437+
self.env.begin_rw_txn()
438+
}
441439
}
442440

443441
pub(crate) mod iterator;

0 commit comments

Comments
 (0)