2023-08-31 22:09:29 -04:00
|
|
|
use scale::{Encode, Decode};
|
Replace ExternalBlock with Batch
The initial TODO was simply to use one ExternalBlock per all batches in the
block. This would require publishing ExternalBlock after the last batch,
requiring knowing the last batch. While we could add such a pipeline, it'd
require:
1) Initial preprocesses using a distinct message from BatchPreprocess
2) An additional message sent after all BatchPreprocess are sent
Unfortunately, both would require tweaks to the SubstrateSigner which aren't
worth the complexity compared to the solution here, at least, not at this time.
While this will cause, if a Tributary is signing a block whose total batch data
exceeds 25 kB, to use multiple transactions which could be optimized out by
'better' local data pipelining, that's an extreme edge case. Given the temporal
nature of each Tributary, it's also an acceptable edge.
This does no longer achieve synchrony over external blocks accordingly. While
signed batches have synchrony, as they embed their block hash, batches being
signed don't have cryptographic synchrony on their contents. This means
validators who are eclipsed may produce invalid shares, as they sign a
different batch. This will be introduced in a follow-up commit.
2023-08-31 22:48:02 -04:00
|
|
|
use serai_client::{primitives::NetworkId, in_instructions::primitives::SignedBatch};
|
2023-08-24 19:06:22 -04:00
|
|
|
|
2023-04-23 04:31:00 -04:00
|
|
|
pub use serai_db::*;
|
|
|
|
|
|
|
|
|
|
use crate::tributary::TributarySpec;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-04-26 00:10:06 -04:00
|
|
|
pub struct MainDb<'a, D: Db>(&'a mut D);
|
|
|
|
|
impl<'a, D: Db> MainDb<'a, D> {
|
|
|
|
|
pub fn new(db: &'a mut D) -> Self {
|
2023-04-23 04:31:00 -04:00
|
|
|
Self(db)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main_key(dst: &'static [u8], key: impl AsRef<[u8]>) -> Vec<u8> {
|
2023-09-01 01:00:24 -04:00
|
|
|
D::key(b"coordinator_main", dst, key)
|
2023-04-23 04:31:00 -04:00
|
|
|
}
|
|
|
|
|
|
2023-09-26 23:28:05 -04:00
|
|
|
fn handled_message_key(id: u64) -> Vec<u8> {
|
|
|
|
|
Self::main_key(b"handled_message", id.to_le_bytes())
|
|
|
|
|
}
|
|
|
|
|
pub fn save_handled_message(txn: &mut D::Transaction<'_>, id: u64) {
|
|
|
|
|
txn.put(Self::handled_message_key(id), []);
|
|
|
|
|
}
|
|
|
|
|
pub fn handled_message<G: Get>(getter: &G, id: u64) -> bool {
|
|
|
|
|
getter.get(Self::handled_message_key(id)).is_some()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 04:31:00 -04:00
|
|
|
fn acive_tributaries_key() -> Vec<u8> {
|
|
|
|
|
Self::main_key(b"active_tributaries", [])
|
|
|
|
|
}
|
|
|
|
|
pub fn active_tributaries(&self) -> (Vec<u8>, Vec<TributarySpec>) {
|
|
|
|
|
let bytes = self.0.get(Self::acive_tributaries_key()).unwrap_or(vec![]);
|
|
|
|
|
let mut bytes_ref: &[u8] = bytes.as_ref();
|
|
|
|
|
|
|
|
|
|
let mut tributaries = vec![];
|
|
|
|
|
while !bytes_ref.is_empty() {
|
|
|
|
|
tributaries.push(TributarySpec::read(&mut bytes_ref).unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(bytes, tributaries)
|
|
|
|
|
}
|
|
|
|
|
pub fn add_active_tributary(&mut self, spec: &TributarySpec) {
|
|
|
|
|
let key = Self::acive_tributaries_key();
|
|
|
|
|
let (mut existing_bytes, existing) = self.active_tributaries();
|
|
|
|
|
for tributary in &existing {
|
|
|
|
|
if tributary == spec {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spec.write(&mut existing_bytes).unwrap();
|
|
|
|
|
let mut txn = self.0.txn();
|
|
|
|
|
txn.put(key, existing_bytes);
|
|
|
|
|
txn.commit();
|
|
|
|
|
}
|
2023-05-08 22:20:51 -04:00
|
|
|
|
|
|
|
|
fn first_preprocess_key(id: [u8; 32]) -> Vec<u8> {
|
|
|
|
|
Self::main_key(b"first_preprocess", id)
|
|
|
|
|
}
|
|
|
|
|
pub fn save_first_preprocess(txn: &mut D::Transaction<'_>, id: [u8; 32], preprocess: Vec<u8>) {
|
|
|
|
|
let key = Self::first_preprocess_key(id);
|
|
|
|
|
if let Some(existing) = txn.get(&key) {
|
|
|
|
|
assert_eq!(existing, preprocess, "saved a distinct first preprocess");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
txn.put(key, preprocess);
|
|
|
|
|
}
|
2023-08-26 21:09:57 -04:00
|
|
|
pub fn first_preprocess<G: Get>(getter: &G, id: [u8; 32]) -> Option<Vec<u8>> {
|
|
|
|
|
getter.get(Self::first_preprocess_key(id))
|
2023-05-08 22:20:51 -04:00
|
|
|
}
|
2023-08-31 22:09:29 -04:00
|
|
|
|
|
|
|
|
fn batch_key(network: NetworkId, id: u32) -> Vec<u8> {
|
|
|
|
|
Self::main_key(b"batch", (network, id).encode())
|
|
|
|
|
}
|
|
|
|
|
pub fn save_batch(&mut self, batch: SignedBatch) {
|
|
|
|
|
let mut txn = self.0.txn();
|
|
|
|
|
txn.put(Self::batch_key(batch.batch.network, batch.batch.id), batch.encode());
|
|
|
|
|
txn.commit();
|
|
|
|
|
}
|
|
|
|
|
pub fn batch(&self, network: NetworkId, id: u32) -> Option<SignedBatch> {
|
|
|
|
|
self
|
|
|
|
|
.0
|
|
|
|
|
.get(Self::batch_key(network, id))
|
|
|
|
|
.map(|batch| SignedBatch::decode(&mut batch.as_ref()).unwrap())
|
|
|
|
|
}
|
2023-04-23 04:31:00 -04:00
|
|
|
}
|