2023-04-14 00:35:55 +00:00
|
|
|
use std::collections::HashMap;
|
2023-04-12 15:13:48 +00:00
|
|
|
|
2023-04-14 18:11:19 +00:00
|
|
|
use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
|
|
|
|
|
|
|
|
use serai_db::{DbTxn, Db};
|
2023-04-12 15:13:48 +00:00
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
use crate::{
|
2023-04-14 19:03:01 +00:00
|
|
|
ReadWrite, Signed, TransactionKind, Transaction, ProvidedError, ProvidedTransactions, BlockError,
|
|
|
|
Block, Mempool,
|
2023-04-14 00:35:55 +00:00
|
|
|
};
|
2023-04-12 15:13:48 +00:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2023-04-14 18:11:19 +00:00
|
|
|
pub(crate) struct Blockchain<D: Db, T: Transaction> {
|
|
|
|
db: Option<D>,
|
2023-04-12 15:13:48 +00:00
|
|
|
genesis: [u8; 32],
|
2023-04-14 18:11:19 +00:00
|
|
|
|
|
|
|
block_number: u32,
|
2023-04-12 15:13:48 +00:00
|
|
|
tip: [u8; 32],
|
2023-04-12 16:42:23 +00:00
|
|
|
next_nonces: HashMap<<Ristretto as Ciphersuite>::G, u32>,
|
2023-04-13 13:47:14 +00:00
|
|
|
|
2023-04-14 19:03:01 +00:00
|
|
|
provided: ProvidedTransactions<D, T>,
|
2023-04-14 19:51:43 +00:00
|
|
|
mempool: Mempool<D, T>,
|
2023-04-12 15:13:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 18:11:19 +00:00
|
|
|
impl<D: Db, T: Transaction> Blockchain<D, T> {
|
|
|
|
fn tip_key(&self) -> Vec<u8> {
|
2023-04-14 19:51:43 +00:00
|
|
|
D::key(b"tributary_blockchain", b"tip", self.genesis)
|
2023-04-14 18:11:19 +00:00
|
|
|
}
|
|
|
|
fn block_number_key(&self) -> Vec<u8> {
|
2023-04-14 19:51:43 +00:00
|
|
|
D::key(b"tributary_blockchain", b"block_number", self.genesis)
|
2023-04-14 18:11:19 +00:00
|
|
|
}
|
|
|
|
fn block_key(&self, hash: &[u8; 32]) -> Vec<u8> {
|
|
|
|
// Since block hashes incorporate their parent, and the first parent is the genesis, this is
|
|
|
|
// fine not incorporating the hash unless there's a hash collision
|
2023-04-14 19:51:43 +00:00
|
|
|
D::key(b"tributary_blockchain", b"block", hash)
|
2023-04-14 18:11:19 +00:00
|
|
|
}
|
|
|
|
fn commit_key(&self, hash: &[u8; 32]) -> Vec<u8> {
|
2023-04-14 19:51:43 +00:00
|
|
|
D::key(b"tributary_blockchain", b"commit", hash)
|
2023-04-14 18:11:19 +00:00
|
|
|
}
|
|
|
|
fn next_nonce_key(&self, signer: &<Ristretto as Ciphersuite>::G) -> Vec<u8> {
|
|
|
|
D::key(
|
2023-04-14 19:51:43 +00:00
|
|
|
b"tributary_blockchain",
|
2023-04-14 18:11:19 +00:00
|
|
|
b"next_nonce",
|
|
|
|
[self.genesis.as_ref(), signer.to_bytes().as_ref()].concat(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn new(
|
|
|
|
db: D,
|
|
|
|
genesis: [u8; 32],
|
|
|
|
participants: &[<Ristretto as Ciphersuite>::G],
|
|
|
|
) -> Self {
|
2023-04-12 16:42:23 +00:00
|
|
|
let mut next_nonces = HashMap::new();
|
|
|
|
for participant in participants {
|
|
|
|
next_nonces.insert(*participant, 0);
|
|
|
|
}
|
2023-04-13 13:47:14 +00:00
|
|
|
|
2023-04-14 18:11:19 +00:00
|
|
|
let mut res = Self {
|
2023-04-14 19:03:01 +00:00
|
|
|
db: Some(db.clone()),
|
2023-04-13 13:47:14 +00:00
|
|
|
genesis,
|
|
|
|
|
2023-04-13 22:43:03 +00:00
|
|
|
block_number: 0,
|
2023-04-13 13:47:14 +00:00
|
|
|
tip: genesis,
|
|
|
|
next_nonces,
|
|
|
|
|
2023-04-14 19:51:43 +00:00
|
|
|
provided: ProvidedTransactions::new(db.clone(), genesis),
|
|
|
|
mempool: Mempool::new(db, genesis),
|
2023-04-14 18:11:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some((block_number, tip)) = {
|
|
|
|
let db = res.db.as_ref().unwrap();
|
|
|
|
db.get(res.block_number_key()).map(|number| (number, db.get(res.tip_key()).unwrap()))
|
|
|
|
} {
|
|
|
|
res.block_number = u32::from_le_bytes(block_number.try_into().unwrap());
|
|
|
|
res.tip.copy_from_slice(&tip);
|
2023-04-13 13:47:14 +00:00
|
|
|
}
|
2023-04-14 18:11:19 +00:00
|
|
|
|
|
|
|
for participant in participants {
|
|
|
|
if let Some(next_nonce) = res.db.as_ref().unwrap().get(res.next_nonce_key(participant)) {
|
|
|
|
res.next_nonces.insert(*participant, u32::from_le_bytes(next_nonce.try_into().unwrap()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
2023-04-12 15:13:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) fn tip(&self) -> [u8; 32] {
|
2023-04-12 15:13:48 +00:00
|
|
|
self.tip
|
|
|
|
}
|
|
|
|
|
2023-04-14 18:11:19 +00:00
|
|
|
pub(crate) fn block_number(&self) -> u32 {
|
2023-04-13 22:43:03 +00:00
|
|
|
self.block_number
|
|
|
|
}
|
|
|
|
|
2023-04-15 04:41:48 +00:00
|
|
|
pub(crate) fn block(&self, block: &[u8; 32]) -> Option<Block<T>> {
|
|
|
|
self
|
|
|
|
.db
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.get(self.block_key(block))
|
|
|
|
.map(|bytes| Block::<T>::read::<&[u8]>(&mut bytes.as_ref()).unwrap())
|
|
|
|
}
|
|
|
|
|
2023-04-14 18:11:19 +00:00
|
|
|
pub(crate) fn commit(&self, block: &[u8; 32]) -> Option<Vec<u8>> {
|
|
|
|
self.db.as_ref().unwrap().get(self.commit_key(block))
|
|
|
|
}
|
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) fn add_transaction(&mut self, internal: bool, tx: T) -> bool {
|
|
|
|
self.mempool.add(&self.next_nonces, internal, tx)
|
2023-04-13 13:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 19:03:01 +00:00
|
|
|
pub(crate) fn provide_transaction(&mut self, tx: T) -> Result<(), ProvidedError> {
|
|
|
|
self.provided.provide(tx)
|
2023-04-12 15:13:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
/// Returns the next nonce for signing, or None if they aren't a participant.
|
|
|
|
pub(crate) fn next_nonce(&self, key: <Ristretto as Ciphersuite>::G) -> Option<u32> {
|
|
|
|
Some(self.next_nonces.get(&key).cloned()?.max(self.mempool.next_nonce(&key).unwrap_or(0)))
|
2023-04-12 15:13:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) fn build_block(&mut self) -> Block<T> {
|
|
|
|
let block = Block::new(
|
|
|
|
self.tip,
|
2023-04-20 11:30:49 +00:00
|
|
|
self.provided.transactions.values().flatten().cloned().collect(),
|
2023-04-14 00:35:55 +00:00
|
|
|
self.mempool.block(&self.next_nonces),
|
|
|
|
);
|
2023-04-12 15:13:48 +00:00
|
|
|
// build_block should not return invalid blocks
|
|
|
|
self.verify_block(&block).unwrap();
|
|
|
|
block
|
|
|
|
}
|
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) fn verify_block(&self, block: &Block<T>) -> Result<(), BlockError> {
|
|
|
|
block.verify(
|
|
|
|
self.genesis,
|
|
|
|
self.tip,
|
2023-04-20 11:30:49 +00:00
|
|
|
self.provided.transactions.clone(),
|
2023-04-14 00:35:55 +00:00
|
|
|
self.next_nonces.clone(),
|
|
|
|
)
|
2023-04-12 15:13:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 20:18:42 +00:00
|
|
|
/// Add a block.
|
2023-04-14 18:11:19 +00:00
|
|
|
pub(crate) fn add_block(&mut self, block: &Block<T>, commit: Vec<u8>) -> Result<(), BlockError> {
|
2023-04-12 22:04:28 +00:00
|
|
|
self.verify_block(block)?;
|
2023-04-12 20:18:42 +00:00
|
|
|
|
|
|
|
// None of the following assertions should be reachable since we verified the block
|
2023-04-14 18:11:19 +00:00
|
|
|
|
|
|
|
// Take it from the Option so Rust doesn't consider self as mutably borrowed thanks to the
|
|
|
|
// existence of the txn
|
|
|
|
let mut db = self.db.take().unwrap();
|
|
|
|
let mut txn = db.txn();
|
|
|
|
|
2023-04-12 15:13:48 +00:00
|
|
|
self.tip = block.hash();
|
2023-04-14 18:11:19 +00:00
|
|
|
txn.put(self.tip_key(), self.tip);
|
|
|
|
|
2023-04-13 22:43:03 +00:00
|
|
|
self.block_number += 1;
|
2023-04-14 18:11:19 +00:00
|
|
|
txn.put(self.block_number_key(), self.block_number.to_le_bytes());
|
|
|
|
|
|
|
|
txn.put(self.block_key(&self.tip), block.serialize());
|
|
|
|
txn.put(self.commit_key(&self.tip), commit);
|
|
|
|
|
2023-04-12 15:13:48 +00:00
|
|
|
for tx in &block.transactions {
|
|
|
|
match tx.kind() {
|
2023-04-20 11:30:49 +00:00
|
|
|
TransactionKind::Provided(order) => {
|
|
|
|
self.provided.complete(&mut txn, order, tx.hash());
|
2023-04-12 15:13:48 +00:00
|
|
|
}
|
|
|
|
TransactionKind::Unsigned => {}
|
|
|
|
TransactionKind::Signed(Signed { signer, nonce, .. }) => {
|
2023-04-14 18:11:19 +00:00
|
|
|
let next_nonce = nonce + 1;
|
2023-04-12 16:42:23 +00:00
|
|
|
let prev = self
|
|
|
|
.next_nonces
|
2023-04-14 18:11:19 +00:00
|
|
|
.insert(*signer, next_nonce)
|
2023-04-12 16:42:23 +00:00
|
|
|
.expect("block had signed transaction from non-participant");
|
|
|
|
if prev != *nonce {
|
2023-04-12 20:18:42 +00:00
|
|
|
panic!("verified block had an invalid nonce");
|
2023-04-12 15:13:48 +00:00
|
|
|
}
|
2023-04-14 00:35:55 +00:00
|
|
|
|
2023-04-14 18:11:19 +00:00
|
|
|
txn.put(self.next_nonce_key(signer), next_nonce.to_le_bytes());
|
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
self.mempool.remove(&tx.hash());
|
2023-04-12 15:13:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 20:18:42 +00:00
|
|
|
|
2023-04-14 18:11:19 +00:00
|
|
|
txn.commit();
|
|
|
|
self.db = Some(db);
|
|
|
|
|
2023-04-12 22:04:28 +00:00
|
|
|
Ok(())
|
2023-04-12 15:13:48 +00:00
|
|
|
}
|
|
|
|
}
|