Skip to content

Commit

Permalink
More simplifications around view context objects (#2438)
Browse files Browse the repository at this point in the history
* move MemoryContext to context.rs, remove all other context aliases

* Rename ContextFromStore into ViewContext

* SDK: hide the notion of KV store during initialization

* rename ViewContext::create into ViewContext::create_root_context

* fix graphql schema again

* reviewer suggestions
  • Loading branch information
ma2bd authored Sep 2, 2024
1 parent 7ee42e9 commit 761bb2e
Show file tree
Hide file tree
Showing 71 changed files with 541 additions and 616 deletions.
4 changes: 2 additions & 2 deletions examples/amm/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use amm::{AmmAbi, Message, Operation, Parameters};
use fungible::{Account, FungibleTokenAbi};
use linera_sdk::{
base::{AccountOwner, Amount, ApplicationId, ChainId, WithContractAbi},
views::{RootView, View, ViewStorageContext},
views::{RootView, View},
Contract, ContractRuntime,
};
use num_bigint::BigUint;
Expand All @@ -34,7 +34,7 @@ impl Contract for AmmContract {
type Parameters = Parameters;

async fn load(runtime: ContractRuntime<Self>) -> Self {
let state = Amm::load(ViewStorageContext::from(runtime.key_value_store()))
let state = Amm::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
AmmContract { state, runtime }
Expand Down
7 changes: 2 additions & 5 deletions examples/amm/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use std::sync::Arc;
use amm::{Operation, Parameters};
use async_graphql::{EmptySubscription, Request, Response, Schema};
use linera_sdk::{
base::WithServiceAbi,
graphql::GraphQLMutationRoot,
views::{View, ViewStorageContext},
Service, ServiceRuntime,
base::WithServiceAbi, graphql::GraphQLMutationRoot, views::View, Service, ServiceRuntime,
};

use self::state::Amm;
Expand All @@ -32,7 +29,7 @@ impl Service for AmmService {
type Parameters = Parameters;

async fn new(runtime: ServiceRuntime<Self>) -> Self {
let state = Amm::load(ViewStorageContext::from(runtime.key_value_store()))
let state = Amm::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
AmmService {
Expand Down
12 changes: 4 additions & 8 deletions examples/counter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod state;
use counter::CounterAbi;
use linera_sdk::{
base::WithContractAbi,
views::{RootView, View, ViewStorageContext},
views::{RootView, View},
Contract, ContractRuntime,
};

Expand All @@ -31,7 +31,7 @@ impl Contract for CounterContract {
type Parameters = ();

async fn load(runtime: ContractRuntime<Self>) -> Self {
let state = Counter::load(ViewStorageContext::from(runtime.key_value_store()))
let state = Counter::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
CounterContract { state, runtime }
Expand Down Expand Up @@ -62,11 +62,7 @@ impl Contract for CounterContract {
#[cfg(test)]
mod tests {
use futures::FutureExt as _;
use linera_sdk::{
util::BlockingWait,
views::{View, ViewStorageContext},
Contract, ContractRuntime,
};
use linera_sdk::{util::BlockingWait, views::View, Contract, ContractRuntime};

use super::{Counter, CounterContract};

Expand Down Expand Up @@ -121,7 +117,7 @@ mod tests {
fn create_and_instantiate_counter(initial_value: u64) -> CounterContract {
let runtime = ContractRuntime::new().with_application_parameters(());
let mut contract = CounterContract {
state: Counter::load(ViewStorageContext::from(runtime.key_value_store()))
state: Counter::load(runtime.root_view_storage_context())
.blocking_wait()
.expect("Failed to read from mock key value store"),
runtime,
Expand Down
16 changes: 4 additions & 12 deletions examples/counter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
mod state;

use async_graphql::{EmptySubscription, Object, Request, Response, Schema};
use linera_sdk::{
base::WithServiceAbi,
views::{View, ViewStorageContext},
Service, ServiceRuntime,
};
use linera_sdk::{base::WithServiceAbi, views::View, Service, ServiceRuntime};

use self::state::Counter;

Expand All @@ -28,7 +24,7 @@ impl Service for CounterService {
type Parameters = ();

async fn new(runtime: ServiceRuntime<Self>) -> Self {
let state = Counter::load(ViewStorageContext::from(runtime.key_value_store()))
let state = Counter::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
CounterService { state }
Expand Down Expand Up @@ -71,11 +67,7 @@ impl QueryRoot {
mod tests {
use async_graphql::{Request, Response, Value};
use futures::FutureExt as _;
use linera_sdk::{
util::BlockingWait,
views::{View, ViewStorageContext},
Service, ServiceRuntime,
};
use linera_sdk::{util::BlockingWait, views::View, Service, ServiceRuntime};
use serde_json::json;

use super::{Counter, CounterService};
Expand All @@ -84,7 +76,7 @@ mod tests {
fn query() {
let value = 61_098_721_u64;
let runtime = ServiceRuntime::<CounterService>::new();
let mut state = Counter::load(ViewStorageContext::from(runtime.key_value_store()))
let mut state = Counter::load(runtime.root_view_storage_context())
.blocking_wait()
.expect("Failed to read from mock key value store");
state.value.set(value);
Expand Down
4 changes: 2 additions & 2 deletions examples/crowd-funding/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crowd_funding::{CrowdFundingAbi, InstantiationArgument, Message, Operation};
use fungible::{Account, FungibleTokenAbi};
use linera_sdk::{
base::{AccountOwner, Amount, ApplicationId, WithContractAbi},
views::{RootView, View, ViewStorageContext},
views::{RootView, View},
Contract, ContractRuntime,
};
use state::{CrowdFunding, Status};
Expand All @@ -31,7 +31,7 @@ impl Contract for CrowdFundingContract {
type Parameters = ApplicationId<fungible::FungibleTokenAbi>;

async fn load(runtime: ContractRuntime<Self>) -> Self {
let state = CrowdFunding::load(ViewStorageContext::from(runtime.key_value_store()))
let state = CrowdFunding::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
CrowdFundingContract { state, runtime }
Expand Down
4 changes: 2 additions & 2 deletions examples/crowd-funding/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crowd_funding::Operation;
use linera_sdk::{
base::{ApplicationId, WithServiceAbi},
graphql::GraphQLMutationRoot,
views::{View, ViewStorageContext},
views::View,
Service, ServiceRuntime,
};
use state::CrowdFunding;
Expand All @@ -31,7 +31,7 @@ impl Service for CrowdFundingService {
type Parameters = ApplicationId<fungible::FungibleTokenAbi>;

async fn new(runtime: ServiceRuntime<Self>) -> Self {
let state = CrowdFunding::load(ViewStorageContext::from(runtime.key_value_store()))
let state = CrowdFunding::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
CrowdFundingService {
Expand Down
4 changes: 2 additions & 2 deletions examples/ethereum-tracker/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ethereum_tracker::{EthereumTrackerAbi, InstantiationArgument, U256Cont};
use linera_sdk::{
base::WithContractAbi,
ethereum::{EthereumClient, EthereumDataType, EthereumQueries as _},
views::{RootView, View, ViewStorageContext},
views::{RootView, View},
Contract, ContractRuntime,
};

Expand All @@ -32,7 +32,7 @@ impl Contract for EthereumTrackerContract {
type Parameters = ();

async fn load(runtime: ContractRuntime<Self>) -> Self {
let state = EthereumTracker::load(ViewStorageContext::from(runtime.key_value_store()))
let state = EthereumTracker::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
EthereumTrackerContract { state, runtime }
Expand Down
7 changes: 2 additions & 5 deletions examples/ethereum-tracker/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use std::sync::Arc;
use async_graphql::{EmptySubscription, Request, Response, Schema};
use ethereum_tracker::Operation;
use linera_sdk::{
base::WithServiceAbi,
graphql::GraphQLMutationRoot,
views::{View, ViewStorageContext},
Service, ServiceRuntime,
base::WithServiceAbi, graphql::GraphQLMutationRoot, views::View, Service, ServiceRuntime,
};

use self::state::EthereumTracker;
Expand All @@ -33,7 +30,7 @@ impl Service for EthereumTrackerService {
type Parameters = ();

async fn new(runtime: ServiceRuntime<Self>) -> Self {
let state = EthereumTracker::load(ViewStorageContext::from(runtime.key_value_store()))
let state = EthereumTracker::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
EthereumTrackerService {
Expand Down
4 changes: 2 additions & 2 deletions examples/fungible/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use fungible::{
};
use linera_sdk::{
base::{AccountOwner, Amount, WithContractAbi},
views::{RootView, View, ViewStorageContext},
views::{RootView, View},
Contract, ContractRuntime,
};

Expand All @@ -35,7 +35,7 @@ impl Contract for FungibleTokenContract {
type InstantiationArgument = InitialState;

async fn load(runtime: ContractRuntime<Self>) -> Self {
let state = FungibleToken::load(ViewStorageContext::from(runtime.key_value_store()))
let state = FungibleToken::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
FungibleTokenContract { state, runtime }
Expand Down
4 changes: 2 additions & 2 deletions examples/fungible/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use fungible::{Operation, Parameters};
use linera_sdk::{
base::{AccountOwner, Amount, WithServiceAbi},
graphql::GraphQLMutationRoot,
views::{MapView, View, ViewStorageContext},
views::{MapView, View},
Service, ServiceRuntime,
};

Expand All @@ -34,7 +34,7 @@ impl Service for FungibleTokenService {
type Parameters = Parameters;

async fn new(runtime: ServiceRuntime<Self>) -> Self {
let state = FungibleToken::load(ViewStorageContext::from(runtime.key_value_store()))
let state = FungibleToken::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
FungibleTokenService {
Expand Down
4 changes: 2 additions & 2 deletions examples/gen-nft/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use fungible::Account;
use gen_nft::{GenNftAbi, Message, Nft, Operation, TokenId};
use linera_sdk::{
base::{AccountOwner, WithContractAbi},
views::{RootView, View, ViewStorageContext},
views::{RootView, View},
Contract, ContractRuntime,
};

Expand All @@ -34,7 +34,7 @@ impl Contract for GenNftContract {
type Parameters = ();

async fn load(runtime: ContractRuntime<Self>) -> Self {
let state = GenNft::load(ViewStorageContext::from(runtime.key_value_store()))
let state = GenNft::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
GenNftContract { state, runtime }
Expand Down
4 changes: 2 additions & 2 deletions examples/gen-nft/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use fungible::Account;
use gen_nft::{NftOutput, Operation, TokenId};
use linera_sdk::{
base::{AccountOwner, WithServiceAbi},
views::{View, ViewStorageContext},
views::View,
Service, ServiceRuntime,
};
use log::info;
Expand All @@ -42,7 +42,7 @@ impl Service for GenNftService {
type Parameters = ();

async fn new(runtime: ServiceRuntime<Self>) -> Self {
let state = GenNft::load(ViewStorageContext::from(runtime.key_value_store()))
let state = GenNft::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
GenNftService {
Expand Down
4 changes: 2 additions & 2 deletions examples/hex-game/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use linera_sdk::{
Amount, ApplicationPermissions, ChainId, ChainOwnership, Owner, PublicKey, TimeoutConfig,
WithContractAbi,
},
views::{RootView, View, ViewStorageContext},
views::{RootView, View},
Contract, ContractRuntime,
};
use serde::{Deserialize, Serialize};
Expand All @@ -35,7 +35,7 @@ impl Contract for HexContract {
type Parameters = ();

async fn load(runtime: ContractRuntime<Self>) -> Self {
let state = HexState::load(ViewStorageContext::from(runtime.key_value_store()))
let state = HexState::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
HexContract { state, runtime }
Expand Down
15 changes: 4 additions & 11 deletions examples/hex-game/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use std::sync::{Arc, Mutex};
use async_graphql::{ComplexObject, Context, EmptySubscription, Request, Response, Schema};
use hex_game::{Operation, Player};
use linera_sdk::{
base::WithServiceAbi,
graphql::GraphQLMutationRoot,
views::{View, ViewStorageContext},
Service, ServiceRuntime,
base::WithServiceAbi, graphql::GraphQLMutationRoot, views::View, Service, ServiceRuntime,
};

use self::state::HexState;
Expand All @@ -34,7 +31,7 @@ impl Service for HexService {
type Parameters = ();

async fn new(runtime: ServiceRuntime<Self>) -> Self {
let state = HexState::load(ViewStorageContext::from(runtime.key_value_store()))
let state = HexState::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
HexService {
Expand Down Expand Up @@ -76,19 +73,15 @@ impl HexState {
#[cfg(test)]
mod tests {
use async_graphql::{futures_util::FutureExt, Request};
use linera_sdk::{
util::BlockingWait,
views::{View, ViewStorageContext},
Service, ServiceRuntime,
};
use linera_sdk::{util::BlockingWait, views::View, Service, ServiceRuntime};
use serde_json::json;

use super::*;

#[test]
fn query() {
let runtime = ServiceRuntime::<HexService>::new();
let state = HexState::load(ViewStorageContext::from(runtime.key_value_store()))
let state = HexState::load(runtime.root_view_storage_context())
.blocking_wait()
.expect("Failed to read from mock key value store");

Expand Down
4 changes: 2 additions & 2 deletions examples/matching-engine/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::cmp::min;
use fungible::{Account, FungibleTokenAbi};
use linera_sdk::{
base::{AccountOwner, Amount, ApplicationId, ChainId, WithContractAbi},
views::{RootView, View, ViewStorageContext},
views::{RootView, View},
Contract, ContractRuntime,
};
use matching_engine::{
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Contract for MatchingEngineContract {
type Parameters = Parameters;

async fn load(runtime: ContractRuntime<Self>) -> Self {
let state = MatchingEngine::load(ViewStorageContext::from(runtime.key_value_store()))
let state = MatchingEngine::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
MatchingEngineContract { state, runtime }
Expand Down
7 changes: 2 additions & 5 deletions examples/matching-engine/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use std::sync::Arc;

use async_graphql::{EmptySubscription, Request, Response, Schema};
use linera_sdk::{
base::WithServiceAbi,
graphql::GraphQLMutationRoot,
views::{View, ViewStorageContext},
Service, ServiceRuntime,
base::WithServiceAbi, graphql::GraphQLMutationRoot, views::View, Service, ServiceRuntime,
};
use matching_engine::{Operation, Parameters};

Expand All @@ -32,7 +29,7 @@ impl Service for MatchingEngineService {
type Parameters = Parameters;

async fn new(runtime: ServiceRuntime<Self>) -> Self {
let state = MatchingEngine::load(ViewStorageContext::from(runtime.key_value_store()))
let state = MatchingEngine::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
MatchingEngineService {
Expand Down
4 changes: 2 additions & 2 deletions examples/non-fungible/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::BTreeSet;
use fungible::Account;
use linera_sdk::{
base::{AccountOwner, WithContractAbi},
views::{RootView, View, ViewStorageContext},
views::{RootView, View},
Contract, ContractRuntime, DataBlobHash,
};
use non_fungible::{Message, Nft, NonFungibleTokenAbi, Operation, TokenId};
Expand All @@ -34,7 +34,7 @@ impl Contract for NonFungibleTokenContract {
type Parameters = ();

async fn load(runtime: ContractRuntime<Self>) -> Self {
let state = NonFungibleToken::load(ViewStorageContext::from(runtime.key_value_store()))
let state = NonFungibleToken::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
NonFungibleTokenContract { state, runtime }
Expand Down
Loading

0 comments on commit 761bb2e

Please sign in to comment.