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

refactor(sdk)!: unify state transition processing #2338

Merged
merged 30 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
89f4f29
feat: retry broadcast
lklimek Nov 18, 2024
da8ec79
chore(sdk): remove unused file
lklimek Nov 18, 2024
4618f55
refactor(sdk): sepaate StateTransition::wait_for_response()
lklimek Nov 18, 2024
c10700c
refactor(sdk)!: pass settings to ST broadcast
lklimek Nov 19, 2024
1ec37c1
refactor(sdk): broadcast st
lklimek Nov 20, 2024
a82fd36
chore: fix build issues
lklimek Nov 20, 2024
235ebd5
Merge branch 'v1.6-dev' into feat/sdk-retry-broadcast
lklimek Nov 20, 2024
9b663ea
doc(sdk): improve documentation a bit
lklimek Nov 20, 2024
2d5318d
chore: rename Wrap trait
lklimek Nov 20, 2024
83c8160
test(sdk): fix tests to address new error msgs
lklimek Nov 20, 2024
21e229a
chore: add TODO
lklimek Nov 20, 2024
ebf61e0
refactor(sdk)!: change state transitions api
lklimek Nov 20, 2024
654e563
test(sdk): update tests
lklimek Nov 20, 2024
d5e03b3
chore(sdk): retry 3 times by default
lklimek Nov 21, 2024
89333d7
chore(dapi-client): add WARN log when banning address
lklimek Nov 21, 2024
b1e3e6c
chore(dapi-client): improve logging
lklimek Nov 21, 2024
79ed045
chore: unify error msg
lklimek Nov 21, 2024
ff54061
Merge branch 'v1.6-dev' into feat/sdk-retry-broadcast
lklimek Nov 25, 2024
146d83d
chore: rename some fn
lklimek Nov 26, 2024
21af0e7
chore: fix tests
lklimek Nov 26, 2024
687ba20
Merge remote-tracking branch 'origin/v1.6-dev' into feat/sdk-retry-br…
lklimek Nov 26, 2024
f540de4
Merge commit '687ba2038b9b8fedf4a6ee55b7ebe01c686dbb7e' into refactor…
lklimek Nov 28, 2024
0bcd396
Merge remote-tracking branch 'origin/v1.7-dev' into refactor/sdk-put-dry
lklimek Nov 28, 2024
4ab4565
chore: rabbit feedback - crate error handling upgrade
lklimek Nov 28, 2024
4f9190c
Merge branch 'v1.7-dev' into refactor/sdk-put-dry
lklimek Dec 2, 2024
0eaeec8
Merge branch 'v1.7-dev' into refactor/sdk-put-dry
lklimek Dec 3, 2024
c8b6c78
chore: add Waitable supertrait to Put* traits
lklimek Dec 5, 2024
9042fd7
fix(sdk): build overrides default settings instead of replacing
lklimek Dec 5, 2024
8329002
refactor(sdk): impl Waitable for Vote
lklimek Dec 5, 2024
080319a
Merge remote-tracking branch 'origin/v1.7-dev' into refactor/sdk-put-dry
lklimek Dec 5, 2024
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 packages/rs-sdk/src/platform/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod transfer_document;
mod txid;
pub mod update_price_of_document;
pub mod vote;
pub mod waitable;
pub mod withdraw_from_identity;

pub use txid::TxId;
61 changes: 8 additions & 53 deletions packages/rs-sdk/src/platform/transition/purchase_document.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
use std::sync::Arc;

use crate::{Error, Sdk};

use super::broadcast::BroadcastStateTransition;
use super::waitable::Waitable;
use crate::platform::transition::put_settings::PutSettings;
use crate::{Error, Sdk};
use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters;
use dpp::data_contract::document_type::DocumentType;
use dpp::data_contract::DataContract;
use dpp::document::{Document, DocumentV0Getters};
use dpp::document::Document;
use dpp::fee::Credits;
use dpp::identity::signer::Signer;
use dpp::identity::IdentityPublicKey;
use dpp::prelude::Identifier;
use dpp::state_transition::documents_batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0;
use dpp::state_transition::documents_batch_transition::DocumentsBatchTransition;
use dpp::state_transition::proof_result::StateTransitionProofResult;
use dpp::state_transition::StateTransition;

use super::broadcast::BroadcastStateTransition;

#[async_trait::async_trait]
/// A trait for purchasing a document on Platform
pub trait PurchaseDocument<S: Signer> {
Expand All @@ -34,14 +29,6 @@ pub trait PurchaseDocument<S: Signer> {
settings: Option<PutSettings>,
) -> Result<StateTransition, Error>;

/// Waits for the response of a state transition after it has been broadcast
async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
data_contract: Arc<DataContract>,
) -> Result<Document, Error>;

/// Tries to purchase a document on platform and waits for the response
async fn purchase_document_and_wait_for_response(
&self,
Expand All @@ -50,8 +37,8 @@ pub trait PurchaseDocument<S: Signer> {
document_type: DocumentType,
purchaser_id: Identifier,
identity_public_key: IdentityPublicKey,
data_contract: Arc<DataContract>,
signer: &S,
settings: Option<PutSettings>,
) -> Result<Document, Error>;
}

Expand Down Expand Up @@ -98,39 +85,15 @@ impl<S: Signer> PurchaseDocument<S> for Document {
Ok(transition)
}

async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
_data_contract: Arc<DataContract>,
) -> Result<Document, Error> {
let result = state_transition.wait_for_response(sdk, None).await?;

match result {
StateTransitionProofResult::VerifiedDocuments(mut documents) => {
let document = documents
.remove(self.id_ref())
.ok_or(Error::InvalidProvedResponse(
"did not prove the sent document".to_string(),
))?
.ok_or(Error::InvalidProvedResponse(
"expected there to actually be a document".to_string(),
))?;
Ok(document)
}
_ => Err(Error::DapiClientError("proved a non document".to_string())),
}
}

async fn purchase_document_and_wait_for_response(
&self,
price: Credits,
sdk: &Sdk,
document_type: DocumentType,
purchaser_id: Identifier,
identity_public_key: IdentityPublicKey,
data_contract: Arc<DataContract>,
signer: &S,
settings: Option<PutSettings>,
) -> Result<Document, Error> {
let state_transition = self
.purchase_document(
Expand All @@ -140,18 +103,10 @@ impl<S: Signer> PurchaseDocument<S> for Document {
purchaser_id,
identity_public_key,
signer,
None,
settings,
)
.await?;

let document = <Self as PurchaseDocument<S>>::wait_for_response(
self,
sdk,
state_transition,
data_contract,
)
.await?;

Ok(document)
<Self as Waitable>::wait_for_response(sdk, state_transition, settings).await
}
}
35 changes: 6 additions & 29 deletions packages/rs-sdk/src/platform/transition/put_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use dpp::identity::signer::Signer;
use dpp::identity::{IdentityPublicKey, PartialIdentity};
use dpp::state_transition::data_contract_create_transition::methods::DataContractCreateTransitionMethodsV0;
use dpp::state_transition::data_contract_create_transition::DataContractCreateTransition;
use dpp::state_transition::proof_result::StateTransitionProofResult;
use dpp::state_transition::StateTransition;

use super::broadcast::BroadcastStateTransition;
use super::waitable::Waitable;

#[async_trait::async_trait]
/// A trait for putting a contract to platform
pub trait PutContract<S: Signer> {
pub trait PutContract<S: Signer>: Waitable {
shumkov marked this conversation as resolved.
Show resolved Hide resolved
/// Puts a document on platform
/// setting settings to `None` sets default connection behavior
async fn put_to_platform(
Expand All @@ -28,19 +28,13 @@ pub trait PutContract<S: Signer> {
settings: Option<PutSettings>,
) -> Result<StateTransition, Error>;

/// Waits for the response of a state transition after it has been broadcast
async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
) -> Result<DataContract, Error>;

/// Puts a contract on platform and waits for the confirmation proof
async fn put_to_platform_and_wait_for_response(
&self,
sdk: &Sdk,
identity_public_key: IdentityPublicKey,
signer: &S,
settings: Option<PutSettings>,
) -> Result<DataContract, Error>;
}

Expand Down Expand Up @@ -82,34 +76,17 @@ impl<S: Signer> PutContract<S> for DataContract {
Ok(transition)
}

async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
) -> Result<DataContract, Error> {
let result = state_transition.wait_for_response(sdk, None).await?;

//todo verify

match result {
StateTransitionProofResult::VerifiedDataContract(data_contract) => Ok(data_contract),
_ => Err(Error::DapiClientError("proved a non document".to_string())),
}
}

async fn put_to_platform_and_wait_for_response(
&self,
sdk: &Sdk,
identity_public_key: IdentityPublicKey,
signer: &S,
settings: Option<PutSettings>,
) -> Result<DataContract, Error> {
let state_transition = self
.put_to_platform(sdk, identity_public_key, signer, None)
.put_to_platform(sdk, identity_public_key, signer, settings)
.await?;

let data_contract =
<Self as PutContract<S>>::wait_for_response(self, sdk, state_transition).await?;

Ok(data_contract)
Self::wait_for_response(sdk, state_transition, settings).await
}
}
58 changes: 5 additions & 53 deletions packages/rs-sdk/src/platform/transition/put_document.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use super::broadcast::BroadcastStateTransition;
use super::waitable::Waitable;
use crate::platform::transition::put_settings::PutSettings;
use crate::{Error, Sdk};
use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters;
use dpp::data_contract::document_type::DocumentType;
use dpp::data_contract::DataContract;
use dpp::document::{Document, DocumentV0Getters};
use dpp::identity::signer::Signer;
use dpp::identity::IdentityPublicKey;
use dpp::state_transition::documents_batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0;
use dpp::state_transition::documents_batch_transition::DocumentsBatchTransition;
use dpp::state_transition::proof_result::StateTransitionProofResult;
use dpp::state_transition::StateTransition;
use std::sync::Arc;

#[async_trait::async_trait]
/// A trait for putting a document to platform
Expand All @@ -28,23 +26,15 @@ pub trait PutDocument<S: Signer> {
settings: Option<PutSettings>,
) -> Result<StateTransition, Error>;

/// Waits for the response of a state transition after it has been broadcast
async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
data_contract: Arc<DataContract>,
) -> Result<Document, Error>;

/// Puts an identity on platform and waits for the confirmation proof
async fn put_to_platform_and_wait_for_response(
&self,
sdk: &Sdk,
document_type: DocumentType,
document_state_transition_entropy: [u8; 32],
identity_public_key: IdentityPublicKey,
data_contract: Arc<DataContract>,
signer: &S,
settings: Option<PutSettings>,
) -> Result<Document, Error>;
}

Expand Down Expand Up @@ -89,38 +79,14 @@ impl<S: Signer> PutDocument<S> for Document {
Ok(transition)
}

async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
_data_contract: Arc<DataContract>,
) -> Result<Document, Error> {
let result = state_transition.wait_for_response(sdk, None).await?;
//todo verify
match result {
StateTransitionProofResult::VerifiedDocuments(mut documents) => {
let document = documents
.remove(self.id_ref())
.ok_or(Error::InvalidProvedResponse(
"did not prove the sent document".to_string(),
))?
.ok_or(Error::InvalidProvedResponse(
"expected there to actually be a document".to_string(),
))?;
Ok(document)
}
_ => Err(Error::DapiClientError("proved a non document".to_string())),
}
}

async fn put_to_platform_and_wait_for_response(
&self,
sdk: &Sdk,
document_type: DocumentType,
document_state_transition_entropy: [u8; 32],
identity_public_key: IdentityPublicKey,
_data_contract: Arc<DataContract>,
signer: &S,
settings: Option<PutSettings>,
) -> Result<Document, Error> {
let state_transition = self
.put_to_platform(
Expand All @@ -129,24 +95,10 @@ impl<S: Signer> PutDocument<S> for Document {
document_state_transition_entropy,
identity_public_key,
signer,
None,
settings,
)
.await?;

let result = state_transition.broadcast_and_wait(sdk, None).await?;
match result {
StateTransitionProofResult::VerifiedDocuments(mut documents) => {
let document = documents
.remove(self.id_ref())
.ok_or(Error::InvalidProvedResponse(
"did not prove the sent document".to_string(),
))?
.ok_or(Error::InvalidProvedResponse(
"expected there to actually be a document".to_string(),
))?;
Ok(document)
}
_ => Err(Error::DapiClientError("proved a non document".to_string())),
}
<Self as Waitable>::wait_for_response(sdk, state_transition, settings).await
}
}
Loading
Loading