mirror of
https://github.com/serai-dex/serai.git
synced 2025-03-16 16:42:03 +00:00
ExternalBlock handler
This commit is contained in:
parent
f99a91b34d
commit
70d866af6a
2 changed files with 96 additions and 67 deletions
|
@ -25,6 +25,14 @@ impl<D: Db> TributaryDb<D> {
|
||||||
self.0.get(Self::block_key(genesis)).unwrap_or(genesis.to_vec()).try_into().unwrap()
|
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> {
|
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())
|
Self::tributary_key(b"recognized", [label.as_bytes(), genesis.as_ref(), id.as_ref()].concat())
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,82 +46,86 @@ async fn handle_block<D: Db, Pro: Processor, P: P2p>(
|
||||||
Sign,
|
Sign,
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut handle = |zone, label, needed, id, attempt, mut bytes: Vec<u8>, signed: Signed| {
|
impl Zone {
|
||||||
if zone == Zone::Dkg {
|
fn label(&self) -> &'static str {
|
||||||
// Since Dkg doesn't have an ID, solely attempts, this should just be [0; 32]
|
match self {
|
||||||
assert_eq!(id, [0; 32], "DKG, which shouldn't have IDs, had a non-0 ID");
|
Zone::Dkg => {
|
||||||
} else if !TributaryDb::<D>::recognized_id(
|
panic!("getting the label for dkg despite dkg code paths not needing a label")
|
||||||
&txn,
|
}
|
||||||
match zone {
|
|
||||||
Zone::Dkg => panic!("zone was Dkg despite prior if clause handling Dkg"),
|
|
||||||
Zone::Batch => "batch",
|
Zone::Batch => "batch",
|
||||||
Zone::Sign => "sign",
|
Zone::Sign => "sign",
|
||||||
},
|
}
|
||||||
tributary.genesis(),
|
|
||||||
id,
|
|
||||||
) {
|
|
||||||
// TODO: Full slash
|
|
||||||
todo!();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If they've already published a TX for this attempt, slash
|
let mut handle =
|
||||||
if let Some(data) =
|
|zone: Zone, label, needed, id, attempt, mut bytes: Vec<u8>, signed: Signed| {
|
||||||
TributaryDb::<D>::data(label, &txn, tributary.genesis(), id, attempt, &signed.signer)
|
if zone == Zone::Dkg {
|
||||||
{
|
// Since Dkg doesn't have an ID, solely attempts, this should just be [0; 32]
|
||||||
if data != bytes {
|
assert_eq!(id, [0; 32], "DKG, which shouldn't have IDs, had a non-0 ID");
|
||||||
|
} else if !TributaryDb::<D>::recognized_id(&txn, zone.label(), tributary.genesis(), id) {
|
||||||
// TODO: Full slash
|
// TODO: Full slash
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Slash
|
// If they've already published a TX for this attempt, slash
|
||||||
return None;
|
if let Some(data) =
|
||||||
}
|
TributaryDb::<D>::data(label, &txn, tributary.genesis(), id, attempt, &signed.signer)
|
||||||
|
{
|
||||||
|
if data != bytes {
|
||||||
|
// TODO: Full slash
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
|
||||||
// If the attempt is lesser than the blockchain's, slash
|
// TODO: Slash
|
||||||
let curr_attempt = TributaryDb::<D>::attempt(&txn, tributary.genesis(), id);
|
return None;
|
||||||
if attempt < curr_attempt {
|
|
||||||
// TODO: Slash for being late
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
if attempt > curr_attempt {
|
|
||||||
// TODO: Full slash
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store this data
|
|
||||||
let received = TributaryDb::<D>::set_data(
|
|
||||||
label,
|
|
||||||
&mut txn,
|
|
||||||
tributary.genesis(),
|
|
||||||
id,
|
|
||||||
attempt,
|
|
||||||
&signed.signer,
|
|
||||||
&bytes,
|
|
||||||
);
|
|
||||||
|
|
||||||
// If we have all the needed commitments/preprocesses/shares, tell the processor
|
|
||||||
if received == needed {
|
|
||||||
let mut data = HashMap::new();
|
|
||||||
for validator in spec.validators().keys() {
|
|
||||||
data.insert(
|
|
||||||
spec.i(*validator).unwrap(),
|
|
||||||
if validator == &signed.signer {
|
|
||||||
bytes.split_off(0)
|
|
||||||
} else if let Some(data) =
|
|
||||||
TributaryDb::<D>::data(label, &txn, tributary.genesis(), id, attempt, validator)
|
|
||||||
{
|
|
||||||
data
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
assert_eq!(data.len(), usize::from(needed));
|
|
||||||
|
|
||||||
return Some(data);
|
// If the attempt is lesser than the blockchain's, slash
|
||||||
}
|
let curr_attempt = TributaryDb::<D>::attempt(&txn, tributary.genesis(), id);
|
||||||
None
|
if attempt < curr_attempt {
|
||||||
};
|
// TODO: Slash for being late
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if attempt > curr_attempt {
|
||||||
|
// TODO: Full slash
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store this data
|
||||||
|
let received = TributaryDb::<D>::set_data(
|
||||||
|
label,
|
||||||
|
&mut txn,
|
||||||
|
tributary.genesis(),
|
||||||
|
id,
|
||||||
|
attempt,
|
||||||
|
&signed.signer,
|
||||||
|
&bytes,
|
||||||
|
);
|
||||||
|
|
||||||
|
// If we have all the needed commitments/preprocesses/shares, tell the processor
|
||||||
|
if received == needed {
|
||||||
|
let mut data = HashMap::new();
|
||||||
|
for validator in spec.validators().keys() {
|
||||||
|
data.insert(
|
||||||
|
spec.i(*validator).unwrap(),
|
||||||
|
if validator == &signed.signer {
|
||||||
|
bytes.split_off(0)
|
||||||
|
} else if let Some(data) =
|
||||||
|
TributaryDb::<D>::data(label, &txn, tributary.genesis(), id, attempt, validator)
|
||||||
|
{
|
||||||
|
data
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert_eq!(data.len(), usize::from(needed));
|
||||||
|
|
||||||
|
return Some(data);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
match tx {
|
match tx {
|
||||||
Transaction::DkgCommitments(attempt, bytes, signed) => {
|
Transaction::DkgCommitments(attempt, bytes, signed) => {
|
||||||
|
@ -163,8 +167,25 @@ async fn handle_block<D: Db, Pro: Processor, P: P2p>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
Transaction::ExternalBlock(block) => {
|
||||||
Transaction::ExternalBlock(..) => todo!(),
|
// 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::SeraiBlock(..) => todo!(),
|
||||||
|
|
||||||
Transaction::BatchPreprocess(data) => {
|
Transaction::BatchPreprocess(data) => {
|
||||||
|
|
Loading…
Reference in a new issue