Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
173d8cf
feat(wasm-dpp): implement ChainAssetLockProof binding with tests
markin-io Dec 2, 2022
debfd61
refactor(rs-dpp): return type for chain_asset_lock_proof#createIdenti…
markin-io Dec 2, 2022
806c22a
refactor(rs-dpp): fix formatting
markin-io Dec 2, 2022
84feec6
refactor(wasm-dpp): removed unnecessary chain asset lock fixture
markin-io Dec 2, 2022
3bd659e
feat(wasm-dpp): bootstrap instant_asset_lock_proof.rs binding
markin-io Dec 2, 2022
d780efb
feat(wasm-dpp): instant_asset_lock_proof.rs bindings for getOutPoint …
markin-io Dec 5, 2022
7bc882b
feat(wasm-dpp): instant_asset_lock_proof.rs finish bindings
markin-io Dec 5, 2022
1e3014c
feat(wasm-dpp): createAssetLockProofInstance binding
markin-io Dec 6, 2022
024b827
feat(wasm-dpp): add identity_create_transition constructor
markin-io Dec 8, 2022
1d9f81d
feat(wasm-dpp): IdentityCreateTransition set and get assetLockProof
markin-io Dec 8, 2022
8a77765
feat(wasm-dpp): IdentityCreateTransition setPublicKeys, getPublicKeys…
markin-io Dec 9, 2022
378f717
feat(wasm-dpp): IdentityCreateTransition toObject, toJSON
markin-io Dec 12, 2022
df0a715
feat(wasm-dpp): IdentityCreateTransition getModifiedDataIds
markin-io Dec 12, 2022
4d3ab88
test(wasm-dpp): lint tests
markin-io Dec 13, 2022
be69f5e
fix(wasm-dpp): identity_create_transition unknown asset lock proof
markin-io Dec 20, 2022
6b3cb7c
fix(wasm-dpp): missing protocol version
markin-io Dec 20, 2022
5a11187
Merge branch 'v0.24-dev' of github.com:dashpay/platform into feat/imp…
markin-io Dec 20, 2022
efeb344
feat(wasm-dpp): implement validateIdentityCreateTransitionStateFactory
markin-io Dec 20, 2022
3addb28
Merge branch 'v0.24-dev' into feat/implement_identity_create_st
markin-io Dec 20, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where

// payToId identity exists
let pay_to_identifier = Identifier::from_string(pay_to_id, Encoding::Base58)?;
let maybe_identifier: Option<Vec<u8>> = context
let maybe_identifier = context
.state_repository
.fetch_identity(
&pay_to_identifier,
Expand Down
3 changes: 3 additions & 0 deletions packages/rs-dpp/src/errors/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ pub enum ProtocolError {
DocumentNotProvided {
document_transition: DocumentTransition,
},

#[error("Identity is not present")]
IdentityNotPresentError { id: Identifier },
}

impl From<NonConsensusError> for ProtocolError {
Expand Down
5 changes: 4 additions & 1 deletion packages/rs-dpp/src/errors/non_consensus_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
CompatibleProtocolVersionIsNotDefinedError, InvalidVectorSizeError, SerdeParsingError,
};

#[derive(Debug, Error, Clone)]
#[derive(Debug, Error)]
pub enum NonConsensusError {
#[error("Unexpected serde parsing error: {0:#}")]
SerdeParsingError(SerdeParsingError),
Expand Down Expand Up @@ -34,6 +34,9 @@ pub enum NonConsensusError {
object_name: &'static str,
details: String,
},

#[error(transparent)]
Error(#[from] anyhow::Error),
}

pub mod object_names {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;

use crate::identifier::Identifier;
use crate::util::hash::hash;
use crate::util::vec::vec_to_array;
use crate::{
errors::NonConsensusError, identifier::Identifier, util::hash::hash, util::vec::vec_to_array,
};

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChainAssetLockProof {
#[serde(rename = "type")]
asset_lock_type: u8,
Expand Down Expand Up @@ -40,10 +41,8 @@ impl ChainAssetLockProof {
}

/// Create identifier
pub fn create_identifier(&self) -> Identifier {
return Identifier::new(
vec_to_array(hash(self.out_point()).as_ref())
.expect("Expected hash function to give a 32 byte output"),
);
pub fn create_identifier(&self) -> Result<Identifier, NonConsensusError> {
let array = vec_to_array(hash(self.out_point).as_ref())?;
Ok(Identifier::new(array))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ impl AssetLockProof {
pub fn create_identifier(&self) -> Result<Identifier, NonConsensusError> {
match self {
AssetLockProof::Instant(instant_proof) => instant_proof.create_identifier(),
AssetLockProof::Chain(chain_proof) => {
// TODO: fix return type
Ok(chain_proof.create_identifier())
}
AssetLockProof::Chain(chain_proof) => chain_proof.create_identifier(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ impl IdentityCreateTransition {
.iter()
.map(|val| serde_json::from_value(val.clone()))
.collect::<Result<Vec<IdentityPublicKey>, serde_json::Error>>()?;
state_transition = state_transition.set_public_keys(keys);
state_transition.set_public_keys(keys);
}

if let Some(proof) = transition_map.get(property_names::ASSET_LOCK_PROOF) {
state_transition.set_asset_lock_proof(AssetLockProof::try_from(proof)?)?;
}

if let Some(protocol_version) = transition_map.get(property_names::PROTOCOL_VERSION) {
state_transition.protocol_version = protocol_version.as_u64().unwrap() as u32;
}

Ok(state_transition)
}

Expand Down Expand Up @@ -142,14 +146,14 @@ impl IdentityCreateTransition {
}

/// Replaces existing set of public keys with a new one
pub fn set_public_keys(mut self, public_keys: Vec<IdentityPublicKey>) -> Self {
pub fn set_public_keys(&mut self, public_keys: Vec<IdentityPublicKey>) -> &mut Self {
self.public_keys = public_keys;

self
}

/// Adds public keys to the existing public keys array
pub fn add_public_keys(mut self, public_keys: &mut Vec<IdentityPublicKey>) -> Self {
pub fn add_public_keys(&mut self, public_keys: &mut Vec<IdentityPublicKey>) -> &mut Self {
self.public_keys.append(public_keys);

self
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::consensus::state::identity::IdentityAlreadyExistsError;
use crate::identity::state_transition::identity_create_transition::IdentityCreateTransition;
use crate::prelude::Identity;
use crate::state_repository::StateRepositoryLike;
use crate::state_transition::StateTransitionLike;
use crate::validation::ValidationResult;
use crate::NonConsensusError;
use std::convert::TryInto;

/// Validate that identity exists
///
Expand All @@ -21,8 +21,11 @@ pub async fn validate_identity_create_transition_state(

let identity_id = state_transition.get_identity_id();
let maybe_identity = state_repository
.fetch_identity::<Identity>(identity_id, state_transition.get_execution_context())
.await
.fetch_identity(identity_id, state_transition.get_execution_context())
.await?
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)
.map_err(|e| NonConsensusError::StateRepositoryFetchError(e.to_string()))?;

if state_transition.get_execution_context().is_dry_run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use dashcore::{
Script, TxOut,
};
use lazy_static::__Deref;
use std::convert::TryInto;

use crate::{
identity::convert_credits_to_satoshi, prelude::Identity, state_repository::StateRepositoryLike,
identity::convert_credits_to_satoshi, state_repository::StateRepositoryLike,
state_transition::StateTransitionLike,
};

Expand Down Expand Up @@ -67,13 +68,16 @@ where
.enqueue_withdrawal_transaction(latest_withdrawal_index, transaction_buffer)
.await?;

let maybe_existing_identity: Option<Identity> = self
let maybe_existing_identity = self
.state_repository
.fetch_identity(
&state_transition.identity_id,
state_transition.get_execution_context(),
)
.await?;
.await?
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)?;

let mut existing_identity =
maybe_existing_identity.ok_or_else(|| anyhow!("Identity not found"))?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::convert::TryInto;
use std::sync::Arc;

use anyhow::Result;

use crate::{
consensus::basic::{identity::IdentityInsufficientBalanceError, BasicError},
identity::state_transition::identity_credit_withdrawal_transition::IdentityCreditWithdrawalTransition,
prelude::Identity,
state_repository::StateRepositoryLike,
state_transition::StateTransitionLike,
validation::ValidationResult,
Expand Down Expand Up @@ -33,14 +33,17 @@ where
) -> Result<ValidationResult<()>, NonConsensusError> {
let mut result: ValidationResult<()> = ValidationResult::default();

let maybe_existing_identity: Option<Identity> = self
let maybe_existing_identity = self
.state_repository
.fetch_identity(
&state_transition.identity_id,
state_transition.get_execution_context(),
)
.await
.map_err(|err| NonConsensusError::StateRepositoryFetchError(err.to_string()))?;
.await?
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)
.map_err(|e| NonConsensusError::StateRepositoryFetchError(e.to_string()))?;

let existing_identity = match maybe_existing_identity {
None => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::convert::TryInto;
use std::sync::Arc;

use anyhow::{anyhow, Result};

use crate::identity::state_transition::asset_lock_proof::AssetLockTransactionOutputFetcher;
use crate::identity::state_transition::identity_topup_transition::IdentityTopUpTransition;
use crate::identity::{convert_satoshi_to_credits, get_biggest_possible_identity, Identity};
use crate::identity::{convert_satoshi_to_credits, get_biggest_possible_identity};
use crate::state_repository::StateRepositoryLike;
use crate::state_transition::StateTransitionLike;

Expand Down Expand Up @@ -47,11 +48,13 @@ where
.out_point()
.ok_or_else(|| anyhow!("Out point is missing from asset lock proof"))?;
let identity_id = state_transition.get_identity_id();

let mut maybe_identity = self
.state_repository
.fetch_identity::<Identity>(identity_id, state_transition.get_execution_context())
.await?;
.fetch_identity(identity_id, state_transition.get_execution_context())
.await?
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)?;

if is_dry_run {
maybe_identity = Some(get_biggest_possible_identity())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::convert::TryInto;
use std::sync::Arc;

use crate::{
consensus::{basic::BasicError, ConsensusError},
identity::get_biggest_possible_identity,
prelude::{Identifier, Identity},
prelude::Identifier,
state_repository::StateRepositoryLike,
state_transition::StateTransitionLike,
ProtocolError,
Expand Down Expand Up @@ -33,12 +34,15 @@ pub async fn apply_identity_update_transition(
state_repository: &impl StateRepositoryLike,
state_transition: IdentityUpdateTransition,
) -> Result<(), ProtocolError> {
let mut maybe_identity: Option<Identity> = state_repository
let mut maybe_identity = state_repository
.fetch_identity(
state_transition.get_identity_id(),
state_transition.get_execution_context(),
)
.await?;
.await?
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)?;

if state_transition.get_execution_context().is_dry_run() {
maybe_identity = Some(get_biggest_possible_identity())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use dashcore::BlockHeader;
use serde_json::Value;
use std::convert::TryInto;
use std::sync::Arc;

use crate::{
block_time_window::validate_time_in_block_time_window::validate_time_in_block_time_window,
consensus::basic::BasicError,
identity::validation::{RequiredPurposeAndSecurityLevelValidator, TPublicKeysValidator},
prelude::Identity,
state_repository::StateRepositoryLike,
state_transition::StateTransitionLike,
validation::SimpleValidationResult,
Expand Down Expand Up @@ -38,13 +38,16 @@ where
) -> Result<SimpleValidationResult, NonConsensusError> {
let mut validation_result = SimpleValidationResult::default();

let maybe_stored_identity: Option<Identity> = self
let maybe_stored_identity = self
.state_repository
.fetch_identity(
state_transition.get_identity_id(),
state_transition.get_execution_context(),
)
.await
.await?
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)
.map_err(|e| NonConsensusError::StateRepositoryFetchError(e.to_string()))?;

if state_transition.get_execution_context().is_dry_run() {
Expand Down
9 changes: 4 additions & 5 deletions packages/rs-dpp/src/state_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ impl From<Infallible> for ProtocolError {
}

// Let StateRepositoryLike mock return DataContracts instead of bytes to simplify things a bit.
#[cfg_attr(test, automock(type ConversionError=Infallible; type FetchDataContract=DataContract;))]
#[cfg_attr(test, automock(type ConversionError=Infallible; type FetchDataContract=DataContract; type FetchIdentity=Identity;))]
#[async_trait]
pub trait StateRepositoryLike: Send + Sync {
type ConversionError: Into<ProtocolError>;
type FetchDataContract: TryInto<DataContract, Error = Self::ConversionError>;
type FetchIdentity: TryInto<Identity, Error = Self::ConversionError>;

/// Fetch the Data Contract by ID
/// By default, the method should return data as bytes (`Vec<u8>`), but the deserialization to [`DataContract`] should be also possible
Expand Down Expand Up @@ -87,13 +88,11 @@ pub trait StateRepositoryLike: Send + Sync {

/// Fetch Identity by ID
/// By default, the method should return data as bytes (`Vec<u8>`), but the deserialization to [`Identity`] should be also possible
async fn fetch_identity<T>(
async fn fetch_identity(
&self,
id: &Identifier,
execution_context: &StateTransitionExecutionContext,
) -> AnyResult<Option<T>>
where
T: for<'de> serde::de::Deserialize<'de> + 'static;
) -> AnyResult<Option<Self::FetchIdentity>>;

/// Store Public Key hashes and Identity id pair
async fn store_identity_public_key_hashes(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use anyhow::Context;
use std::convert::TryInto;

use crate::{
consensus::fee::FeeError,
identity::{
convert_satoshi_to_credits,
state_transition::asset_lock_proof::AssetLockTransactionOutputFetcher,
},
prelude::Identity,
state_repository::StateRepositoryLike,
state_transition::{StateTransition, StateTransitionIdentitySigned, StateTransitionLike},
validation::SimpleValidationResult,
Expand Down Expand Up @@ -66,11 +66,17 @@ where
})?;
let balance = convert_satoshi_to_credits(output.value);
let identity_id = st.get_owner_id();
let identity: Identity = self
let identity = self
.state_repository
.fetch_identity(identity_id, execution_context)
.fetch_identity(identity_id, st.get_execution_context())
.await?
.with_context(|| format!("identity with ID {}' doesn't exist", identity_id))?;
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)?
.ok_or_else(|| ProtocolError::IdentityNotPresentError {
id: identity_id.clone(),
})?;

if execution_context.is_dry_run() {
return Ok(result);
}
Expand Down Expand Up @@ -128,11 +134,16 @@ where
st: &impl StateTransitionIdentitySigned,
) -> Result<u64, ProtocolError> {
let identity_id = st.get_owner_id();
let identity: Identity = self
let identity = self
.state_repository
.fetch_identity(identity_id, st.get_execution_context())
.await?
.with_context(|| format!("identity with ID {}' doesn't exist", identity_id))?;
.map(TryInto::try_into)
.transpose()
.map_err(Into::into)?
.ok_or_else(|| ProtocolError::IdentityNotPresentError {
id: identity_id.clone(),
})?;

Ok(identity.get_balance())
}
Expand Down
Loading