Skip to content

Commit

Permalink
feat(pallet-gear-builtin): Add proxy builtin (#4259)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Novikov <[email protected]>
  • Loading branch information
techraed and breathx authored Oct 24, 2024
1 parent 291bf27 commit 73929e3
Show file tree
Hide file tree
Showing 17 changed files with 682 additions and 48 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ members = [
"examples/program-factory",
"examples/program-generator",
"examples/proxy",
"examples/proxy-broker",
"examples/proxy-relay",
"examples/proxy-reservation-with-gas",
"examples/read-big-state",
Expand Down Expand Up @@ -216,6 +217,7 @@ galloc = { path = "galloc" }
gbuiltin-bls381 = { path = "gbuiltins/bls381", default-features = false }
gbuiltin-eth-bridge = { path = "gbuiltins/eth-bridge", default-features = false }
gbuiltin-staking = { path = "gbuiltins/staking" }
gbuiltin-proxy = { path = "gbuiltins/proxy" }
gcore = { path = "gcore" }
gcli = { path = "gcli" }
gclient = { path = "gclient" }
Expand Down Expand Up @@ -484,6 +486,7 @@ demo-rwlock = { path = "examples/rwlock" }
demo-send-from-reservation = { path = "examples/send-from-reservation" }
demo-signal-entry = { path = "examples/signal-entry", default-features = false }
demo-staking-broker = { path = "examples/staking-broker" }
demo-proxy-broker = { path = "examples/proxy-broker" }
demo-state-rollback = { path = "examples/state-rollback" }
demo-sync-duplicate = { path = "examples/sync-duplicate" }
demo-vec = { path = "examples/vec" }
Expand Down
21 changes: 21 additions & 0 deletions examples/proxy-broker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "demo-proxy-broker"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
gbuiltin-proxy.workspace = true
gstd.workspace = true
parity-scale-codec.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true

[features]
debug = ["gstd/debug"]
std = []
default = ["std"]
21 changes: 21 additions & 0 deletions examples/proxy-broker/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is part of Gear.

// Copyright (C) 2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

fn main() {
gear_wasm_builder::build();
}
33 changes: 33 additions & 0 deletions examples/proxy-broker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This file is part of Gear.
//
// Copyright (C) 2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
mod code {
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
}

#[cfg(feature = "std")]
pub use code::WASM_BINARY_OPT as WASM_BINARY;

#[cfg(not(feature = "std"))]
pub const WASM_BINARY: &[u8] = &[];

#[cfg(not(feature = "std"))]
pub mod wasm;
72 changes: 72 additions & 0 deletions examples/proxy-broker/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is part of Gear.
//
// Copyright (C) 2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Basic implementation of the proxy-broker for demo purpose only.

use gbuiltin_proxy::Request;
use gstd::{actor_id, debug, errors::Error, msg, ActorId};

// Proxy builtin actor program id (hardcoded for all runtimes);
//
// Calculated as hash((b"built/in", 3u64).encode())
const BUILTIN_ADDRESS: ActorId =
actor_id!("0xf2816ced0b15749595392d3a18b5a2363d6fefe5b3b6153739f218151b7acdbf");

#[gstd::async_main]
async fn main() {
let request: Request = msg::load().expect("handle: invalid payload received");
match request {
add_proxy @ Request::AddProxy { .. } => {
debug!(
"handle: Sending `add_proxy` request with data {:?}",
add_proxy
);

send_request(add_proxy).await;
}
remove_proxy @ Request::RemoveProxy { .. } => {
debug!(
"handle: Sending `remove_proxy` request with data {:?}",
remove_proxy
);

send_request(remove_proxy).await;
}
}
}

async fn send_request(req: Request) {
let res = msg::send_for_reply(BUILTIN_ADDRESS, req, 0, 0)
.expect("handle::send_request: failed sending message for reply")
.await;
match res {
Ok(_) => {
debug!("handle::send_request: Success reply from builtin actor received");
msg::reply_bytes(b"Success", 0).expect("Failed to send reply");
}
Err(e) => {
debug!("handle::send_request: Error reply from builtin actor received: {e:?}");
match e {
Error::ErrorReply(payload, _) => {
panic!("{}", payload);
}
_ => panic!("Error in upstream program"),
}
}
}
}
14 changes: 14 additions & 0 deletions gbuiltins/proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "gbuiltin-proxy"
description = "Types and traits to interact with proxy builtin actor."
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
gprimitives.workspace = true
derive_more.workspace = true
scale-info = { workspace = true, features = ["derive"] }
64 changes: 64 additions & 0 deletions gbuiltins/proxy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This file is part of Gear.

// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Types used to communicate with proxy built-in.

#![no_std]

use gprimitives::ActorId;
use scale_info::scale::{self, Decode, Encode};

/// Request that can be handled by the proxy builtin.
///
/// Currently all proxies aren't required to send announcement,
/// i.e. no delays for the delegate actions.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Encode, Decode)]
#[codec(crate = scale)]
pub enum Request {
/// Add proxy request.
///
/// Requests to add `delegate` as a delegate for the actions
/// defined by `proxy_type` to be done on behalf of the request
/// sender.
AddProxy {
delegate: ActorId,
proxy_type: ProxyType,
},
/// Remove proxy request.
///
/// Request sender asks to remove `delegate` with set of allowed actions
/// defined in `proxy_type` from his list of proxies.
RemoveProxy {
delegate: ActorId,
proxy_type: ProxyType,
},
}

/// Proxy type.
///
/// The mirror enum for the one defined in vara-runtime crate.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Encode, Decode)]
#[codec(crate = scale)]
pub enum ProxyType {
Any,
NonTransfer,
Governance,
Staking,
IdentityJudgement,
CancelProxy,
}
4 changes: 4 additions & 0 deletions pallets/gear-builtin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ common.workspace = true
core-processor.workspace = true
gbuiltin-bls381.workspace = true
gbuiltin-staking.workspace = true
gbuiltin-proxy.workspace = true
gear-core.workspace = true
gear-core-errors.workspace = true
gear-runtime-interface.workspace = true
Expand All @@ -41,10 +42,12 @@ sp-std.workspace = true
sp-runtime = { workspace = true, features = ["serde"] }
pallet-gear.workspace = true
pallet-staking.workspace = true
pallet-proxy.workspace = true

[dev-dependencies]
demo-waiting-proxy.workspace = true
demo-staking-broker.workspace = true
demo-proxy-broker.workspace = true
gprimitives.workspace = true
sp-core = { workspace = true, features = ["std"] }
sp-externalities = { workspace = true, features = ["std"] }
Expand Down Expand Up @@ -81,6 +84,7 @@ std = [
"gear-runtime-interface/std",
"pallet-gear/std",
"pallet-staking/std",
"pallet-proxy/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-crypto-ec-utils/std",
Expand Down
Loading

0 comments on commit 73929e3

Please sign in to comment.