Implement Tributary syncing

Also adds a forwards-lookup to the Tributary blockchain.
This commit is contained in:
Luke Parker
2023-04-24 00:53:15 -04:00
parent 215155f84b
commit 14388e746c
6 changed files with 119 additions and 55 deletions

View File

@@ -37,6 +37,9 @@ impl<D: Db, T: Transaction> Blockchain<D, T> {
fn commit_key(hash: &[u8; 32]) -> Vec<u8> {
D::key(b"tributary_blockchain", b"commit", hash)
}
fn block_after_key(hash: &[u8; 32]) -> Vec<u8> {
D::key(b"tributary_blockchain", b"block_after", hash)
}
fn next_nonce_key(&self, signer: &<Ristretto as Ciphersuite>::G) -> Vec<u8> {
D::key(
b"tributary_blockchain",
@@ -105,6 +108,10 @@ impl<D: Db, T: Transaction> Blockchain<D, T> {
Self::commit_from_db(self.db.as_ref().unwrap(), block)
}
pub(crate) fn block_after(db: &D, block: &[u8; 32]) -> Option<[u8; 32]> {
db.get(Self::block_after_key(block)).map(|bytes| bytes.try_into().unwrap())
}
pub(crate) fn add_transaction(&mut self, internal: bool, tx: T) -> bool {
self.mempool.add(&self.next_nonces, internal, tx)
}
@@ -158,6 +165,8 @@ impl<D: Db, T: Transaction> Blockchain<D, T> {
txn.put(Self::block_key(&self.tip), block.serialize());
txn.put(Self::commit_key(&self.tip), commit);
txn.put(Self::block_after_key(&block.parent()), block.hash());
for tx in &block.transactions {
match tx.kind() {
TransactionKind::Provided(order) => {

View File

@@ -149,6 +149,9 @@ impl<D: Db, T: Transaction, P: P2p> Tributary<D, T, P> {
pub fn commit(&self, hash: &[u8; 32]) -> Option<Vec<u8>> {
Blockchain::<D, T>::commit_from_db(&self.db, hash)
}
pub fn block_after(&self, hash: &[u8; 32]) -> Option<[u8; 32]> {
Blockchain::<D, T>::block_after(&self.db, hash)
}
pub fn time_of_block(&self, hash: &[u8; 32]) -> Option<u64> {
self
.commit(hash)

View File

@@ -23,17 +23,18 @@ fn new_genesis() -> [u8; 32] {
fn new_blockchain<T: Transaction>(
genesis: [u8; 32],
participants: &[<Ristretto as Ciphersuite>::G],
) -> Blockchain<MemDb, T> {
let blockchain = Blockchain::new(MemDb::new(), genesis, participants);
) -> (MemDb, Blockchain<MemDb, T>) {
let db = MemDb::new();
let blockchain = Blockchain::new(db.clone(), genesis, participants);
assert_eq!(blockchain.tip(), genesis);
assert_eq!(blockchain.block_number(), 0);
blockchain
(db, blockchain)
}
#[test]
fn block_addition() {
let genesis = new_genesis();
let mut blockchain = new_blockchain::<SignedTransaction>(genesis, &[]);
let (db, mut blockchain) = new_blockchain::<SignedTransaction>(genesis, &[]);
let block = blockchain.build_block();
assert_eq!(block.header.parent, genesis);
assert_eq!(block.header.transactions, [0; 32]);
@@ -41,12 +42,16 @@ fn block_addition() {
assert!(blockchain.add_block(&block, vec![]).is_ok());
assert_eq!(blockchain.tip(), block.hash());
assert_eq!(blockchain.block_number(), 1);
assert_eq!(
Blockchain::<MemDb, SignedTransaction>::block_after(&db, &block.parent()).unwrap(),
block.hash()
);
}
#[test]
fn invalid_block() {
let genesis = new_genesis();
let mut blockchain = new_blockchain::<SignedTransaction>(genesis, &[]);
let (_, mut blockchain) = new_blockchain::<SignedTransaction>(genesis, &[]);
let block = blockchain.build_block();
@@ -77,7 +82,7 @@ fn invalid_block() {
}
// Run the rest of the tests with them as a participant
let blockchain = new_blockchain(genesis, &[tx.1.signer]);
let (_, blockchain) = new_blockchain(genesis, &[tx.1.signer]);
// Re-run the not a participant block to make sure it now works
{
@@ -130,7 +135,7 @@ fn signed_transaction() {
let tx = crate::tests::signed_transaction(&mut OsRng, genesis, &key, 0);
let signer = tx.1.signer;
let mut blockchain = new_blockchain::<SignedTransaction>(genesis, &[signer]);
let (_, mut blockchain) = new_blockchain::<SignedTransaction>(genesis, &[signer]);
assert_eq!(blockchain.next_nonce(signer), Some(0));
let test = |blockchain: &mut Blockchain<MemDb, SignedTransaction>,
@@ -176,7 +181,7 @@ fn signed_transaction() {
#[test]
fn provided_transaction() {
let genesis = new_genesis();
let mut blockchain = new_blockchain::<ProvidedTransaction>(genesis, &[]);
let (_, mut blockchain) = new_blockchain::<ProvidedTransaction>(genesis, &[]);
let tx = random_provided_transaction(&mut OsRng);