Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context to working set #832

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions examples/demo-nft-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<C: sov_modules_api::Context> Module for NonFungibleToken<C> {
fn genesis(
&self,
_config: &Self::Config,
_working_set: &mut WorkingSet<C::Storage>,
_working_set: &mut WorkingSet<C>,
) -> anyhow::Result<(), Error> {
Ok(())
}
Expand All @@ -191,7 +191,7 @@ impl<C: sov_modules_api::Context> Module for NonFungibleToken<C> {
&self,
_msg: Self::CallMessage,
_context: &Self::Context,
_working_set: &mut WorkingSet<C::Storage>,
_working_set: &mut WorkingSet<C>,
) -> anyhow::Result<sov_modules_api::CallResponse, Error> {
Ok(sov_modules_api::CallResponse::default())
}
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<C: sov_modules_api::Context> sov_modules_api::Module for NonFungibleToken<C
fn genesis(
&self,
config: &Self::Config,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> Result<(), Error> {
Ok(self.init_module(config, working_set)?)
}
Expand All @@ -230,7 +230,7 @@ impl<C: sov_modules_api::Context> NonFungibleToken<C> {
pub(crate) fn init_module(
&self,
config: &<Self as sov_modules_api::Module>::Config,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<()> {
self.admin.set(&config.admin, working_set);
for (id, owner) in config.owners.iter() {
Expand All @@ -256,7 +256,7 @@ impl<C: sov_modules_api::Context> NonFungibleToken<C> {
&self,
id: u64,
context: &C,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<sov_modules_api::CallResponse> {
if self.owners.get(&id, working_set).is_some() {
bail!("Token with id {} already exists", id);
Expand All @@ -273,7 +273,7 @@ impl<C: sov_modules_api::Context> NonFungibleToken<C> {
id: u64,
to: C::Address,
context: &C,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<sov_modules_api::CallResponse> {
let token_owner = match self.owners.get(&id, working_set) {
None => {
Expand All @@ -296,7 +296,7 @@ impl<C: sov_modules_api::Context> NonFungibleToken<C> {
&self,
id: u64,
context: &C,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<sov_modules_api::CallResponse> {
let token_owner = match self.owners.get(&id, working_set) {
None => {
Expand Down Expand Up @@ -326,7 +326,7 @@ impl<C: sov_modules_api::Context> sov_modules_api::Module for NonFungibleToken<C
&self,
msg: Self::CallMessage,
context: &Self::Context,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> Result<sov_modules_api::CallResponse, Error> {
let call_result = match msg {
CallMessage::Mint { id } => self.mint(id, context, working_set),
Expand Down Expand Up @@ -361,7 +361,7 @@ impl<C: sov_modules_api::Context> NonFungibleToken<C> {
pub fn get_owner(
&self,
token_id: u64,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> RpcResult<OwnerResponse<C>> {
Ok(OwnerResponse {
owner: self.owners.get(&token_id, working_set),
Expand Down
6 changes: 3 additions & 3 deletions examples/demo-nft-module/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<C: Context> NonFungibleToken<C> {
&self,
id: u64,
context: &C,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> Result<CallResponse> {
if self.owners.get(&id, working_set).is_some() {
bail!("Token with id {} already exists", id);
Expand All @@ -52,7 +52,7 @@ impl<C: Context> NonFungibleToken<C> {
id: u64,
to: C::Address,
context: &C,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> Result<CallResponse> {
let token_owner = match self.owners.get(&id, working_set) {
None => {
Expand All @@ -75,7 +75,7 @@ impl<C: Context> NonFungibleToken<C> {
&self,
id: u64,
context: &C,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> Result<CallResponse> {
let token_owner = match self.owners.get(&id, working_set) {
None => {
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-nft-module/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl<C: Context> NonFungibleToken<C> {
pub(crate) fn init_module(
&self,
config: &<Self as sov_modules_api::Module>::Config,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> Result<()> {
self.admin.set(&config.admin, working_set);
for (id, owner) in config.owners.iter() {
Expand Down
8 changes: 2 additions & 6 deletions examples/demo-nft-module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,15 @@ impl<C: Context> Module for NonFungibleToken<C> {

type CallMessage = CallMessage<C>;

fn genesis(
&self,
config: &Self::Config,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<(), Error> {
fn genesis(&self, config: &Self::Config, working_set: &mut WorkingSet<C>) -> Result<(), Error> {
Ok(self.init_module(config, working_set)?)
}

fn call(
&self,
msg: Self::CallMessage,
context: &Self::Context,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> Result<CallResponse, Error> {
let call_result = match msg {
CallMessage::Mint { id } => self.mint(id, context, working_set),
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-nft-module/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<C: Context> NonFungibleToken<C> {
pub fn get_owner(
&self,
token_id: u64,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> RpcResult<OwnerResponse<C>> {
Ok(OwnerResponse {
owner: self.owners.get(&token_id, working_set),
Expand Down
15 changes: 8 additions & 7 deletions examples/demo-nft-module/tests/nft_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ fn transfer() {
owners: vec![(0, admin), (1, owner1), (2, owner2)],
};
let tmpdir = tempfile::tempdir().unwrap();
let mut working_set = WorkingSet::new(ProverStorage::with_path(tmpdir.path()).unwrap());
let mut working_set: WorkingSet<C> =
WorkingSet::new(ProverStorage::with_path(tmpdir.path()).unwrap());
let nft = NonFungibleToken::default();
nft.genesis(&config, &mut working_set).unwrap();

Expand All @@ -83,11 +84,10 @@ fn transfer() {
let error_message = transfer_attempt.err().unwrap().to_string();
assert_eq!("Only token owner can transfer token", error_message);

let query_token_owner =
|token_id: u64, working_set: &mut WorkingSet<Storage>| -> Option<Address> {
let query: OwnerResponse<C> = nft.get_owner(token_id, working_set).unwrap();
query.owner
};
let query_token_owner = |token_id: u64, working_set: &mut WorkingSet<C>| -> Option<Address> {
let query: OwnerResponse<C> = nft.get_owner(token_id, working_set).unwrap();
query.owner
};

// Normal transfer
let token1_owner = query_token_owner(1, &mut working_set);
Expand Down Expand Up @@ -126,7 +126,8 @@ fn burn() {
};

let tmpdir = tempfile::tempdir().unwrap();
let mut working_set = WorkingSet::new(ProverStorage::with_path(tmpdir.path()).unwrap());
let mut working_set: WorkingSet<C> =
WorkingSet::new(ProverStorage::with_path(tmpdir.path()).unwrap());
let nft = NonFungibleToken::default();
nft.genesis(&config, &mut working_set).unwrap();

Expand Down
8 changes: 4 additions & 4 deletions examples/demo-stf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ impl<C: Context> TxHooks for Runtime<C> {
fn pre_dispatch_tx_hook(
&self,
tx: Transaction<Self::Context>,
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<<Self::Context as Spec>::Address> {
self.accounts.pre_dispatch_tx_hook(tx, working_set)
}

fn post_dispatch_tx_hook(
&self,
tx: &Transaction<Self::Context>,
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<()> {
self.accounts.post_dispatch_tx_hook(tx, working_set)
}
Expand All @@ -137,15 +137,15 @@ impl<C: Context> ApplyBlobHooks for Runtime<C> {
fn lock_sequencer_bond(
&self,
sequencer: &[u8],
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<()> {
self.sequencer.lock_sequencer_bond(sequencer, working_set)
}

fn reward_sequencer(
&self,
amount: u64,
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<()> {
self.sequencer.reward_sequencer(amount, working_set)
}
Expand Down
20 changes: 7 additions & 13 deletions examples/demo-stf/src/hooks_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ impl<C: Context, Da: DaSpec> TxHooks for Runtime<C, Da> {
fn pre_dispatch_tx_hook(
&self,
tx: &Transaction<Self::Context>,
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<<Self::Context as Spec>::Address> {
self.accounts.pre_dispatch_tx_hook(tx, working_set)
}

fn post_dispatch_tx_hook(
&self,
tx: &Transaction<Self::Context>,
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<()> {
self.accounts.post_dispatch_tx_hook(tx, working_set)
}
Expand All @@ -38,15 +38,15 @@ impl<C: Context, Da: DaSpec> ApplyBlobHooks<Da::BlobTransaction> for Runtime<C,
fn begin_blob_hook(
&self,
blob: &mut Da::BlobTransaction,
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<()> {
self.sequencer_registry.begin_blob_hook(blob, working_set)
}

fn end_blob_hook(
&self,
result: Self::BlobResult,
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
working_set: &mut WorkingSet<C>,
) -> anyhow::Result<()> {
match result {
SequencerOutcome::Rewarded(_reward) => {
Expand Down Expand Up @@ -82,9 +82,7 @@ impl<C: Context, Da: DaSpec> SlotHooks<Da> for Runtime<C, Da> {
&self,
#[allow(unused_variables)] slot_header: &Da::BlockHeader,
#[allow(unused_variables)] validity_condition: &Da::ValidityCondition,
#[allow(unused_variables)] working_set: &mut sov_modules_api::WorkingSet<
<Self::Context as Spec>::Storage,
>,
#[allow(unused_variables)] working_set: &mut sov_modules_api::WorkingSet<C>,
) {
#[cfg(feature = "experimental")]
self.evm
Expand All @@ -93,9 +91,7 @@ impl<C: Context, Da: DaSpec> SlotHooks<Da> for Runtime<C, Da> {

fn end_slot_hook(
&self,
#[allow(unused_variables)] working_set: &mut sov_modules_api::WorkingSet<
<Self::Context as Spec>::Storage,
>,
#[allow(unused_variables)] working_set: &mut sov_modules_api::WorkingSet<C>,
) {
#[cfg(feature = "experimental")]
self.evm.end_slot_hook(working_set);
Expand All @@ -108,9 +104,7 @@ impl<C: Context, Da: sov_modules_api::DaSpec> FinalizeHook<Da> for Runtime<C, Da
fn finalize_slot_hook(
&self,
#[allow(unused_variables)] root_hash: [u8; 32],
#[allow(unused_variables)] accesorry_working_set: &mut AccessoryWorkingSet<
<Self::Context as Spec>::Storage,
>,
#[allow(unused_variables)] accesorry_working_set: &mut AccessoryWorkingSet<C>,
) {
#[cfg(feature = "experimental")]
self.evm
Expand Down
4 changes: 2 additions & 2 deletions examples/demo-stf/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::macros::DefaultRuntime;
#[cfg(feature = "native")]
use sov_modules_api::macros::{expose_rpc, CliWallet};
use sov_modules_api::{Context, DispatchCall, Genesis, MessageCodec, Spec};
use sov_modules_api::{Context, DispatchCall, Genesis, MessageCodec};
use sov_rollup_interface::da::DaSpec;
#[cfg(feature = "native")]
use sov_sequencer_registry::{SequencerRegistryRpcImpl, SequencerRegistryRpcServer};
Expand Down Expand Up @@ -110,7 +110,7 @@ impl<C: Context, Da: DaSpec> BlobSelector<Da> for Runtime<C, Da> {
fn get_blobs_for_this_slot<'a, I>(
&self,
current_blobs: I,
working_set: &mut sov_modules_api::WorkingSet<<Self::Context as Spec>::Storage>,
working_set: &mut sov_modules_api::WorkingSet<C>,
) -> anyhow::Result<Vec<BlobRefOrOwned<'a, Da::BlobTransaction>>>
where
I: IntoIterator<Item = &'a mut Da::BlobTransaction>,
Expand Down
6 changes: 3 additions & 3 deletions module-system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ accessible to other modules, but cannot be directly invoked by other users. A go

```rust
impl<C: Context> Bank<C> {
pub fn transfer_from(&self, from: &C::Address, to: &C::Address, coins: Coins, working_set: &mut WorkingSet<C::Storage>) {
pub fn transfer_from(&self, from: &C::Address, to: &C::Address, coins: Coins, working_set: &mut WorkingSet<C>) {
// Implementation elided...
}
}
Expand All @@ -75,7 +75,7 @@ tells the `call` function which inner method of the module to invoke. So a typic
```rust
impl<C: sov_modules_api::Context> sov_modules_api::Module for Bank<C> {
// Several definitions elided here ...
fn call(&self, msg: Self::CallMessage, context: &Self::Context, working_set: &mut WorkingSet<C::Storage>) {
fn call(&self, msg: Self::CallMessage, context: &Self::Context, working_set: &mut WorkingSet<C>) {
match msg {
CallMessage::CreateToken {
token_name,
Expand All @@ -101,7 +101,7 @@ impl<C: sov_modules_api::Context> Bank<C> {
&self,
user_address: C::Address,
token_address: C::Address,
working_set: &mut WorkingSet<C::Storage>,
working_set: &mut WorkingSet<C>,
) -> RpcResult<BalanceResponse> {
Ok(BalanceResponse {
amount: self.get_balance_of(user_address, token_address, working_set),
Expand Down
Loading