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

Expose reset wallet method #328

Merged
merged 6 commits into from
Mar 15, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]
### Added
- [\#328](https://github.com/Manta-Network/manta-rs/pull/328) Expose reset wallet method.

### Changed

Expand Down
4 changes: 2 additions & 2 deletions manta-accounting/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ where

/// Resets the state of the wallet to the default starting state.
#[inline]
fn reset_state(&mut self) {
pub fn reset_state(&mut self) {
self.checkpoint = Default::default();
self.assets = Default::default();
}
Expand Down Expand Up @@ -242,7 +242,7 @@ where
/// [`restart`](Self::restart) to avoid querying the ledger at genesis when a known later
/// checkpoint exists.
#[inline]
async fn load_initial_state(&mut self) -> Result<(), Error<C, L, S>> {
pub async fn load_initial_state(&mut self) -> Result<(), Error<C, L, S>> {
self.signer_sync(Default::default()).await
}

Expand Down
27 changes: 27 additions & 0 deletions manta-accounting/src/wallet/signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,30 @@ where
&mut self.state.rng,
)
}

/// Builds a new [`StorageStateOption`] from `self`.
#[inline]
pub fn get_storage(&self) -> StorageStateOption<C>
where
C::UtxoAccumulator: Clone,
C::AssetMap: Clone,
{
Some(StorageState::from_signer(self))
}

/// Tries to update `self` from `storage_state`.
#[inline]
pub fn set_storage(&mut self, storage_state: &StorageStateOption<C>) -> bool
where
C::UtxoAccumulator: Clone,
C::AssetMap: Clone,
{
if let Some(storage_state) = storage_state {
storage_state.update_signer(self);
return true;
}
false
}
}

impl<C> Connection<C> for Signer<C>
Expand Down Expand Up @@ -1352,3 +1376,6 @@ where
signer
}
}

/// Storage State Option
pub type StorageStateOption<C> = Option<StorageState<C>>;
18 changes: 7 additions & 11 deletions manta-pay/src/bin/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ pub fn main() {
.worker_threads(6)
.build()
{
Ok(runtime) => runtime.block_on(async {
simulation
.run(
&parameters,
&utxo_accumulator_model,
&proving_context,
verifying_context,
&mut rng,
)
.await
}),
Ok(runtime) => runtime.block_on(simulation.run(
&parameters,
&utxo_accumulator_model,
&proving_context,
verifying_context,
&mut rng,
)),
Err(err) => Simulation::command()
.error(
ErrorKind::Io,
Expand Down
39 changes: 17 additions & 22 deletions manta-pay/src/signer/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use manta_accounting::wallet::{self, signer};
use manta_util::{
future::LocalBoxFutureResult,
http::reqwest::{self, IntoUrl, KnownUrlClient},
serde::{de::DeserializeOwned, Serialize},
};

#[doc(inline)]
Expand Down Expand Up @@ -76,6 +77,16 @@ impl Client {
message: request,
}
}

/// Sends a POST of type `command` with query string `request`.
#[inline]
pub async fn post_request<T, R>(&self, command: &str, request: T) -> reqwest::Result<R>
where
T: Serialize,
R: DeserializeOwned,
{
self.base.post(command, &self.wrap_request(request)).await
}
}

impl signer::Connection<Config> for Client {
Expand All @@ -88,59 +99,43 @@ impl signer::Connection<Config> for Client {
&mut self,
request: SyncRequest,
) -> LocalBoxFutureResult<Result<SyncResponse, SyncError>, Self::Error> {
Box::pin(async move { self.base.post("sync", &self.wrap_request(request)).await })
Box::pin(self.post_request("sync", request))
}

#[inline]
fn sign(
&mut self,
request: SignRequest,
) -> LocalBoxFutureResult<Result<SignResponse, SignError>, Self::Error> {
Box::pin(async move { self.base.post("sign", &self.wrap_request(request)).await })
Box::pin(self.post_request("sign", request))
}

#[inline]
fn address(&mut self) -> LocalBoxFutureResult<Option<Address>, Self::Error> {
Box::pin(async move {
self.base
.post("address", &self.wrap_request(GetRequest::Get))
.await
})
Box::pin(self.post_request("address", GetRequest::Get))
}

#[inline]
fn transaction_data(
&mut self,
request: TransactionDataRequest,
) -> LocalBoxFutureResult<TransactionDataResponse, Self::Error> {
Box::pin(async move {
self.base
.post("transaction_data", &self.wrap_request(request))
.await
})
Box::pin(self.post_request("transaction_data", request))
}

#[inline]
fn identity_proof(
&mut self,
request: IdentityRequest,
) -> LocalBoxFutureResult<IdentityResponse, Self::Error> {
Box::pin(async move {
self.base
.post("identity", &self.wrap_request(request))
.await
})
Box::pin(self.post_request("identity", request))
}

#[inline]
fn sign_with_transaction_data(
&mut self,
request: SignRequest,
) -> LocalBoxFutureResult<SignWithTransactionDataResult, Self::Error> {
Box::pin(async move {
self.base
.post("sign_with_transaction_data", &self.wrap_request(request))
.await
})
Box::pin(self.post_request("sign_with_transaction_data", request))
}
}
12 changes: 6 additions & 6 deletions manta-pay/src/signer/client/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,43 +137,43 @@ impl signer::Connection<Config> for Client {
&mut self,
request: SyncRequest,
) -> LocalBoxFutureResult<Result<SyncResponse, SyncError>, Self::Error> {
Box::pin(async move { self.send("sync", request).await })
Box::pin(self.send("sync", request))
}

#[inline]
fn sign(
&mut self,
request: SignRequest,
) -> LocalBoxFutureResult<Result<SignResponse, SignError>, Self::Error> {
Box::pin(async move { self.send("sign", request).await })
Box::pin(self.send("sign", request))
}

#[inline]
fn address(&mut self) -> LocalBoxFutureResult<Option<Address>, Self::Error> {
Box::pin(async move { self.send("address", GetRequest::Get).await })
Box::pin(self.send("address", GetRequest::Get))
}

#[inline]
fn transaction_data(
&mut self,
request: TransactionDataRequest,
) -> LocalBoxFutureResult<TransactionDataResponse, Self::Error> {
Box::pin(async move { self.send("transaction_data", request).await })
Box::pin(self.send("transaction_data", request))
}

#[inline]
fn identity_proof(
&mut self,
request: IdentityRequest,
) -> LocalBoxFutureResult<IdentityResponse, Self::Error> {
Box::pin(async move { self.send("identity", request).await })
Box::pin(self.send("identity", request))
}

#[inline]
fn sign_with_transaction_data(
&mut self,
request: SignRequest,
) -> LocalBoxFutureResult<SignWithTransactionDataResult, Self::Error> {
Box::pin(async move { self.send("sign_with_transaction_data", request).await })
Box::pin(self.send("sign_with_transaction_data", request))
}
}
4 changes: 2 additions & 2 deletions manta-pay/src/signer/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ pub fn new_signer(

/// Builds a new [`StorageStateOption`] from `signer`.
#[inline]
pub fn set_storage(signer: &Signer) -> StorageStateOption {
pub fn get_storage(signer: &Signer) -> StorageStateOption {
Some(StorageState::from_signer(signer))
}

/// Tries to update `signer` from `storage_state`.
#[inline]
pub fn get_storage(signer: &mut Signer, storage_state: &StorageStateOption) -> bool {
pub fn set_storage(signer: &mut Signer, storage_state: &StorageStateOption) -> bool {
if let Some(storage_state) = storage_state {
storage_state.update_signer(signer);
return true;
Expand Down
45 changes: 22 additions & 23 deletions manta-pay/src/simulation/ledger/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ use manta_accounting::{
};
use manta_util::{
future::{LocalBoxFuture, LocalBoxFutureResult},
http::reqwest::{Error, IntoUrl, KnownUrlClient},
http::reqwest::{self, Error, IntoUrl, KnownUrlClient},
serde::{de::DeserializeOwned, Serialize},
};

/// HTTP Ledger Client
Expand All @@ -57,6 +58,24 @@ impl Client {
client: KnownUrlClient::new(server_url)?,
})
}

/// Sends a POST of type `command` with query string `request`.
#[inline]
pub async fn post_request<T, R>(&self, command: &str, request: T) -> reqwest::Result<R>
where
T: Serialize,
R: DeserializeOwned,
{
self.client
.post(
command,
&Request {
account: self.account,
request,
},
)
.await
}
}

impl ledger::Connection for Client {
Expand All @@ -71,17 +90,7 @@ impl ledger::Read<SyncData<Config>> for Client {
&'s mut self,
checkpoint: &'s Self::Checkpoint,
) -> LocalBoxFutureResult<'s, ReadResponse<SyncData<Config>>, Self::Error> {
Box::pin(async move {
self.client
.post(
"pull",
&Request {
account: self.account,
request: checkpoint,
},
)
.await
})
Box::pin(self.post_request("pull", checkpoint))
}
}

Expand All @@ -93,17 +102,7 @@ impl ledger::Write<Vec<TransferPost>> for Client {
&mut self,
posts: Vec<TransferPost>,
) -> LocalBoxFutureResult<Self::Response, Self::Error> {
Box::pin(async move {
self.client
.post(
"push",
&Request {
account: self.account,
request: posts,
},
)
.await
})
Box::pin(self.post_request("push", posts))
}
}

Expand Down
7 changes: 2 additions & 5 deletions manta-pay/src/simulation/ledger/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Server {
Fut: Future<Output = R>,
{
let account = request.query::<AccountId>()?;
Self::into_body(move || async move { f(request.state().clone(), account).await }).await
Self::into_body(move || f(request.state().clone(), account)).await
}

/// Executes `f` on the incoming `request` parsing the full query.
Expand All @@ -114,10 +114,7 @@ impl Server {
Fut: Future<Output = R>,
{
let args = request.query::<Request<T>>()?;
Self::into_body(move || async move {
f(request.state().clone(), args.account, args.request).await
})
.await
Self::into_body(move || f(request.state().clone(), args.account, args.request)).await
}

/// Generates the JSON body for the output of `f`, returning an HTTP reponse.
Expand Down
3 changes: 1 addition & 2 deletions manta-trusted-setup/src/bin/groth16_phase2_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ impl Arguments {
{
Ok(runtime) => {
let pk = Array::from_unchecked(*pk.as_bytes());
runtime
.block_on(async { client_contribute::<Config>(sk, pk, self.url).await })
runtime.block_on(client_contribute::<Config>(sk, pk, self.url))
}
Err(e) => panic!("I/O Error while setting up the tokio Runtime: {e:?}"),
}
Expand Down
1 change: 1 addition & 0 deletions manta-trusted-setup/src/groth16/ceremony/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ where
}

/// Recovers from disk files at `path` and uses `path` as the backup directory.
#[allow(clippy::redundant_async_block)] // Necessary to spawn a task with a server clone
#[inline]
pub fn recover(
path: PathBuf,
Expand Down
2 changes: 1 addition & 1 deletion manta-util/src/http/tide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
Fut: Future<Output = Result<R, E>>,
{
let args = request.body_json::<T>().await?;
into_body(move || async move { f(request.state().clone(), args).await }).await
into_body(move || f(request.state().clone(), args)).await
}

/// Registers a `POST` command with the given `path` and execution `f`.
Expand Down