|
| 1 | +use proc_macro2::{Ident, TokenStream}; |
| 2 | +use quote::quote; |
| 3 | +use syn::{ |
| 4 | + punctuated::{Iter, Punctuated}, |
| 5 | + token::Comma, |
| 6 | + Variant, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::utils::{get_enum_variant_type_path, Imports}; |
| 10 | + |
| 11 | +pub(crate) fn impl_ClientStateCommon( |
| 12 | + client_state_enum_name: &Ident, |
| 13 | + enum_variants: &Punctuated<Variant, Comma>, |
| 14 | +) -> TokenStream { |
| 15 | + let verify_consensus_state_impl = delegate_call_in_match( |
| 16 | + client_state_enum_name, |
| 17 | + enum_variants.iter(), |
| 18 | + quote! { verify_consensus_state(cs, consensus_state) }, |
| 19 | + ); |
| 20 | + let client_type_impl = delegate_call_in_match( |
| 21 | + client_state_enum_name, |
| 22 | + enum_variants.iter(), |
| 23 | + quote! {client_type(cs)}, |
| 24 | + ); |
| 25 | + let latest_height_impl = delegate_call_in_match( |
| 26 | + client_state_enum_name, |
| 27 | + enum_variants.iter(), |
| 28 | + quote! {latest_height(cs)}, |
| 29 | + ); |
| 30 | + let validate_proof_height_impl = delegate_call_in_match( |
| 31 | + client_state_enum_name, |
| 32 | + enum_variants.iter(), |
| 33 | + quote! {validate_proof_height(cs, proof_height)}, |
| 34 | + ); |
| 35 | + let confirm_not_frozen_impl = delegate_call_in_match( |
| 36 | + client_state_enum_name, |
| 37 | + enum_variants.iter(), |
| 38 | + quote! {confirm_not_frozen(cs)}, |
| 39 | + ); |
| 40 | + let expired_impl = delegate_call_in_match( |
| 41 | + client_state_enum_name, |
| 42 | + enum_variants.iter(), |
| 43 | + quote! {expired(cs, elapsed)}, |
| 44 | + ); |
| 45 | + let verify_upgrade_client_impl = delegate_call_in_match( |
| 46 | + client_state_enum_name, |
| 47 | + enum_variants.iter(), |
| 48 | + quote! {verify_upgrade_client(cs, upgraded_client_state, upgraded_consensus_state, proof_upgrade_client, proof_upgrade_consensus_state, root)}, |
| 49 | + ); |
| 50 | + let verify_membership_impl = delegate_call_in_match( |
| 51 | + client_state_enum_name, |
| 52 | + enum_variants.iter(), |
| 53 | + quote! {verify_membership(cs, prefix, proof, root, path, value)}, |
| 54 | + ); |
| 55 | + let verify_non_membership_impl = delegate_call_in_match( |
| 56 | + client_state_enum_name, |
| 57 | + enum_variants.iter(), |
| 58 | + quote! {verify_non_membership(cs, prefix, proof, root, path)}, |
| 59 | + ); |
| 60 | + |
| 61 | + let HostClientState = client_state_enum_name; |
| 62 | + |
| 63 | + let Any = Imports::Any(); |
| 64 | + let CommitmentRoot = Imports::CommitmentRoot(); |
| 65 | + let CommitmentPrefix = Imports::CommitmentPrefix(); |
| 66 | + let CommitmentProofBytes = Imports::CommitmentProofBytes(); |
| 67 | + let ClientStateCommon = Imports::ClientStateCommon(); |
| 68 | + let ClientType = Imports::ClientType(); |
| 69 | + let ClientError = Imports::ClientError(); |
| 70 | + let Height = Imports::Height(); |
| 71 | + let MerkleProof = Imports::MerkleProof(); |
| 72 | + let Path = Imports::Path(); |
| 73 | + |
| 74 | + quote! { |
| 75 | + impl #ClientStateCommon for #HostClientState { |
| 76 | + fn verify_consensus_state(&self, consensus_state: #Any) -> Result<(), #ClientError> { |
| 77 | + match self { |
| 78 | + #(#verify_consensus_state_impl),* |
| 79 | + } |
| 80 | + } |
| 81 | + fn client_type(&self) -> #ClientType { |
| 82 | + match self { |
| 83 | + #(#client_type_impl),* |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + fn latest_height(&self) -> #Height { |
| 88 | + match self { |
| 89 | + #(#latest_height_impl),* |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + fn validate_proof_height(&self, proof_height: #Height) -> core::result::Result<(), #ClientError> { |
| 94 | + match self { |
| 95 | + #(#validate_proof_height_impl),* |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn confirm_not_frozen(&self) -> core::result::Result<(), #ClientError> { |
| 100 | + match self { |
| 101 | + #(#confirm_not_frozen_impl),* |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fn expired(&self, elapsed: core::time::Duration) -> bool { |
| 106 | + match self { |
| 107 | + #(#expired_impl),* |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + fn verify_upgrade_client( |
| 112 | + &self, |
| 113 | + upgraded_client_state: #Any, |
| 114 | + upgraded_consensus_state: #Any, |
| 115 | + proof_upgrade_client: #MerkleProof, |
| 116 | + proof_upgrade_consensus_state: #MerkleProof, |
| 117 | + root: &#CommitmentRoot, |
| 118 | + ) -> core::result::Result<(), #ClientError> { |
| 119 | + match self { |
| 120 | + #(#verify_upgrade_client_impl),* |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + fn verify_membership( |
| 125 | + &self, |
| 126 | + prefix: &#CommitmentPrefix, |
| 127 | + proof: &#CommitmentProofBytes, |
| 128 | + root: &#CommitmentRoot, |
| 129 | + path: #Path, |
| 130 | + value: Vec<u8>, |
| 131 | + ) -> core::result::Result<(), #ClientError> { |
| 132 | + match self { |
| 133 | + #(#verify_membership_impl),* |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + fn verify_non_membership( |
| 138 | + &self, |
| 139 | + prefix: &#CommitmentPrefix, |
| 140 | + proof: &#CommitmentProofBytes, |
| 141 | + root: &#CommitmentRoot, |
| 142 | + path: #Path, |
| 143 | + ) -> core::result::Result<(), #ClientError> { |
| 144 | + match self { |
| 145 | + #(#verify_non_membership_impl),* |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/// |
| 154 | +/// Generates the per-enum variant function call delegation token streams. |
| 155 | +/// |
| 156 | +/// enum_name: The user's enum identifier (e.g. `HostClientState`) |
| 157 | +/// enum_variants: An iterator of all enum variants (e.g. `[HostClientState::Tendermint, HostClientState::Mock]`) |
| 158 | +/// fn_call: The tokens for the function call. Fully-qualified syntax is assumed, where the name for `self` |
| 159 | +/// is `cs` (e.g. `client_type(cs)`). |
| 160 | +/// |
| 161 | +/// For example, |
| 162 | +/// |
| 163 | +/// ```ignore |
| 164 | +/// impl ClientStateCommon for HostClientState { |
| 165 | +/// fn client_type(&self) -> ClientType { |
| 166 | +/// match self { |
| 167 | +/// // BEGIN code generated |
| 168 | +/// |
| 169 | +/// // 1st TokenStream returned |
| 170 | +/// HostClientState::Tendermint(cs) => <TmClientState as ClientStateCommon>::client_type(cs), |
| 171 | +/// // 2nd TokenStream returned |
| 172 | +/// HostClientState::Mock(cs) => <MockClientState as ClientStateCommon>::client_type(cs), |
| 173 | +/// |
| 174 | +/// // END code generated |
| 175 | +/// } |
| 176 | +/// } |
| 177 | +/// } |
| 178 | +/// ``` |
| 179 | +/// |
| 180 | +fn delegate_call_in_match( |
| 181 | + enum_name: &Ident, |
| 182 | + enum_variants: Iter<'_, Variant>, |
| 183 | + fn_call: TokenStream, |
| 184 | +) -> Vec<TokenStream> { |
| 185 | + let ClientStateCommon = Imports::ClientStateCommon(); |
| 186 | + |
| 187 | + enum_variants |
| 188 | + .map(|variant| { |
| 189 | + let variant_name = &variant.ident; |
| 190 | + let variant_type_name = get_enum_variant_type_path(variant); |
| 191 | + |
| 192 | + quote! { |
| 193 | + #enum_name::#variant_name(cs) => <#variant_type_name as #ClientStateCommon>::#fn_call |
| 194 | + } |
| 195 | + }) |
| 196 | + .collect() |
| 197 | +} |
0 commit comments