Skip to content

Commit

Permalink
simulate machine context fields in the TestVM
Browse files Browse the repository at this point in the history
  • Loading branch information
alexytsu committed Oct 30, 2023
1 parent 0f202bb commit 649515a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
55 changes: 38 additions & 17 deletions test_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ pub struct TestVM {
pub primitives: FakePrimitives,
pub store: Rc<MemoryBlockstore>,
pub state_root: RefCell<Cid>,
circulating_supply: RefCell<TokenAmount>,
actors_dirty: RefCell<bool>,
actors_cache: RefCell<HashMap<Address, ActorState>>,
invocations: RefCell<Vec<InvocationTrace>>,
// MachineContext equivalents
network_version: NetworkVersion,
curr_epoch: RefCell<ChainEpoch>,
invocations: RefCell<Vec<InvocationTrace>>,
circulating_supply: RefCell<TokenAmount>,
base_fee: RefCell<TokenAmount>,
timestamp: RefCell<u64>,
}

impl TestVM {
Expand All @@ -81,6 +84,8 @@ impl TestVM {
network_version: NetworkVersion::V16,
curr_epoch: RefCell::new(ChainEpoch::zero()),
invocations: RefCell::new(vec![]),
base_fee: RefCell::new(TokenAmount::zero()),
timestamp: RefCell::new(0),
}
}

Expand Down Expand Up @@ -274,10 +279,6 @@ impl VM for TestVM {
self.store.as_ref()
}

fn epoch(&self) -> ChainEpoch {
*self.curr_epoch.borrow()
}

fn execute_message(
&self,
from: &Address,
Expand Down Expand Up @@ -361,10 +362,6 @@ impl VM for TestVM {
st.resolve_address(&self.store, address).unwrap()
}

fn set_epoch(&self, epoch: ChainEpoch) {
self.curr_epoch.replace(epoch);
}

fn balance(&self, address: &Address) -> TokenAmount {
let a = self.actor(address);
a.map_or(TokenAmount::zero(), |a| a.balance)
Expand Down Expand Up @@ -400,13 +397,6 @@ impl VM for TestVM {
fn actor_manifest(&self) -> BTreeMap<Cid, Type> {
ACTOR_TYPES.clone()
}
fn circulating_supply(&self) -> TokenAmount {
self.circulating_supply.borrow().clone()
}

fn set_circulating_supply(&self, supply: TokenAmount) {
self.circulating_supply.replace(supply);
}

fn actor_states(&self) -> BTreeMap<Address, ActorState> {
let map = self.actor_map();
Expand All @@ -419,4 +409,35 @@ impl VM for TestVM {

tree
}

fn epoch(&self) -> ChainEpoch {
*self.curr_epoch.borrow()
}

fn set_epoch(&self, epoch: ChainEpoch) {
self.curr_epoch.replace(epoch);
}
fn circulating_supply(&self) -> TokenAmount {
self.circulating_supply.borrow().clone()
}

fn set_circulating_supply(&self, supply: TokenAmount) {
self.circulating_supply.replace(supply);
}

fn base_fee(&self) -> TokenAmount {
self.base_fee.borrow().clone()
}

fn set_base_fee(&self, amount: TokenAmount) {
self.base_fee.replace(amount);
}

fn timestamp(&self) -> u64 {
*self.timestamp.borrow()
}

fn set_timestamp(&self, timestamp: u64) {
self.timestamp.replace(timestamp);
}
}
36 changes: 24 additions & 12 deletions vm_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ pub trait VM {
/// Returns the underlying blockstore of the VM
fn blockstore(&self) -> &dyn Blockstore;

/// Get the current chain epoch
fn epoch(&self) -> ChainEpoch;

/// Sets the epoch to the specified value
fn set_epoch(&self, epoch: ChainEpoch);

/// Get information about an actor
fn actor(&self, address: &Address) -> Option<ActorState>;

Expand Down Expand Up @@ -76,12 +70,6 @@ pub trait VM {
/// Take all the invocations that have been made since the last call to this method
fn take_invocations(&self) -> Vec<InvocationTrace>;

/// Set the circulating supply constant for the network
fn set_circulating_supply(&self, supply: TokenAmount);

/// Get the circulating supply constant for the network
fn circulating_supply(&self) -> TokenAmount;

/// Provides access to VM primitives
fn primitives(&self) -> &dyn Primitives;

Expand All @@ -90,6 +78,30 @@ pub trait VM {

/// Returns a map of all actor addresses to their corresponding states
fn actor_states(&self) -> BTreeMap<Address, ActorState>;

/// Get the current chain epoch
fn epoch(&self) -> ChainEpoch;

/// Sets the epoch to the specified value
fn set_epoch(&self, epoch: ChainEpoch);

/// Get the circulating supply constant for the network
fn circulating_supply(&self) -> TokenAmount;

/// Set the circulating supply constant for the network
fn set_circulating_supply(&self, supply: TokenAmount);

/// Get the current base fee
fn base_fee(&self) -> TokenAmount;

/// Set the current base fee
fn set_base_fee(&self, amount: TokenAmount);

/// Get the current timestamp
fn timestamp(&self) -> u64;

/// Set the current timestamp
fn set_timestamp(&self, timestamp: u64);
}

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down

0 comments on commit 649515a

Please sign in to comment.