diff --git a/crates/store/.sqlx/query-7cea58b049202783f484beaf52578a0a1bf32cc599d8e3762e83ad90efc234ac.json b/crates/store/.sqlx/query-7cea58b049202783f484beaf52578a0a1bf32cc599d8e3762e83ad90efc234ac.json new file mode 100644 index 000000000..2710e7767 --- /dev/null +++ b/crates/store/.sqlx/query-7cea58b049202783f484beaf52578a0a1bf32cc599d8e3762e83ad90efc234ac.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "UPDATE node SET reward = ? WHERE peer_id = ?", + "describe": { + "columns": [], + "parameters": { + "Right": 2 + }, + "nullable": [] + }, + "hash": "7cea58b049202783f484beaf52578a0a1bf32cc599d8e3762e83ad90efc234ac" +} diff --git a/crates/store/.sqlx/query-8a1a70d94e874d7e8024a0af59501cda92c54dfc43f81d6477d17e186ba34b9d.json b/crates/store/.sqlx/query-8a1a70d94e874d7e8024a0af59501cda92c54dfc43f81d6477d17e186ba34b9d.json deleted file mode 100644 index 591ce2f5c..000000000 --- a/crates/store/.sqlx/query-8a1a70d94e874d7e8024a0af59501cda92c54dfc43f81d6477d17e186ba34b9d.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "SQLite", - "query": "UPDATE node SET reward = reward + ? WHERE peer_id = ?", - "describe": { - "columns": [], - "parameters": { - "Right": 2 - }, - "nullable": [] - }, - "hash": "8a1a70d94e874d7e8024a0af59501cda92c54dfc43f81d6477d17e186ba34b9d" -} diff --git a/crates/store/src/localdb/node.rs b/crates/store/src/localdb/node.rs index 06d131eef..6bea06bf0 100644 --- a/crates/store/src/localdb/node.rs +++ b/crates/store/src/localdb/node.rs @@ -164,6 +164,8 @@ impl StorageProcessor<'_> { Ok(res.rows_affected()) } + /// Stores `reward` as the node's new total; the caller has already added + /// the increment. // Do not update the `updated_at` field; this field is updated based on heartbeat messages // and is used to determine whether a node is alive. pub async fn update_node_reward_by_peer_id( @@ -171,7 +173,7 @@ impl StorageProcessor<'_> { peer_id: &str, reward: &str, ) -> anyhow::Result<()> { - sqlx::query!(r#"UPDATE node SET reward = reward + ? WHERE peer_id = ?"#, reward, peer_id) + sqlx::query!(r#"UPDATE node SET reward = ? WHERE peer_id = ?"#, reward, peer_id) .execute(self.conn()) .await?; Ok(()) diff --git a/node/src/scheduled_tasks/event_watch/gateway.rs b/node/src/scheduled_tasks/event_watch/gateway.rs index 4f528a027..a387c31aa 100644 --- a/node/src/scheduled_tasks/event_watch/gateway.rs +++ b/node/src/scheduled_tasks/event_watch/gateway.rs @@ -731,6 +731,24 @@ mod tests { } } + #[tokio::test] + async fn node_reward_is_added_once() { + let (db, _dir) = db().await; + let mut storage = db.acquire().await.unwrap(); + let node = store::Node { + peer_id: "operator".into(), + goat_addr: "0xabc".into(), + ..Default::default() + }; + storage.upsert_node(&node).await.unwrap(); + for _ in 0..2 { + add_node_reward(&mut storage, "0xabc", U256::from(5)).await.unwrap(); + } + let nodes = storage.find_nodes(&NodeQuery::default()).await.unwrap().0; + // Adding the increment in SQL as well would give 0 + 5, then 5 + 10. + assert_eq!(nodes[0].reward, "10"); + } + #[tokio::test] async fn a_posted_graph_unknown_locally_is_pulled_by_the_watching_roles() { let (db, _dir) = db().await;