Skip to content

Commit 4e8d850

Browse files
committed
Add store and load methods
1 parent ce2a014 commit 4e8d850

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

sequencer/api/migrations/sqlite/V302__epoch_drb_and_root.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ CREATE TABLE epoch_drb_and_root (
22
epoch BIGINT PRIMARY KEY,
33
drb_result BLOB,
44
block_header BLOB
5-
);
5+
stake BLOB
6+
);

sequencer/src/persistence/sql.rs

+37
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use derive_more::derive::{From, Into};
99
use espresso_types::{
1010
parse_duration, parse_size, upgrade_commitment_map,
1111
v0::traits::{EventConsumer, PersistenceOptions, SequencerPersistence, StateCatchup},
12+
v0_3::CombinedStakeTable,
1213
BackoffParams, BlockMerkleTree, FeeMerkleTree, Leaf, Leaf2, NetworkConfig, Payload,
1314
};
1415
use futures::stream::StreamExt;
@@ -1018,6 +1019,42 @@ impl SequencerPersistence for Persistence {
10181019
Ok(())
10191020
}
10201021

1022+
async fn load_stake(&self, epoch: EpochNumber) -> anyhow::Result<Option<CombinedStakeTable>> {
1023+
let result = self
1024+
.db
1025+
.read()
1026+
.await?
1027+
.fetch_optional(
1028+
query("SELECT stake FROM epoch_drb_and_root WHERE epoch = ?1")
1029+
.bind(epoch.u64() as i64),
1030+
)
1031+
.await?;
1032+
1033+
result
1034+
.map(|row| {
1035+
let bytes: Vec<u8> = row.get("stake");
1036+
anyhow::Result::<_>::Ok(bincode::deserialize(&bytes)?)
1037+
})
1038+
.transpose()
1039+
}
1040+
async fn store_stake(
1041+
&self,
1042+
epoch: EpochNumber,
1043+
stake: CombinedStakeTable,
1044+
) -> anyhow::Result<()> {
1045+
let stake_table_bytes = bincode::serialize(&stake).context("serializing stake table")?;
1046+
1047+
let mut tx = self.db.write().await?;
1048+
tx.upsert(
1049+
"epoch_drb_and_root",
1050+
["epoch", "drb_result"],
1051+
["epoch"],
1052+
[(epoch.u64() as i64, stake_table_bytes)],
1053+
)
1054+
.await?;
1055+
tx.commit().await
1056+
}
1057+
10211058
async fn load_latest_acted_view(&self) -> anyhow::Result<Option<ViewNumber>> {
10221059
Ok(self
10231060
.db

types/src/v0/traits.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ use itertools::Itertools;
3131
use serde::{de::DeserializeOwned, Serialize};
3232

3333
use super::{
34-
impls::NodeState, utils::BackoffParams, EpochCommittees, EpochVersion, Leaf, SequencerVersions,
34+
impls::NodeState, utils::BackoffParams, v0_3::CombinedStakeTable, EpochCommittees,
35+
EpochVersion, Leaf, SequencerVersions,
3536
};
3637
use crate::{
3738
v0::impls::ValidatedState, v0_99::ChainConfig, BlockMerkleTree, Event, FeeAccount,
@@ -539,6 +540,12 @@ pub trait SequencerPersistence: Sized + Send + Sync + Clone + 'static {
539540
&self,
540541
) -> anyhow::Result<Option<UpgradeCertificate<SeqTypes>>>;
541542
async fn load_start_epoch_info(&self) -> anyhow::Result<Vec<InitializerEpochInfo<SeqTypes>>>;
543+
async fn load_stake(&self, epoch: EpochNumber) -> anyhow::Result<Option<CombinedStakeTable>>;
544+
async fn store_stake(
545+
&self,
546+
epoch: EpochNumber,
547+
stake: CombinedStakeTable,
548+
) -> anyhow::Result<()>;
542549

543550
/// Load the latest known consensus state.
544551
///

0 commit comments

Comments
 (0)