diff --git a/coordinator/tributary/src/blockchain.rs b/coordinator/tributary/src/blockchain.rs index b019e0f5b..fb65d78b1 100644 --- a/coordinator/tributary/src/blockchain.rs +++ b/coordinator/tributary/src/blockchain.rs @@ -9,7 +9,7 @@ use scale::{Encode, Decode}; use tendermint::ext::{Network, Commit}; use crate::{ - ReadWrite, ProvidedError, ProvidedTransactions, BlockQuantityDb, LocalQuantityDb, BlockError, + ReadWrite, ProvidedError, ProvidedTransactions, BlockProvidedQuantityDb, LocalProvidedQuantityDb, BlockError, Block, Mempool, Transaction, transaction::{Signed, TransactionKind, TransactionError, Transaction as TransactionTrait}, }; @@ -32,14 +32,14 @@ pub(crate) struct Blockchain { create_db!( TributaryBlockchainDb { TipsDb: (genesis: [u8; 32]) -> [u8; 32], - BlockNumberDb: (genesis: [u8; 32]) -> Vec, + BlockNumberDb: (genesis: [u8; 32]) -> u32, BlockDb: (genesis: [u8; 32], hash: &[u8; 32]) -> Vec, BlockHashDb: (genesis: [u8; 32], block_number: u32) -> [u8; 32], CommitDb: (genesis: [u8; 32], block: &[u8; 32]) -> Vec, BlockAfterDb: (genesis: [u8; 32], hash: [u8; 32]) -> [u8; 32], - UnsignedIncludedDb: (genesis: [u8; 32], hash: [u8; 32]) -> Vec, - ProvidedIncludedDb: (genesis: [u8; 32], hash: [u8; 32]) -> [u8; 0], - NextNonceDb: (genesis: [u8; 32], hash: [u8; 32]) -> Vec + UnsignedIncludedDb: (genesis: [u8; 32], hash: [u8; 32]) -> (), + ProvidedIncludedDb: (genesis: [u8; 32], hash: [u8; 32]) -> (), + NextNonceDb: (genesis: [u8; 32], signer: [u8; 32]) -> u32 } ); @@ -72,13 +72,13 @@ impl Blockchain { BlockNumberDb::get(self_db, genesis) .map(|number| (number, TipsDb::get(self_db, genesis).unwrap())) } { - res.block_number = u32::from_le_bytes(block_number.try_into().unwrap()); + res.block_number = block_number; res.tip.copy_from_slice(&tip); } for participant in participants { if let Some(next_nonce) = NextNonceDb::get(self_db, genesis, participant.to_bytes()) { - res.next_nonces.insert(*participant, u32::from_le_bytes(next_nonce.try_into().unwrap())); + res.next_nonces.insert(*participant, next_nonce); } } @@ -106,7 +106,7 @@ impl Blockchain { CommitDb::get( self.db.as_ref().unwrap(), self.genesis, - &BlockHashDb::get(self.db.as_ref().unwrap(), self.genesis, block).unwrap(), + &BlockHashDb::get(self.db.as_ref().unwrap(), self.genesis, block)?, ) } @@ -117,8 +117,8 @@ impl Blockchain { order: &str, ) -> bool { let order_bytes = order.as_bytes(); - let local = LocalQuantityDb::get(db, genesis, order_bytes).unwrap_or_default(); - let block = BlockQuantityDb::get(db, genesis, block, order_bytes).unwrap_or_default(); + let local = LocalProvidedQuantityDb::get(db, genesis, order_bytes).unwrap_or_default(); + let block = BlockProvidedQuantityDb::get(db, genesis, block, order_bytes).unwrap_or_default(); local >= block } @@ -228,7 +228,7 @@ impl Blockchain { TipsDb::set(&mut txn, self.genesis, &self.tip); self.block_number += 1; - BlockNumberDb::set(&mut txn, self.genesis, &self.block_number.to_le_bytes()); + BlockNumberDb::set(&mut txn, self.genesis, &self.block_number); BlockHashDb::set(&mut txn, self.genesis, self.block_number, &self.tip); @@ -243,12 +243,12 @@ impl Blockchain { TransactionKind::Provided(order) => { let hash = tx.hash(); self.provided.complete(&mut txn, order, self.tip, hash); - ProvidedIncludedDb::set(&mut txn, self.genesis, hash, &[] as &[u8; 0]); + ProvidedIncludedDb::set(&mut txn, self.genesis, hash, &()); } TransactionKind::Unsigned => { let hash = tx.hash(); // Save as included on chain - UnsignedIncludedDb::set(&mut txn, self.genesis, hash, &[] as &[u8; 0]); + UnsignedIncludedDb::set(&mut txn, self.genesis, hash, &()); // remove from the mempool self.mempool.remove(&hash); } @@ -261,7 +261,7 @@ impl Blockchain { if prev != *nonce { panic!("verified block had an invalid nonce"); } - NextNonceDb::set(&mut txn, self.genesis, signer.to_bytes(), &next_nonce.to_le_bytes()); + NextNonceDb::set(&mut txn, self.genesis, signer.to_bytes(), &next_nonce); self.mempool.remove(&tx.hash()); } diff --git a/coordinator/tributary/src/provided.rs b/coordinator/tributary/src/provided.rs index e76c82031..fb201dadf 100644 --- a/coordinator/tributary/src/provided.rs +++ b/coordinator/tributary/src/provided.rs @@ -32,23 +32,23 @@ pub struct ProvidedTransactions { create_db!( TributaryProvidedDb { - TransactionDb: (genesis: &[u8], hash: &[u8]) -> Vec, - CurrentDb: (genesis: &[u8]) -> Vec, - LocalQuantityDb: (genesis: &[u8], order: &[u8]) -> u32, - OnChainQuantityDb: (genesis: &[u8], order: &[u8]) -> u32, - BlockQuantityDb: (genesis: &[u8], block: &[u8], order: &[u8]) -> u32, - OnChainTxDb: (genesis: &[u8], order: &[u8], id: u32) -> [u8; 32] + TransactionDb: (genesis: &[u8], hash: [u8; 32]) -> Vec, + CurrentProvidedDb: (genesis: &[u8]) -> Vec<[u8; 32]>, + LocalProvidedQuantityDb: (genesis: &[u8], order: &[u8]) -> u32, + OnChainProvidedQuantityDb: (genesis: &[u8], order: &[u8]) -> u32, + BlockProvidedQuantityDb: (genesis: &[u8], block: &[u8], order: &[u8]) -> u32, + OnChainProvidedDb: (genesis: &[u8], order: &[u8], id: u32) -> [u8; 32] } ); impl ProvidedTransactions { pub(crate) fn new(db: D, genesis: [u8; 32]) -> Self { let mut res = ProvidedTransactions { db, genesis, transactions: HashMap::new() }; - let currently_provided = CurrentDb::get(&res.db, &genesis).unwrap_or_default(); + let currently_provided = CurrentProvidedDb::get(&res.db, &genesis).unwrap_or_default(); let mut i = 0; while i < currently_provided.len() { let tx = T::read::<&[u8]>( - &mut TransactionDb::get(&res.db, &res.genesis, ¤tly_provided[i .. (i + 32)]) + &mut TransactionDb::get(&res.db, &res.genesis, currently_provided[i]) .unwrap() .as_ref(), ) @@ -80,40 +80,40 @@ impl ProvidedTransactions { let tx_hash = tx.hash(); // Check it wasn't already provided - if TransactionDb::get(&self.db, &self.genesis, &tx_hash).is_some() { + if TransactionDb::get(&self.db, &self.genesis, tx_hash).is_some() { Err(ProvidedError::AlreadyProvided)?; } // get local and on-chain tx numbers let order_bytes = order.as_bytes(); let mut local_quantity = - LocalQuantityDb::get(&self.db, &self.genesis, order_bytes).unwrap_or_default(); + LocalProvidedQuantityDb::get(&self.db, &self.genesis, order_bytes).unwrap_or_default(); let on_chain_quantity = - OnChainQuantityDb::get(&self.db, &self.genesis, order_bytes).unwrap_or_default(); + OnChainProvidedQuantityDb::get(&self.db, &self.genesis, order_bytes).unwrap_or_default(); // This would have a race-condition with multiple calls to provide, though this takes &mut self // peventing multiple calls at once let mut txn = self.db.txn(); - TransactionDb::set(&mut txn, &self.genesis, &tx_hash, &tx.serialize()); + TransactionDb::set(&mut txn, &self.genesis, tx_hash, &tx.serialize()); let this_provided_id = local_quantity; local_quantity += 1; - LocalQuantityDb::set(&mut txn, &self.genesis, order_bytes, &local_quantity); + LocalProvidedQuantityDb::set(&mut txn, &self.genesis, order_bytes, &local_quantity); if this_provided_id < on_chain_quantity { // Verify against the on-chain version if tx_hash.as_ref() != - OnChainTxDb::get(&txn, &self.genesis, order_bytes, this_provided_id).unwrap() + OnChainProvidedDb::get(&txn, &self.genesis, order_bytes, this_provided_id).unwrap() { Err(ProvidedError::LocalMismatchesOnChain)?; } txn.commit(); } else { #[allow(clippy::unwrap_or_default)] - let mut currently_provided = CurrentDb::get(&txn, &self.genesis).unwrap_or_default(); - currently_provided.extend(tx_hash); - CurrentDb::set(&mut txn, &self.genesis, ¤tly_provided); + let mut currently_provided = CurrentProvidedDb::get(&txn, &self.genesis).unwrap_or_default(); + currently_provided.push(tx_hash); + CurrentProvidedDb::set(&mut txn, &self.genesis, ¤tly_provided); txn.commit(); if self.transactions.get(order).is_none() { @@ -135,13 +135,13 @@ impl ProvidedTransactions { ) { if let Some(next_tx) = self.transactions.get_mut(order).and_then(|queue| queue.pop_front()) { assert_eq!(next_tx.hash(), tx); - let mut currently_provided = CurrentDb::get(txn, &self.genesis).unwrap(); + let currently_provided = CurrentProvidedDb::get(txn, &self.genesis).unwrap(); // Find this TX's hash let mut i = 0; loop { - if currently_provided[i .. (i + 32)] == tx { - assert_eq!(¤tly_provided.drain(i .. (i + 32)).collect::>(), &tx); + if currently_provided[i] == tx { + assert_eq!(currently_provided[i], tx); break; } @@ -150,21 +150,17 @@ impl ProvidedTransactions { panic!("couldn't find completed TX in currently provided"); } } - CurrentDb::set(txn, &self.genesis, ¤tly_provided); + CurrentProvidedDb::set(txn, &self.genesis, ¤tly_provided); } // bump the on-chain tx number. let order_bytes = order.as_bytes(); let mut on_chain_quantity = - OnChainQuantityDb::get(&self.db, &self.genesis, order_bytes).unwrap_or_default(); + OnChainProvidedQuantityDb::get(&self.db, &self.genesis, order_bytes).unwrap_or_default(); let this_provided_id = on_chain_quantity; - - // let block_order_key = Self::block_provided_quantity_key(&self.genesis, &block, order); - - OnChainTxDb::set(txn, &self.genesis, order_bytes, this_provided_id, &tx); - + OnChainProvidedDb::set(txn, &self.genesis, order_bytes, this_provided_id, &tx); on_chain_quantity += 1; - OnChainQuantityDb::set(txn, &self.genesis, order_bytes, &on_chain_quantity); - BlockQuantityDb::set(txn, &self.genesis, &block, order_bytes, &on_chain_quantity); + OnChainProvidedQuantityDb::set(txn, &self.genesis, order_bytes, &on_chain_quantity); + BlockProvidedQuantityDb::set(txn, &self.genesis, &block, order_bytes, &on_chain_quantity); } }