refactor: Add the Clock trait and EnrEntry#443
Conversation
|
@claude review the changes |
|
@claude review the changes |
|
Claude finished @therustmonk's task in 2m 28s —— View job Code Review
Overall this is a clean, well-motivated refactor — both seams are clearer than the Issues1. PR description claims a blanket impl that doesn't exist — The PR body says:
There is no such impl in the code — only Two reasonable resolutions — pick one:
2.
Consider relocating to a more neutral home — Nits3. The trailing 4.
5. Charon parity divergence (informational, not a bug) Charon ( Positives
VerdictLand after resolving #1 (either add the blanket impl or amend the PR description so the "non-breaking" claim is accurate). #2–#5 are non-blocking. |
|
|
||
| /// [`Clock`] backed by the system wall clock via [`chrono::Utc::now`]. | ||
| #[derive(Debug, Clone, Copy, Default)] | ||
| pub struct SystemClock; |
There was a problem hiding this comment.
Better use ChronoClock to distinguish from std::time::SystemTime
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum EnrEntry { | ||
| /// Sets the IP address. | ||
| Ip(Ipv4Addr), |
Replace two
Box<dyn Fn>/Arc<dyn Fn>indirections with explicit abstractionsthat better express their intent, remove boxing, and improve testability.
crates/app/src/retry.rs— time provider →Clocktraittrait Clock: Send + Sync + 'static { fn now(&self) -> DateTime<Utc>; }with a blanket impl for
Fn() -> DateTime<Utc>.time_fn: Arc<dyn Fn() -> DateTime<Utc>>field withclock: Arc<dyn Clock>.with_timenow acceptsimpl Clock; existing closure callers keep workingunchanged via the blanket impl.
deadline_fnleft as a closure (value-specific data mapping, not an injected dependency).crates/eth2util/src/enr.rs— functional options →EnrEntryenumtype OptionFn = Box<dyn Fn(&mut HashMap<..>)>and thewith_ip/tcp/udp_implconstructors with
enum EnrEntry { Ip(Ipv4Addr), Tcp(u16), Udp(u16) }and a privateapplyusing an exhaustivematch.Record::newnow takesVec<EnrEntry>.relay-serverand thepeerinfoexample(
with_ip_impl(ip)→EnrEntry::Ip(ip)).Benefits
match— adding a field is compiler-checked.EnrEntryisCopy + Debug + Eq;Clockis a named, mockable seam for tests.