Skip to content

Commit

Permalink
Clean committed_nonce table after revert
Browse files Browse the repository at this point in the history
Signed-off-by: deniallugo <[email protected]>
  • Loading branch information
Deniallugo committed Mar 17, 2022
1 parent 2703485 commit f714b2d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
6 changes: 6 additions & 0 deletions core/bin/block_revert/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ async fn revert_blocks_in_storage(
.return_executed_txs_to_mempool(last_block)
.await?;
println!("`mempool_txs`, `executed_transactions` tables are updated");
transaction
.chain()
.state_schema()
.clean_current_nonce_table(last_block)
.await?;
println!("`committed_nonce` table is updated");
transaction
.chain()
.block_schema()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE IF NOT EXISTS committed_nonce
(
account_id bigint not null primary key,
nonce bigint not null
nonce bigint not null,
block_number bigint not null
);
39 changes: 26 additions & 13 deletions core/lib/storage/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -913,19 +913,6 @@
]
}
},
"152ce07704066b1c5002831ca7187dbe14073b30f9e5f5db92f50a925621b86f": {
"query": "INSERT INTO committed_nonce (account_id, nonce) VALUES ($1, $2) \n ON CONFLICT (account_id) \n DO UPDATE \n SET nonce = $2\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8",
"Int8"
]
},
"nullable": []
}
},
"15faacf14edd991dedc35011ef12eefc5a04771a6b3f24a4c655f9259c9ea572": {
"query": "SELECT * FROM account_balance_updates WHERE block_number > $1 AND block_number <= $2 ",
"describe": {
Expand Down Expand Up @@ -2212,6 +2199,20 @@
"nullable": []
}
},
"3a7e35223c276d6b34493b6ec498d9926376bf0d7ec8b8adc7df07a302dcdc80": {
"query": "INSERT INTO committed_nonce (account_id, nonce, block_number) VALUES ($1, $2, $3) \n ON CONFLICT (account_id) \n DO UPDATE \n SET nonce = $2, block_number = $3\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8",
"Int8",
"Int8"
]
},
"nullable": []
}
},
"3e63555f8c8d341b2536bec02e1c60755888686fab50cad8dde060c3aca96f9b": {
"query": "SELECT sequence_number FROM executed_transactions\n WHERE tx_hash = $1",
"describe": {
Expand Down Expand Up @@ -3134,6 +3135,18 @@
]
}
},
"6134f8101d08e7be0c6c62c70237c1a28c782281367a4d6ad7a6b53ee02fdc52": {
"query": "DELETE FROM committed_nonce WHERE block_number > $1",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": []
}
},
"62304acbc93efab5117766689c6413d152dc0104c49c6f305e26b245b6ff7cde": {
"query": "SELECT * FROM executed_priority_operations WHERE eth_hash = $1",
"describe": {
Expand Down
18 changes: 15 additions & 3 deletions core/lib/storage/src/chain/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,17 @@ impl<'a, 'c> StateSchema<'a, 'c> {
}

// Update committed nonce
let block_number = i64::from(*block_number);
for (account_id, nonce) in nonce_updates.iter() {
sqlx::query!(
"INSERT INTO committed_nonce (account_id, nonce) VALUES ($1, $2)
"INSERT INTO committed_nonce (account_id, nonce, block_number) VALUES ($1, $2, $3)
ON CONFLICT (account_id)
DO UPDATE
SET nonce = $2
SET nonce = $2, block_number = $3
",
account_id,
nonce
nonce,
block_number
)
.execute(transaction.conn())
.await?;
Expand All @@ -218,6 +220,16 @@ impl<'a, 'c> StateSchema<'a, 'c> {
Ok(())
}

pub async fn clean_current_nonce_table(&mut self, last_block: BlockNumber) -> QueryResult<()> {
sqlx::query!(
"DELETE FROM committed_nonce WHERE block_number > $1",
*last_block as i64
)
.execute(self.0.conn())
.await?;
Ok(())
}

pub(crate) async fn apply_storage_account_diff(
&mut self,
acc_update: StorageAccountDiff,
Expand Down

0 comments on commit f714b2d

Please sign in to comment.