test.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // rocksdb is the blockchain database
  2. // it is a key value store
  3. // sqlite is the encrypted wallet
  4. use rusqlite::{Connection, Result};
  5. use rocksdb::DB;
  6. fn main() -> Result<()> {
  7. wallet()?;
  8. blockchain()?;
  9. Ok(())
  10. }
  11. fn wallet() -> Result<()> {
  12. let connector = connect()?;
  13. encrypt(&connector)?;
  14. println!("Created encrypted database.");
  15. decrypt(&connector)?;
  16. println!("Decrypted database.");
  17. Ok(())
  18. }
  19. fn connect() -> Result<Connection> {
  20. println!("Attempting to establish a connection...");
  21. let path = "/home/x/src/sapvi/src/bin/wallet/wallet.db";
  22. let connector = Connection::open(&path);
  23. println!("Path created at {}", path);
  24. println!("Connection established");
  25. connector
  26. }
  27. fn encrypt(conn: &Connection) -> Result<()> {
  28. println!("Attempting to create an encrypted database...");
  29. conn.execute_batch(
  30. "ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'testkey';
  31. SELECT sqlcipher_export('encrypted');
  32. DETACH DATABASE encrypted;",
  33. )
  34. }
  35. fn decrypt(conn: &Connection) -> Result<()> {
  36. println!("Attempting to decrypt database...");
  37. conn.execute_batch(
  38. "ATTACH DATABASE 'plaintext.db' AS plaintext KEY 'testkey';
  39. SELECT sqlcipher_export('plaintext');
  40. DETACH DATABASE plaintext;",
  41. )
  42. }
  43. fn blockchain() -> Result<()> {
  44. let db = create_db();
  45. write_db(&db)?;
  46. test_db(&db);
  47. Ok(())
  48. }
  49. fn create_db() -> DB {
  50. println!("Creating a blockchain database...");
  51. let path = "/home/x/src/sapvi/src/bin/wallet/blockchain.db";
  52. let db = DB::open_default(path).unwrap();
  53. db
  54. }
  55. fn write_db(db: &DB) -> Result<()> {
  56. println!("Writing to the blockchain...");
  57. db.put(b"test-value", b"test-key").unwrap();
  58. Ok(())
  59. }
  60. fn test_db(db: &DB) {
  61. println!("Testing if write was successful...");
  62. match db.get(b"test-value") {
  63. Ok(Some(value)) => println!("retrieved value {}", String::from_utf8(value).unwrap()),
  64. Ok(None) => println!("value not found"),
  65. Err(e) => println!("operational problem encountered: {}", e),
  66. }
  67. }
  68. //fn configure_blockchain() {
  69. // let mut opts = Options::default();
  70. // let mut block_opts = BlockBasedOptions::default();
  71. // block_opts.set_index_type(BlockBasedIndexType::HashSearch);
  72. //}