Skip to content

Commit

Permalink
Upgrade ink to 4.0-rc
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Feb 6, 2023
1 parent d4d4e8d commit afdd767
Show file tree
Hide file tree
Showing 48 changed files with 802 additions and 550 deletions.
336 changes: 206 additions & 130 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions crates/phactory/src/contracts/pink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,11 @@ pub mod cluster {
let output = result.result.map_err(|err| {
anyhow::anyhow!("Failed to get the system contract version: {err:?}")
})?;
self.config.version = Decode::decode(&mut &output.data[..])
.context("Failed to decode the system contract version")?;
self.config.version = Result::<_, ()>::decode(&mut &output.data[..])
.context("Failed to decode the system contract version")?
.or(Err(anyhow::anyhow!(
"Failed to get the system contract version"
)))?;
const SUPPORTED_API_VERSION: u16 = 0;
if self.config.version.0 > SUPPORTED_API_VERSION {
anyhow::bail!(
Expand Down
2 changes: 1 addition & 1 deletion crates/phactory/src/prpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ impl<Platform: pal::Platform + Serialize + DeserializeOwned> Phactory<Platform>
pub fn upload_sidevm_code(&mut self, contract_id: ContractId, code: Vec<u8>) -> RpcResult<()> {
self.system()?
.upload_sidevm_code(contract_id, code)
.map_err(from_display)
.map_err(from_debug)
}

pub fn load_chain_state(
Expand Down
1 change: 1 addition & 0 deletions crates/phactory/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,7 @@ pub(crate) fn apply_pink_events(
}
PinkEvent::SetContractWeight { contract, weight } => {
ensure_system!();
info!("Set contract weight for {:?} to {:?}", contract, weight);
let contract = get_contract!(&contract);
contract.set_weight(weight);
contracts.weight_changed = true;
Expand Down
1 change: 1 addition & 0 deletions crates/pink-drivers/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mkdir -p dist

build() {
NAME=$1
echo "Building $1"
if [ x$2 = x1 ]; then
(cd $NAME && make)
cp $NAME/sideprog.wasm dist/$NAME.sidevm.wasm
Expand Down
14 changes: 3 additions & 11 deletions crates/pink-drivers/log_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ edition = "2018"
resolver = "2"

[dependencies]
ink_primitives = { version = "3.4.0", default-features = false }
ink_metadata = { version = "3.4.0", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.4.0", default-features = false }
ink_storage = { version = "3.4.0", default-features = false }
ink_lang = { version = "3.4.0", default-features = false }

ink = { version = "4.0.0-rc", default-features = false }
scale = { package = "parity-scale-codec", version = "3.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.1", default-features = false, features = ["derive"], optional = true }

pink-extension = { version = "0.2", path = "../../pink/pink-extension", default-features = false }
pink-extension = { version = "0.4", default-features = false, path = "../../pink/pink-extension", features = ["ink-as-dependency"] }

[build-dependencies]
run_shell = "0.1"
Expand All @@ -33,10 +28,7 @@ crate-type = [
[features]
default = ["std"]
std = [
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_primitives/std",
"ink/std",
"scale/std",
"scale-info/std",
]
Expand Down
18 changes: 5 additions & 13 deletions crates/pink-drivers/sidevm_deployer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ edition = "2021"
description = "This is a demo describes how to write a pink driver contract"

[dependencies]
ink_primitives = { version = "3.4.0", default-features = false }
ink_metadata = { version = "3.4.0", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.4.0", default-features = false }
ink_storage = { version = "3.4.0", default-features = false }
ink_lang = { version = "3.4.0", default-features = false }

ink = { version = "4.0.0-rc", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

pink-extension = { version = "0.2", default-features = false, path = "../../pink/pink-extension", features = ["ink-as-dependency"] }
pink-system = { version = "0.1", default-features = false, path = "../system", features = ["ink-as-dependency"] }
pink-extension = { version = "0.4", default-features = false, path = "../../pink/pink-extension", features = ["ink-as-dependency"] }
system = { version = "0.1", default-features = false, path = "../system", features = ["ink-as-dependency"] }

[lib]
name = "sidevm_deployer"
Expand All @@ -29,13 +24,10 @@ crate-type = [
[features]
default = ["std"]
std = [
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_primitives/std",
"ink/std",
"scale/std",
"scale-info/std",
"pink-extension/std",
"pink-system/std",
"system/std",
]
ink-as-dependency = []
20 changes: 9 additions & 11 deletions crates/pink-drivers/sidevm_deployer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ use pink_extension as pink;
#[pink::contract(env = PinkEnvironment)]
mod sidevm_deployer {
use super::pink;
use ink_storage::{traits::SpreadAllocate, Mapping};
use ink::storage::Mapping;
use pink::system::DriverError as Error;
use pink::PinkEnvironment;

type Result<T> = core::result::Result<T, Error>;

#[ink(storage)]
#[derive(SpreadAllocate)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct SidevmOp {
/// Owner of the contract
owner: AccountId,
Expand All @@ -26,9 +24,10 @@ mod sidevm_deployer {
impl SidevmOp {
#[ink(constructor)]
pub fn default() -> Self {
ink_lang::utils::initialize_contract(|me: &mut Self| {
me.owner = Self::env().caller();
})
Self {
owner: Self::env().caller(),
whitelist: Default::default()
}
}
#[ink(message)]
pub fn allow(&mut self, contract: AccountId) -> Result<()> {
Expand Down Expand Up @@ -62,17 +61,16 @@ mod sidevm_deployer {
mod tests {
use super::*;

use ink_lang as ink;
use pink_system::System;
use system::System;

const SYSTEM_ADDR: [u8; 32] = [42u8; 32];
const SIDEVMOP_ADDR: [u8; 32] = [24u8; 32];

fn with_callee<T>(callee: [u8; 32], f: impl FnOnce() -> T) -> T {
let prev = ink_env::test::callee::<PinkEnvironment>();
ink_env::test::set_callee::<PinkEnvironment>(callee.into());
let prev = ink::env::test::callee::<PinkEnvironment>();
ink::env::test::set_callee::<PinkEnvironment>(callee.into());
let ret = f();
ink_env::test::set_callee::<PinkEnvironment>(prev);
ink::env::test::set_callee::<PinkEnvironment>(prev);
ret
}

Expand Down
16 changes: 4 additions & 12 deletions crates/pink-drivers/system/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
[package]
name = "pink-system"
name = "system"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"

[dependencies]
ink_primitives = { version = "3.4.0", default-features = false }
ink_metadata = { version = "3.4.0", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.4.0", default-features = false }
ink_storage = { version = "3.4.0", default-features = false }
ink_lang = { version = "3.4.0", default-features = false }

ink = { version = "4.0.0-rc", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

pink-extension = { version = "0.2", path = "../../pink/pink-extension", default-features = false }
pink-extension = { version = "0.4", default-features = false, path = "../../pink/pink-extension" }

[lib]
name = "system"
Expand All @@ -27,10 +22,7 @@ crate-type = [
[features]
default = ["std"]
std = [
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_primitives/std",
"ink/std",
"scale/std",
"scale-info/std",
"pink-extension/std",
Expand Down
33 changes: 17 additions & 16 deletions crates/pink-drivers/system/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ pub use system::System;
mod system {
use super::pink;
use alloc::string::String;
use ink_storage::{traits::SpreadAllocate, Mapping};
use ink::storage::Mapping;
use pink::system::{ContractDeposit, ContractDepositRef, DriverError, Error, Result};
use pink::{HookPoint, PinkEnvironment};

/// Pink's system contract.
#[ink(storage)]
#[derive(SpreadAllocate)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct System {
/// The owner of the contract(the cluster).
owner: AccountId,
Expand All @@ -30,7 +28,11 @@ mod system {
impl System {
#[ink(constructor, selector = 0xed4b9d1b)]
pub fn default() -> Self {
ink_lang::utils::initialize_contract(|me: &mut Self| me.owner = Self::env().caller())
Self {
owner: Self::env().caller(),
administrators: Default::default(),
drivers: Default::default(),
}
}

fn ensure_owner(&self) -> Result<AccountId> {
Expand Down Expand Up @@ -67,7 +69,7 @@ mod system {
impl pink::system::System for System {
#[ink(message)]
fn version(&self) -> (u16, u16) {
(0, 1)
(1, 0)
}

#[ink(message)]
Expand Down Expand Up @@ -160,7 +162,7 @@ mod system {
deposit: Balance,
) -> Result<(), DriverError> {
self.ensure_self()?;
let flags = ink_env::CallFlags::default().set_allow_reentry(true);
let flags = ink::env::CallFlags::default().set_allow_reentry(true);
match ContractDepositRef::instance_with_call_flags(flags) {
Some(mut driver) => driver.change_deposit(contract_id, deposit),
None => Ok(()),
Expand All @@ -171,13 +173,12 @@ mod system {
#[cfg(test)]
mod tests {
use super::*;
use ink_lang as ink;
use pink::system::SystemRef;

const OWNER: [u8; 32] = [2u8; 32];

fn test_system() -> SystemRef {
ink_env::test::set_caller::<PinkEnvironment>(OWNER.into());
ink::env::test::set_caller::<PinkEnvironment>(OWNER.into());
SystemRef::mock_with(System::default());
SystemRef::instance()
}
Expand All @@ -186,14 +187,14 @@ mod system {
fn grant_admin_permissions() {
let mut system = test_system();
// The generated SystemRef would set current callee as caller before forwarding the call
ink_env::test::set_callee::<PinkEnvironment>(OWNER.into());
ink::env::test::set_callee::<PinkEnvironment>(OWNER.into());
assert_eq!(system.grant_admin([42u8; 32].into()), Ok(()));

ink_env::test::set_callee::<PinkEnvironment>([42u8; 32].into());
ink::env::test::set_callee::<PinkEnvironment>([42u8; 32].into());
assert_eq!(system.grant_admin([43u8; 32].into()), Err(Error::BadOrigin));
assert_eq!(system.set_driver("Test".into(), Default::default()), Ok(()));

ink_env::test::set_callee::<PinkEnvironment>([43u8; 32].into());
ink::env::test::set_callee::<PinkEnvironment>([43u8; 32].into());
assert_eq!(
system.set_driver("Test".into(), Default::default()),
Err(Error::BadOrigin)
Expand All @@ -204,7 +205,7 @@ mod system {
fn set_driver_permissions() {
let driver_name = "Hello";
let mut system = test_system();
ink_env::test::set_callee::<PinkEnvironment>(OWNER.into());
ink::env::test::set_callee::<PinkEnvironment>(OWNER.into());

let driver = system.get_driver(driver_name.into());
assert_eq!(driver, None);
Expand All @@ -216,17 +217,17 @@ mod system {
assert_eq!(driver, Some(driver_id));

// The others can not set driver
ink_env::test::set_callee::<PinkEnvironment>([42u8; 32].into());
ink::env::test::set_callee::<PinkEnvironment>([42u8; 32].into());
assert_eq!(
system.set_driver(driver_name.into(), [2u8; 32].into()),
Err(Error::BadOrigin)
);
assert_eq!(driver, Some(driver_id));

// The others can set driver after granted admin
ink_env::test::set_callee::<PinkEnvironment>(OWNER.into());
ink::env::test::set_callee::<PinkEnvironment>(OWNER.into());
assert_eq!(system.grant_admin([42u8; 32].into()), Ok(()));
ink_env::test::set_callee::<PinkEnvironment>([42u8; 32].into());
ink::env::test::set_callee::<PinkEnvironment>([42u8; 32].into());
assert_eq!(
system.set_driver(driver_name.into(), [2u8; 32].into()),
Ok(())
Expand All @@ -236,7 +237,7 @@ mod system {
#[ink::test]
fn deploy_sidevm_permissions() {
let mut system = test_system();
ink_env::test::set_callee::<PinkEnvironment>(OWNER.into());
ink::env::test::set_callee::<PinkEnvironment>(OWNER.into());

// The owner can not deploy a sidevm
assert_eq!(
Expand Down
14 changes: 3 additions & 11 deletions crates/pink-drivers/tokenomic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ authors = ["[your_name] <[your_email]>"]
edition = "2021"

[dependencies]
ink_primitives = { version = "3.4.0", default-features = false }
ink_metadata = { version = "3.4.0", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.4.0", default-features = false }
ink_storage = { version = "3.4.0", default-features = false }
ink_lang = { version = "3.4.0", default-features = false }

ink = { version = "4.0.0-rc", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

pink-extension = { version = "0.2", path = "../../../crates/pink/pink-extension", default-features = false }
pink-extension = { version = "0.4", default-features = false, path = "../../../crates/pink/pink-extension" }

[build-dependencies]
sp-core = "7"
Expand All @@ -32,10 +27,7 @@ crate-type = [
[features]
default = ["std"]
std = [
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_primitives/std",
"ink/std",
"scale/std",
"scale-info/std",
"pink-extension/std"
Expand Down
4 changes: 2 additions & 2 deletions crates/pink-libs/s3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
keywords = ["fat-contract", "pink", "ink", "S3"]

[dependencies]
pink-extension = { version = "0.2.0", path = "../../pink/pink-extension", default-features = false }
pink-extension = { version = "0.4.0", default-features = false, path = "../../pink/pink-extension" }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }
sha2 = { version = "0.10.2", default-features = false }
Expand All @@ -17,7 +17,7 @@ base16 = { version = "0.2.1", default-features = false, features = ["alloc"] }
chrono = { version = "0.4", default-features = false, features = ["alloc"] }

[dev-dependencies]
pink-extension-runtime = { version = "0.2.0", path = "../../pink/pink-extension-runtime" }
pink-extension-runtime = { path = "../../pink/pink-extension-runtime" }

[features]
default = ["std"]
Expand Down
6 changes: 3 additions & 3 deletions crates/pink-libs/subrpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pink-subrpc"
version = "0.2.1"
version = "0.3.0"
authors = ["Phala Network"]
edition = "2021"
license = "Apache-2.0"
Expand All @@ -21,7 +21,7 @@ hex-literal = "0.3"
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
impl-serde = { version = "0.4.0", optional = true }

pink-extension = { version = "0.2", path = "../../pink/pink-extension", default-features = false, features = ["ink-as-dependency"] }
pink-extension = { version = "0.4", path = "../../pink/pink-extension", default-features = false, features = ["ink-as-dependency"] }
sp-core-hashing = { version = "5", default-features = false }

serde = { version = "1.0.140", default-features = false, features = ["derive", "alloc"]}
Expand All @@ -31,7 +31,7 @@ ss58-registry = { version = "1.33.0", default-features = false }
base58 = { version = "0.2.0", default-features = false }

[dev-dependencies]
pink-extension-runtime = { version = "0.2", path = "../../pink/pink-extension-runtime" }
pink-extension-runtime = { version = "0.4", path = "../../pink/pink-extension-runtime" }
xcm = { git = "/~https://github.com/paritytech/polkadot", branch = "release-v0.9.37", default-features = false, features = ["std"] }

[features]
Expand Down
Loading

0 comments on commit afdd767

Please sign in to comment.