-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pallet-gear-builtin): Add proxy builtin (#4259)
Co-authored-by: Dmitry Novikov <[email protected]>
- Loading branch information
Showing
17 changed files
with
682 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.