-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NNS1-829: Appropriately lock neuron on configure_neuron
- Loading branch information
Showing
10 changed files
with
360 additions
and
27 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
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
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,56 @@ | ||
use ic_base_types::PrincipalId; | ||
use ic_nns_common::pb::v1::NeuronId; | ||
use ic_nns_governance::governance::Governance; | ||
use ic_nns_governance::pb::v1::{ | ||
manage_neuron, manage_neuron::NeuronIdOrSubaccount, ManageNeuron, ManageNeuronResponse, | ||
}; | ||
|
||
pub async fn increase_dissolve_delay_raw( | ||
gov: &mut Governance, | ||
principal_id: &PrincipalId, | ||
neuron_id: NeuronId, | ||
delay_increase: u32, | ||
) -> ManageNeuronResponse { | ||
gov.manage_neuron( | ||
principal_id, | ||
&ManageNeuron { | ||
id: None, | ||
neuron_id_or_subaccount: Some(NeuronIdOrSubaccount::NeuronId(neuron_id)), | ||
command: Some(manage_neuron::Command::Configure( | ||
manage_neuron::Configure { | ||
operation: Some(manage_neuron::configure::Operation::IncreaseDissolveDelay( | ||
manage_neuron::IncreaseDissolveDelay { | ||
additional_dissolve_delay_seconds: delay_increase, | ||
}, | ||
)), | ||
}, | ||
)), | ||
}, | ||
) | ||
.await | ||
} | ||
|
||
pub async fn set_dissolve_delay_raw( | ||
gov: &mut Governance, | ||
principal_id: &PrincipalId, | ||
neuron_id: NeuronId, | ||
timestamp_seconds: u64, | ||
) -> ManageNeuronResponse { | ||
gov.manage_neuron( | ||
principal_id, | ||
&ManageNeuron { | ||
id: None, | ||
neuron_id_or_subaccount: Some(NeuronIdOrSubaccount::NeuronId(neuron_id)), | ||
command: Some(manage_neuron::Command::Configure( | ||
manage_neuron::Configure { | ||
operation: Some(manage_neuron::configure::Operation::SetDissolveTimestamp( | ||
manage_neuron::SetDissolveTimestamp { | ||
dissolve_timestamp_seconds: timestamp_seconds, | ||
}, | ||
)), | ||
}, | ||
)), | ||
}, | ||
) | ||
.await | ||
} |
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
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,101 @@ | ||
//! Utilities to help with testing interleavings of calls to the governance | ||
//! canister | ||
use async_trait::async_trait; | ||
use futures::channel::mpsc::UnboundedSender as USender; | ||
use futures::channel::oneshot::{self, Sender as OSender}; | ||
use std::sync::atomic; | ||
use std::sync::atomic::Ordering as AOrdering; | ||
|
||
use ic_nns_governance::{ | ||
governance::Ledger, | ||
pb::v1::{governance_error::ErrorType, GovernanceError}, | ||
}; | ||
use ledger_canister::Subaccount; | ||
use ledger_canister::{AccountIdentifier, Tokens}; | ||
|
||
/// Reifies the methods of the Ledger trait, such that they can be sent over a | ||
/// channel | ||
#[derive(Debug)] | ||
pub enum LedgerMessage { | ||
Transfer { | ||
amount_e8s: u64, | ||
fee_e8s: u64, | ||
from_subaccount: Option<Subaccount>, | ||
to: AccountIdentifier, | ||
memo: u64, | ||
}, | ||
TotalSupply, | ||
BalanceQuery(AccountIdentifier), | ||
} | ||
|
||
pub type LedgerControlMessage = (LedgerMessage, OSender<Result<(), GovernanceError>>); | ||
|
||
pub type LedgerObserver = USender<LedgerControlMessage>; | ||
|
||
/// A mock ledger to test interleavings of governance method calls. | ||
pub struct InterleavingTestLedger { | ||
underlying: Box<dyn Ledger>, | ||
observer: LedgerObserver, | ||
} | ||
|
||
impl InterleavingTestLedger { | ||
/// The ledger intercepts calls to an underlying ledger implementation, | ||
/// sends the reified calls over the provided observer channel, and | ||
/// blocks. The receiver side of the channel can then inspect the | ||
/// results, and decide at what point to go ahead with the call to the | ||
/// underlying ledger, or, alternatively, return an error. This is done | ||
/// through a one-shot channel, the sender side of which is sent to the | ||
/// observer. | ||
pub fn new(underlying: Box<dyn Ledger>, observer: LedgerObserver) -> Self { | ||
InterleavingTestLedger { | ||
underlying, | ||
observer, | ||
} | ||
} | ||
|
||
// Notifies the observer that a ledger method has been called, and blocks until | ||
// it receives a message to continue. | ||
async fn notify(&self, msg: LedgerMessage) -> Result<(), GovernanceError> { | ||
let (tx, rx) = oneshot::channel::<Result<(), GovernanceError>>(); | ||
self.observer.unbounded_send((msg, tx)).unwrap(); | ||
rx.await | ||
.map_err(|_e| GovernanceError::new(ErrorType::Unavailable))? | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Ledger for InterleavingTestLedger { | ||
async fn transfer_funds( | ||
&self, | ||
amount_e8s: u64, | ||
fee_e8s: u64, | ||
from_subaccount: Option<Subaccount>, | ||
to: AccountIdentifier, | ||
memo: u64, | ||
) -> Result<u64, GovernanceError> { | ||
let msg = LedgerMessage::Transfer { | ||
amount_e8s, | ||
fee_e8s, | ||
from_subaccount, | ||
to, | ||
memo, | ||
}; | ||
atomic::fence(AOrdering::SeqCst); | ||
self.notify(msg).await?; | ||
self.underlying | ||
.transfer_funds(amount_e8s, fee_e8s, from_subaccount, to, memo) | ||
.await | ||
} | ||
|
||
async fn total_supply(&self) -> Result<Tokens, GovernanceError> { | ||
atomic::fence(AOrdering::SeqCst); | ||
self.notify(LedgerMessage::TotalSupply).await?; | ||
self.underlying.total_supply().await | ||
} | ||
|
||
async fn account_balance(&self, account: AccountIdentifier) -> Result<Tokens, GovernanceError> { | ||
atomic::fence(AOrdering::SeqCst); | ||
self.notify(LedgerMessage::BalanceQuery(account)).await?; | ||
self.underlying.account_balance(account).await | ||
} | ||
} |
Oops, something went wrong.