|
| 1 | +use cid::Cid; |
| 2 | +use fvm_ipld_hamt::{BytesKey, Hamt, Sha256}; |
| 3 | +use fvm_shared::address::Address; |
| 4 | +use fvm_shared::bigint::bigint_ser; |
| 5 | +use fvm_shared::blockstore::MemoryBlockstore; |
| 6 | +use fvm_shared::econ::TokenAmount; |
| 7 | +use fvm_shared::encoding::tuple::*; |
| 8 | +use std::error::Error; |
| 9 | +use std::fmt; |
| 10 | + |
| 11 | +pub struct VM<'bs> { |
| 12 | + store: &'bs MemoryBlockstore, |
| 13 | + state_root: Cid, |
| 14 | + actors_dirty: bool, |
| 15 | + actors: Hamt<&'bs MemoryBlockstore, Actor, BytesKey>, |
| 16 | +} |
| 17 | + |
| 18 | +impl<'bs> VM<'bs> { |
| 19 | + pub fn new(store: &'bs MemoryBlockstore) -> VM<'bs> { |
| 20 | + let mut actors = Hamt::<&'bs MemoryBlockstore, Actor, BytesKey, Sha256>::new(store); |
| 21 | + VM { store, state_root: actors.flush().unwrap(), actors_dirty: false, actors } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn get_actor(&self, addr: &Address) -> Option<&Actor> { |
| 25 | + self.actors.get(&addr.to_bytes()).unwrap() |
| 26 | + } |
| 27 | + |
| 28 | + // blindly overwrite the actor at this address whether it previously existed or not |
| 29 | + pub fn set_actor(&mut self, key: &Address, a: Actor) { |
| 30 | + let _ = self.actors.set(key.to_bytes().into(), a).unwrap(); |
| 31 | + } |
| 32 | + |
| 33 | + pub fn checkpoint(&mut self) -> Cid { |
| 34 | + self.state_root = self.actors.flush().unwrap(); |
| 35 | + self.actors_dirty = false; |
| 36 | + self.state_root |
| 37 | + } |
| 38 | + |
| 39 | + pub fn rollback(&mut self, root: &Cid) { |
| 40 | + self.actors = |
| 41 | + Hamt::<&'bs MemoryBlockstore, Actor, BytesKey, Sha256>::load(root, self.store).unwrap(); |
| 42 | + self.state_root = *root; |
| 43 | + self.actors_dirty = false; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Serialize_tuple, Deserialize_tuple, Clone, PartialEq, Debug)] |
| 48 | +pub struct Actor { |
| 49 | + pub code: Cid, // Might want to mock this out to avoid dealing with the annoying bundler |
| 50 | + pub head: Cid, |
| 51 | + pub call_seq_num: u64, |
| 52 | + #[serde(with = "bigint_ser")] |
| 53 | + pub balance: TokenAmount, |
| 54 | +} |
| 55 | + |
| 56 | +pub fn actor(code: Cid, head: Cid, seq: u64, bal: TokenAmount) -> Actor { |
| 57 | + Actor { code, head, call_seq_num: seq, balance: bal } |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Debug)] |
| 61 | +pub struct TestVMError { |
| 62 | + msg: String, |
| 63 | +} |
| 64 | + |
| 65 | +impl fmt::Display for TestVMError { |
| 66 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 67 | + write!(f, "{}", self.msg) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl Error for TestVMError { |
| 72 | + fn description(&self) -> &str { |
| 73 | + &self.msg |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl From<fvm_ipld_hamt::Error> for TestVMError { |
| 78 | + fn from(h_err: fvm_ipld_hamt::Error) -> Self { |
| 79 | + vm_err(h_err.to_string().as_str()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +pub fn vm_err(msg: &str) -> TestVMError { |
| 84 | + TestVMError { msg: msg.to_string() } |
| 85 | +} |
0 commit comments