ExternalBlock handler

This commit is contained in:
Luke Parker 2023-04-20 14:51:33 -04:00
parent f99a91b34d
commit 70d866af6a
No known key found for this signature in database
2 changed files with 96 additions and 67 deletions

View file

@ -25,6 +25,14 @@ impl<D: Db> TributaryDb<D> {
self.0.get(Self::block_key(genesis)).unwrap_or(genesis.to_vec()).try_into().unwrap()
}
// This shouldn't need genesis? Yet it's saner to have then quibble about.
fn batch_id_key(genesis: &[u8], ext_block: [u8; 32]) -> Vec<u8> {
Self::tributary_key(b"batch_id", [genesis, ext_block.as_ref()].concat())
}
pub fn batch_id<G: Get>(getter: &G, genesis: [u8; 32], ext_block: [u8; 32]) -> Option<[u8; 32]> {
getter.get(Self::batch_id_key(&genesis, ext_block)).map(|bytes| bytes.try_into().unwrap())
}
fn recognized_id_key(label: &'static str, genesis: [u8; 32], id: [u8; 32]) -> Vec<u8> {
Self::tributary_key(b"recognized", [label.as_bytes(), genesis.as_ref(), id.as_ref()].concat())
}

View file

@ -46,20 +46,24 @@ async fn handle_block<D: Db, Pro: Processor, P: P2p>(
Sign,
}
let mut handle = |zone, label, needed, id, attempt, mut bytes: Vec<u8>, signed: Signed| {
impl Zone {
fn label(&self) -> &'static str {
match self {
Zone::Dkg => {
panic!("getting the label for dkg despite dkg code paths not needing a label")
}
Zone::Batch => "batch",
Zone::Sign => "sign",
}
}
}
let mut handle =
|zone: Zone, label, needed, id, attempt, mut bytes: Vec<u8>, signed: Signed| {
if zone == Zone::Dkg {
// Since Dkg doesn't have an ID, solely attempts, this should just be [0; 32]
assert_eq!(id, [0; 32], "DKG, which shouldn't have IDs, had a non-0 ID");
} else if !TributaryDb::<D>::recognized_id(
&txn,
match zone {
Zone::Dkg => panic!("zone was Dkg despite prior if clause handling Dkg"),
Zone::Batch => "batch",
Zone::Sign => "sign",
},
tributary.genesis(),
id,
) {
} else if !TributaryDb::<D>::recognized_id(&txn, zone.label(), tributary.genesis(), id) {
// TODO: Full slash
todo!();
}
@ -163,8 +167,25 @@ async fn handle_block<D: Db, Pro: Processor, P: P2p>(
}
}
// TODO
Transaction::ExternalBlock(..) => todo!(),
Transaction::ExternalBlock(block) => {
// Because this external block has been finalized, its batch ID should be authorized
// If we didn't provide this transaction, we should halt until we do
// If we provided a distinct transaction, we should error
// If we did provide this transaction, we should've set the batch ID for the block
let batch_id =
TributaryDb::<D>::batch_id(&txn, tributary.genesis(), block).expect(concat!(
"synced a tributary block finalizing a block in a provided transaction despite ",
"us not providing that transaction"
));
TributaryDb::<D>::recognize_id(
&mut txn,
Zone::Batch.label(),
tributary.genesis(),
batch_id,
);
}
Transaction::SeraiBlock(..) => todo!(),
Transaction::BatchPreprocess(data) => {