datrs / hypercore

Secure, distributed, append-only log
https://docs.rs/hypercore
Apache License 2.0
332 stars 37 forks source link

implement as OO #2

Closed yoshuawuyts closed 6 years ago

yoshuawuyts commented 6 years ago

Was thinking, a cleaner way of implementing many of the things we have in-source right now would be by using traits / structs a bit better. Currently we have the following problems:

Something like the following:

Traits

Structs

Storage API

It'd be good to have the Store trait to implement a method that can write to a Storage instance.

trait Storage {
  fn from_bytes(index: usize, buf: &[u8]) -> Self;
  fn to_vec(&self) -> Vec<u8>;
  fn store<T>(&self, index: usize) -> Result<(), Error>;
}

The read/write methods can then respectively just call the .from_bytes() and .to_vec() methods respectively. Persisting to disk can then be done by just calling out to .store().

By having it in a trait we should be able to simplify the Storage module a bunch. We'll still need dedicated methods for reading / writing each type, but the hope is that we'll be able to do away with some of the inline magic numbers (e.g. 40, 32) and move them to be part of the corresponding structs.

Why Encrypt & Hash traits?

I think it's pretty nice to have a separate submodule dedicated to crypto. By creating traits rather than functions, we can create a looser relationship between our crypto internals, and the way we provide data to the crypto functions.

yoshuawuyts commented 6 years ago

This is done too!

Not everything was feasible, but we achieved most of it.