Skip to content

Commit

Permalink
track earnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriptikz committed Aug 15, 2024
1 parent 24b4130 commit 133ab7d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE submissions DROP COLUMN digest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE submissions ADD COLUMN digest BINARY(16)
1 change: 1 addition & 0 deletions migrations/2024-08-14-235001_create_earnings/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE earnings
9 changes: 9 additions & 0 deletions migrations/2024-08-14-235001_create_earnings/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE earnings (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
miner_id INT NOT NULL,
pool_id INT NOT NULL,
challenge_id INT NOT NULL,
amount BIGINT UNSIGNED DEFAULT 0 NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
)
22 changes: 22 additions & 0 deletions src/app_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,26 @@ impl AppDatabase {
};

}

pub async fn add_new_earning(&self, earning: models::InsertEarning) -> Result<(), AppDatabaseError> {
if let Ok(db_conn) = self.connection_pool.get().await {
let res = db_conn.interact(move |conn: &mut MysqlConnection| {
diesel::sql_query("INSERT INTO earnings (miner_id, pool_id, challenge_id, amount) VALUES (?, ?, ?, ?)")
.bind::<Integer, _>(earning.miner_id)
.bind::<Integer, _>(earning.pool_id)
.bind::<Integer, _>(earning.challenge_id)
.bind::<Unsigned<BigInt>, _>(earning.amount)
.execute(conn)
}).await;

if res.is_ok() {
return Ok(());
} else {
return Err(AppDatabaseError::FailedToInsertNewEntity);
}
} else {
return Err(AppDatabaseError::FailedToGetConnectionFromPool);
};

}
}
15 changes: 15 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct MessageInternalMineSuccess {
difficulty: u32,
total_balance: f64,
rewards: u64,
challenge_id: i32,
total_hashpower: u64,
submissions: HashMap<Pubkey, (i32, u32, u64)>
}
Expand Down Expand Up @@ -496,6 +497,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _ = app_database.update_challenge_rewards(proof.challenge.to_vec(), submission_id, rewards).await.unwrap();
let _ = app_database.update_pool_rewards(app_wallet.pubkey().to_string(), rewards).await.unwrap();

let challenge = app_database.get_challenge_by_challenge(proof.challenge.to_vec()).await.unwrap();

let submissions = {
app_epoch_hashes.read().await.submissions.clone()
};
Expand All @@ -509,6 +512,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
difficulty,
total_balance: balance,
rewards,
challenge_id: challenge.id,
total_hashpower,
submissions,
});
Expand Down Expand Up @@ -604,6 +608,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let app_shared_state = shared_state.clone();
let app_app_database = app_database.clone();
let app_config = config.clone();
tokio::spawn(async move {
let app_database = app_app_database;
loop {
Expand All @@ -620,6 +625,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let decimals = 10f64.powf(ORE_TOKEN_DECIMALS as f64);
let earned_rewards = hashpower_percent.saturating_mul(msg.rewards as u128).saturating_div(1_000_000) as u64;
let _ = app_database.update_miner_reward(*miner_id, earned_rewards as u64).await.unwrap();

let new_earning = InsertEarning {
miner_id: *miner_id,
pool_id: app_config.pool_id,
challenge_id: msg.challenge_id,
amount: earned_rewards,
};

let _ = app_database.add_new_earning(new_earning).await.unwrap();

let earned_rewards_dec = (earned_rewards as f64).div(decimals);
let pool_rewards_dec = (msg.rewards as f64).div(decimals);

Expand Down
10 changes: 10 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,14 @@ pub struct Reward {
pub balance: u64,
}

#[derive(Debug, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name = crate::schema::earnings)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct InsertEarning {
pub miner_id: i32,
pub pool_id: i32,
pub challenge_id: i32,
pub amount: u64,
}


15 changes: 15 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ diesel::table! {
}
}

diesel::table! {
earnings (id) {
id -> Integer,
miner_id -> Integer,
pool_id -> Integer,
challenge_id -> Integer,
amount -> Unsigned<Bigint>,
created_at -> Timestamp,
updated_at -> Timestamp,
}
}

diesel::table! {
miners (id) {
id -> Integer,
Expand Down Expand Up @@ -70,6 +82,8 @@ diesel::table! {
nonce -> Unsigned<Bigint>,
created_at -> Timestamp,
updated_at -> Timestamp,
#[max_length = 16]
digest -> Nullable<Binary>,
}
}

Expand All @@ -89,6 +103,7 @@ diesel::table! {
diesel::allow_tables_to_appear_in_same_query!(
challenges,
claims,
earnings,
miners,
pools,
rewards,
Expand Down

0 comments on commit 133ab7d

Please sign in to comment.