2 * API Reference: https://ethereum.github.io/beacon-APIs/
5 #![cfg_attr(debug_assertions, allow(unused_variables, unused_imports, dead_code))]
9 net
::{IpAddr
, Ipv4Addr
},
11 time
::{self, Duration
},
14 use anyhow
::{Context
, Result
};
16 message
::header
::ContentType
, transport
::smtp
::authentication
::Credentials
, Message
,
17 SmtpTransport
, Transport
,
19 use reqwest
::StatusCode
;
20 use serde
::Deserialize
;
21 use serde_json
::{json
, Value
};
23 use crate::config
::Config
;
28 const FILE_CONF
: &str = "config.ron";
29 const CHECK_PERIOD
: Duration
= Duration
::from_secs(10); // 10s.
30 const EMAIL_RESEND_PERIOD
: Duration
= Duration
::from_secs(6 * 60 * 60); // 6h.
31 const BASE_URI
: &str = "http://localhost:5052/eth/v1/";
33 fn main() -> Result
<()> {
34 println!("Staking Watchdog");
36 let config
= Config
::read(FILE_CONF
)?
;
39 "Configuration: {:?}",
41 smtp_password
: "*****".to_string(),
46 let mut time_last_email_send
= time
::Instant
::now() - EMAIL_RESEND_PERIOD
;
49 let time_beginning_loop
= time
::Instant
::now();
51 if let Err(error
) = check_validators(&config
.pub_keys
) {
52 println!("Error: {:?}", error
);
53 if time
::Instant
::now() - time_last_email_send
>= EMAIL_RESEND_PERIOD
{
55 println!("Sending email...");
58 &format!("Error: {:?}", error
),
60 &config
.smtp_password
,
62 Err(email_error
) => println!("Error sending email: {:?}", email_error
),
64 println!("Email successfully sent");
65 time_last_email_send
= time
::Instant
::now();
71 let elapsed
= time
::Instant
::now() - time_beginning_loop
;
73 if elapsed
< CHECK_PERIOD
{
74 let to_wait
= CHECK_PERIOD
- elapsed
;
75 thread
::sleep(to_wait
);
86 UnknownCodeFromHealthCheck(u16),
87 ReqwestError(reqwest
::Error
),
88 ValidatorError
{ pub_key
: String
, message
: String
},
89 ValidatorStatusError
{ pub_key
: String
, message
: String
},
92 impl From
<reqwest
::Error
> for CheckError
{
93 fn from(value
: reqwest
::Error
) -> Self {
94 CheckError
::ReqwestError(value
)
98 #[derive(Deserialize, Debug)]
99 struct JsonValidatorState
{
100 data
: JsonValidatorStateData
,
103 #[derive(Deserialize, Debug)]
104 struct JsonValidatorStateData
{
108 #[derive(Deserialize, Debug)]
114 fn check_validators(pub_keys
: &[String
]) -> std
::result
::Result
<(), CheckError
> {
116 let client
= reqwest
::blocking
::Client
::new();
118 let request_health
= client
119 .get(format!("{url}node/health"))
120 .header("accept", "application/json");
121 match request_health
.send() {
123 // println!("{resp:?}"); // For debug.
124 match resp
.status().as_u16() {
126 206 => return Err(CheckError
::NotSync
),
127 400 => return Err(CheckError
::InvalidSyncStatus
),
128 503 => return Err(CheckError
::NodeHavingIssues
),
129 code
=> return Err(CheckError
::UnknownCodeFromHealthCheck(code
)),
133 println!("{error:?}");
134 return Err(CheckError
::HttpError(error
.to_string()));
138 for pub_key
in pub_keys
{
140 .get(format!("{url}beacon/states/head/validators/0x{pub_key}"))
141 .header("accept", "application/json");
142 match request
.send() {
144 // println!("{resp:?}"); // For debug.
145 match resp
.status().as_u16() {
147 let json
: JsonValidatorState
= resp
.json()?
;
148 if json
.data
.status
!= "active_ongoing" {
149 return Err(CheckError
::ValidatorStatusError
{
150 pub_key
: pub_key
.clone(),
151 message
: format!("Status: {}", json
.data
.status
),
156 let json
: JsonError
= resp
.json()?
;
157 return Err(CheckError
::ValidatorError
{
158 pub_key
: pub_key
.clone(),
160 "Http error code: {}, message: {}",
168 return Err(CheckError
::ValidatorError
{
169 pub_key
: pub_key
.clone(),
170 message
: error
.to_string(),
179 fn send_email(title
: &str, body
: &str, login
: &str, pass
: &str) -> Result
<()> {
180 let email
= Message
::builder()
182 .from("Staking Watchdog <redmine@d-lan.net>".parse()?
)
183 .to("Greg Burri <greg.burri@gmail.com>".parse()?
)
185 .header(ContentType
::TEXT_PLAIN
)
186 .body(body
.to_string())?
;
188 let creds
= Credentials
::new(login
.to_string(), pass
.to_string());
190 // Open a remote connection to gmail
191 let mailer
= SmtpTransport
::relay("mail.gandi.net")?
196 let response
= mailer
.send(&email
)?
;
198 println!("{:?}", response
);