Skip to content

Commit

Permalink
Merge branch 'feature/node' of github.com:jonas089/kairos-lab into fe…
Browse files Browse the repository at this point in the history
…ature/node
  • Loading branch information
Rom3dius committed Mar 25, 2024
2 parents 0c08caf + 60e1815 commit 5cb2124
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
2 changes: 2 additions & 0 deletions node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct Node {
pub address: String,
pub port: u16,
pub counter_uref: String,
pub tree_counter_uref: String,
pub dict_uref: String,
pub secret_key_path: String,
pub chain_name: String,
Expand Down Expand Up @@ -128,6 +129,7 @@ impl Config {
address: "127.0.0.1".to_string(),
port: 11101,
counter_uref: "uref-".to_string(),
tree_counter_uref: "uref-".to_string(),
dict_uref: "uref-".to_string(),
secret_key_path: "/".to_string(),
chain_name: "cspr-dev-cctl".to_string(),
Expand Down
48 changes: 30 additions & 18 deletions node/src/domain/models/transfers.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use diesel::{ExpressionMethods, Insertable, QueryDsl, Queryable, Selectable, SelectableHelper};
use bigdecimal::{BigDecimal, ToPrimitive};
use chrono::{NaiveDateTime, Utc};
use deadpool_diesel::postgres::Pool;
use diesel::RunQueryDsl;
use diesel::{ExpressionMethods, Insertable, QueryDsl, Queryable, Selectable, SelectableHelper};
use kairos_risc0_types::{ToBigDecimal, Transfer};
use chrono::{Utc, NaiveDateTime};
use bigdecimal::{BigDecimal, ToPrimitive};
use serde::{Deserialize, Serialize};
use deadpool_diesel::postgres::Pool;

use crate::database::delta_tree_schema as schema;
use crate::database::delta_tree_schema::transfers;
use crate::database::errors;
use crate::database::delta_tree_schema as schema;

// Define struct for schema for transfers
#[derive(Serialize, Queryable, Selectable, Insertable)]
Expand All @@ -27,8 +27,8 @@ pub struct TransferModel {
impl From<Transfer> for TransferModel {
fn from(transfer: Transfer) -> Self {
TransferModel {
sender: transfer.sender.to_string(),
recipient: transfer.recipient.to_string(),
sender: transfer.sender.to_formatted_string(),
recipient: transfer.recipient.to_formatted_string(),
amount: transfer.amount.to_big_decimal(),
timestamp: Utc::now().naive_utc(),
sig: transfer.signature,
Expand All @@ -41,8 +41,12 @@ impl From<Transfer> for TransferModel {
impl Into<Transfer> for TransferModel {
fn into(self) -> Transfer {
Transfer {
sender: kairos_risc0_types::Key::Account(kairos_risc0_types::AccountHash::from_formatted_str(&self.sender).unwrap()),
recipient: kairos_risc0_types::Key::Account(kairos_risc0_types::AccountHash::from_formatted_str(&self.recipient).unwrap()),
sender: kairos_risc0_types::Key::Account(
kairos_risc0_types::AccountHash::from_formatted_str(&self.sender).unwrap(),
),
recipient: kairos_risc0_types::Key::Account(
kairos_risc0_types::AccountHash::from_formatted_str(&self.recipient).unwrap(),
),
amount: kairos_risc0_types::U512::from_dec_str(&self.amount.to_string()).unwrap(),
timestamp: None,
signature: self.sig,
Expand All @@ -59,22 +63,28 @@ pub struct TransfersFilter {
pub processed: Option<bool>,
}

pub async fn insert(pool: Pool, new_transfer: Transfer) -> Result<TransferModel, errors::DatabaseError> {
pub async fn insert(
pool: Pool,
new_transfer: Transfer,
) -> Result<TransferModel, errors::DatabaseError> {
let conn = pool.get().await?;
let transfer_model = TransferModel::from(new_transfer);
let res = conn
.interact(|conn| {
diesel::insert_into(transfers::table)
.values(transfer_model)
.get_result::<TransferModel>(conn)
})
.interact(|conn| {
diesel::insert_into(transfers::table)
.values(transfer_model)
.get_result::<TransferModel>(conn)
})
.await??;
Ok(res)
}

// TODO - add get function which just retrieves transfer by ID

pub async fn get_all(pool: Pool, filter: TransfersFilter) -> Result<Vec<TransferModel>, errors::DatabaseError> {
pub async fn get_all(
pool: Pool,
filter: TransfersFilter,
) -> Result<Vec<TransferModel>, errors::DatabaseError> {
let conn = pool.get().await?;
let res = conn
.interact(move |conn| {
Expand All @@ -92,8 +102,10 @@ pub async fn get_all(pool: Pool, filter: TransfersFilter) -> Result<Vec<Transfer
query = query.filter(transfers::processed.eq(processed))
}

query.select(TransferModel::as_select()).load::<TransferModel>(conn)
query
.select(TransferModel::as_select())
.load::<TransferModel>(conn)
})
.await??;
Ok(res)
}
}
2 changes: 1 addition & 1 deletion node/src/handlers/delta_tree/submit_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub async fn submit_batch(State(AppState): State<AppState>) -> impl IntoResponse
let deposits: Vec<Deposit> = unprocessed_deposits.into_iter().map(|model| model.into()).collect();

// this counter uref is different!
let tree_index = query::query_counter(&CONFIG.node_address(), &CONFIG.node.port.to_string(), &CONFIG.node.counter_uref).await;
let tree_index = query::query_counter(&CONFIG.node_address(), &CONFIG.node.port.to_string(), &CONFIG.node.tree_counter_uref).await;
let mut previous_tree: KairosDeltaTree = KairosDeltaTree{
zero_node: hash_bytes(vec![0;32]),
zero_levels: Vec::new(),
Expand Down

0 comments on commit 5cb2124

Please sign in to comment.