From ba87ba60b348933b3d85bd3030ead35bf63c3d91 Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Thu, 5 Dec 2024 22:28:19 +0000 Subject: [PATCH 1/5] test that proposal ids are continuous that no ids are missing --- .devcontainer/devcontainer.json | 2 +- .devcontainer/install-dependencies.sh | 1 + readme.md | 2 ++ src/tests.rs | 38 +++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 5d367aa..12251df 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -6,7 +6,7 @@ }, "customizations": { "vscode": { - "extensions": ["rust-lang.rust-analyzer", "github.vscode-github-actions"] + "extensions": ["rust-lang.rust-analyzer", "github.vscode-github-actions","ms-python.python"] } }, "postCreateCommand": "./.devcontainer/install-dependencies.sh" diff --git a/.devcontainer/install-dependencies.sh b/.devcontainer/install-dependencies.sh index b905091..b8dd60d 100755 --- a/.devcontainer/install-dependencies.sh +++ b/.devcontainer/install-dependencies.sh @@ -1,6 +1,7 @@ #!/bin/bash sudo apt update sudo apt install -y postgresql +sudo apt install libudev-dev cargo install sqlx-cli diff --git a/readme.md b/readme.md index 8824c42..ae549e6 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,8 @@ cargo watch -q -c -w src/ -x 'run ' ### Create and run migrations +*Github codespace*: If the database server is not running, you can start it by typing `sudo service postgresql start`. + ```bash sqlx migrate add ``` diff --git a/src/tests.rs b/src/tests.rs index 2a313ce..0d6d909 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,3 +1,8 @@ +use crate::{ + db::db_types::ProposalWithLatestSnapshotView, rpc_service, types::PaginatedResponse, Env, +}; +use near_sdk::AccountId; + /** * Test Nearblocks mocked transactions */ @@ -22,3 +27,36 @@ fn test_index() { let response = client.get("/").dispatch(); assert_eq!(response.into_string().unwrap(), "Welcome from fly.io!!!!!"); } + +#[test] +fn test_proposal_ids_are_continuous() { + use rocket::local::blocking::Client; + + let client = Client::tracked(super::rocket()).expect("valid `Rocket`"); + + for _ in 0..50 { + let response = client + .get("/proposals?order=id_asc&limit=50&offset=0}`") + .dispatch(); + let result = response + .into_json::>() + .unwrap(); + if result.records.len() >= 50 { + break; + } + } + let response = client + .get("/proposals?order=id_asc&limit=50&offset=0}`") + .dispatch(); + let result = response + .into_json::>() + .unwrap(); + assert_eq!(result.records.len(), 50); + let env: Env = envy::from_env::().expect("Failed to load environment variables"); + let account_id: AccountId = env.contract.parse().unwrap(); + + for ndx in 0..50 { + let proposal_id: i32 = ndx as i32; + assert_eq!(result.records[ndx].proposal_id, proposal_id); + } +} From 9fbfe48c10d04358516d92dc649205ff4df30a9b Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Fri, 6 Dec 2024 05:59:21 +0000 Subject: [PATCH 2/5] log new cursor --- src/nearblocks_client/transactions.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/nearblocks_client/transactions.rs b/src/nearblocks_client/transactions.rs index 3adc12b..7ccdc0a 100644 --- a/src/nearblocks_client/transactions.rs +++ b/src/nearblocks_client/transactions.rs @@ -53,13 +53,16 @@ pub async fn update_nearblocks_data( nearblocks_client::transactions::process(&nearblocks_unwrapped.txns, db.into(), contract) .await; + let new_cursor = nearblocks_unwrapped.cursor.unwrap(); + eprintln!("New cursor is {}", new_cursor.clone()); + if let Some(transaction) = nearblocks_unwrapped.txns.last() { let timestamp_nano = transaction.block_timestamp.parse::().unwrap(); let _ = db .set_last_updated_info( timestamp_nano, transaction.block.block_height, - nearblocks_unwrapped.cursor.as_ref().unwrap().clone(), + new_cursor.clone(), ) .await; } From f7c32cb4ed33e6717f172f01a82c1e693d30069a Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Sat, 7 Dec 2024 18:35:07 +0000 Subject: [PATCH 3/5] test that title and status matches --- src/tests.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 0d6d909..fdfd7c5 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,7 +1,10 @@ +use near_api::prelude::{Contract, Reference}; + use crate::{ db::db_types::ProposalWithLatestSnapshotView, rpc_service, types::PaginatedResponse, Env, }; use near_sdk::AccountId; +use serde_json::{json, Value}; /** * Test Nearblocks mocked transactions @@ -28,18 +31,22 @@ fn test_index() { assert_eq!(response.into_string().unwrap(), "Welcome from fly.io!!!!!"); } -#[test] -fn test_proposal_ids_are_continuous() { - use rocket::local::blocking::Client; +#[rocket::async_test] +async fn test_proposal_ids_are_continuous_and_name_and_status_matches() { + use rocket::local::asynchronous::Client; - let client = Client::tracked(super::rocket()).expect("valid `Rocket`"); + let client = Client::tracked(super::rocket()) + .await + .expect("valid `Rocket`"); for _ in 0..50 { let response = client .get("/proposals?order=id_asc&limit=50&offset=0}`") .dispatch(); let result = response + .await .into_json::>() + .await .unwrap(); if result.records.len() >= 50 { break; @@ -49,14 +56,54 @@ fn test_proposal_ids_are_continuous() { .get("/proposals?order=id_asc&limit=50&offset=0}`") .dispatch(); let result = response + .await .into_json::>() + .await .unwrap(); assert_eq!(result.records.len(), 50); + let env: Env = envy::from_env::().expect("Failed to load environment variables"); - let account_id: AccountId = env.contract.parse().unwrap(); + let contract_account_id: AccountId = env.contract.parse().unwrap(); + let contract = Contract(contract_account_id); for ndx in 0..50 { let proposal_id: i32 = ndx as i32; assert_eq!(result.records[ndx].proposal_id, proposal_id); + + let call = contract + .call_function("get_proposal", json!({"proposal_id": proposal_id})) + .unwrap(); + let proposal: Value = call + .read_only() + .fetch_from_mainnet() + .await + .unwrap() + .data; + eprintln!( + "proposal {:?}, {:?}, {:?}, {:?}", + proposal_id, + result.records[ndx].block_height.unwrap(), + proposal["snapshot"]["name"], + proposal["snapshot"]["timeline"]["status"] + ); + assert_eq!( + proposal["snapshot"]["name"].as_str().unwrap(), + result.records[ndx].clone().name.unwrap() + ); + + let timeline: Value = serde_json::from_str( + result.records[ndx] + .clone() + .timeline + .unwrap() + .as_str() + .unwrap(), + ) + .unwrap(); + eprint!("timeline {:?}", timeline); + assert_eq!( + proposal["snapshot"]["timeline"]["status"], + timeline["status"] + ); } } From f937c7f4cff7e5259fdfcc91a0933329141c9d27 Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Sun, 8 Dec 2024 07:03:13 +0000 Subject: [PATCH 4/5] no need to ask query multiple times for getting cache up to date --- src/tests.rs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index fdfd7c5..a35b103 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -39,19 +39,6 @@ async fn test_proposal_ids_are_continuous_and_name_and_status_matches() { .await .expect("valid `Rocket`"); - for _ in 0..50 { - let response = client - .get("/proposals?order=id_asc&limit=50&offset=0}`") - .dispatch(); - let result = response - .await - .into_json::>() - .await - .unwrap(); - if result.records.len() >= 50 { - break; - } - } let response = client .get("/proposals?order=id_asc&limit=50&offset=0}`") .dispatch(); @@ -66,7 +53,7 @@ async fn test_proposal_ids_are_continuous_and_name_and_status_matches() { let contract_account_id: AccountId = env.contract.parse().unwrap(); let contract = Contract(contract_account_id); - for ndx in 0..50 { + for ndx in 0..result.records.len() { let proposal_id: i32 = ndx as i32; assert_eq!(result.records[ndx].proposal_id, proposal_id); From fd6faee9809afaf9a005eb095407141bea371ea9 Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Sun, 8 Dec 2024 07:03:55 +0000 Subject: [PATCH 5/5] use transactions.rs from upstream main --- src/nearblocks_client/transactions.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/nearblocks_client/transactions.rs b/src/nearblocks_client/transactions.rs index be1fe5c..d79d4c9 100644 --- a/src/nearblocks_client/transactions.rs +++ b/src/nearblocks_client/transactions.rs @@ -84,7 +84,11 @@ pub async fn update_nearblocks_data( } pub async fn process( + transactions: &[Transaction], + db: &State, + contract: &AccountId, ) -> Result<(), Status> { + for transaction in transactions.iter() { if let Some(action) = transaction .actions .as_ref()