evm_oracle_state/
layout.rs1use alloy_primitives::{Address, B256};
2
3#[non_exhaustive]
5#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
6pub enum AggregatorLayout {
7 ChainlinkOcr2V1,
9 ChainlinkOcr1V2,
11 ChainlinkOcr1V3,
13 ChainlinkOcr1V4,
15 Unsupported,
18 Unknown,
20}
21
22#[non_exhaustive]
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25pub enum AggregatorLayoutConfidence {
26 TypeAndVersion,
28 CodeHashCache,
30 Unknown,
32}
33
34#[derive(Clone, Debug, PartialEq, Eq)]
36pub struct AggregatorLayoutEvidence {
37 pub aggregator: Address,
39 pub type_and_version: Option<String>,
41 pub code_hash: Option<B256>,
43 pub layout: AggregatorLayout,
45 pub confidence: AggregatorLayoutConfidence,
47}
48
49impl AggregatorLayoutEvidence {
50 pub fn from_type_and_version(
52 aggregator: Address,
53 type_and_version: impl Into<String>,
54 code_hash: Option<B256>,
55 ) -> Self {
56 let type_and_version = type_and_version.into();
57 Self {
58 aggregator,
59 layout: classify_type_and_version(&type_and_version),
60 type_and_version: Some(type_and_version),
61 code_hash,
62 confidence: AggregatorLayoutConfidence::TypeAndVersion,
63 }
64 }
65
66 pub fn from_code_hash_cache(
68 aggregator: Address,
69 code_hash: B256,
70 layout: AggregatorLayout,
71 ) -> Self {
72 Self {
73 aggregator,
74 type_and_version: None,
75 code_hash: Some(code_hash),
76 layout,
77 confidence: AggregatorLayoutConfidence::CodeHashCache,
78 }
79 }
80
81 pub fn unknown(aggregator: Address, code_hash: Option<B256>) -> Self {
83 Self {
84 aggregator,
85 type_and_version: None,
86 code_hash,
87 layout: AggregatorLayout::Unknown,
88 confidence: AggregatorLayoutConfidence::Unknown,
89 }
90 }
91
92 pub fn is_chainlink_ocr2_v1(&self) -> bool {
94 self.layout == AggregatorLayout::ChainlinkOcr2V1
95 }
96
97 pub fn is_chainlink_ocr1(&self) -> bool {
99 matches!(
100 self.layout,
101 AggregatorLayout::ChainlinkOcr1V2
102 | AggregatorLayout::ChainlinkOcr1V3
103 | AggregatorLayout::ChainlinkOcr1V4
104 )
105 }
106}
107
108pub fn classify_type_and_version(type_and_version: &str) -> AggregatorLayout {
110 match type_and_version.trim() {
111 "AccessControlledOCR2Aggregator 1.0.0" | "OCR2Aggregator 1.0.0" => {
112 AggregatorLayout::ChainlinkOcr2V1
113 }
114 "AccessControlledOffchainAggregator 2.0.0" | "OffchainAggregator 2.0.0" => {
115 AggregatorLayout::ChainlinkOcr1V2
116 }
117 "AccessControlledOffchainAggregator 3.0.0" | "OffchainAggregator 3.0.0" => {
118 AggregatorLayout::ChainlinkOcr1V3
119 }
120 "AccessControlledOffchainAggregator 4.0.0" | "OffchainAggregator 4.0.0" => {
121 AggregatorLayout::ChainlinkOcr1V4
122 }
123 "" => AggregatorLayout::Unknown,
124 _ => AggregatorLayout::Unsupported,
125 }
126}