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

Update protocol default configs #139

Merged
merged 7 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions chain-signatures/contract/src/config/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ impl Default for ProtocolConfig {
Self {
message_timeout: min_to_ms(5),
garbage_timeout: hours_to_ms(2),
max_concurrent_introduction: 2,
max_concurrent_generation: 2 * MAX_EXPECTED_PARTICIPANTS,
max_concurrent_introduction: 16,
max_concurrent_generation: 16 * MAX_EXPECTED_PARTICIPANTS,
triple: TripleConfig::default(),
presignature: PresignatureConfig::default(),
signature: Default::default(),
Expand Down
39 changes: 26 additions & 13 deletions chain-signatures/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const RETURN_SIGNATURE_ON_FINISH_CALL_GAS: Gas = Gas::from_tgas(10);
// Prepaid gas for a `update_config` call
const UPDATE_CONFIG_GAS: Gas = Gas::from_tgas(5);

// Maximum number of concurrent requests
const MAX_CONCURRENT_REQUESTS: u32 = 128;

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, Debug)]
pub enum VersionedMpcContract {
Expand Down Expand Up @@ -160,7 +163,7 @@ impl VersionedMpcContract {

match self {
Self::V0(mpc_contract) => {
if mpc_contract.request_counter > 16 {
if mpc_contract.request_counter > MAX_CONCURRENT_REQUESTS {
return Err(SignError::RequestLimitExceeded.into());
}
}
Expand Down Expand Up @@ -224,19 +227,16 @@ impl VersionedMpcContract {

/// This experimental function calculates the fee for a signature request.
/// The fee is volatile and depends on the number of pending requests.
/// If used on a client side, it can give outdate results.
/// If used on a client side, it can give outdated results.
pub fn experimental_signature_deposit(&self) -> U128 {
const CHEAP_REQUESTS: u32 = 3;
let pending_requests = match self {
Self::V0(mpc_contract) => mpc_contract.request_counter,
};
match pending_requests {
0..=CHEAP_REQUESTS => U128::from(1),
_ => {
let expensive_requests = (pending_requests - CHEAP_REQUESTS) as u128;
let price = expensive_requests * NearToken::from_millinear(50).as_yoctonear();
U128::from(price)
}
let load = self.system_load();

match load {
0..=25 => U128(1),
26..=50 => U128(NearToken::from_millinear(50).as_yoctonear()),
51..=75 => U128(NearToken::from_millinear(500).as_yoctonear()),
76..=100 => U128(NearToken::from_near(1).as_yoctonear()),
_ => U128(NearToken::from_near(1).as_yoctonear()),
}
}
}
Expand Down Expand Up @@ -677,6 +677,19 @@ impl VersionedMpcContract {
}
}

pub fn system_load(&self) -> u32 {
let pending_requests = self.pending_requests();
((pending_requests as f64 / MAX_CONCURRENT_REQUESTS as f64) * 100.0)
.min(100.0)
.round() as u32
}

pub fn pending_requests(&self) -> u32 {
match self {
Self::V0(mpc_contract) => mpc_contract.request_counter,
}
}

// contract version
pub fn version(&self) -> String {
env!("CARGO_PKG_VERSION").to_string()
Expand Down
4 changes: 2 additions & 2 deletions chain-signatures/contract/tests/user_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn test_experimental_signature_deposit() -> anyhow::Result<()> {
let alice = worker.dev_create_account().await?;
let path = "test";

for i in 1..5 {
for i in 1..8 {
let msg = format!("hello world {}", i);
println!("submitting: {msg}");
let (payload_hash, _, _) = create_response(alice.id(), &msg, path, &sk).await;
Expand Down Expand Up @@ -97,6 +97,6 @@ async fn test_experimental_signature_deposit() -> anyhow::Result<()> {
.json::<String>()
.unwrap()
.parse()?;
assert_eq!(deposit, NearToken::from_millinear(50).as_yoctonear());
assert_eq!(deposit, NearToken::from_yoctonear(1).as_yoctonear());
Ok(())
}
1 change: 1 addition & 0 deletions chain-signatures/node/src/protocol/presignature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ impl PresignatureManager {
// We will always try to generate a new triple if we have less than the minimum
self.len_mine().await < cfg.presignature.min_presignatures as usize
&& self.introduced.len() < cfg.max_concurrent_introduction as usize
&& self.generators.len() < cfg.max_concurrent_generation as usize
}
};

Expand Down
2 changes: 2 additions & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ impl Default for NodeConfig {
nodes: 3,
threshold: 2,
protocol: ProtocolConfig {
max_concurrent_generation: 16,
max_concurrent_introduction: 2,
triple: TripleConfig {
min_triples: 8,
max_triples: 80,
Expand Down
Loading