Skip to content

Commit

Permalink
feat: add methods for the silo mode (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss authored Nov 7, 2024
1 parent 8fd0b97 commit de299be
Show file tree
Hide file tree
Showing 11 changed files with 691 additions and 431 deletions.
945 changes: 575 additions & 370 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "aurora-cli-rs"
version = "0.1.0"
authors = ["Aurora Labs <[email protected]>"]
edition = "2021"
rust-version = "1.75.0"
rust-version = "1.80.0"
homepage = "https://github.com/aurora-is-near/aurora-cli-rs"
repository = "https://github.com/aurora-is-near/aurora-cli-rs"
description = "Aurora CLI is a command line interface to bootstrap Aurora engine"
Expand All @@ -19,10 +19,10 @@ simple = ["toml"]
advanced = ["near-chain-configs"]

[dependencies]
aurora-engine-precompiles = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.6.4", features = ["std"] }
aurora-engine-sdk = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.6.4", features = ["std"] }
aurora-engine-transactions = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.6.4", features = ["std"] }
aurora-engine-types = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.6.4", features = ["std", "impl-serde"] }
aurora-engine-precompiles = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.7.0", features = ["std"] }
aurora-engine-sdk = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.7.0", features = ["std"] }
aurora-engine-transactions = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.7.0", features = ["std"] }
aurora-engine-types = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.7.0", features = ["std", "impl-serde"] }

anyhow = "1"
borsh = "1"
Expand All @@ -32,20 +32,20 @@ ethabi = "18"
hex = "0.4"
lazy_static = "1"
libsecp256k1 = { version = "0.7", features = ["std"] }
near-chain-configs = { version = "0.25", optional = true }
near-crypto = "0.25"
near-jsonrpc-client = "0.12"
near-jsonrpc-primitives = "0.25"
near-primitives = "0.25"
near-chain-configs = { version = "0.26", optional = true }
near-crypto = "0.26"
near-jsonrpc-client = "0.13"
near-jsonrpc-primitives = "0.26"
near-primitives = "0.26"
reqwest = { version = "0.12", features = ["json"] }
rand = "0.8"
rlp = "0.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = { version = "0.8", optional = true }
shadow-rs = "0.33"
shadow-rs = "0.35"
thiserror = "1"
tokio = { version = "1", features = ["full"] }

[build-dependencies]
shadow-rs = "0.33"
shadow-rs = "0.35"
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.80.0"
channel = "1.81.0"
components = [ "clippy", "rustfmt" ]
2 changes: 1 addition & 1 deletion scripts/different-outputs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export NEARCORE_HOME="/tmp/localnet"

AURORA_PREV_VERSION="3.6.0"
AURORA_PREV_VERSION="3.6.4"
AURORA_LAST_VERSION=$(curl -s https://api.github.com/repos/aurora-is-near/aurora-engine/releases/latest | jq -r .tag_name)
EVM_CODE=$(cat docs/res/HelloWorld.hex)
ABI_PATH="docs/res/HelloWorld.abi"
Expand Down
8 changes: 4 additions & 4 deletions scripts/simple-silo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ aurora-cli --engine $ENGINE_ACCOUNT deploy --code $EVM_CODE --abi-path $ABI_PATH
--aurora-secret-key $AURORA_SECRET_KEY || error_exit
wait_for_block
result=$(aurora-cli --engine $ENGINE_ACCOUNT view-call -a 0xa3078bf607d2e859dca0b1a13878ec2e607f30de -f value \
--abi-path $ABI_PATH || error_exit)
--abi-path $ABI_PATH --from 0x1B16948F011686AE64BB2Ba0477aeFA2Ea97084D || error_exit)
assert_eq "$result" "5"
wait_for_block

Expand All @@ -163,7 +163,7 @@ sleep 2

# Check result
result=$(aurora-cli --engine $ENGINE_ACCOUNT view-call -a 0xa3078bf607d2e859dca0b1a13878ec2e607f30de -f value \
--abi-path $ABI_PATH || error_exit)
--abi-path $ABI_PATH --from 0x1B16948F011686AE64BB2Ba0477aeFA2Ea97084D || error_exit)
assert_eq "$result" "6"
wait_for_block

Expand All @@ -175,7 +175,7 @@ wait_for_block

# Check result
result=$(aurora-cli --engine $ENGINE_ACCOUNT view-call -a 0xa3078bf607d2e859dca0b1a13878ec2e607f30de -f value \
--abi-path $ABI_PATH || error_exit)
--abi-path $ABI_PATH --from 0x1B16948F011686AE64BB2Ba0477aeFA2Ea97084D || error_exit)
assert_eq "$result" "5"
wait_for_block

Expand All @@ -201,7 +201,7 @@ wait_for_block

# Check result
result=$(aurora-cli --engine $ENGINE_ACCOUNT view-call -a 0xa3078bf607d2e859dca0b1a13878ec2e607f30de -f value \
--abi-path $ABI_PATH || error_exit)
--abi-path $ABI_PATH --from 0x1B16948F011686AE64BB2Ba0477aeFA2Ea97084D || error_exit)
assert_eq "$result" "4"
wait_for_block

Expand Down
13 changes: 6 additions & 7 deletions scripts/simple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export NEARCORE_HOME="/tmp/localnet"

AURORA_PREV_VERSION="3.6.0"
AURORA_PREV_VERSION="3.6.4"
AURORA_LAST_VERSION=$(curl -s https://api.github.com/repos/aurora-is-near/aurora-engine/releases/latest | jq -r .tag_name)
EVM_CODE=$(cat docs/res/HelloWorld.hex)
ABI_PATH="docs/res/HelloWorld.abi"
Expand Down Expand Up @@ -95,7 +95,6 @@ sleep 4
aurora-cli --engine $ENGINE_ACCOUNT init \
--chain-id 1313161556 \
--owner-id $ENGINE_ACCOUNT \
--bridge-prover-id "prover" \
--upgrade-delay-blocks 1 \
--custodian-address 0x1B16948F011686AE64BB2Ba0477aeFA2Ea97084D \
--ft-metadata-path docs/res/ft_metadata.json || error_exit
Expand Down Expand Up @@ -141,7 +140,7 @@ export NEAR_KEY_PATH=$RELAYER_KEY_PATH
aurora-cli --engine $ENGINE_ACCOUNT deploy --code "$EVM_CODE" --aurora-secret-key $AURORA_SECRET_KEY || error_exit
wait_for_block
result=$(aurora-cli --engine $ENGINE_ACCOUNT view-call -a 0xa3078bf607d2e859dca0b1a13878ec2e607f30de -f greet \
--abi-path $ABI_PATH || error_exit)
--abi-path $ABI_PATH --from 0x1B16948F011686AE64BB2Ba0477aeFA2Ea97084D || error_exit)
assert_eq "$result" "Hello, World!"
wait_for_block

Expand All @@ -158,23 +157,23 @@ aurora-cli --engine $ENGINE_ACCOUNT deploy --code $EVM_CODE --abi-path $ABI_PATH
--aurora-secret-key $AURORA_SECRET_KEY || error_exit
wait_for_block
result=$(aurora-cli --engine $ENGINE_ACCOUNT view-call -a 0x4cf003049d1a9c4918c73e9bf62464d904184555 -f value \
--abi-path $ABI_PATH || error_exit)
--abi-path $ABI_PATH --from 0x1B16948F011686AE64BB2Ba0477aeFA2Ea97084D || error_exit)
assert_eq "$result" "5"
wait_for_block
aurora-cli --engine $ENGINE_ACCOUNT call -a 0x4cf003049d1a9c4918c73e9bf62464d904184555 -f increment \
--abi-path $ABI_PATH \
--aurora-secret-key 611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c || error_exit
wait_for_block
result=$(aurora-cli --engine $ENGINE_ACCOUNT view-call -a 0x4cf003049d1a9c4918c73e9bf62464d904184555 -f value \
--abi-path $ABI_PATH || error_exit)
--abi-path $ABI_PATH --from 0x1B16948F011686AE64BB2Ba0477aeFA2Ea97084D || error_exit)
assert_eq "$result" "6"
wait_for_block
aurora-cli --engine $ENGINE_ACCOUNT call -a 0x4cf003049d1a9c4918c73e9bf62464d904184555 -f decrement \
--abi-path $ABI_PATH \
--aurora-secret-key 611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c || error_exit
wait_for_block
result=$(aurora-cli --engine $ENGINE_ACCOUNT view-call -a 0x4cf003049d1a9c4918c73e9bf62464d904184555 -f value \
--abi-path $ABI_PATH || error_exit)
--abi-path $ABI_PATH --from 0x1B16948F011686AE64BB2Ba0477aeFA2Ea97084D || error_exit)
assert_eq "$result" "5"
wait_for_block

Expand All @@ -187,7 +186,7 @@ aurora-cli --engine $ENGINE_ACCOUNT pause-contract
wait_for_block

# Start Hashchain. The aurora-engine will resume the contract automatically
aurora-cli --engine $ENGINE_ACCOUNT start-hashchain --block-height 0 --block-hashchain 0000000000000000000000000000000000000000000000000000000000000000
aurora-cli --engine $ENGINE_ACCOUNT start-hashchain --block-height 0 --block-hashchain 0000000000000000000000000000000000000000000000000000000000000000
wait_for_block

# Change the key manager of ENGINE_ACCOUNT back to MANAGER_ACCOUNT
Expand Down
20 changes: 19 additions & 1 deletion src/cli/simple/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ pub async fn view_call(
address: String,
function: String,
args: Option<String>,
from: String,
abi_path: String,
) -> anyhow::Result<()> {
let target = hex_to_address(&address)?;
Expand All @@ -273,10 +274,11 @@ pub async fn view_call(
let args: Value = args.map_or(Ok(Value::Null), |args| serde_json::from_str(&args))?;
let tokens = utils::abi::parse_args(&func.inputs, &args)?;
let input = func.encode_input(&tokens)?;
let from = hex_to_address(&from)?;
let result = context
.client
.near()
.view_contract_call(Address::default(), target, Wei::zero(), input)
.view_contract_call(from, target, Wei::zero(), input)
.await?;

if let TransactionStatus::Succeed(bytes) = result {
Expand Down Expand Up @@ -338,6 +340,22 @@ pub async fn call(
TransactionStatus::OutOfFund => "out_of_fund",
TransactionStatus::OutOfOffset => "out_of_offset",
TransactionStatus::CallTooDeep => "call_too_deep",
TransactionStatus::StackUnderflow => "stack_underflow",
TransactionStatus::StackOverflow => "stack_overflow",
TransactionStatus::InvalidJump => "invalid_jump",
TransactionStatus::InvalidRange => "invalid_range",
TransactionStatus::DesignatedInvalid => "designated_invalid",
TransactionStatus::CreateCollision => "create_collision",
TransactionStatus::CreateContractLimit => "create_contract_limit",
TransactionStatus::InvalidCode(_) => "invalid_code",
TransactionStatus::PCUnderflow => "pc_underflow",
TransactionStatus::CreateEmpty => "create_empty",
TransactionStatus::MaxNonce => "max_nonce",
TransactionStatus::UsizeOverflow => "usize_overflow",
TransactionStatus::Other(_) => "other",
TransactionStatus::CreateContractStartingWithEF => {
"create_contract_starting_with_ef"
}
};

(result.gas_used, status)
Expand Down
42 changes: 42 additions & 0 deletions src/cli/simple/command/silo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ pub async fn set_fixed_gas(client: Context, cost: u64) -> anyhow::Result<()> {
.await
}

/// Return Silo parameters.
pub async fn get_silo_params(client: Context) -> anyhow::Result<()> {
get_value::<SiloParams>(client, "get_silo_params", None).await
}

pub async fn set_silo_params(
client: Context,
gas: u64,
Expand All @@ -52,6 +57,19 @@ pub async fn set_silo_params(
.await
}

/// Turn off silo mode.
pub async fn disable_silo_mode(client: Context) -> anyhow::Result<()> {
let args = borsh::to_vec(&None::<SiloParamsArgs>)?;

contract_call!(
"set_silo_params",
"The silo mode has been disabled successfully",
"Error while disabling silo mode"
)
.proceed(client, args)
.await
}

/// Get a status of the whitelist.
pub async fn get_whitelist_status(client: Context, kind: String) -> anyhow::Result<()> {
let args = borsh::to_vec(&WhitelistKindArgs {
Expand Down Expand Up @@ -191,3 +209,27 @@ impl Display for FixedGas {
f.write_str(&value)
}
}

struct SiloParams(Option<SiloParamsArgs>);

impl FromCallResult for SiloParams {
fn from_result(result: CallResult) -> anyhow::Result<Self> {
let args = Option::<SiloParamsArgs>::try_from_slice(&result.result)?;
Ok(Self(args))
}
}

impl Display for SiloParams {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(params) = &self.0 {
let gas = params.fixed_gas;
let fallback_address = params.erc20_fallback_address.encode();

f.write_fmt(format_args!(
"FixedGas: {gas}, fallback address: 0x{fallback_address}"
))
} else {
f.write_str("Silo mode is disabled")
}
}
}
48 changes: 22 additions & 26 deletions src/cli/simple/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use std::str::FromStr;

use aurora_engine_types::account_id::AccountId;
use aurora_engine_types::public_key::{KeyType, PublicKey};
use clap::{Parser, Subcommand};
use lazy_static::lazy_static;
use clap::{Parser, Subcommand, ValueEnum};
use shadow_rs::shadow;
use std::str::FromStr;
use std::sync::LazyLock;

pub mod command;

lazy_static! {
static ref VERSION: String = {
shadow!(build);
format!("{}-{}", build::PKG_VERSION, build::SHORT_COMMIT)
};
}
static VERSION: LazyLock<String> = LazyLock::new(|| {
shadow!(build);
format!("{}-{}", build::PKG_VERSION, build::SHORT_COMMIT)
});

fn get_version() -> &'static str {
VERSION.as_str()
Expand All @@ -25,7 +22,7 @@ fn get_version() -> &'static str {
#[command(version = get_version())]
pub struct Cli {
/// NEAR network ID
#[arg(long, default_value = "localnet")]
#[arg(long, value_enum, default_value_t = Network::Localnet)]
pub network: Network,
/// Aurora EVM account
#[arg(long, value_name = "ACCOUNT_ID", default_value = "aurora")]
Expand Down Expand Up @@ -177,6 +174,9 @@ pub enum Command {
/// Arguments with values in JSON
#[arg(long)]
args: Option<String>,
/// Sender address
#[arg(long)]
from: String,
/// Path to ABI of the contract
#[arg(long)]
abi_path: String,
Expand Down Expand Up @@ -227,6 +227,8 @@ pub enum Command {
/// Fixed gas in `EthGas`.
cost: u64,
},
/// Return Silo params
GetSiloParams,
/// Set SILO params.
SetSiloParams {
/// Fixed gas in `EthGas`.
Expand All @@ -236,6 +238,8 @@ pub enum Command {
#[arg(long, short)]
fallback_address: String,
},
/// Disable SILO mode.
DisableSiloMode,
/// Return a status of the whitelist
GetWhitelistStatus {
/// Kind of the whitelist.
Expand Down Expand Up @@ -371,26 +375,13 @@ pub enum Command {
GetPausedFlags,
}

#[derive(Clone)]
#[derive(Debug, Clone, ValueEnum)]
pub enum Network {
Localnet,
Mainnet,
Testnet,
}

impl FromStr for Network {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"localnet" => Ok(Self::Localnet),
"mainnet" => Ok(Self::Mainnet),
"testnet" => Ok(Self::Testnet),
_ => anyhow::bail!("unknown network: {s}"),
}
}
}

#[derive(Default, Clone)]
pub enum OutputFormat {
#[default]
Expand Down Expand Up @@ -479,8 +470,9 @@ pub async fn run(args: Cli) -> anyhow::Result<()> {
address,
function,
args,
from,
abi_path,
} => command::view_call(context, address, function, args, abi_path).await?,
} => command::view_call(context, address, function, args, from, abi_path).await?,
Command::PausePrecompiles { mask } => command::pause_precompiles(context, mask).await?,
Command::ResumePrecompiles { mask } => command::resume_precompiles(context, mask).await?,
Command::PausedPrecompiles => command::paused_precompiles(context).await?,
Expand Down Expand Up @@ -547,12 +539,16 @@ pub async fn run(args: Cli) -> anyhow::Result<()> {
Command::SetFixedGas { cost } => {
command::silo::set_fixed_gas(context, cost).await?;
}
Command::GetSiloParams => command::silo::get_silo_params(context).await?,
Command::SetSiloParams {
gas,
fallback_address,
} => {
command::silo::set_silo_params(context, gas, fallback_address).await?;
}
Command::DisableSiloMode => {
command::silo::disable_silo_mode(context).await?;
}
Command::GetWhitelistStatus { kind } => {
command::silo::get_whitelist_status(context, kind).await?;
}
Expand Down
Loading

0 comments on commit de299be

Please sign in to comment.