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

Feature Gate Program #4987

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ members = [
"examples/rust/sysvar",
"examples/rust/transfer-lamports",
"examples/rust/transfer-tokens",
"feature-gate/cli",
"feature-gate/program",
"feature-proposal/program",
"feature-proposal/cli",
"governance/addin-mock/program",
Expand Down
10 changes: 10 additions & 0 deletions feature-gate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Feature Gate Program

This program serves to manage new features on Solana.

It serves two main purposes: activating new features and revoking features that
are pending activation.

More information & documentation will follow as this program matures, but you
can follow the discussions
[here](https://github.com/solana-labs/solana/issues/32780)!
24 changes: 24 additions & 0 deletions feature-gate/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "spl-feature-gate-cli"
version = "1.2.0"
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved
description = "SPL Feature Gate Command-line Utility"
authors = ["Solana Labs Maintainers <[email protected]>"]
repository = "https://github.com/solana-labs/solana-program-library"
license = "Apache-2.0"
edition = "2021"

[dependencies]
clap = "2.33.3"
solana-clap-utils = "1.16.3"
solana-cli-config = "1.16.3"
solana-client = "1.16.3"
solana-logger = "1.16.3"
solana-sdk = "1.16.3"
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved
spl-feature-gate = { version = "0.0.1", path = "../program", features = ["no-entrypoint"] }

[[bin]]
name = "spl-feature-gate"
path = "src/main.rs"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
268 changes: 268 additions & 0 deletions feature-gate/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
#![allow(clippy::integer_arithmetic)]
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved

use {
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg, SubCommand},
solana_clap_utils::{
input_parsers::{keypair_of, pubkey_of},
input_validators::{is_keypair, is_url, is_valid_pubkey},
},
solana_client::rpc_client::RpcClient,
solana_sdk::{
commitment_config::CommitmentConfig,
feature::Feature,
pubkey::Pubkey,
rent::Rent,
signature::{read_keypair_file, Keypair, Signer},
system_instruction,
transaction::Transaction,
},
spl_feature_gate::instruction::{activate, revoke},
};

fn keypair_clone(kp: &Keypair) -> Keypair {
Keypair::from_bytes(&kp.to_bytes()).expect("failed to copy keypair")
}
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved

#[allow(dead_code)]
struct Config {
keypair: Keypair,
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved
json_rpc_url: String,
verbose: bool,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_matches = App::new(crate_name!())
.about(crate_description!())
.version(crate_version!())
.setting(AppSettings::SubcommandRequiredElseHelp)
.arg({
let arg = Arg::with_name("config_file")
.short("C")
.long("config")
.value_name("PATH")
.takes_value(true)
.global(true)
.help("Configuration file to use");
if let Some(ref config_file) = *solana_cli_config::CONFIG_FILE {
arg.default_value(config_file)
} else {
arg
}
})
.arg(
Arg::with_name("keypair")
.long("keypair")
.value_name("KEYPAIR")
.validator(is_keypair)
.takes_value(true)
.global(true)
.help("Filepath or URL to a keypair [default: client keypair]"),
)
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved
.arg(
Arg::with_name("verbose")
.long("verbose")
.short("v")
.takes_value(false)
.global(true)
.help("Show additional information"),
)
.arg(
Arg::with_name("json_rpc_url")
.long("url")
.value_name("URL")
.takes_value(true)
.global(true)
.validator(is_url)
.help("JSON RPC URL for the cluster [default: value from configuration file]"),
)
.subcommand(
SubCommand::with_name("activate")
.about("Activate a feature")
.arg(
Arg::with_name("feature_keypair")
.value_name("FEATURE_KEYPAIR")
.validator(is_keypair)
.index(1)
.required(true)
.help("Path to keypair of the feature"),
)
.arg(
Arg::with_name("authority_keypair")
.value_name("AUTHORITY_KEYPAIR")
.validator(is_keypair)
.required(true)
.help("Path to keypair of the authority"),
)
.arg(
Arg::with_name("payer_keypair")
.value_name("PAYER_KEYPAIR")
.validator(is_keypair)
.help(
"Path to keypair of the payer to fund the feature account (defaults \
to authority)",
),
),
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved
)
.subcommand(
SubCommand::with_name("revoke")
.about("Revoke a pending feature activation")
.arg(
Arg::with_name("feature_id")
.value_name("FEATURE_ID")
.validator(is_valid_pubkey)
.index(1)
.required(true)
.help("The address of the feature (feature ID)"),
)
.arg(
Arg::with_name("destination")
.value_name("DESTINATION")
.validator(is_valid_pubkey)
.index(2)
.required(true)
.help("The address of the destination for the refunded lamports"),
)
.arg(
Arg::with_name("authority_keypair")
.value_name("AUTHORITY_KEYPAIR")
.validator(is_keypair)
.required(true)
.help("Path to keypair of the authority"),
),
)
.get_matches();

let (sub_command, sub_matches) = app_matches.subcommand();
let matches = sub_matches.unwrap();

let config = {
let cli_config = if let Some(config_file) = matches.value_of("config_file") {
solana_cli_config::Config::load(config_file).unwrap_or_default()
} else {
solana_cli_config::Config::default()
};

Config {
json_rpc_url: matches
.value_of("json_rpc_url")
.unwrap_or(&cli_config.json_rpc_url)
.to_string(),
keypair: read_keypair_file(
matches
.value_of("keypair")
.unwrap_or(&cli_config.keypair_path),
)?,
verbose: matches.is_present("verbose"),
}
};
solana_logger::setup_with_default("solana=info");
let rpc_client =
RpcClient::new_with_commitment(config.json_rpc_url.clone(), CommitmentConfig::confirmed());

match (sub_command, sub_matches) {
("activate", Some(arg_matches)) => {
let feature_keypair = keypair_of(arg_matches, "feature_keypair").unwrap();
let authority_keypair = keypair_of(arg_matches, "authority_keypair").unwrap();
let payer_keypair = keypair_of(arg_matches, "payer_keypair")
.unwrap_or(keypair_clone(&authority_keypair));

process_activate(
&rpc_client,
&config,
&feature_keypair,
&payer_keypair,
&authority_keypair,
)
}
("revoke", Some(arg_matches)) => {
let feature_id = pubkey_of(arg_matches, "feature_id").unwrap();
let destination = pubkey_of(arg_matches, "destination").unwrap();
let authority_keypair = keypair_of(arg_matches, "authority_keypair").unwrap();

process_revoke(
&rpc_client,
&config,
&feature_id,
&destination,
&authority_keypair,
)
}
_ => unreachable!(),
}
}

fn process_activate(
rpc_client: &RpcClient,
config: &Config,
feature_keypair: &Keypair,
payer_keypair: &Keypair,
authority_keypair: &Keypair,
) -> Result<(), Box<dyn std::error::Error>> {
println!();
println!("Activating feature...");
println!("Feature ID: {}", feature_keypair.pubkey());
println!("Payer: {}", payer_keypair.pubkey());
println!("Authority: {}", authority_keypair.pubkey());
println!();
println!("JSON RPC URL: {}", config.json_rpc_url);
println!();

let rent_lamports = Rent::default().minimum_balance(Feature::size_of());

let transaction = Transaction::new_signed_with_payer(
&[
system_instruction::transfer(
&payer_keypair.pubkey(),
&feature_keypair.pubkey(),
rent_lamports,
),
activate(
&spl_feature_gate::id(),
&feature_keypair.pubkey(),
&authority_keypair.pubkey(),
),
],
Some(&payer_keypair.pubkey()),
&[payer_keypair, feature_keypair, authority_keypair],
rpc_client.get_latest_blockhash()?,
);
rpc_client.send_and_confirm_transaction_with_spinner(&transaction)?;

println!();
println!("Feature is marked for activation!");
Ok(())
}

fn process_revoke(
rpc_client: &RpcClient,
config: &Config,
feature_id: &Pubkey,
destination: &Pubkey,
authority_keypair: &Keypair,
) -> Result<(), Box<dyn std::error::Error>> {
println!();
println!("Revoking feature...");
println!("Feature ID: {}", feature_id);
println!("Destination: {}", destination);
println!("Authority: {}", authority_keypair.pubkey());
println!();
println!("JSON RPC URL: {}", config.json_rpc_url);
println!();

let transaction = Transaction::new_signed_with_payer(
&[revoke(
&spl_feature_gate::id(),
feature_id,
destination,
&authority_keypair.pubkey(),
)],
Some(&authority_keypair.pubkey()),
&[authority_keypair],
rpc_client.get_latest_blockhash()?,
);
rpc_client.send_and_confirm_transaction_with_spinner(&transaction)?;

println!();
println!("Feature successfully revoked!");
Ok(())
}
27 changes: 27 additions & 0 deletions feature-gate/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "spl-feature-gate"
version = "0.0.1"
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved
description = "Solana Program Library Feature Gate Program"
authors = ["Solana Labs Maintainers <[email protected]>"]
repository = "https://github.com/solana-labs/solana-program-library"
license = "Apache-2.0"
edition = "2021"

[features]
no-entrypoint = []
test-sbf = []

[dependencies]
borsh = "0.10.3"
solana-program = "1.16.3"
spl-program-error = { version = "0.2.0", path = "../../libraries/program-error" }

[dev-dependencies]
solana-program-test = "1.16.3"
solana-sdk = "1.16.3"

[lib]
crate-type = ["cdylib", "lib"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
17 changes: 17 additions & 0 deletions feature-gate/program/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Program entrypoint

use {
crate::processor,
solana_program::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, pubkey::Pubkey,
},
};

entrypoint!(process_instruction);
fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
input: &[u8],
) -> ProgramResult {
processor::process(program_id, accounts, input)
}
Loading