Skip to content

Commit

Permalink
Merge pull request #70 from pshenmic/feat/identities
Browse files Browse the repository at this point in the history
Implement all Identities state transitions
  • Loading branch information
pshenmic authored Oct 26, 2023
2 parents 944aafe + 3a55a42 commit 5fd1a61
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 76 deletions.
20 changes: 10 additions & 10 deletions packages/indexer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/indexer/migrations/V14__add_identities.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE identities (
id SERIAL PRIMARY KEY,
identifier char(44) NOT NULL,
revision int NOT NULL,
state_transition_hash char(64) NOT NULL references state_transitions(hash)
);
7 changes: 7 additions & 0 deletions packages/indexer/migrations/V15__add_transfers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE transfers (
id SERIAL PRIMARY KEY,
amount bigint NOT NULL,
sender char(44),
recipient char(44),
state_transition_hash char(64) NOT NULL references state_transitions(hash)
);
6 changes: 0 additions & 6 deletions packages/indexer/src/entities/data_contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use std::collections::BTreeMap;
use std::time::SystemTime;
use chrono::{DateTime, Utc};
use dpp::data_contract::DocumentName;
use dpp::data_contract::serialized_version::DataContractInSerializationFormat;
use dpp::data_contracts::SystemDataContract;
use dpp::identifier::Identifier;
use dpp::platform_value::string_encoding::Encoding;
use dpp::state_transition::data_contract_create_transition::DataContractCreateTransition;
use dpp::state_transition::data_contract_update_transition::accessors::DataContractUpdateTransitionAccessorsV0;
use dpp::state_transition::data_contract_update_transition::DataContractUpdateTransition;
use dpp::state_transition::StateTransition;
use serde_json::Value;
use tokio_postgres::Row;

Expand Down
4 changes: 0 additions & 4 deletions packages/indexer/src/entities/document.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use std::collections::BTreeMap;
use std::iter::Rev;
use std::time::SystemTime;
use chrono::{DateTime, Utc};
use dpp::identifier::Identifier;
use dpp::platform_value::string_encoding::Encoding;
use dpp::prelude::Revision;
Expand Down
40 changes: 40 additions & 0 deletions packages/indexer/src/entities/identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use dpp::identifier::Identifier;
use dpp::prelude::Revision;
use dpp::state_transition::identity_create_transition::accessors::IdentityCreateTransitionAccessorsV0;
use dpp::state_transition::identity_create_transition::IdentityCreateTransition;
use dpp::state_transition::identity_update_transition::accessors::IdentityUpdateTransitionAccessorsV0;
use dpp::state_transition::identity_update_transition::IdentityUpdateTransition;

#[derive(Clone)]
pub struct Identity {
pub id: Option<u32>,
pub identifier: Identifier,
pub revision: Revision,
pub balance: Option<u64>,
}

impl From<IdentityCreateTransition> for Identity {
fn from(state_transition: IdentityCreateTransition) -> Self {
return Identity {
id: None,
identifier: state_transition.identity_id(),
balance: None,
revision: Revision::from(0 as u64),
};
}
}

impl From<IdentityUpdateTransition> for Identity {
fn from(state_transition: IdentityUpdateTransition) -> Self {
let identifier = state_transition.identity_id();
let revision = state_transition.revision();

return Identity {
id: None,
identifier,
balance: None,
revision,
};
}
}

2 changes: 2 additions & 0 deletions packages/indexer/src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ pub mod block_header;
pub mod block;
pub mod data_contract;
pub mod document;
pub mod identity;
pub mod transfer;
66 changes: 66 additions & 0 deletions packages/indexer/src/entities/transfer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use dpp::identifier::Identifier;
use dpp::state_transition::identity_credit_transfer_transition::accessors::IdentityCreditTransferTransitionAccessorsV0;
use dpp::state_transition::identity_credit_transfer_transition::IdentityCreditTransferTransition;
use dpp::state_transition::identity_credit_withdrawal_transition::accessors::IdentityCreditWithdrawalTransitionAccessorsV0;
use dpp::state_transition::identity_credit_withdrawal_transition::IdentityCreditWithdrawalTransition;
use dpp::state_transition::identity_topup_transition::accessors::IdentityTopUpTransitionAccessorsV0;
use dpp::state_transition::identity_topup_transition::IdentityTopUpTransition;

#[derive(Clone)]
pub struct Transfer {
pub id: Option<u32>,
pub sender: Option<Identifier>,
pub recipient: Option<Identifier>,
pub amount: u64,
}

impl From<IdentityTopUpTransition> for Transfer {
fn from(state_transition: IdentityTopUpTransition) -> Self {
let identifier = state_transition.identity_id().clone();
let asset_lock = state_transition.asset_lock_proof().clone();
let vout_index = asset_lock.instant_lock_output_index().unwrap();
let tx_out = asset_lock
.transaction()
.unwrap().clone()
.output.get(vout_index)
.cloned().unwrap();
let amount = tx_out.value * 1000;

return Transfer {
id: None,
sender:None,
recipient: Some(identifier),
amount
};
}
}

impl From<IdentityCreditWithdrawalTransition> for Transfer {
fn from(state_transition: IdentityCreditWithdrawalTransition) -> Self {
let identifier = state_transition.identity_id().clone();
let amount = state_transition.amount();

return Transfer {
id: None,
sender: Some(identifier),
recipient: None,
amount
};
}
}

impl From<IdentityCreditTransferTransition> for Transfer {
fn from(state_transition: IdentityCreditTransferTransition) -> Self {
let sender = state_transition.identity_id().clone();
let recipient = state_transition.recipient_id().clone();
let amount = state_transition.amount();

return Transfer {
id: None,
sender: Some(sender),
recipient: Some(recipient),
amount
};
}
}

56 changes: 46 additions & 10 deletions packages/indexer/src/processor/psql/dao/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use std::env;
use std::time::SystemTime;
use deadpool_postgres::{Config, ManagerConfig, Pool, PoolError, RecyclingMethod, Runtime, tokio_postgres};
use deadpool_postgres::{Config, ManagerConfig, Pool, PoolError, RecyclingMethod, Runtime};
use deadpool_postgres::tokio_postgres::{NoTls, Row};
use dpp::platform_value::string_encoding::Encoding;
use dpp::state_transition::data_contract_create_transition::accessors::DataContractCreateTransitionAccessorsV0;
use dpp::state_transition::data_contract_create_transition::DataContractCreateTransition;
use sha256::{digest};
use crate::entities::document::Document;
use base64::{Engine as _, engine::{general_purpose}};
use dpp::identifier::Identifier;
use dpp::platform_value::string_encoding::Encoding::{Base58, Base64};
use dpp::serialization::PlatformSerializable;
use dpp::state_transition::documents_batch_transition::DocumentCreateTransition;
use dpp::state_transition::StateTransition;
use dpp::platform_value::string_encoding::Encoding::{Base58};
use crate::entities::block_header::BlockHeader;
use crate::entities::data_contract::DataContract;
use crate::entities::identity::Identity;
use crate::entities::transfer::Transfer;

pub struct PostgresDAO {
connection_pool: Pool,
Expand All @@ -40,16 +37,18 @@ impl PostgresDAO {
return PostgresDAO { connection_pool };
}

pub async fn create_state_transition(&self, block_hash: String, st_type: i32, index:i32, bytes: Vec<u8>) {
pub async fn create_state_transition(&self, block_hash: String, st_type: u32, index: u32, bytes: Vec<u8>) {
let data = general_purpose::STANDARD.encode(&bytes);
let hash = digest(bytes.clone()).to_uppercase();
let st_type = st_type as i32;
let index_i32 = index as i32;

let query = "INSERT INTO state_transitions(hash, data, type, index, block_hash) VALUES ($1, $2, $3, $4, $5);";

let client = self.connection_pool.get().await.unwrap();
let stmt = client.prepare_cached(query).await.unwrap();

client.query(&stmt, &[&hash, &data, &st_type, &index, &block_hash]).await.unwrap();
client.query(&stmt, &[&hash, &data, &st_type, &index_i32, &block_hash]).await.unwrap();

println!("Created ST with hash {} from block with hash {}", &hash, &block_hash);
}
Expand Down Expand Up @@ -95,6 +94,43 @@ impl PostgresDAO {
Ok(())
}

pub async fn create_identity(&self, identity: Identity, st_hash: String) -> Result<(), PoolError> {
let identifier = identity.identifier;
let revision = identity.revision;
let revision_i32 = revision as i32;

let client = self.connection_pool.get().await.unwrap();

let query = "INSERT INTO identities(identifier,revision,state_transition_hash) VALUES ($1, $2, $3);";

let stmt = client.prepare_cached(query).await.unwrap();

client.query(&stmt, &[&identifier.to_string(Encoding::Base58), &revision_i32, &st_hash]).await.unwrap();

println!("Created Identity {}", identifier.to_string(Base58));

Ok(())
}

pub async fn create_transfer(&self, transfer: Transfer, st_hash: String) -> Result<(), PoolError> {
let amount = transfer.amount as i64;

let sender = transfer.sender.map(|t| { t.to_string(Base58)});
let recipient = transfer.recipient.map(|t| { t.to_string(Base58)});

let client = self.connection_pool.get().await.unwrap();

let query = "INSERT INTO transfers(amount,sender,recipient,state_transition_hash) VALUES ($1, $2, $3, $4);";

let stmt = client.prepare_cached(query).await.unwrap();

client.query(&stmt, &[&amount, &sender, &recipient, &st_hash]).await.unwrap();

println!("Created Transfer");

Ok(())
}

pub async fn get_block_header_by_height(&self, block_height: i32) -> Result<Option<BlockHeader>, PoolError> {
let client = self.connection_pool.get().await?;

Expand Down Expand Up @@ -155,7 +191,7 @@ impl PostgresDAO {

let rows = client.query(&stmt, &[&block_header.hash, &block_header.height, &SystemTime::from(block_header.timestamp), &block_header.block_version, &block_header.app_version, &block_header.l1_locked_height]).await.unwrap();

let block_hash:String = rows[0].get(0);
let block_hash: String = rows[0].get(0);

return block_hash;
}
Expand Down
Loading

0 comments on commit 5fd1a61

Please sign in to comment.