mirror of
https://github.com/serai-dex/serai.git
synced 2024-12-23 03:59:22 +00:00
Route the SubstrateBlock message, which is the last Tributary transaction type
This commit is contained in:
parent
a404944b90
commit
09c3c9cc9e
5 changed files with 41 additions and 11 deletions
|
@ -197,6 +197,7 @@ async fn handle_batch_and_burns<Pro: Processor>(
|
||||||
serai_time: block.time().unwrap(),
|
serai_time: block.time().unwrap(),
|
||||||
coin_latest_finalized_block,
|
coin_latest_finalized_block,
|
||||||
},
|
},
|
||||||
|
block: block.number(),
|
||||||
key: serai
|
key: serai
|
||||||
.get_keys(ValidatorSet { network, session: Session(0) }) // TODO2
|
.get_keys(ValidatorSet { network, session: Session(0) }) // TODO2
|
||||||
.await?
|
.await?
|
||||||
|
|
|
@ -76,7 +76,7 @@ fn serialize_transaction() {
|
||||||
OsRng.fill_bytes(&mut ext_block);
|
OsRng.fill_bytes(&mut ext_block);
|
||||||
test_read_write(Transaction::ExternalBlock(ext_block));
|
test_read_write(Transaction::ExternalBlock(ext_block));
|
||||||
}
|
}
|
||||||
test_read_write(Transaction::SeraiBlock(OsRng.next_u64()));
|
test_read_write(Transaction::SubstrateBlock(OsRng.next_u64()));
|
||||||
|
|
||||||
test_read_write(Transaction::BatchPreprocess(random_sign_data(&mut OsRng)));
|
test_read_write(Transaction::BatchPreprocess(random_sign_data(&mut OsRng)));
|
||||||
test_read_write(Transaction::BatchShare(random_sign_data(&mut OsRng)));
|
test_read_write(Transaction::BatchShare(random_sign_data(&mut OsRng)));
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
|
use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
|
||||||
|
|
||||||
pub use serai_db::*;
|
pub use serai_db::*;
|
||||||
|
@ -33,6 +35,22 @@ impl<D: Db> TributaryDb<D> {
|
||||||
getter.get(Self::batch_id_key(&genesis, ext_block)).map(|bytes| bytes.try_into().unwrap())
|
getter.get(Self::batch_id_key(&genesis, ext_block)).map(|bytes| bytes.try_into().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn plan_ids_key(genesis: &[u8], block: u64) -> Vec<u8> {
|
||||||
|
Self::tributary_key(b"plan_ids", [genesis, block.to_le_bytes().as_ref()].concat())
|
||||||
|
}
|
||||||
|
pub fn plan_ids<G: Get>(getter: &G, genesis: [u8; 32], block: u64) -> Option<Vec<[u8; 32]>> {
|
||||||
|
getter.get(Self::plan_ids_key(&genesis, block)).map(|bytes| {
|
||||||
|
let mut res = vec![];
|
||||||
|
let mut bytes_ref: &[u8] = bytes.as_ref();
|
||||||
|
while !bytes_ref.is_empty() {
|
||||||
|
let mut id = [0; 32];
|
||||||
|
bytes_ref.read_exact(&mut id).unwrap();
|
||||||
|
res.push(id);
|
||||||
|
}
|
||||||
|
res
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
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())
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,7 +162,7 @@ pub enum Transaction {
|
||||||
ExternalBlock([u8; 32]),
|
ExternalBlock([u8; 32]),
|
||||||
// When a Serai block is finalized, with the contained batches, we can allow the associated plan
|
// When a Serai block is finalized, with the contained batches, we can allow the associated plan
|
||||||
// IDs
|
// IDs
|
||||||
SeraiBlock(u64),
|
SubstrateBlock(u64),
|
||||||
|
|
||||||
BatchPreprocess(SignData),
|
BatchPreprocess(SignData),
|
||||||
BatchShare(SignData),
|
BatchShare(SignData),
|
||||||
|
@ -232,7 +232,7 @@ impl ReadWrite for Transaction {
|
||||||
3 => {
|
3 => {
|
||||||
let mut block = [0; 8];
|
let mut block = [0; 8];
|
||||||
reader.read_exact(&mut block)?;
|
reader.read_exact(&mut block)?;
|
||||||
Ok(Transaction::SeraiBlock(u64::from_le_bytes(block)))
|
Ok(Transaction::SubstrateBlock(u64::from_le_bytes(block)))
|
||||||
}
|
}
|
||||||
|
|
||||||
4 => SignData::read(reader).map(Transaction::BatchPreprocess),
|
4 => SignData::read(reader).map(Transaction::BatchPreprocess),
|
||||||
|
@ -291,7 +291,7 @@ impl ReadWrite for Transaction {
|
||||||
writer.write_all(block)
|
writer.write_all(block)
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction::SeraiBlock(block) => {
|
Transaction::SubstrateBlock(block) => {
|
||||||
writer.write_all(&[3])?;
|
writer.write_all(&[3])?;
|
||||||
writer.write_all(&block.to_le_bytes())
|
writer.write_all(&block.to_le_bytes())
|
||||||
}
|
}
|
||||||
|
@ -324,7 +324,7 @@ impl TransactionTrait for Transaction {
|
||||||
Transaction::DkgShares(_, _, signed) => TransactionKind::Signed(signed),
|
Transaction::DkgShares(_, _, signed) => TransactionKind::Signed(signed),
|
||||||
|
|
||||||
Transaction::ExternalBlock(_) => TransactionKind::Provided("external"),
|
Transaction::ExternalBlock(_) => TransactionKind::Provided("external"),
|
||||||
Transaction::SeraiBlock(_) => TransactionKind::Provided("serai"),
|
Transaction::SubstrateBlock(_) => TransactionKind::Provided("serai"),
|
||||||
|
|
||||||
Transaction::BatchPreprocess(data) => TransactionKind::Signed(&data.signed),
|
Transaction::BatchPreprocess(data) => TransactionKind::Signed(&data.signed),
|
||||||
Transaction::BatchShare(data) => TransactionKind::Signed(&data.signed),
|
Transaction::BatchShare(data) => TransactionKind::Signed(&data.signed),
|
||||||
|
|
|
@ -173,11 +173,10 @@ async fn handle_block<D: Db, Pro: Processor, P: P2p>(
|
||||||
// If we didn't provide this transaction, we should halt until we do
|
// If we didn't provide this transaction, we should halt until we do
|
||||||
// If we provided a distinct transaction, we should error
|
// 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
|
// If we did provide this transaction, we should've set the batch ID for the block
|
||||||
let batch_id =
|
let batch_id = TributaryDb::<D>::batch_id(&txn, tributary.genesis(), block).expect(
|
||||||
TributaryDb::<D>::batch_id(&txn, tributary.genesis(), block).expect(concat!(
|
"synced a tributary block finalizing a external block in a provided transaction \
|
||||||
"synced a tributary block finalizing a block in a provided transaction despite ",
|
despite us not providing that transaction",
|
||||||
"us not providing that transaction"
|
);
|
||||||
));
|
|
||||||
|
|
||||||
TributaryDb::<D>::recognize_id(
|
TributaryDb::<D>::recognize_id(
|
||||||
&mut txn,
|
&mut txn,
|
||||||
|
@ -186,7 +185,17 @@ async fn handle_block<D: Db, Pro: Processor, P: P2p>(
|
||||||
batch_id,
|
batch_id,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Transaction::SeraiBlock(..) => todo!(),
|
|
||||||
|
Transaction::SubstrateBlock(block) => {
|
||||||
|
let plan_ids = TributaryDb::<D>::plan_ids(&txn, tributary.genesis(), block).expect(
|
||||||
|
"synced a tributary block finalizing a substrate block in a provided transaction \
|
||||||
|
despite us not providing that transaction",
|
||||||
|
);
|
||||||
|
|
||||||
|
for id in plan_ids {
|
||||||
|
TributaryDb::<D>::recognize_id(&mut txn, Zone::Sign.label(), tributary.genesis(), id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Transaction::BatchPreprocess(data) => {
|
Transaction::BatchPreprocess(data) => {
|
||||||
if let Some(preprocesses) = handle(
|
if let Some(preprocesses) = handle(
|
||||||
|
@ -291,6 +300,8 @@ pub async fn handle_new_blocks<D: Db, Pro: Processor, P: P2p>(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Only handle n blocks at a time.
|
||||||
|
// This may load the entire chain into RAM as-is.
|
||||||
let mut blocks = vec![tributary.block(&latest).unwrap()];
|
let mut blocks = vec![tributary.block(&latest).unwrap()];
|
||||||
while blocks.last().unwrap().parent() != *last_block {
|
while blocks.last().unwrap().parent() != *last_block {
|
||||||
blocks.push(tributary.block(&blocks.last().unwrap().parent()).unwrap());
|
blocks.push(tributary.block(&blocks.last().unwrap().parent()).unwrap());
|
||||||
|
|
Loading…
Reference in a new issue