Skip to content

Commit

Permalink
Made changes but tests are broken
Browse files Browse the repository at this point in the history
  • Loading branch information
econsta committed Nov 28, 2023
1 parent 46d1375 commit d94970a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
28 changes: 14 additions & 14 deletions coordinator/tributary/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -32,14 +32,14 @@ pub(crate) struct Blockchain<D: Db, T: TransactionTrait> {
create_db!(
TributaryBlockchainDb {
TipsDb: (genesis: [u8; 32]) -> [u8; 32],
BlockNumberDb: (genesis: [u8; 32]) -> Vec<u8>,
BlockNumberDb: (genesis: [u8; 32]) -> u32,
BlockDb: (genesis: [u8; 32], hash: &[u8; 32]) -> Vec<u8>,
BlockHashDb: (genesis: [u8; 32], block_number: u32) -> [u8; 32],
CommitDb: (genesis: [u8; 32], block: &[u8; 32]) -> Vec<u8>,
BlockAfterDb: (genesis: [u8; 32], hash: [u8; 32]) -> [u8; 32],
UnsignedIncludedDb: (genesis: [u8; 32], hash: [u8; 32]) -> Vec<u8>,
ProvidedIncludedDb: (genesis: [u8; 32], hash: [u8; 32]) -> [u8; 0],
NextNonceDb: (genesis: [u8; 32], hash: [u8; 32]) -> Vec<u8>
UnsignedIncludedDb: (genesis: [u8; 32], hash: [u8; 32]) -> (),
ProvidedIncludedDb: (genesis: [u8; 32], hash: [u8; 32]) -> (),
NextNonceDb: (genesis: [u8; 32], signer: [u8; 32]) -> u32
}
);

Expand Down Expand Up @@ -72,13 +72,13 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
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);
}
}

Expand Down Expand Up @@ -106,7 +106,7 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
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)?,
)
}

Expand All @@ -117,8 +117,8 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
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
}

Expand Down Expand Up @@ -228,7 +228,7 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
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);

Expand All @@ -243,12 +243,12 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
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);
}
Expand All @@ -261,7 +261,7 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
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());
}
Expand Down
54 changes: 25 additions & 29 deletions coordinator/tributary/src/provided.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ pub struct ProvidedTransactions<D: Db, T: Transaction> {

create_db!(
TributaryProvidedDb {
TransactionDb: (genesis: &[u8], hash: &[u8]) -> Vec<u8>,
CurrentDb: (genesis: &[u8]) -> Vec<u8>,
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<u8>,
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<D: Db, T: Transaction> ProvidedTransactions<D, T> {
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, &currently_provided[i .. (i + 32)])
&mut TransactionDb::get(&res.db, &res.genesis, currently_provided[i])
.unwrap()
.as_ref(),
)
Expand Down Expand Up @@ -80,40 +80,40 @@ impl<D: Db, T: Transaction> ProvidedTransactions<D, T> {
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, &currently_provided);
let mut currently_provided = CurrentProvidedDb::get(&txn, &self.genesis).unwrap_or_default();
currently_provided.push(tx_hash);
CurrentProvidedDb::set(&mut txn, &self.genesis, &currently_provided);
txn.commit();

if self.transactions.get(order).is_none() {
Expand All @@ -135,13 +135,13 @@ impl<D: Db, T: Transaction> ProvidedTransactions<D, T> {
) {
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!(&currently_provided.drain(i .. (i + 32)).collect::<Vec<_>>(), &tx);
if currently_provided[i] == tx {
assert_eq!(currently_provided[i], tx);
break;
}

Expand All @@ -150,21 +150,17 @@ impl<D: Db, T: Transaction> ProvidedTransactions<D, T> {
panic!("couldn't find completed TX in currently provided");
}
}
CurrentDb::set(txn, &self.genesis, &currently_provided);
CurrentProvidedDb::set(txn, &self.genesis, &currently_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);
}
}

0 comments on commit d94970a

Please sign in to comment.