Skip to content

Commit

Permalink
Apply state changes in a database transaction (#1445)
Browse files Browse the repository at this point in the history
I'm hoping this avoids us making incomplete mutations to the state
trie, leading to inconsistent data. From my limited local testing,
it appears to make the problem go away.
  • Loading branch information
JamesHinshelwood authored Sep 13, 2024
1 parent 3094673 commit 8f437c5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Unreleased changes, in reverse chronological order. New entries are added at the top of this list.

- [#1445](https://github.com/Zilliqa/zq2/pull/1445): Apply state changes from transactions atomically.
- [#1448](https://github.com/Zilliqa/zq2/pull/1448): Fail gracefully if we are unable to parse a gossipsub message.
- [#1423](https://github.com/Zilliqa/zq2/pull/1423): Fix invalid value returned by calling `_balance` in a scilla transition.

Expand Down
16 changes: 14 additions & 2 deletions zilliqa/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rusqlite::{
Connection, OptionalExtension, Row, ToSql,
};
use serde::{Deserialize, Serialize};
use sled::{Batch, Tree};
use sled::{transaction::TransactionError, Batch, Tree};
use tracing::warn;

use crate::{
Expand Down Expand Up @@ -888,7 +888,19 @@ impl eth_trie::DB for TrieStorage {
for (key, value) in keys.into_iter().zip(values) {
batch.insert(key, value);
}
self.db.apply_batch(batch)?;
if let Err(e) = self.db.transaction(|db| {
db.apply_batch(&batch)?;
Ok(())
}) {
match e {
TransactionError::Abort(()) => {
unreachable!("we don't call `abort` during this transaction")
}
TransactionError::Storage(e) => {
return Err(e);
}
}
}
Ok(())
}

Expand Down

0 comments on commit 8f437c5

Please sign in to comment.